From fec32aa8df5e26899f4a64b02982e69b262687e8 Mon Sep 17 00:00:00 2001 From: Jasper Selman Date: Fri, 17 Nov 2023 14:17:13 +0100 Subject: [PATCH] fix: cast number param for dateadd to int for databricks --- macros/multiple_databases/dateadd.sql | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/macros/multiple_databases/dateadd.sql b/macros/multiple_databases/dateadd.sql index b095b87..db5df5e 100644 --- a/macros/multiple_databases/dateadd.sql +++ b/macros/multiple_databases/dateadd.sql @@ -4,6 +4,9 @@ {%- 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 -%} @@ -11,23 +14,23 @@ 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 -%}