From 2dd71e46c55146588a69e019d44fe0f5f2499e37 Mon Sep 17 00:00:00 2001 From: Anders Date: Sun, 13 Jun 2021 12:01:35 -0400 Subject: [PATCH 1/5] typo --- macros/calendar_date/from_unixtimestamp.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/macros/calendar_date/from_unixtimestamp.sql b/macros/calendar_date/from_unixtimestamp.sql index 98831c2..44af81b 100644 --- a/macros/calendar_date/from_unixtimestamp.sql +++ b/macros/calendar_date/from_unixtimestamp.sql @@ -15,8 +15,9 @@ "value " ~ format ~ " for `format` for from_unixtimestamp is not supported." ) }} - to_timestamp_ntz({{ epochs }}, {{ scale }}) {% endif -%} + + to_timestamp_ntz({{ epochs }}, {{ scale }}) {%- endmacro %} From 15e8d0bddd6319a9b5d311c1885661cef2731ef2 Mon Sep 17 00:00:00 2001 From: clausherther Date: Mon, 14 Jun 2021 08:05:36 -0700 Subject: [PATCH 2/5] Add test timestamps --- integration_tests/macros/get_test_dates.sql | 38 +++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/integration_tests/macros/get_test_dates.sql b/integration_tests/macros/get_test_dates.sql index fe8cfd4..26aedf8 100644 --- a/integration_tests/macros/get_test_dates.sql +++ b/integration_tests/macros/get_test_dates.sql @@ -17,9 +17,12 @@ select cast('2020-11-29' as date) as iso_week_end_date, 48 as iso_week_of_year, 'November' as month_name, - 'Nov' as month_name_short + 'Nov' as month_name_short, + 1623076520 as unix_epoch, + cast('{{ get_test_timestamps()[0] }}' as {{ dbt_utils.type_timestamp() }}) as time_stamp, + cast('{{ get_test_timestamps()[1] }}' as {{ dbt_utils.type_timestamp() }}) as time_stamp_utc - union all +union all select cast('2020-12-01' as date) as date_day, @@ -38,7 +41,12 @@ select cast('2020-12-06' as date) as iso_week_end_date, 49 as iso_week_of_year, 'December' as month_name, - 'Dec' as month_name_short + 'Dec' as month_name_short, + {# 1623051320 as unix_epoch, #} + 1623076520 as unix_epoch, + cast('{{ get_test_timestamps()[0] }}' as {{ dbt_utils.type_timestamp() }}) as time_stamp, + cast('{{ get_test_timestamps()[1] }}' as {{ dbt_utils.type_timestamp() }}) as time_stamp_utc + {%- endmacro %} {% macro get_test_week_of_year() -%} @@ -55,3 +63,27 @@ select {# Snowflake uses ISO year #} {{ return([48,49]) }} {%- endmacro %} + + + +{% macro get_test_timestamps() -%} + {{ return(adapter.dispatch('get_test_timestamps', packages = dbt_date._get_utils_namespaces()) ()) }} +{%- endmacro %} + +{% macro default__get_test_timestamps() -%} + {{ return(['2021-06-07 07:35:20.000000 America/Los_Angeles', + '2021-06-07 14:35:20.000000 UTC']) }} +{%- endmacro %} + +{% macro bigquery__get_test_timestamps() -%} + {{ return(['2021-06-07 07:35:20.000000', + '2021-06-07 14:35:20.000000']) }} +{%- endmacro %} + +{% macro snowflake__get_test_timestamps() -%} + {{ return(['2021-06-07 07:35:20.000000 -0700', + '2021-06-07 14:35:20.000000 -0000']) }} +{%- endmacro %} + + + From bc5a637868ba5e2fb358e88820cbd5d458d4715c Mon Sep 17 00:00:00 2001 From: clausherther Date: Mon, 14 Jun 2021 08:06:09 -0700 Subject: [PATCH 3/5] Add default and PG-specific versions for from_unixtimestamp --- macros/calendar_date/from_unixtimestamp.sql | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/macros/calendar_date/from_unixtimestamp.sql b/macros/calendar_date/from_unixtimestamp.sql index 44af81b..e1dd55d 100644 --- a/macros/calendar_date/from_unixtimestamp.sql +++ b/macros/calendar_date/from_unixtimestamp.sql @@ -2,8 +2,28 @@ {{ adapter.dispatch('from_unixtimestamp', packages = dbt_date._get_utils_namespaces()) (epochs, format) }} {%- endmacro %} +{%- macro default__from_unixtimestamp(epochs, format="seconds") -%} + {%- if format != "seconds" -%} + {{ exceptions.raise_compiler_error( + "value " ~ format ~ " for `format` for from_unixtimestamp is not supported." + ) + }} + {% endif -%} + to_timestamp({{ epochs }}) +{%- endmacro %} + +{%- macro postgres__from_unixtimestamp(epochs, format="seconds") -%} + {%- if format != "seconds" -%} + {{ exceptions.raise_compiler_error( + "value " ~ format ~ " for `format` for from_unixtimestamp is not supported." + ) + }} + {% endif -%} + cast(to_timestamp({{ epochs }}) at time zone 'UTC' as timestamp) +{%- endmacro %} + {%- macro snowflake__from_unixtimestamp(epochs, format) -%} - + {%- if format == "seconds" -%} {%- set scale = 0 -%} {%- elif format == "milliseconds" -%} @@ -13,10 +33,10 @@ {%- else -%} {{ exceptions.raise_compiler_error( "value " ~ format ~ " for `format` for from_unixtimestamp is not supported." - ) + ) }} {% endif -%} - + to_timestamp_ntz({{ epochs }}, {{ scale }}) {%- endmacro %} @@ -31,7 +51,7 @@ {%- else -%} {{ exceptions.raise_compiler_error( "value " ~ format ~ " for `format` for from_unixtimestamp is not supported." - ) + ) }} {% endif -%} {%- endmacro %} From 13706204109abd6303ab22bbbe32358fe47c9259 Mon Sep 17 00:00:00 2001 From: clausherther Date: Mon, 14 Jun 2021 08:06:29 -0700 Subject: [PATCH 4/5] Update SF and default version of to_unixtimestamp --- macros/calendar_date/to_unixtimestamp.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/macros/calendar_date/to_unixtimestamp.sql b/macros/calendar_date/to_unixtimestamp.sql index ec3319a..d023c6f 100644 --- a/macros/calendar_date/to_unixtimestamp.sql +++ b/macros/calendar_date/to_unixtimestamp.sql @@ -3,6 +3,10 @@ {%- endmacro %} {%- macro default__to_unixtimestamp(timestamp) -%} + {{ dbt_date.date_part('epoch', timestamp) }} +{%- endmacro %} + +{%- macro snowflake__to_unixtimestamp(timestamp) -%} {{ dbt_date.date_part('epoch_seconds', timestamp) }} {%- endmacro %} From 1c191978e23f178a4b027c9dd4a13fee80ee185b Mon Sep 17 00:00:00 2001 From: clausherther Date: Mon, 14 Jun 2021 08:06:38 -0700 Subject: [PATCH 5/5] Add timestamp integration tests --- integration_tests/models/test_dates.yml | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/integration_tests/models/test_dates.yml b/integration_tests/models/test_dates.yml index ebb40c4..b321af0 100644 --- a/integration_tests/models/test_dates.yml +++ b/integration_tests/models/test_dates.yml @@ -7,30 +7,36 @@ models: - dbt_utils.expression_is_true: expression: "next_date_day = {{ dbt_date.tomorrow('date_day') }}" - dbt_utils.expression_is_true: - expression: "day_name = {{ dbt_date.day_name('date_day', short=False) }}" + expression: "day_name = {{ dbt_date.day_name('date_day', short=False) }}" - dbt_utils.expression_is_true: - expression: "day_name_short = {{ dbt_date.day_name('date_day', short=True) }}" + expression: "day_name_short = {{ dbt_date.day_name('date_day', short=True) }}" - dbt_utils.expression_is_true: - expression: "day_of_month = {{ dbt_date.day_of_month('date_day') }}" + expression: "day_of_month = {{ dbt_date.day_of_month('date_day') }}" - dbt_utils.expression_is_true: - expression: "day_of_week = {{ dbt_date.day_of_week('date_day', isoweek=False) }}" + expression: "day_of_week = {{ dbt_date.day_of_week('date_day', isoweek=False) }}" - dbt_utils.expression_is_true: - expression: "iso_day_of_week = {{ dbt_date.day_of_week('date_day', isoweek=True) }}" + expression: "iso_day_of_week = {{ dbt_date.day_of_week('date_day', isoweek=True) }}" - dbt_utils.expression_is_true: - expression: "day_of_year = {{ dbt_date.day_of_year('date_day') }}" + expression: "day_of_year = {{ dbt_date.day_of_year('date_day') }}" - dbt_utils.expression_is_true: - expression: "week_start_date = {{ dbt_date.week_start('date_day') }}" + expression: "week_start_date = {{ dbt_date.week_start('date_day') }}" - dbt_utils.expression_is_true: - expression: "week_end_date = {{ dbt_date.week_end('date_day') }}" + expression: "week_end_date = {{ dbt_date.week_end('date_day') }}" - dbt_utils.expression_is_true: - expression: "week_of_year = {{ dbt_date.week_of_year('date_day') }}" + expression: "week_of_year = {{ dbt_date.week_of_year('date_day') }}" - dbt_utils.expression_is_true: - expression: "iso_week_start_date = {{ dbt_date.iso_week_start('date_day') }}" + expression: "iso_week_start_date = {{ dbt_date.iso_week_start('date_day') }}" - dbt_utils.expression_is_true: - expression: "iso_week_end_date = {{ dbt_date.iso_week_end('date_day') }}" + expression: "iso_week_end_date = {{ dbt_date.iso_week_end('date_day') }}" - dbt_utils.expression_is_true: - expression: "iso_week_of_year = {{ dbt_date.iso_week_of_year('date_day') }}" + expression: "iso_week_of_year = {{ dbt_date.iso_week_of_year('date_day') }}" + - dbt_utils.expression_is_true: + expression: "time_stamp_utc = {{ dbt_date.from_unixtimestamp('unix_epoch') }}" + - dbt_utils.expression_is_true: + expression: "unix_epoch = {{ dbt_date.to_unixtimestamp('time_stamp_utc') }}" + - dbt_utils.expression_is_true: + expression: "time_stamp = {{ dbt_date.convert_timezone('time_stamp_utc') }}" columns: - name: date_day