diff --git a/.changes/unreleased/Fixes-20230320-153225.yaml b/.changes/unreleased/Fixes-20230320-153225.yaml new file mode 100644 index 00000000000..be49986203f --- /dev/null +++ b/.changes/unreleased/Fixes-20230320-153225.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: add negative part_number arg for split part macro +time: 2023-03-20T15:32:25.5932-05:00 +custom: + Author: dave-connors-3 + Issue: "7915" diff --git a/core/dbt/include/global_project/macros/utils/split_part.sql b/core/dbt/include/global_project/macros/utils/split_part.sql index cecc4497b58..766539ac0c1 100644 --- a/core/dbt/include/global_project/macros/utils/split_part.sql +++ b/core/dbt/include/global_project/macros/utils/split_part.sql @@ -20,7 +20,7 @@ length({{ string_text }}) - length( replace({{ string_text }}, {{ delimiter_text }}, '') - ) + 2 {{ part_number }} + ) + 2 + {{ part_number }} ) {% endmacro %} diff --git a/tests/adapter/dbt/tests/adapter/utils/fixture_split_part.py b/tests/adapter/dbt/tests/adapter/utils/fixture_split_part.py index 7fc70e5bf54..244fbdb7464 100644 --- a/tests/adapter/dbt/tests/adapter/utils/fixture_split_part.py +++ b/tests/adapter/dbt/tests/adapter/utils/fixture_split_part.py @@ -1,9 +1,9 @@ # split_part -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 -,|,,, +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 +,|,,,, """ @@ -34,6 +34,14 @@ {{ split_part('parts', 'split_on', 3) }} as actual, result_3 as expected +from data + +union all + +select + {{ split_part('parts', 'split_on', -1) }} as actual, + result_4 as expected + from data """