Skip to content

Commit

Permalink
Finish off upivot boolean logic
Browse files Browse the repository at this point in the history
  • Loading branch information
clrcrl committed Dec 14, 2020
1 parent 7c1082d commit ed95b14
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions integration_tests/data/cross_db/data_cast_bool_to_text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,my_bool
1,true
2,false
3,
2 changes: 1 addition & 1 deletion integration_tests/data/sql/data_unpivot_bool.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
customer_id,created_at,status,segment,is_updated
123,2017-01-01,active,tier 1,TRUE
234,2017-02-01,active,tier 3,FALSE
567,2017-03-01,churned,tier 2,TRUE
567,2017-03-01,churned,tier 2,
18 changes: 9 additions & 9 deletions integration_tests/data/sql/data_unpivot_bool_expected.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
customer_id,created_at,prop,val
123,"2017-01-01","segment","tier 1"
123,"2017-01-01","status","active"
123,"2017-01-01","is_updated","true"
234,"2017-02-01","segment","tier 3"
234,"2017-02-01","status","active"
234,"2017-02-01","is_updated","false"
567,"2017-03-01","status","churned"
567,"2017-03-01","is_updated","true"
567,"2017-03-01","segment","tier 2"
123,2017-01-01,segment,tier 1
123,2017-01-01,status,active
123,2017-01-01,is_updated,true
234,2017-02-01,segment,tier 3
234,2017-02-01,status,active
234,2017-02-01,is_updated,false
567,2017-03-01,status,churned
567,2017-03-01,is_updated,
567,2017-03-01,segment,tier 2
15 changes: 15 additions & 0 deletions macros/cross_db_utils/cast_bool_to_text.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro cast_bool_to_text(field) %}
{{ adapter.dispatch('cast_bool_to_text', packages = dbt_utils._get_utils_namespaces()) (field) }}
{% endmacro %}


{% macro default__cast_bool_to_text(field) %}
cast({{ col }} as {{ type }})
{% endmacro %}

{% macro redshift__cast_bool_to_text(field) %}
case
when {{ field }} is true then 'true'
when {{ field }} is false then 'false'
end::text
{% endmacro %}
2 changes: 1 addition & 1 deletion macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Arguments:

cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as {{ field_name }},
cast( {% if col.data_type == 'boolean' %}
case when {{ col.column }} then 'true' else 'false' end
{{ dbt_utils.cast_bool_to_text(col.column) }}
{% else %}
{{ col.column }}
{% endif %}
Expand Down

0 comments on commit ed95b14

Please sign in to comment.