Skip to content

Commit

Permalink
Merge pull request #53 from fishtown-analytics/feature/surrogate-key
Browse files Browse the repository at this point in the history
Feature/surrogate key
  • Loading branch information
jthandy authored Feb 1, 2018
2 parents f290082 + 4439d02 commit e8297d1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ Usage:
{{ dbt_utils.generate_series(upper_bound=1000) }}
```

#### surrogate_key ([source](macros/sql/surrogate_key.sql))
Implements a cross-database way to generate a hashed surrogate key using the fields specified.

Usage:
```
{{ dbt_utils.surrogate_key('field_a', 'field_b'[,...]) }}
```

#### pivot ([source](macros/sql/pivot.sql))
This macro pivots values from rows to columns.

Expand Down
23 changes: 23 additions & 0 deletions macros/cross_db_utils/concat.sql
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 %}
21 changes: 21 additions & 0 deletions macros/cross_db_utils/datatypes.sql
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 %}
20 changes: 20 additions & 0 deletions macros/cross_db_utils/safe_cast.sql
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 %}
19 changes: 19 additions & 0 deletions macros/sql/surrogate_key.sql
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 -%}

0 comments on commit e8297d1

Please sign in to comment.