Skip to content

Commit

Permalink
Fix/557 split part negative part number (#559)
Browse files Browse the repository at this point in the history
* Add support for negative part number for Postgres, Redshift and BigQuery

* Add test for negative part_number

* Keep the positive behaviour for part_number = 0

* Update CHANGELOG

Co-authored-by: Doug Beatty <[email protected]>
Co-authored-by: Doug Beatty <[email protected]>
  • Loading branch information
3 people authored May 12, 2022
1 parent 7e3da7c commit f4c4ae6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/models/cross_db_utils/test_split_part.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions macros/cross_db_utils/split_part.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

0 comments on commit f4c4ae6

Please sign in to comment.