From 63d14c10f750f0292a2c91f56e933d3c3e806f10 Mon Sep 17 00:00:00 2001 From: cakkinep Date: Mon, 26 Feb 2024 11:46:39 -0500 Subject: [PATCH 1/3] alias column for snowflake external table --- macros/plugins/snowflake/create_external_table.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/macros/plugins/snowflake/create_external_table.sql b/macros/plugins/snowflake/create_external_table.sql index f48b87a7..851580f9 100644 --- a/macros/plugins/snowflake/create_external_table.sql +++ b/macros/plugins/snowflake/create_external_table.sql @@ -26,12 +26,14 @@ {%- 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 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) {%- endset %} - {{column_quoted}} {{column.data_type}} as ({{col_expression}}::{{column.data_type}}) + {{column_alias_quoted}} {{column.data_type}} as ({{col_expression}}::{{column.data_type}}) {{- ',' if not loop.last -}} {% endfor %} {% else %} From 0460d59a9f03d5efa31c214d80c911ae44daba41 Mon Sep 17 00:00:00 2001 From: cakkinep Date: Wed, 10 Apr 2024 23:09:37 -0400 Subject: [PATCH 2/3] added column alias test --- .../models/plugins/snowflake/snowflake_external.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/integration_tests/models/plugins/snowflake/snowflake_external.yml b/integration_tests/models/plugins/snowflake/snowflake_external.yml index 195cca9d..5d1e0eac 100644 --- a/integration_tests/models/plugins/snowflake/snowflake_external.yml +++ b/integration_tests/models/plugins/snowflake/snowflake_external.yml @@ -93,6 +93,14 @@ sources: quote: true data_type: varchar(64) tests: *same-rowcount + + - name: people_csv_needing_alias_column + external: *csv-people + columns: + - name: SELECT + alias: SELECT_COL + data_type: varchar(64) + tests: *same-rowcount - name: people_json_unpartitioned_no_columns external: *json-people From 91e80c720589182c15d11b84f90dc4cefcdccdef Mon Sep 17 00:00:00 2001 From: cakkinep Date: Wed, 10 Apr 2024 23:27:53 -0400 Subject: [PATCH 3/3] adding integration test for alias column --- .../models/plugins/snowflake/snowflake_external.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/integration_tests/models/plugins/snowflake/snowflake_external.yml b/integration_tests/models/plugins/snowflake/snowflake_external.yml index 5d1e0eac..4e8c24b6 100644 --- a/integration_tests/models/plugins/snowflake/snowflake_external.yml +++ b/integration_tests/models/plugins/snowflake/snowflake_external.yml @@ -20,6 +20,9 @@ sources: data_type: varchar(64) - name: email data_type: varchar(64) + - name: email + alias: email_alias + data_type: varchar(64) tests: &equal-to-the-people - dbt_utils.equality: compare_model: ref('people') @@ -28,6 +31,7 @@ sources: - first_name - last_name - email + - email_alias - name: people_csv_partitioned external: @@ -93,14 +97,6 @@ sources: quote: true data_type: varchar(64) tests: *same-rowcount - - - name: people_csv_needing_alias_column - external: *csv-people - columns: - - name: SELECT - alias: SELECT_COL - data_type: varchar(64) - tests: *same-rowcount - name: people_json_unpartitioned_no_columns external: *json-people