Skip to content

Commit eeea5cc

Browse files
authored
Merge pull request #113 from fishtown-analytics/fix/cast-in-union-tables
fix errant uses of safe_cast
2 parents 3a3a397 + 4f3d3aa commit eeea5cc

File tree

7 files changed

+47
-25
lines changed

7 files changed

+47
-25
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ jobs:
6363
cd integration_tests
6464
dbt deps --target snowflake
6565
dbt seed --target snowflake --full-refresh
66-
dbt run --target snowflake --exclude test_unpivot
67-
dbt test --target snowflake --exclude test_unpivot
66+
dbt run --target snowflake
67+
dbt test --target snowflake
6868
6969
- run:
7070
name: "Run Tests - BigQuery"
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
id,name,favorite_color
2-
1,"drew",
3-
2,"bob",
4-
3,"alice",
5-
1,,"green"
6-
2,,"pink"
1+
id,name,favorite_color,favorite_number
2+
1,"drew",,pi
3+
2,"bob",,e
4+
3,"alice",,4
5+
1,,"green",7
6+
2,,"pink",13
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
id,name
2-
1,drew
3-
2,bob
4-
3,alice
1+
id,name,favorite_number
2+
1,drew,pi
3+
2,bob,e
4+
3,alice,4
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
id,favorite_color
2-
1,green
3-
2,pink
1+
id,favorite_color,favorite_number
2+
1,green,7
3+
2,pink,13
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
{{ dbt_utils.unpivot(
2-
table=ref('data_unpivot'),
3-
cast_to=dbt_utils.type_string(),
4-
exclude=['customer_id','created_at']
51

6-
) }}
2+
-- snowflake messes with these tests pretty badly since the
3+
-- output of the macro considers the casing of the source
4+
-- table columns. Using some hacks here to get this to work,
5+
-- but we should consider lowercasing the unpivot macro output
6+
-- at some point in the future for consistency
7+
8+
{% if target.name == 'snowflake' %}
9+
{% set exclude = ['CUSTOMER_ID', 'CREATED_AT'] %}
10+
{% else %}
11+
{% set exclude = ['customer_id', 'created_at'] %}
12+
{% endif %}
13+
14+
select
15+
customer_id,
16+
created_at,
17+
case
18+
when '{{ target.name }}' = 'snowflake' then lower(field_name)
19+
else field_name
20+
end as field_name,
21+
value
22+
23+
from (
24+
{{ dbt_utils.unpivot(
25+
table=ref('data_unpivot'),
26+
cast_to=dbt_utils.type_string(),
27+
exclude=exclude
28+
) }}
29+
) as sbq

macros/sql/union.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@
5656
(
5757
select
5858

59-
{{ dbt_utils.safe_cast(dbt_utils.string_literal(table), dbt_utils.type_string()) }} as _dbt_source_table,
59+
cast({{ dbt_utils.string_literal(table) }} as {{ dbt_utils.type_string() }}) as _dbt_source_table,
6060

6161
{% for col_name in ordered_column_names -%}
6262

6363
{%- set col = column_superset[col_name] %}
6464
{%- set col_type = column_override.get(col.column, col.data_type) %}
6565
{%- set col_name = adapter.quote(col_name) if col_name in table_columns[table] else 'null' %}
66-
67-
{{ dbt_utils.safe_cast(col_name, col_type) }} as {{ col.quoted }} {% if not loop.last %},{% endif %}
66+
cast({{ col_name }} as {{ col_type }}) as {{ col.quoted }} {% if not loop.last %},{% endif %}
6867
{%- endfor %}
6968

7069
from {{ table }}

macros/sql/unpivot.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Arguments:
2828
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}
2929

3030
{%- for col in cols -%}
31-
{%- if col.column not in exclude -%}
31+
{%- if col.column.lower() not in exclude|map('lower') -%}
3232
{% set _ = include_cols.append(col) %}
3333
{%- endif %}
3434
{%- endfor %}
@@ -40,7 +40,7 @@ Arguments:
4040
{{ exclude_col }},
4141
{%- endfor %}
4242
cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as field_name,
43-
{{ dbt_utils.safe_cast(field=col.column, type=cast_to) }} as value
43+
cast({{ col.column }} as {{ cast_to }}) as value
4444
from {{ table }}
4545
{% if not loop.last -%}
4646
union all

0 commit comments

Comments
 (0)