Skip to content

Commit

Permalink
Fix split part negative part number (#689)
Browse files Browse the repository at this point in the history
* pin dev branch for tests

* update split part

* changie

* update to delimiter text for replace

* whitespace

* update to new core branch

* Add a negative number (rather than subtract a positive number)

* Expected negative test case

* Restore original dev-requirements.txt

---------

Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>
  • Loading branch information
dave-connors-3 and dbeatty10 authored Jul 11, 2023
1 parent 46e991a commit b685297
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
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 %}

{% 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

0 comments on commit b685297

Please sign in to comment.