forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsurrogate_key.sql
More file actions
33 lines (21 loc) · 849 Bytes
/
surrogate_key.sql
File metadata and controls
33 lines (21 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{%- macro surrogate_key(columns, normalize_case = none) -%}
{{ return(adapter.dispatch('surrogate_key', 'cc_dbt_utils')(columns, normalize_case)) }}
{% endmacro %}
{%- macro default__surrogate_key(columns, normalize_case = none) -%}
{% set fields = [] %}
{%- for field in columns -%}
{% if normalize_case is none %}
{%- set _ = fields.append(
"coalesce(cast(" ~ field ~ " as " ~ cc_dbt_utils.type_string() ~ "), '')"
) -%}
{% elif normalize_case is not none %}
{%- set _ = fields.append(
"upper(coalesce(cast(" ~ field ~ " as " ~ cc_dbt_utils.type_string() ~ "), ''))"
) -%}
{% endif %}
{% if not loop.last %}
{% set _ = fields.append("'-'") %}
{% endif %}
{%- endfor -%}
{{cc_dbt_utils.hash(cc_dbt_utils.concat(fields))}}
{%- endmacro -%}