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 f874789 commit 68be94e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 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
10 changes: 1 addition & 9 deletions integration_tests/models/sql/test_unpivot_quote_identifiers.sql
Original file line number Diff line number Diff line change
@@ -1,11 +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 %}
Expand All @@ -22,7 +14,7 @@ select
from (
{{ dbt_utils.unpivot(
relation=ref('data_unpivot_quote_identifiers'),
cast_to=dbt_utils.type_string(),
cast_to=type_string(),
exclude=exclude,
field_name='prop',
value_name='val',
Expand Down
14 changes: 5 additions & 9 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 68be94e

Please sign in to comment.