From 880bf7f321fabcad39c17e9066d137e025cab91d Mon Sep 17 00:00:00 2001 From: Gary James Date: Sat, 8 Oct 2022 14:10:57 +0100 Subject: [PATCH] Update unpivot.sql --- integration_tests/.env/postgres.env | 6 +++--- .../sql/data_unpivot_quote_identifiers.csv | 8 ++++---- ...ata_unpivot_quote_identifiers_expected.csv | 12 ++++++------ integration_tests/dbt_project.yml | 3 +++ .../sql/test_unpivot_quote_identifiers.sql | 19 ++----------------- macros/sql/unpivot.sql | 18 +++++++----------- 6 files changed, 25 insertions(+), 41 deletions(-) diff --git a/integration_tests/.env/postgres.env b/integration_tests/.env/postgres.env index c3f7dd88..e7d6f783 100644 --- a/integration_tests/.env/postgres.env +++ b/integration_tests/.env/postgres.env @@ -1,5 +1,5 @@ POSTGRES_TEST_HOST=localhost -POSTGRES_TEST_USER=root +POSTGRES_TEST_USER=postgres POSTGRES_TEST_PASS='' -POSTGRES_TEST_PORT=5432 -POSTGRES_TEST_DBNAME=circle_test +POSTGRES_TEST_PORT=5444 +POSTGRES_TEST_DBNAME=GtheSheep diff --git a/integration_tests/data/sql/data_unpivot_quote_identifiers.csv b/integration_tests/data/sql/data_unpivot_quote_identifiers.csv index ab6865f8..75891b15 100644 --- a/integration_tests/data/sql/data_unpivot_quote_identifiers.csv +++ b/integration_tests/data/sql/data_unpivot_quote_identifiers.csv @@ -1,4 +1,4 @@ -customer_id,created_at,Status,Segment -123,2017-01-01,active,tier 1 -234,2017-02-01,active,tier 3 -567,2017-03-01,churned,tier 2 +customer_id,created_at,status,"2022-01-01" +123,"2017-01-01","active",1 +234,"2017-02-01","active",2 +567,"2017-03-01","churned",3 diff --git a/integration_tests/data/sql/data_unpivot_quote_identifiers_expected.csv b/integration_tests/data/sql/data_unpivot_quote_identifiers_expected.csv index 82e6700f..798e55fd 100644 --- a/integration_tests/data/sql/data_unpivot_quote_identifiers_expected.csv +++ b/integration_tests/data/sql/data_unpivot_quote_identifiers_expected.csv @@ -1,7 +1,7 @@ customer_id,created_at,prop,val -123,2017-01-01,"Segment",tier 1 -123,2017-01-01,"Status",active -234,2017-02-01,"Segment",tier 3 -234,2017-02-01,"Status",active -567,2017-03-01,"Status",churned -567,2017-03-01,"Segment",tier 2 +123,"2017-01-01","2022-01-01",1 +123,"2017-01-01","status","active" +234,"2017-02-01","2022-01-01",2 +234,"2017-02-01","status","active" +567,"2017-03-01","2022-01-01",3 +567,"2017-03-01","status","churned" diff --git a/integration_tests/dbt_project.yml b/integration_tests/dbt_project.yml index 61b0c5db..6e015156 100644 --- a/integration_tests/dbt_project.yml +++ b/integration_tests/dbt_project.yml @@ -61,6 +61,9 @@ seeds: min_value: float max_value: float + data_unpivot_quote_identifiers: + +quote_columns: true + schema_tests: data_test_sequential_timestamps: +column_types: diff --git a/integration_tests/models/sql/test_unpivot_quote_identifiers.sql b/integration_tests/models/sql/test_unpivot_quote_identifiers.sql index e1018d35..fc43cb6b 100644 --- a/integration_tests/models/sql/test_unpivot_quote_identifiers.sql +++ b/integration_tests/models/sql/test_unpivot_quote_identifiers.sql @@ -1,18 +1,3 @@ - --- snowflake messes with these tests pretty badly since the --- output of the macro considers the casing of the source --- table columns. Using some hacks here to get this to work, --- but we should consider lowercasing the unpivot macro output --- at some point in the future for consistency - - -{% if target.name == 'snowflake' %} - {% set exclude = ['CUSTOMER_ID', 'CREATED_AT'] %} -{% else %} - {% set exclude = ['customer_id', 'created_at'] %} -{% endif %} - - select customer_id, created_at, @@ -22,8 +7,8 @@ select from ( {{ dbt_utils.unpivot( relation=ref('data_unpivot_quote_identifiers'), - cast_to=dbt_utils.type_string(), - exclude=exclude, + cast_to=type_string(), + exclude=['customer_id', 'created_at'], field_name='prop', value_name='val', quote_identifiers=true diff --git a/macros/sql/unpivot.sql b/macros/sql/unpivot.sql index 22cf40b7..fa678501 100644 --- a/macros/sql/unpivot.sql +++ b/macros/sql/unpivot.sql @@ -13,11 +13,11 @@ Arguments: quote_identifiers: Whether to surround column aliases with double quotes, default is false #} -{% macro unpivot(relation=none, cast_to='varchar', exclude=none, remove=none, field_name='field_name', value_name='value', quote_identifiers=fals) -%} +{% macro unpivot(relation=none, cast_to='varchar', exclude=none, remove=none, field_name='field_name', value_name='value', quote_identifiers=false) -%} {{ return(adapter.dispatch('unpivot', 'dbt_utils')(relation, cast_to, exclude, remove, field_name, value_name, quote_identifiers)) }} {% endmacro %} -{% macro default__unpivot(relation=none, cast_to='varchar', exclude=none, remove=none, field_name='field_name', value_name='value', quote_identifiers=fals) -%} +{% macro default__unpivot(relation=none, cast_to='varchar', exclude=none, remove=none, field_name='field_name', value_name='value', quote_identifiers=false) -%} {% if not relation %} {{ exceptions.raise_compiler_error("Error: argument `relation` is required for `unpivot` macro.") }} @@ -44,21 +44,17 @@ Arguments: {%- for col in include_cols -%} + {% set column_identifier = adapter.quote(col.column) if quote_identifiers else col.column %} select {%- for exclude_col in exclude %} {{ exclude_col }}, {%- endfor %} - cast('{{ col.column }}' as {{ type_string() }}) as {{ field_name }}, - cast( {% if quote_identifiers %} - {% set column_identifier = adapter.quote(col.column) %} + cast( '{{ col.column }}' as {{ type_string() }}) as {{ field_name }}, + cast( {% if col.data_type == 'boolean' %} + {{ cast_bool_to_text(column_identifier) }} {% else %} - {% set column_identifier = col.column %} - {% endif %} - {% if col.data_type == 'boolean' %} - {{ dbt_utils.cast_bool_to_text(column_identifier) }} - {% else %} - {{ column_identifier }} + {{ column_identifier }} {% endif %} as {{ cast_to }}) as {{ value_name }}