Skip to content

Commit

Permalink
Update unpivot.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
GtheSheep committed Nov 7, 2022
1 parent 05c8e22 commit 880bf7f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
6 changes: 3 additions & 3 deletions integration_tests/.env/postgres.env
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions integration_tests/data/sql/data_unpivot_quote_identifiers.csv
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 2 additions & 17 deletions integration_tests/models/sql/test_unpivot_quote_identifiers.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
18 changes: 7 additions & 11 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.") }}
Expand All @@ -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 }}

Expand Down

0 comments on commit 880bf7f

Please sign in to comment.