Skip to content

Commit

Permalink
geography and geometry types + cast refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Feb 1, 2024
1 parent 9a2bfe8 commit cd2513d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions dbt/include/snowflake/macros/utils/cast.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% macro snowflake__cast(field, type) %}
{% if (type|upper == "GEOGRAPHY") -%}
to_geography({{field}} as {{type}})
to_geography({{field}})
{% elif (type|upper == "GEOMETRY") -%}
to_geometry({{field}} as {{type}})
to_geometry({{field}})
{% else -%}
cast({{field}} as {{type}})
{% endif -%}
Expand Down
6 changes: 5 additions & 1 deletion dbt/include/snowflake/macros/utils/safe_cast.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% macro snowflake__safe_cast(field, type) %}
{% if (type|upper != "VARIANT") -%}
{% if type|upper == "VARIANT" -%}
{% set field_as_sting = dbt.string_literal(field) if field is number else field %}
try_cast({{field_as_sting}} as {{type}})
{% elif type|upper == "GEOMETRY" -%}
try_to_geometry({{field}})
{% elif type|upper == "GEOGRAPHY" -%}
try_to_geography({{field}})
{% else -%}
{{ adapter.dispatch('cast', 'dbt')(field, type) }}
{% endif -%}
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/adapter/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def data_types(self):
["'2013-11-03 00:00:00-0'::TIMESTAMPTZ", "2013-11-03 00:00:00-0"],
["TO_NUMBER('3', 10, 9)", "3"],
["3::VARIANT", "3"],
["TO_GEOMETRY('POINT(1820.12 890.56)')", "POINT(1820.12 890.56)"],
["TO_GEOGRAPHY('POINT(-122.35 37.55)')", "POINT(-122.35 37.55)"],
[
"{'Alberta':'Edmonton','Manitoba':'Winnipeg'}",
"{'Alberta':'Edmonton','Manitoba':'Winnipeg'}",
Expand Down

0 comments on commit cd2513d

Please sign in to comment.