From 2df4a14b27a6f47394fbc8d375aaaa6e3e5b786f Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Thu, 9 May 2024 19:28:37 -0600 Subject: [PATCH 1/3] Apply column quoting to unit tests --- .../macros/materializations/tests/unit.sql | 13 ++++++++++--- .../macros/unit_test_sql/get_fixture_sql.sql | 11 +++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/dbt/include/global_project/macros/materializations/tests/unit.sql b/dbt/include/global_project/macros/materializations/tests/unit.sql index 78c6f6bc3..407954269 100644 --- a/dbt/include/global_project/macros/materializations/tests/unit.sql +++ b/dbt/include/global_project/macros/materializations/tests/unit.sql @@ -4,21 +4,28 @@ {% set expected_rows = config.get('expected_rows') %} {% set expected_sql = config.get('expected_sql') %} - {% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %} %} + {% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %} {%- set target_relation = this.incorporate(type='table') -%} {%- set temp_relation = make_temp_relation(target_relation)-%} {% do run_query(get_create_table_as_sql(True, temp_relation, get_empty_subquery_sql(sql))) %} {%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%} {%- set column_name_to_data_types = {} -%} + {%- set column_name_to_quoted = {} -%} {%- for column in columns_in_relation -%} {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} + {%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%} + {%- endfor -%} + + {%- set expected_column_names_quoted = [] -%} + {%- for column_name in tested_expected_column_names -%} + {%- do expected_column_names_quoted.append(column_name_to_quoted[column_name]) -%} {%- endfor -%} {% if not expected_sql %} - {% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types) %} + {% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types, column_name_to_quoted) %} {% endif %} - {% set unit_test_sql = get_unit_test_sql(sql, expected_sql, tested_expected_column_names) %} + {% set unit_test_sql = get_unit_test_sql(sql, expected_sql, expected_column_names_quoted) %} {% call statement('main', fetch_result=True) -%} diff --git a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql index 53d7a93b3..db6e23751 100644 --- a/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql +++ b/dbt/include/global_project/macros/unit_test_sql/get_fixture_sql.sql @@ -8,9 +8,12 @@ {%- set columns_in_relation = adapter.get_columns_in_relation(this_or_defer_relation) -%} {%- set column_name_to_data_types = {} -%} +{%- set column_name_to_quoted = {} -%} {%- for column in columns_in_relation -%} + {#-- This needs to be a case-insensitive comparison --#} {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} +{%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%} {%- endfor -%} {%- endif -%} @@ -28,7 +31,7 @@ {%- set default_row_copy = default_row.copy() -%} {%- do default_row_copy.update(formatted_row) -%} select -{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} +{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} {%- endfor %} {%- if not loop.last %} union all @@ -37,14 +40,14 @@ union all {%- if (rows | length) == 0 -%} select - {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%},{%- endif %} + {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %} {%- endfor %} limit 0 {%- endif -%} {% endmacro %} -{% macro get_expected_sql(rows, column_name_to_data_types) %} +{% macro get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %} {%- if (rows | length) == 0 -%} select * from dbt_internal_unit_test_actual @@ -53,7 +56,7 @@ union all {%- for row in rows -%} {%- set formatted_row = format_row(row, column_name_to_data_types) -%} select -{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name }}{% if not loop.last -%}, {%- endif %} +{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} {%- endfor %} {%- if not loop.last %} union all From cbc3b3e7a2c164f763bd96d378a303fcf37a14bb Mon Sep 17 00:00:00 2001 From: Mike Patek Date: Sat, 11 May 2024 08:44:21 -0400 Subject: [PATCH 2/3] Quote column names in dbt_internal_unit_test_actual and dbt_internal_unit_test_expected macros --- .../global_project/macros/materializations/tests/helpers.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/include/global_project/macros/materializations/tests/helpers.sql b/dbt/include/global_project/macros/materializations/tests/helpers.sql index a385d1eab..1718bbbaa 100644 --- a/dbt/include/global_project/macros/materializations/tests/helpers.sql +++ b/dbt/include/global_project/macros/materializations/tests/helpers.sql @@ -24,7 +24,7 @@ -- Build actual result given inputs with dbt_internal_unit_test_actual as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }} + {% for expected_column_name in expected_column_names %}{{ adapter.quote(expected_column_name) }}{% if not loop.last -%},{% endif %}{%- endfor -%}, {{ dbt.string_literal("actual") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ main_sql }} ) _dbt_internal_unit_test_actual @@ -32,7 +32,7 @@ with dbt_internal_unit_test_actual as ( -- Build expected result dbt_internal_unit_test_expected as ( select - {% for expected_column_name in expected_column_names %}{{expected_column_name}}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }} + {% for expected_column_name in expected_column_names %}{{ adapter.quote(expected_column_name) }}{% if not loop.last -%}, {% endif %}{%- endfor -%}, {{ dbt.string_literal("expected") }} as {{ adapter.quote("actual_or_expected") }} from ( {{ expected_fixture_sql }} ) _dbt_internal_unit_test_expected From eec445f04bdfc32896f8abf2e7ab7776e3dc60e2 Mon Sep 17 00:00:00 2001 From: Mike Patek Date: Sun, 12 May 2024 09:54:06 -0400 Subject: [PATCH 3/3] Add changelog entry --- .changes/unreleased/Fixes-20240512-095350.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240512-095350.yaml diff --git a/.changes/unreleased/Fixes-20240512-095350.yaml b/.changes/unreleased/Fixes-20240512-095350.yaml new file mode 100644 index 000000000..ef4b27c5f --- /dev/null +++ b/.changes/unreleased/Fixes-20240512-095350.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Apply column quoting to unit tests +time: 2024-05-12T09:53:50.381050004-04:00 +custom: + Author: mpatek dbeatty10 + Issue: "205"