-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom date spine along with tests
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |