Skip to content

Commit 4acbdd3

Browse files
avishalomclrcrl
authored andcommitted
Adding some logic to handle unpivoting boolean columns (#305)
Readme & deleted the 'remove' line which does nothing. Finish off upivot boolean logic Update changelog [ci skip]
1 parent 9d9e720 commit 4acbdd3

File tree

9 files changed

+79
-4
lines changed

9 files changed

+79
-4
lines changed

CHANGELOG.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# dbt-utils v0.6.5 (unreleased)
22
## Features
33
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
4-
* Make `expression_is_true` work as a column test (code originally in [#226](https://github.com/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https://github.com/elliottohara), merged via [#313])
4+
* Make `expression_is_true` work as a column test (code originally in [#226](https://github.com/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https://github.com/elliottohara), merged via [#313])
55

66
## Fixes
7-
7+
* Handle booleans gracefully in the unpivot macro ([#305](https://github.com/fishtown-analytics/dbt-utils/pull/305) [@avishalom](https://github.com/avishalom))
88
## Under the hood
99

1010
# dbt-utils v0.6.4
@@ -24,7 +24,6 @@
2424

2525
- Bump `require-dbt-version` to `[">=0.18.0", "<0.20.0"]` to support dbt v0.19.0 ([#308](https://github.com/fishtown-analytics/dbt-utils/pull/308), [#309](https://github.com/fishtown-analytics/dbt-utils/pull/309))
2626

27-
## Fixes
2827

2928
# dbt-utils v0.6.2
3029

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ Arguments:
707707

708708
#### unpivot ([source](macros/sql/unpivot.sql))
709709
This macro "un-pivots" a table from wide format to long format. Functionality is similar to pandas [melt](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html) function.
710+
Boolean values are replaced with the strings 'true'|'false'
710711

711712
Usage:
712713
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,my_bool
2+
1,true
3+
2,false
4+
3,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
customer_id,created_at,status,segment,is_updated
2+
123,2017-01-01,active,tier 1,TRUE
3+
234,2017-02-01,active,tier 3,FALSE
4+
567,2017-03-01,churned,tier 2,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
customer_id,created_at,prop,val
2+
123,2017-01-01,segment,tier 1
3+
123,2017-01-01,status,active
4+
123,2017-01-01,is_updated,true
5+
234,2017-02-01,segment,tier 3
6+
234,2017-02-01,status,active
7+
234,2017-02-01,is_updated,false
8+
567,2017-03-01,status,churned
9+
567,2017-03-01,is_updated,
10+
567,2017-03-01,segment,tier 2

integration_tests/models/sql/schema.yml

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ models:
9696
- dbt_utils.equality:
9797
compare_model: ref('data_unpivot_expected')
9898

99+
- name: test_unpivot_bool
100+
tests:
101+
- dbt_utils.equality:
102+
compare_model: ref('data_unpivot_bool_expected')
103+
99104
- name: test_star
100105
tests:
101106
- dbt_utils.equality:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
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+
15+
select
16+
customer_id,
17+
created_at,
18+
case
19+
when '{{ target.name }}' = 'snowflake' then lower(prop)
20+
else prop
21+
end as prop,
22+
val
23+
24+
from (
25+
{{ dbt_utils.unpivot(
26+
relation=ref('data_unpivot_bool'),
27+
cast_to=dbt_utils.type_string(),
28+
exclude=exclude,
29+
field_name='prop',
30+
value_name='val'
31+
) }}
32+
) as sbq
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% macro cast_bool_to_text(field) %}
2+
{{ adapter.dispatch('cast_bool_to_text', packages = dbt_utils._get_utils_namespaces()) (field) }}
3+
{% endmacro %}
4+
5+
6+
{% macro default__cast_bool_to_text(field) %}
7+
cast({{ field }} as text)
8+
{% endmacro %}
9+
10+
{% macro redshift__cast_bool_to_text(field) %}
11+
case
12+
when {{ field }} is true then 'true'
13+
when {{ field }} is false then 'false'
14+
end::text
15+
{% endmacro %}

macros/sql/unpivot.sql

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ Arguments:
6262
{%- endfor %}
6363

6464
cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as {{ field_name }},
65-
cast({{ col.column }} as {{ cast_to }}) as {{ value_name }}
65+
cast( {% if col.data_type == 'boolean' %}
66+
{{ dbt_utils.cast_bool_to_text(col.column) }}
67+
{% else %}
68+
{{ col.column }}
69+
{% endif %}
70+
as {{ cast_to }}) as {{ value_name }}
6671

6772
from {{ relation }}
6873

0 commit comments

Comments
 (0)