-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from fishtown-analytics/feature/surrogate-key
Feature/surrogate key
- Loading branch information
Showing
5 changed files
with
91 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
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,23 @@ | ||
{% macro concat(fields) %} | ||
{{ adapter_macro('dbt_utils.concat', fields) }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__concat(fields) -%} | ||
concat({{ fields|join(', ') }}) | ||
{%- endmacro %} | ||
|
||
|
||
{% macro alternative_concat(fields) %} | ||
{{ fields|join(' || ') }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro redshift__concat(fields) %} | ||
{{dbt_utils.alternative_concat(fields)}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro snowflake__concat(fields) %} | ||
{{dbt_utils.alternative_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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{# string ------------------------------------------------- #} | ||
|
||
{% macro type_string() %} | ||
{{ adapter_macro('dbt_utils.type_string') }} | ||
{% endmacro %} | ||
|
||
{% macro default__type_string() %} | ||
string | ||
{% endmacro %} | ||
|
||
{%- macro redshift__type_string() -%} | ||
varchar | ||
{%- endmacro -%} | ||
|
||
{% macro postgres__type_string() %} | ||
varchar | ||
{% endmacro %} | ||
|
||
{% macro snowflake__type_string() %} | ||
varchar | ||
{% 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% macro safe_cast(field, type) %} | ||
{{ adapter_macro('dbt_utils.safe_cast', field, type) }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__safe_cast(field, type) %} | ||
{# most databases don't support this function yet | ||
so we just need to use cast #} | ||
cast({{field}} as {{type}}) | ||
{% endmacro %} | ||
|
||
|
||
{% macro snowflake__safe_cast(field, type) %} | ||
try_cast({{field}} as {{type}}) | ||
{% endmacro %} | ||
|
||
|
||
{% macro bigquery__safe_cast(field, type) %} | ||
safe_cast({{field}} as {{type}}) | ||
{% 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{%- macro surrogate_key() -%} | ||
|
||
{% set fields = [] %} | ||
|
||
{%- for field in varargs -%} | ||
|
||
{% set _ = fields.append( | ||
"coalesce(cast(" ~ field ~ " as " ~ dbt_utils.type_string() ~ "), '')" | ||
) %} | ||
|
||
{% if not loop.last %} | ||
{% set _ = fields.append("'-'") %} | ||
{% endif %} | ||
|
||
{%- endfor -%} | ||
|
||
md5({{dbt_utils.concat(fields)}}) | ||
|
||
{%- endmacro -%} |