Skip to content

Commit

Permalink
fix: cast number param for dateadd to int for databricks
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperSelmanUiPath committed Nov 17, 2023
1 parent 3221bc2 commit fec32aa
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions macros/multiple_databases/dateadd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
{%- elif target.type == 'sqlserver' -%}
dateadd({{ datepart }}, {{ number }}, try_convert(datetime2, {{ date_or_datetime_field }}))
{%- elif target.type == 'databricks' -%}
{%- set number_int -%}
try_cast({{ number }} as INT)
{%- endset -%}
{%- set datetime_field -%}
try_to_timestamp({{ date_or_datetime_field }})
{%- endset -%}
{%- set time_field -%}
unix_millis({{ datetime_field }}) - unix_millis(date_trunc('DD', {{ datetime_field }}))
{%- endset -%}
{%- if datepart == 'millisecond' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }})
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number_int }})
{%- elif datepart == 'second' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*1000)
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number_int }}*1000)
{%- elif datepart == 'minute' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*60000)
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number_int }}*60000)
{%- elif datepart == 'hour' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*3600000)
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number_int }}*3600000)
{%- elif datepart == 'day' -%}
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number }}))) + {{ time_field }})
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number_int }}))) + {{ time_field }})
{%- elif datepart == 'week' -%}
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number }}*7))) + {{ time_field }})
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number_int }}*7))) + {{ time_field }})
{%- elif datepart == 'month' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}))) + {{ time_field }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number_int }}))) + {{ time_field }})
{%- elif datepart == 'quarter' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}*3))) + {{ time_field }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number_int }}*3))) + {{ time_field }})
{%- elif datepart == 'year' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}*12))) + {{ time_field }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number_int }}*12))) + {{ time_field }})
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

0 comments on commit fec32aa

Please sign in to comment.