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

add support for date_spine #27

Closed
dataders opened this issue Feb 11, 2021 · 2 comments
Closed

add support for date_spine #27

dataders opened this issue Feb 11, 2021 · 2 comments

Comments

@dataders
Copy link
Contributor

dataders commented Feb 11, 2021

In this dbt slack thread, someone shared a dbt-utils-esque version of the date_spine macro.

We should bring this into this package. It might even also work for Synapse, I don't see any recursive CTEs...

steps to implement macro

  1. create a datetime folder inside of tsql-utils/macros/dbt_utils/
  2. put the macro in a file called date_spine.sql to the newly created datetime folder
  3. add a macro to the end of the file to make it also work for synapse
    {% macro synapse__date_spine(datepart, start_date, end_date) -%}
        {% do return( tsql_utils.sqlserver__date_spine(datepart, start_date, end_date)) %}
    {%- endmacro %}
  4. delete these lines
    datetime:
    test_date_spine: &disabled # BROKEN DUE TO MODEL DEF
    +enabled: false

example macro

{% macro sqlserver__date_spine_sql(start_date, end_date, datepart) %}
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_utils.datediff(start_date, end_date, datepart)}})  + rownum -1 as n
    from nums
    order by rownum
),
all_periods as (
    select (
        {{
            dbt_utils.dateadd(
                datepart,
                'n',
                start_date
            )
        }}
    ) as date_{{datepart}}
    from rawdata
),
filtered as (
    select *
    from all_periods
    where date_{{datepart}} <= {{ end_date }}
)
select * from filtered
{% endmacro %}
{% macro sqlserver__date_spine(start_date, end_date, datepart) -%}
    {% set date_spine_query %}
        {{sqlserver__date_spine_sql(start_date, end_date, datepart)}} 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 %}
@alittlesliceoftom
Copy link
Contributor

alittlesliceoftom commented Feb 11, 2021 via email

@dataders
Copy link
Contributor Author

closed by #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants