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 split part negative part number #689

Merged
merged 13 commits into from
Jul 11, 2023
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230319-155618.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: allow negative args for spark split part
time: 2023-03-19T15:56:18.630146-05:00
custom:
Author: dave-connors-3
Issue: "688"
32 changes: 26 additions & 6 deletions dbt/include/spark/macros/utils/split_part.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,34 @@

{% endset %}

{% set split_part_expr %}
{% if part_number >= 0 %}

split(
{{ string_text }},
{{ delimiter_expr }}
)[({{ part_number - 1 }})]
{% set split_part_expr %}

{% endset %}
split(
{{ string_text }},
{{ delimiter_expr }}
)[({{ part_number - 1 if part_number > 0 else part_number }})]

{% endset %}

{% else %}
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved

{% set split_part_expr %}

split(
{{ string_text }},
{{ delimiter_expr }}
)[(
length({{ string_text }})
- length(
replace({{ string_text }}, {{ delimiter_text }}, '')
) + 1 + {{ part_number }}
)]

{% endset %}

{% endif %}

{{ return(split_part_expr) }}

Expand Down
8 changes: 4 additions & 4 deletions tests/functional/adapter/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
from dbt.tests.adapter.utils.fixture_listagg import models__test_listagg_yml
from tests.functional.adapter.utils.fixture_listagg import models__test_listagg_no_order_by_sql

seeds__data_split_part_csv = """parts,split_on,result_1,result_2,result_3
a|b|c,|,a,b,c
1|2|3,|,1,2,3
EMPTY|EMPTY|EMPTY,|,EMPTY,EMPTY,EMPTY
seeds__data_split_part_csv = """parts,split_on,result_1,result_2,result_3,result_4
a|b|c,|,a,b,c,c
1|2|3,|,1,2,3,3
EMPTY|EMPTY|EMPTY,|,EMPTY,EMPTY,EMPTY,EMPTY
"""

seeds__data_last_day_csv = """date_day,date_part,result
Expand Down
Loading