Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spike] [Snowflake] Support complex types unit testing #9131

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
-- 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 actual_or_expected
{% 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") }}
Copy link
Contributor Author

@jtcohen6 jtcohen6 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a column name is quoted, Snowflake will preserve its case. The alternative is to lowercase column names in the agate result before we access it here:

lambda row: row["actual_or_expected"] == actual_or_expected

Otherwise, we get one of these:

22:45:30  Unhandled error while executing target/run/my_dbt_project/models/my_model.yml/models/my_model__test_my_model.sql
'actual_or_expected'
22:45:30  1 of 1 ERROR my_model__test_my_model ........................................... [ERROR in 24.16s]
22:45:30
22:45:30  Finished running 1 unit_test in 0 hours 0 minutes and 24.17 seconds (24.17s).
22:45:30
22:45:30  Completed with 1 error and 0 warnings:
22:45:30
22:45:30    'actual_or_expected'
22:45:30

from (
{{ main_sql }}
) _dbt_internal_unit_test_actual
),
-- 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 actual_or_expected
{% 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") }}
from (
{{ expected_fixture_sql }}
) _dbt_internal_unit_test_expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% set columns_in_relation = adapter.get_column_schema_from_query(get_empty_subquery_sql(sql)) %}
{%- set column_name_to_data_types = {} -%}
{%- for column in columns_in_relation -%}
{%- do column_name_to_data_types.update({column.name: column.data_type}) -%}
{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is naively assuming that the user has provided their columns in all lowercase. We'd obviously want to lower on both sides of the comparison.

{%- endfor -%}

{% set unit_test_sql = get_unit_test_sql(sql, get_expected_sql(expected_rows, column_name_to_data_types), tested_expected_column_names) %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

{%- set column_name_to_data_types = {} -%}
{%- for column in columns_in_relation -%}
{%- do column_name_to_data_types.update({column.name: column.data_type}) -%}
{#-- This needs to be a case-insensitive comparison --#}
{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
{%- endfor -%}
{%- endif -%}

Expand Down Expand Up @@ -67,7 +68,7 @@ union all
{%- for column_name, column_value in row.items() -%}
{% set row_update = {column_name: column_value} %}
{%- if column_value is string -%}
{%- set row_update = {column_name: safe_cast(dbt.string_literal(column_value), column_name_to_data_types[column_name]) } -%}
{%- set row_update = {column_name: safe_cast(dbt.string_literal(dbt.escape_single_quotes(column_value)), column_name_to_data_types[column_name]) } -%}
Copy link
Contributor Author

@jtcohen6 jtcohen6 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that it's:

select cast(parse_json('[\'a\',\'b\',\'c\']') as ARRAY) AS tested_column

Rather than:

select cast(parse_json('['a','b','c']') as ARRAY) AS tested_column

{%- elif column_value is none -%}
{%- set row_update = {column_name: safe_cast('null', column_name_to_data_types[column_name]) } -%}
{%- else -%}
Expand Down
Loading