Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/557 split part negative part number #559

Merged
merged 5 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}