Skip to content

Commit

Permalink
implement safe_cast + add tests for array, map, named_struct for unit…
Browse files Browse the repository at this point in the history
… testing
  • Loading branch information
MichelleArk committed Feb 20, 2024
1 parent 24bc6c1 commit 4e3b10c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 11 additions & 0 deletions dbt/include/spark/macros/utils/safe_cast.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% macro spark__safe_cast(field, type) %}
{%- if cast_from_string_unsupported_for(type) and field is string -%}
cast({{field.strip('"').strip("'")}} as {{type}})
{%- else -%}
safe_cast({{field}} as {{type}})
{%- endif -%}
{% endmacro %}

{% macro cast_from_string_unsupported_for(type) %}
{{ return(type.lower().startswith('struct') or type.lower().startswith('array') or type.lower().startswith('map')) }}
{% endmacro %}
9 changes: 6 additions & 3 deletions tests/functional/adapter/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ def data_types(self):
["true", "true"],
["date '2011-11-11'", "2011-11-11"],
["timestamp '2013-11-03 00:00:00-0'", "2013-11-03 00:00:00-0"],
# ["map(struct('Hello', 'World'), 'Greeting')", '''"map(struct('Hello', 'World'), 'Greeting')"'''],
# ['named_struct("a", 1, "b", 2, "c", 3)', """'named_struct("a", 1, "b", 2, "c", 3)'"""],
# ["array(1, 2, 3)", "'array(1, 2, 3)'"],
["array(1, 2, 3)", "'array(1, 2, 3)'"],
[
"map('10', 't', '15', 'f', '20', NULL)",
"""'map("10", "t", "15", "f", "20", NULL)'""",
],
['named_struct("a", 1, "b", 2, "c", 3)', """'named_struct("a", 1, "b", 2, "c", 3)'"""],
]


Expand Down

0 comments on commit 4e3b10c

Please sign in to comment.