From 2ef59aaf686eb18dddef12ef86484c7f9a74f8c9 Mon Sep 17 00:00:00 2001 From: colin-rogers-dbt <111200756+colin-rogers-dbt@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:57:09 -0700 Subject: [PATCH] consolidate timestamp macros (#273) * consolidate timestamp macros * add testing changes * deleting extra timestamps.sql * changie entry * fix backcompat * Update Breaking Changes-20220923-112314.yaml * Update dev-requirements.txt * remove current_timestamp_in_utc * fix backcompat * change test import * fix BaseCurrentTimestamps import * update dev-requirements * Update change log body * fix changie log --- .../unreleased/Feature-20220923-112314.yaml | 7 +++++ dbt/include/snowflake/macros/adapters.sql | 15 --------- .../snowflake/macros/utils/timestamps.sql | 20 ++++++++++++ tests/functional/adapter/test_timestamps.py | 31 +++++++++++++++++++ 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 .changes/unreleased/Feature-20220923-112314.yaml create mode 100644 dbt/include/snowflake/macros/utils/timestamps.sql create mode 100644 tests/functional/adapter/test_timestamps.py diff --git a/.changes/unreleased/Feature-20220923-112314.yaml b/.changes/unreleased/Feature-20220923-112314.yaml new file mode 100644 index 000000000..3810f8f34 --- /dev/null +++ b/.changes/unreleased/Feature-20220923-112314.yaml @@ -0,0 +1,7 @@ +kind: Feature +body: Migrate dbt-utils current_timestamp macros into core + adapters +time: 2022-09-23T11:23:14.312738-07:00 +custom: + Author: colin-rogers-dbt + Issue: "276" + PR: "273" diff --git a/dbt/include/snowflake/macros/adapters.sql b/dbt/include/snowflake/macros/adapters.sql index e956982c5..cc0cfb930 100644 --- a/dbt/include/snowflake/macros/adapters.sql +++ b/dbt/include/snowflake/macros/adapters.sql @@ -161,21 +161,6 @@ {{ return(load_result('check_schema_exists').table) }} {%- endmacro %} -{% macro snowflake__current_timestamp() -%} - convert_timezone('UTC', current_timestamp()) -{%- endmacro %} - - -{% macro snowflake__snapshot_string_as_time(timestamp) -%} - {%- set result = "to_timestamp_ntz('" ~ timestamp ~ "')" -%} - {{ return(result) }} -{%- endmacro %} - - -{% macro snowflake__snapshot_get_time() -%} - to_timestamp_ntz({{ current_timestamp() }}) -{%- endmacro %} - {% macro snowflake__rename_relation(from_relation, to_relation) -%} {% call statement('rename_relation') -%} diff --git a/dbt/include/snowflake/macros/utils/timestamps.sql b/dbt/include/snowflake/macros/utils/timestamps.sql new file mode 100644 index 000000000..ecbc9940b --- /dev/null +++ b/dbt/include/snowflake/macros/utils/timestamps.sql @@ -0,0 +1,20 @@ +{% macro snowflake__current_timestamp() -%} + convert_timezone('UTC', current_timestamp()) +{%- endmacro %} + +{% macro snowflake__snapshot_string_as_time(timestamp) -%} + {%- set result = "to_timestamp_ntz('" ~ timestamp ~ "')" -%} + {{ return(result) }} +{%- endmacro %} + +{% macro snowflake__snapshot_get_time() -%} + to_timestamp_ntz({{ current_timestamp() }}) +{%- endmacro %} + +{% macro snowflake__current_timestamp_backcompat() %} + current_timestamp::{{ type_timestamp() }} +{% endmacro %} + +{% macro snowflake__current_timestamp_in_utc_backcompat() %} + convert_timezone('UTC', {{ snowflake__current_timestamp_backcompat() }})::{{ type_timestamp() }} +{% endmacro %} diff --git a/tests/functional/adapter/test_timestamps.py b/tests/functional/adapter/test_timestamps.py new file mode 100644 index 000000000..d120100a9 --- /dev/null +++ b/tests/functional/adapter/test_timestamps.py @@ -0,0 +1,31 @@ +import imp +import pytest +from dbt.tests.adapter.utils.test_timestamps import BaseCurrentTimestamps + +_MODEL_CURRENT_TIMESTAMP = """ +SELECT {{current_timestamp()}} as current_timestamp, + {{current_timestamp_in_utc_backcompat()}} as current_timestamp_in_utc_backcompat, + {{current_timestamp_backcompat()}} as current_timestamp_backcompat +""" + + +class TestCurrentTimestampSnowflake(BaseCurrentTimestamps): + @pytest.fixture(scope="class") + def models(self): + return {"get_current_timestamp.sql": _MODEL_CURRENT_TIMESTAMP} + + @pytest.fixture(scope="class") + def expected_schema(self): + return { + "CURRENT_TIMESTAMP": "TIMESTAMP_TZ", + "CURRENT_TIMESTAMP_IN_UTC_BACKCOMPAT": "TIMESTAMP_NTZ", + "CURRENT_TIMESTAMP_BACKCOMPAT": "TIMESTAMP_NTZ", + } + + @pytest.fixture(scope="class") + def expected_sql(self): + return """ + select convert_timezone('UTC', current_timestamp()) as current_timestamp, + convert_timezone('UTC', current_timestamp::TIMESTAMP)::TIMESTAMP as current_timestamp_in_utc_backcompat, + current_timestamp::TIMESTAMP as current_timestamp_backcompat + """