Skip to content

Commit

Permalink
Following up on drew s PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
remigabillet committed Jun 19, 2019
1 parent 03657b5 commit 96ae631
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ Arguments:

- table: Table name, required
- cast_to: The data type to cast the unpivoted values to, default is varchar
- exclude: A list of columns to exclude from the unpivot.
- exclude: A list of columns to exclude from the unpivot operation but keep in the resulting table.
- remove: A list of columns to remove from the resulting table
- field_name: column name in the resulting table for field
- value_name: column name in the resulting table for value

---
### Web
Expand Down
15 changes: 9 additions & 6 deletions integration_tests/data/sql/data_unpivot_expected.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
customer_id,created_at,prop,val
123,2017-01-01,status,active
123,2017-01-01,segment,tier 1
234,2017-02-01,status,active
234,2017-02-01,segment,tier 3
567,2017-03-01,status,churned
567,2017-03-01,segment,tier 2
123,"2017-01-01","segment","tier 1"
123,"2017-01-01","status","active"
123,"2017-01-01","name","name 1"
234,"2017-02-01","segment","tier 3"
234,"2017-02-01","status","active"
234,"2017-02-01","name","name 3"
567,"2017-03-01","status","churned"
567,"2017-03-01","name","name 2"
567,"2017-03-01","segment","tier 2"
5 changes: 5 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ models:
- dbt_utils.equality:
compare_model: ref('data_unpivot_original_api_expected')

- name: test_unpivot
tests:
- dbt_utils.equality:
compare_model: ref('data_unpivot_expected')

- name: test_star
tests:
- dbt_utils.equality:
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/models/sql/test_unpivot_original_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ select
customer_id,
created_at,
case
when '{{ target.name }}' = 'snowflake' then lower(field_name)
when '{{ target.name }}' = 'snowflake' then lower(FIELD_NAME)
else field_name
end as field_name,
value
Expand Down
4 changes: 2 additions & 2 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Arguments:
{{ exclude_col }},
{%- endfor %}

cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as "{{ field_name }}",
cast({{ col.column }} as {{ cast_to }}) as "{{ value_name }}"
cast('{{ col.column }}' as {{ dbt_utils.type_string() }}) as {{ field_name }},
cast({{ col.column }} as {{ cast_to }}) as {{ value_name }}

from {{ table }}

Expand Down

0 comments on commit 96ae631

Please sign in to comment.