Skip to content

Commit

Permalink
refactor: use variables to make the lines of code shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
cverhoef committed Jan 4, 2024
1 parent 661bbe4 commit 45362e2
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions macros/multiple_databases/dateadd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@
{%- set number_bigint -%}
try_cast({{ number }} as bigint)
{%- endset -%}
{# Convert the base date and the units to be added both to milliseconds to do the addition.
{# Convert the base timestamp and the units to be added both to milliseconds to do the addition.
Convert the total milliseconds back to a timestamp using timestamp_millis. #}
{%- set base_timestamp_in_ms -%}
unix_millis(try_to_timestamp({{ date_or_datetime_field }}))
{%- endset -%}
{%- if datepart == 'millisecond' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }})
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }})
{%- elif datepart == 'second' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000)
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }} * 1000)
{%- elif datepart == 'minute' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60)
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }} * 1000 * 60)
{%- elif datepart == 'hour' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60 * 60)
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }} * 1000 * 60 * 60)
{%- elif datepart == 'day' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60 * 60 * 24)
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }} * 1000 * 60 * 60 * 24)
{%- elif datepart == 'week' -%}
timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60 * 60 * 24 * 7)
timestamp_millis({{ base_timestamp_in_ms }} + {{ number_bigint }} * 1000 * 60 * 60 * 24 * 7)
{%- endif -%}
{# Since the number of days in a month can differ, use the add_months function for month, quarter and year.
Add the time part separately because add_months needs dates as input. Both parts are converted to milliseconds to do the addition. #}
{%- set time_in_milliseconds -%}
unix_millis(try_to_timestamp({{ date_or_datetime_field }})) - unix_millis(date_trunc('DD', try_to_timestamp({{ date_or_datetime_field }})))
The add_months function needs a date as input. Compute the milliseconds of the date and time part separately and add them together. #}
{%- set time_part_in_ms -%}
{{ base_timestamp_in_ms }} - unix_millis(date_trunc('DD', try_to_timestamp({{ date_or_datetime_field }})))
{%- endset -%}
{%- if datepart == 'month' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }}))) + {{ time_in_milliseconds }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }}))) + {{ time_part_in_ms }})
{%- elif datepart == 'quarter' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }} * 3))) + {{ time_in_milliseconds }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }} * 3))) + {{ time_part_in_ms }})
{%- elif datepart == 'year' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }} * 12))) + {{ time_in_milliseconds }})
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }} * 12))) + {{ time_part_in_ms }})
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

0 comments on commit 45362e2

Please sign in to comment.