-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dbt_utils/sqlserver__generate_surrogate_key macro to fix concat bug
- Loading branch information
1 parent
f8d775f
commit a647364
Showing
1 changed file
with
29 additions
and
0 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,29 @@ | ||
{%- macro sqlserver__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 " ~ dbt.type_string() ~ "), '" ~ default_null_value ~"')" | ||
) -%} | ||
|
||
{%- if not loop.last %} | ||
{%- do fields.append("'-'") -%} | ||
{%- endif -%} | ||
|
||
{%- endfor -%} | ||
|
||
{%- if fields|length > 1 %} | ||
{{ dbt.hash(dbt.concat(fields)) }} | ||
{%- else -%} | ||
{{ dbt.hash(fields[0]) }} | ||
{%- endif -%} | ||
|
||
{%- endmacro -%} |