forked from calogica/dbt-date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_base_dates.sql
61 lines (47 loc) · 1.6 KB
/
get_base_dates.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{% macro get_base_dates(start_date=None, end_date=None, n_dateparts=None, datepart="day") %}
{{ adapter.dispatch('get_base_dates', 'dbt_date') (start_date, end_date, n_dateparts, datepart) }}
{% endmacro %}
{% macro default__get_base_dates(start_date, end_date, n_dateparts, datepart) %}
{%- if start_date and end_date -%}
{%- set start_date="cast('" ~ start_date ~ "' as " ~ type_timestamp() ~ ")" -%}
{%- set end_date="cast('" ~ end_date ~ "' as " ~ type_timestamp() ~ ")" -%}
{%- elif n_dateparts and datepart -%}
{%- set start_date = dateadd(datepart, -1 * n_dateparts, dbt_date.today()) -%}
{%- set end_date = dbt_date.tomorrow() -%}
{%- endif -%}
with date_spine as
(
{{ dbt_utils.date_spine(
datepart=datepart,
start_date=start_date,
end_date=end_date,
)
}}
)
select
cast(d.date_{{ datepart }} as {{ type_timestamp() }}) as date_{{ datepart }}
from
date_spine d
{% endmacro %}
{% macro bigquery__get_base_dates(start_date, end_date, n_dateparts, datepart) %}
{%- if start_date and end_date -%}
{%- set start_date="cast('" ~ start_date ~ "' as date )" -%}
{%- set end_date="cast('" ~ end_date ~ "' as date )" -%}
{%- elif n_dateparts and datepart -%}
{%- set start_date = dateadd(datepart, -1 * n_dateparts, dbt_date.today()) -%}
{%- set end_date = dbt_date.tomorrow() -%}
{%- endif -%}
with date_spine as
(
{{ dbt_utils.date_spine(
datepart=datepart,
start_date=start_date,
end_date=end_date,
)
}}
)
select
cast(d.date_{{ datepart }} as {{ type_timestamp() }}) as date_{{ datepart }}
from
date_spine d
{% endmacro %}