Skip to content

Commit

Permalink
Merge pull request #38 from swanderz/patch-1
Browse files Browse the repository at this point in the history
typo in snowflake__from_unixtimestamp
  • Loading branch information
clausherther authored Jun 14, 2021
2 parents a2a337f + 1c19197 commit 4b572cb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 19 deletions.
38 changes: 35 additions & 3 deletions integration_tests/macros/get_test_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() -%}
Expand All @@ -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 %}



30 changes: 18 additions & 12 deletions integration_tests/models/test_dates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions macros/calendar_date/from_unixtimestamp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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" -%}
Expand All @@ -13,11 +33,12 @@
{%- else -%}
{{ exceptions.raise_compiler_error(
"value " ~ format ~ " for `format` for from_unixtimestamp is not supported."
)
)
}}
to_timestamp_ntz({{ epochs }}, {{ scale }})
{% endif -%}

to_timestamp_ntz({{ epochs }}, {{ scale }})

{%- endmacro %}

{%- macro bigquery__from_unixtimestamp(epochs, format) -%}
Expand All @@ -30,7 +51,7 @@
{%- else -%}
{{ exceptions.raise_compiler_error(
"value " ~ format ~ " for `format` for from_unixtimestamp is not supported."
)
)
}}
{% endif -%}
{%- endmacro %}
4 changes: 4 additions & 0 deletions macros/calendar_date/to_unixtimestamp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down

0 comments on commit 4b572cb

Please sign in to comment.