From 96ae63161780c175c98311ef24dc690b8d309388 Mon Sep 17 00:00:00 2001 From: Remi Gabillet Date: Wed, 19 Jun 2019 13:36:59 -0700 Subject: [PATCH] Following up on drew s PR review --- README.md | 5 ++++- .../data/sql/data_unpivot_expected.csv | 15 +++++++++------ integration_tests/models/sql/schema.yml | 5 +++++ .../models/sql/test_unpivot_original_api.sql | 2 +- macros/sql/unpivot.sql | 4 ++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 288dfb3fc..4eeffb914 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/integration_tests/data/sql/data_unpivot_expected.csv b/integration_tests/data/sql/data_unpivot_expected.csv index c74746ea7..bdfa5b229 100644 --- a/integration_tests/data/sql/data_unpivot_expected.csv +++ b/integration_tests/data/sql/data_unpivot_expected.csv @@ -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 \ No newline at end of file +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" diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index 0750f69fa..cea818f04 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -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: diff --git a/integration_tests/models/sql/test_unpivot_original_api.sql b/integration_tests/models/sql/test_unpivot_original_api.sql index 6718beee7..f6b9395a4 100644 --- a/integration_tests/models/sql/test_unpivot_original_api.sql +++ b/integration_tests/models/sql/test_unpivot_original_api.sql @@ -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 diff --git a/macros/sql/unpivot.sql b/macros/sql/unpivot.sql index ea06a56d7..12f0bfb85 100644 --- a/macros/sql/unpivot.sql +++ b/macros/sql/unpivot.sql @@ -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 }}