Skip to content

Commit

Permalink
refactor safe_cast for readability, changelog entry
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Feb 21, 2024
1 parent fa083cf commit f45280d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240220-195925.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Implement spark__safe_cast and add functional tests for unit testing
time: 2024-02-20T19:59:25.907821-05:00
custom:
Author: michelleark
Issue: "987"
7 changes: 2 additions & 5 deletions dbt/include/spark/macros/utils/safe_cast.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{% macro spark__safe_cast(field, type) %}
{%- if cast_from_string_unsupported_for(type) and field is string -%}
cast({{field.strip('"').strip("'")}} as {{type}})
{%- else -%}
cast({{field}} as {{type}})
{%- endif -%}
{%- set field_clean = field.strip('"').strip("'") if (cast_from_string_unsupported_for(type) and field is string) else field -%}
cast({{field_clean}} as {{type}})
{% endmacro %}

{% macro cast_from_string_unsupported_for(type) %}
Expand Down

0 comments on commit f45280d

Please sign in to comment.