Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snowflake: Add expression parameter to columns #275

Merged
merged 13 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ sources:
data_type: varchar(64)
- name: email
data_type: varchar(64)
- name: email domain
data_type: varchar(64)
quote: true
identifier: email
expression: split_part(value:email::VARCHAR, '@', 2)
dataders marked this conversation as resolved.
Show resolved Hide resolved
kyleburke-meq marked this conversation as resolved.
Show resolved Hide resolved
tests: &equal-to-the-people
- dbt_utils.equality:
compare_model: ref('people')
Expand Down
17 changes: 15 additions & 2 deletions macros/plugins/snowflake/create_external_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@
{%- 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_identifier -%}
{%- if 'identifier' in column and column.quote -%}
{{adapter.quote(column.identifier)}}
{%- elif 'identifier' in column -%}
{{column.identifier}}
{%- 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_identifier -%}
(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}})
{{- ',' if not loop.last -}}
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 identifier lets us rename etl_tstamp to "etl timestamp"
identifier: etl_tstamp
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
Loading