Skip to content

Commit

Permalink
add custom date spine along with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nszoni committed Feb 14, 2024
1 parent fdbd8e1 commit 5c390b3
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
114 changes: 114 additions & 0 deletions dbt/include/synapse/macros/utils/date_spine.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{% macro synapse__date_spine_sql(datepart, start_date, end_date) %}


with

l0 as (

select c
from (select 1 union all select 1) as d(c)

),
l1 as (

select
1 as c
from l0 as a
cross join l0 as b

),

l2 as (

select 1 as c
from l1 as a
cross join l1 as b
),

l3 as (

select 1 as c
from l2 as a
cross join l2 as b
),

l4 as (

select 1 as c
from l3 as a
cross join l3 as b
),

l5 as (

select 1 as c
from l4 as a
cross join l4 as b
),

nums as (

select row_number() over (order by (select null)) as rownum
from l5
),

rawdata as (

select top ({{ dbt.datediff(start_date, end_date, datepart)}}) rownum -1 as n
from nums
order by rownum
),

all_periods as (

select cast((
{{
dbt.dateadd(
datepart,
'n',
start_date
)
}}
) as date) as date_{{datepart}}
from rawdata
),

filtered as (

select *
from all_periods
where date_{{datepart}} <= {{ end_date }}

)

select * from filtered

{% endmacro %}


{% macro synapse__date_spine(datepart, start_date, end_date) -%}

{% set date_spine_query %}

{{ synapse__date_spine_sql(datepart, start_date, end_date) }} order by 1

{% endset %}


{% set results = run_query(date_spine_query) %}

{% if execute %}

{% set results_list = results.columns[0].values() %}

{% else %}

{% set results_list = [] %}

{% endif %}

{%- for date_field in results_list %}
select '{{ date_field }}' as date_{{datepart}} {{ 'union all ' if not loop.last else '' }}
{% endfor -%}

{% endmacro %}
5 changes: 5 additions & 0 deletions tests/functional/adapter/test_date_spine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dbt.tests.adapter.utils.test_date_spine import BaseDateSpine


class TestDateSpineSynapse(BaseDateSpine):
pass

0 comments on commit 5c390b3

Please sign in to comment.