diff --git a/CHANGELOG.md b/CHANGELOG.md index f15cb319..f72afbd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # Unreleased ## New features - Add an optional `where` clause parameter to `get_column_values()` to filter values returned ([#511](https://github.com/dbt-labs/dbt-utils/issues/511), [#583](https://github.com/dbt-labs/dbt-utils/pull/583)) - -## New features - Add `where` parameter to `union_relations` macro ([#554](https://github.com/dbt-labs/dbt-utils/pull/554)) + +## Fixes +- Enable a negative part_number for `split_part()` ([#557](https://github.com/dbt-labs/dbt-utils/issues/557), [#559](https://github.com/dbt-labs/dbt-utils/pull/559)) + ## Quality of life - Documentation about listagg macro ([#544](https://github.com/dbt-labs/dbt-utils/issues/544), [#560](https://github.com/dbt-labs/dbt-utils/pull/560)) - Fix links to macro section in table of contents ([#555](https://github.com/dbt-labs/dbt-utils/pull/555)) @@ -18,6 +20,7 @@ - [@judahrand](https://github.com/judahrand) (#552) - [@clausherther](https://github.com/clausherther) (#555) - [@epapineau](https://github.com/epapineau) (#583) +- [@b-per](https://github.com/b-per) (#559) # dbt-utils v0.8.4 ## Fixes diff --git a/integration_tests/models/cross_db_utils/test_split_part.sql b/integration_tests/models/cross_db_utils/test_split_part.sql index 6a10327c..22470549 100644 --- a/integration_tests/models/cross_db_utils/test_split_part.sql +++ b/integration_tests/models/cross_db_utils/test_split_part.sql @@ -26,3 +26,11 @@ select result_3 as expected from data + +union all + +select + {{ dbt_utils.split_part('parts', 'split_on', -1) }} as actual, + result_3 as expected + +from data diff --git a/macros/cross_db_utils/split_part.sql b/macros/cross_db_utils/split_part.sql index 036f7d5a..2f9daeac 100644 --- a/macros/cross_db_utils/split_part.sql +++ b/macros/cross_db_utils/split_part.sql @@ -14,11 +14,59 @@ {% endmacro %} +{% macro _split_part_negative(string_text, delimiter_text, part_number) %} + + split_part( + {{ string_text }}, + {{ delimiter_text }}, + length({{ string_text }}) + - length( + replace({{ string_text }}, {{ delimiter_text }}, '') + ) + 2 {{ part_number }} + ) + +{% endmacro %} + + +{% macro postgres__split_part(string_text, delimiter_text, part_number) %} + + {% if part_number >= 0 %} + {{ dbt_utils.default__split_part(string_text, delimiter_text, part_number) }} + {% else %} + {{ dbt_utils._split_part_negative(string_text, delimiter_text, part_number) }} + {% endif %} + +{% endmacro %} + + +{% macro redshift__split_part(string_text, delimiter_text, part_number) %} + + {% if part_number >= 0 %} + {{ dbt_utils.default__split_part(string_text, delimiter_text, part_number) }} + {% else %} + {{ dbt_utils._split_part_negative(string_text, delimiter_text, part_number) }} + {% endif %} + +{% endmacro %} + + {% macro bigquery__split_part(string_text, delimiter_text, part_number) %} + {% if part_number >= 0 %} split( {{ string_text }}, {{ delimiter_text }} )[safe_offset({{ part_number - 1 }})] + {% else %} + split( + {{ string_text }}, + {{ delimiter_text }} + )[safe_offset( + length({{ string_text }}) + - length( + replace({{ string_text }}, {{ delimiter_text }}, '') + ) + 1 + )] + {% endif %} {% endmacro %}