From 8bad96ae1c598d59a510b7a2762992bfa98b52b1 Mon Sep 17 00:00:00 2001 From: Pedro Sousa Date: Thu, 18 Apr 2024 18:00:18 +0100 Subject: [PATCH] Cease using uppercase for snowflake column identifiers --- .../models/model_with_quoted_columns_1.sql | 6 ++ .../models/model_with_quoted_columns_2.sql | 17 ++++++ .../unit/model_with_quoted_columns_2.sql | 14 +++++ macros/utils.sql | 56 +++++++++++-------- 4 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 integration-tests/models/model_with_quoted_columns_1.sql create mode 100644 integration-tests/models/model_with_quoted_columns_2.sql create mode 100644 integration-tests/tests/unit/model_with_quoted_columns_2.sql diff --git a/integration-tests/models/model_with_quoted_columns_1.sql b/integration-tests/models/model_with_quoted_columns_1.sql new file mode 100644 index 0000000..a3c694b --- /dev/null +++ b/integration-tests/models/model_with_quoted_columns_1.sql @@ -0,0 +1,6 @@ +select 1 as lower, + 2 as UPPER, + 3 as MiXeD, + 4 as "lower quoted", + 5 as "UPPER QUOTED", + 6 as "MiXeD Quoted" diff --git a/integration-tests/models/model_with_quoted_columns_2.sql b/integration-tests/models/model_with_quoted_columns_2.sql new file mode 100644 index 0000000..899c783 --- /dev/null +++ b/integration-tests/models/model_with_quoted_columns_2.sql @@ -0,0 +1,17 @@ +select + lower, + UPPER, + MiXeD, + "lower quoted", + "UPPER QUOTED", + "MiXeD Quoted" +from {{ dbt_unit_testing.ref('model_with_quoted_columns_1') }} +where + (lower = 1 or lower is null) +and (UPPER = 2 or UPPER is null) +and (MiXeD = 3 or MiXeD is null) +and ("lower quoted" = 4 or "lower quoted" is null) +and ("UPPER QUOTED" = 5 or "UPPER QUOTED" is null) +and ("MiXeD Quoted" = 6 or "MiXeD Quoted" is null) + + diff --git a/integration-tests/tests/unit/model_with_quoted_columns_2.sql b/integration-tests/tests/unit/model_with_quoted_columns_2.sql new file mode 100644 index 0000000..655c706 --- /dev/null +++ b/integration-tests/tests/unit/model_with_quoted_columns_2.sql @@ -0,0 +1,14 @@ +{{ + config( + tags=['unit-test', 'bigquery', 'snowflake', 'postgres'] + ) +}} + +{% call dbt_unit_testing.test('model_with_quoted_columns_2') %} + {% call dbt_unit_testing.mock_ref ('model_with_quoted_columns_1', options={"include_missing_columns": true}) %} + select 1 as lower + {% endcall %} + {% call dbt_unit_testing.expect() %} + select 1 as lower + {% endcall %} +{% endcall %} diff --git a/macros/utils.sql b/macros/utils.sql index 3055561..2df31af 100644 --- a/macros/utils.sql +++ b/macros/utils.sql @@ -25,7 +25,7 @@ {% endmacro %} {% macro quote_and_join_columns(columns) %} - {% set columns = dbt_unit_testing.map(columns, dbt_unit_testing.quote_identifier) | join(", ") %} + {% set columns = dbt_unit_testing.map(columns, dbt_unit_testing.quote_column_identifier) | join(", ") %} {{ return (columns) }} {% endmacro %} @@ -39,7 +39,7 @@ {% endmacro %} {% macro apply_transformations_to_column(column, column_transformations, use_alias) %} - {% set quoted_column = dbt_unit_testing.quote_identifier(column) %} + {% set quoted_column = dbt_unit_testing.quote_column_identifier(column) %} {% set transformation = column_transformations.get(column, "") %} {% if transformation != "" %} {% set transformation = transformation | replace("##column##", quoted_column) %} @@ -91,7 +91,7 @@ {% endmacro %} {% macro has_value(v) %} - {{ return (v is defined and v is not none) }} + {{ return (v is defined and v is not none) }} {% endmacro %} {% macro model_node (node) %} @@ -179,39 +179,47 @@ {{ return (dbt_unit_testing.deep_merge_jsons([unit_tests_config] + configs)) }} {% endmacro %} -{% macro quote_identifier(identifier) %} - {{ return(adapter.dispatch('quote_identifier','dbt_unit_testing')(identifier)) }} +{% macro quote_column_identifier(identifier) %} + {{ return(adapter.dispatch('quote_column_identifier','dbt_unit_testing')(identifier)) }} {% endmacro %} -{% macro default__quote_identifier(identifier) -%} - {% if identifier.startswith('"') %} - {{ return(identifier) }} - {% else %} - {{ return('"' ~ identifier ~ '"') }} - {% endif %} +{% macro default__quote_column_identifier(identifier) -%} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '"')) }} {%- endmacro %} -{% macro bigquery__quote_identifier(identifier) %} - {% if identifier.startswith('`') %} - {{ return(identifier) }} - {% else %} - {{ return('`' ~ identifier ~ '`') }} - {% endif %} +{% macro spark__quote_column_identifier(identifier) %} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '`')) }} +{% endmacro %} + +{% macro bigquery__quote_column_identifier(identifier) %} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '`')) }} {% endmacro %} {% macro databricks__quote_identifier(identifier) %} - {% if identifier.startswith('`') %} - {{ return(identifier) }} - {% else %} - {{ return('`' ~ identifier ~ '`') }} - {% endif %} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '`')) }} {% endmacro %} +{% macro snowflake__quote_column_identifier(identifier) %} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '"')) }} +{% endmacro %} + +{% macro quote_identifier(identifier) %} + {{ return(adapter.dispatch('quote_identifier','dbt_unit_testing')(identifier)) }} +{% endmacro %} + +{% macro default__quote_identifier(identifier) -%} + {{ return(dbt_unit_testing.quote_column_identifier(identifier)) }} +{%- endmacro %} + {% macro snowflake__quote_identifier(identifier) %} - {% if identifier.startswith('"') %} + {{ return(dbt_unit_testing.quote_identifier_if_needed(identifier, '"') | upper) }} +{% endmacro %} + +{% macro quote_identifier_if_needed(identifier, quote) %} + {% if identifier.startswith(quote) %} {{ return(identifier) }} {% else %} - {{ return('"' ~ identifier | upper ~ '"') }} + {{ return(quote ~ identifier ~ quote) }} {% endif %} {% endmacro %}