-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/update-surrogate-key-macro' into utils-v1
- Loading branch information
Showing
7 changed files
with
59 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
integration_tests/models/sql/test_generate_surrogate_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} |