Skip to content

Commit

Permalink
Merge branch 'feature/update-surrogate-key-macro' into utils-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
joellabes committed Sep 28, 2022
2 parents c15d55e + 44f1828 commit c59af05
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 44 deletions.
5 changes: 5 additions & 0 deletions integration_tests/data/sql/data_generate_surrogate_key.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
column_1,column_2,column_3,expected_column_1_only,expected_all_columns
a,b,c,0cc175b9c0f1b6a831c399e269772661,7b193b3d33184464106f41ddf733783b
a,,c,0cc175b9c0f1b6a831c399e269772661,4f32a73dc87b7bbb7a654d8898d58c7e
,,c,f14cc5cdce0420f4a5a6b6d9d7b85f39,d9c538b129f1a3ad6ecfe55345c32a05
,,,f14cc5cdce0420f4a5a6b6d9d7b85f39,2fa5491950d66d153d23cfbcfea4e164
5 changes: 0 additions & 5 deletions integration_tests/data/sql/data_surrogate_key.csv

This file was deleted.

2 changes: 1 addition & 1 deletion integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ models:
- dbt_utils.equality:
compare_model: ref('data_star_expected')

- name: test_surrogate_key
- name: test_generate_surrogate_key
tests:
- assert_equal:
actual: actual_column_1_only
Expand Down
14 changes: 14 additions & 0 deletions integration_tests/models/sql/test_generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

with data as (

select * from {{ ref('data_generate_surrogate_key') }}

)

select
{{ dbt_utils.generate_surrogate_key(['column_1']) }} as actual_column_1_only,
expected_column_1_only,
{{ dbt_utils.generate_surrogate_key(['column_1', 'column_2', 'column_3']) }} as actual_all_columns_list,
expected_all_columns

from data
14 changes: 0 additions & 14 deletions integration_tests/models/sql/test_surrogate_key.sql

This file was deleted.

29 changes: 29 additions & 0 deletions macros/sql/generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{%- macro generate_surrogate_key(field_list) -%}
{{ return(adapter.dispatch('generate_surrogate_key', 'dbt_utils')(field_list)) }}
{% endmacro %}

{%- macro default__generate_surrogate_key(field_list) -%}

{% if var('surrogate_key_treat_nulls_as_empty_strings', False) %}
{% set default_null_value = "" %}
{% else %}
{% set default_null_value = '_dbt_utils_surrogate_key_null_'%}
{% endif %}

{%- set fields = [] -%}

{%- for field in field_list -%}

{%- do fields.append(
"coalesce(cast(" ~ field ~ " as " ~ type_string() ~ "), '" ~ default_null_value ~"')"
) -%}

{%- if not loop.last %}
{%- do fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{{ hash(concat(fields)) }}

{%- endmacro -%}
34 changes: 10 additions & 24 deletions macros/sql/surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
{%- macro surrogate_key(field_list) -%}
{{ return(adapter.dispatch('surrogate_key', 'dbt_utils')(field_list)) }}
{% set frustrating_jinja_feature = varargs %}
{{ return(adapter.dispatch('surrogate_key', 'dbt_utils')(field_list, *varargs)) }}
{% endmacro %}

{%- macro default__surrogate_key(field_list) -%}

{%- if field_list is not iterable or field_list is string or field_list is mapping -%}

{%- set error_message = '
Warning: the `surrogate_key` macro now takes a single list argument instead of \
string arguments. The {}.{} model triggered this warning. \
Warning: `dbt_utils.surrogate_key` has been replaced by \
`dbt_utils.generate_surrogate_key`. The new macro treats null values \
differently to empty strings. To restore the behaviour of the original \
macro, add a variable scoped to the dbt_utils package called \
`surrogate_key_treat_nulls_as_empty_strings` to your \
dbt_project.yml file with a value of True. \
The {}.{} model triggered this warning. \
'.format(model.package_name, model.name) -%}

{%- do exceptions.warn(error_message) -%}

{%- endif -%}

{%- set fields = [] -%}

{%- for field in field_list -%}

{%- do fields.append(
"coalesce(cast(" ~ field ~ " as " ~ type_string() ~ "), '')"
) -%}

{%- if not loop.last %}
{%- do fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{{ hash(concat(fields)) }}
{%- do exceptions.raise_compiler_error(error_message) -%}

{%- endmacro -%}

0 comments on commit c59af05

Please sign in to comment.