Skip to content

Commit

Permalink
Merge pull request #275 from kyleburke-meq/main
Browse files Browse the repository at this point in the history
Snowflake: Add expression parameter to columns
  • Loading branch information
dataders authored Apr 19, 2024
2 parents 772ae8c + 0b04472 commit d318127
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
{{ dbt_utils.star(from=ref('people')) }},
split_part(email, '@', 2) as email_domain
FROM {{ ref('people') }}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,31 @@ sources:
- id
- first_name
- last_name
- email_alias
- email_alias

# test for column expression
- name: people_json_expression
external: *json-people
columns:
- name: id
data_type: int
- name: first_name
data_type: varchar(64)
- name: last_name
data_type: varchar(64)
- name: email
data_type: varchar(64)
- name: email_domain
data_type: varchar(64)
alias: EMAIL_DOMAIN
quote: true
expression: split_part(value:email::VARCHAR, '@', 2)
tests:
- dbt_utils.equality:
compare_model: ref('people_expression')
compare_columns:
- id
- first_name
- last_name
- email
- email_domain
21 changes: 16 additions & 5 deletions macros/plugins/snowflake/create_external_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@
{%- endfor -%}{%- endif -%}
{%- if not infer_schema -%}
{%- for column in columns %}
{%- set column_alias = column.alias if column.alias else column.name %}
{%- set column_alias_quoted = adapter.quote(column_alias) if column.quote else column_alias %}
{%- set column_quoted = adapter.quote(column.name) if column.quote else column.name %}
{%- set column_alias -%}
{%- if 'alias' in column and column.quote -%}
{{adapter.quote(column.alias)}}
{%- elif 'alias' in column -%}
{{column.alias}}
{%- else -%}
{{column_quoted}}
{%- endif -%}
{%- endset %}
{%- set col_expression -%}
{%- set col_id = 'value:c' ~ loop.index if is_csv else 'value:' ~ column_quoted -%}
(case when is_null_value({{col_id}}) or lower({{col_id}}) = 'null' then null else {{col_id}} end)
{%- if column.expression -%}
{{column.expression}}
{%- else -%}
{%- set col_id = 'value:c' ~ loop.index if is_csv else 'value:' ~ column_alias -%}
(case when is_null_value({{col_id}}) or lower({{col_id}}) = 'null' then null else {{col_id}} end)
{%- endif -%}
{%- endset %}
{{column_alias_quoted}} {{column.data_type}} as ({{col_expression}}::{{column.data_type}})
{{column_alias}} {{column.data_type}} as ({{col_expression}}::{{column.data_type}})
{{- ',' if not loop.last -}}
{% endfor %}
{% else %}
Expand Down
12 changes: 12 additions & 0 deletions sample_sources/snowflake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ sources:
- name: etl_tstamp
data_type: timestamp
description: "Timestamp event began ETL"
- name: etl timestamp
# Use double-quoted identifiers for name and identifier
quote: true
# Specifying alias lets us rename etl timestamp to "etl_timestamp"
alias: etl_timestamp
data_type: timestamp
description: "Timestamp event began ETL with a double quoted identifier"
- name: etl_date
data_type: date
description: "Date event began ETL"
# Expressions can manipulate the variant value prior to casting to data_type.
expression: TRY_TO_DATE(VALUE:etl_tstamp::VARCHAR, 'YYYYMMDD')
- name: contexts
data_type: variant
description: "Contexts attached to event by Tracker"
Expand Down

0 comments on commit d318127

Please sign in to comment.