Skip to content

Commit

Permalink
Merge pull request #113 from fishtown-analytics/fix/cast-in-union-tables
Browse files Browse the repository at this point in the history
fix errant uses of safe_cast
  • Loading branch information
drewbanin authored Jan 10, 2019
2 parents 3a3a397 + 4f3d3aa commit eeea5cc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ jobs:
cd integration_tests
dbt deps --target snowflake
dbt seed --target snowflake --full-refresh
dbt run --target snowflake --exclude test_unpivot
dbt test --target snowflake --exclude test_unpivot
dbt run --target snowflake
dbt test --target snowflake
- run:
name: "Run Tests - BigQuery"
Expand Down
12 changes: 6 additions & 6 deletions integration_tests/data/sql/data_union_expected.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id,name,favorite_color
1,"drew",
2,"bob",
3,"alice",
1,,"green"
2,,"pink"
id,name,favorite_color,favorite_number
1,"drew",,pi
2,"bob",,e
3,"alice",,4
1,,"green",7
2,,"pink",13
8 changes: 4 additions & 4 deletions integration_tests/data/sql/data_union_table_1.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id,name
1,drew
2,bob
3,alice
id,name,favorite_number
1,drew,pi
2,bob,e
3,alice,4
6 changes: 3 additions & 3 deletions integration_tests/data/sql/data_union_table_2.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id,favorite_color
1,green
2,pink
id,favorite_color,favorite_number
1,green,7
2,pink,13
33 changes: 28 additions & 5 deletions integration_tests/models/sql/test_unpivot.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
{{ dbt_utils.unpivot(
table=ref('data_unpivot'),
cast_to=dbt_utils.type_string(),
exclude=['customer_id','created_at']

) }}
-- 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,
case
when '{{ target.name }}' = 'snowflake' then lower(field_name)
else field_name
end as field_name,
value

from (
{{ dbt_utils.unpivot(
table=ref('data_unpivot'),
cast_to=dbt_utils.type_string(),
exclude=exclude
) }}
) as sbq
5 changes: 2 additions & 3 deletions macros/sql/union.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@
(
select

{{ dbt_utils.safe_cast(dbt_utils.string_literal(table), dbt_utils.type_string()) }} as _dbt_source_table,
cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as _dbt_source_table,

{% for col_name in ordered_column_names -%}

{%- set col = column_superset[col_name] %}
{%- set col_type = column_override.get(col.column, col.data_type) %}
{%- set col_name = adapter.quote(col_name) if col_name in table_columns[table] else 'null' %}

{{ dbt_utils.safe_cast(col_name, col_type) }} as {{ col.quoted }} {% if not loop.last %},{% endif %}
cast({{ col_name }} as {{ col_type }}) as {{ col.quoted }} {% if not loop.last %},{% endif %}
{%- endfor %}

from {{ table }}
Expand Down
4 changes: 2 additions & 2 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Arguments:
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}

{%- for col in cols -%}
{%- if col.column not in exclude -%}
{%- if col.column.lower() not in exclude|map('lower') -%}
{% set _ = include_cols.append(col) %}
{%- endif %}
{%- endfor %}
Expand All @@ -40,7 +40,7 @@ Arguments:
{{ exclude_col }},
{%- endfor %}
cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as field_name,
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
cast({{ col.column }} as {{ cast_to }}) as value
from {{ table }}
{% if not loop.last -%}
union all
Expand Down

0 comments on commit eeea5cc

Please sign in to comment.