Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-database date macro #191

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240501-091604.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Cross-database `date` macro
time: 2024-05-01T09:16:04.909686-06:00
custom:
Author: dbeatty10
Issue: https://github.com/dbt-labs/dbt-core/issues/8831
45 changes: 45 additions & 0 deletions dbt-tests-adapter/dbt/tests/adapter/utils/fixture_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# date

models__test_date_sql = """
with generated_dates as (

{{
dbt.date_spine(
"day",
date(2023, 9, 7),
date(2023, 9, 10),
)
}}

),

expected_dates as (

select cast('2023-09-07' as date) as expected
union all
select cast('2023-09-08' as date) as expected
union all
select cast('2023-09-09' as date) as expected

),

joined as (
select
generated_dates.date_day,
expected_dates.expected
from generated_dates
full outer join expected_dates on generated_dates.date_day = expected_dates.expected
)

select * from joined
"""

models__test_date_yml = """
version: 2
models:
- name: test_date
data_tests:
- assert_equal:
actual: date_day
expected: expected
"""
18 changes: 18 additions & 0 deletions dbt-tests-adapter/dbt/tests/adapter/utils/test_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from dbt.tests.adapter.utils import base_utils, fixture_date


class BaseDate(base_utils.BaseUtils):
@pytest.fixture(scope="class")
def models(self):
return {
"test_date.yml": fixture_date.models__test_date_yml,
"test_date.sql": self.interpolate_macro_namespace(
fixture_date.models__test_date_sql, "date"
),
}


class TestDate(BaseDate):
pass
10 changes: 10 additions & 0 deletions dbt/include/global_project/macros/utils/date.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% macro date(year, month, day) %}
{{ return(adapter.dispatch('date', 'dbt') (year, month, day)) }}
{% endmacro %}


{% macro default__date(year, month, day) -%}
{%- set dt = modules.datetime.date(year, month, day) -%}
{%- set iso_8601_formatted_date = dt.strftime('%Y-%m-%d') -%}
to_date('{{ iso_8601_formatted_date }}', 'YYYY-MM-DD')
{%- endmacro %}
Loading