Skip to content

Commit

Permalink
Merge pull request #14 from Jrmyy/main
Browse files Browse the repository at this point in the history
feat: add support for dbt-metrics 1.4.*
  • Loading branch information
Jrmyy authored Jun 21, 2023
2 parents a4d4a52 + 3c6e1e7 commit 55e43bc
Showing 1 changed file with 49 additions and 61 deletions.
110 changes: 49 additions & 61 deletions macros/dbt_metrics/gen_metric_cte.sql
Original file line number Diff line number Diff line change
@@ -1,84 +1,72 @@

{%- macro athena__gen_metric_cte(metric_name, grain, dimensions, secondary_calculations, start_date, end_date, relevant_periods, calendar_dimensions, treat_null_values_as_zero) %}

, {{metric_name}}__final as (

{%- if not treat_null_values_as_zero -%}
{%- set metric_val = metric_name -%}
{%- else -%}
{%- set metric_val = "coalesce(" ~ metric_name ~ ", 0) as " ~ metric_name -%}
{%- endif %}

{%- macro athena__gen_metric_cte(metrics_dictionary, group_name, group_values, grain, dimensions, secondary_calculations, start_date, end_date, relevant_periods, calendar_dimensions) %}
{%- set combined_dimensions = calendar_dimensions | list + dimensions | list -%}
, {{group_name}}__final as (
{# #}
select
{% if grain != 'all_time' %}
{%- if grain %}
date_{{grain}},
{%- if secondary_calculations | length > 0 -%}
{% for period in relevant_periods %}
parent_metric_cte.date_{{ period }},
{%- endfor -%}
{%- endif -%}
{%- endif -%}

{%- for calendar_dim in calendar_dimensions %}
{{ calendar_dim }},
{%- endfor %}

{%- for dim in dimensions %}
{{ dim }},
{%- endfor %}
{{ metric_val }}

{%- if grain == 'all_time' %}

,metric_start_date
,metric_end_date

from {{metric_name}}__aggregate as parent_metric_cte
{%- for metric_name in group_values.metric_names -%}
{# TODO: coalesce based on the value. Need to bring this config #}
{%- if not metrics_dictionary[metric_name].get("config").get("treat_null_values_as_zero", True) %}
{{ metric_name }}
{%- else %}
coalesce({{ metric_name }}, 0) as {{ metric_name }}
{%- endif %}
{%- if not loop.last-%},{%endif%}
{%- endfor %}

{% else %}
{%- if secondary_calculations | length > 0 %}
from {{group_name}}__spine_time as parent_metric_cte
left outer join {{group_name}}__aggregate
using (date_{{grain}} {%- if combined_dimensions | length > 0 -%}, {{ combined_dimensions | join(", ") }} {%-endif-%} )

from {{metric_name}}__spine_time as parent_metric_cte
left outer join {{metric_name}}__aggregate
using (
date_{{grain}}
{%- for calendar_dim in calendar_dimensions %}
, {{ calendar_dim }}
{%- endfor %}
{%- for dim in dimensions %}
, {{ dim }}
{%- endfor %}
{% if not start_date or not end_date -%}
where (
{% if not start_date and not end_date -%}
date_{{grain}} >= (
select
min(case when has_data then date_{{grain}} end)
from {{group_name}}__aggregate
)
and date_{{grain}} <= (
select
max(case when has_data then date_{{grain}} end)
from {{group_name}}__aggregate
)
{% elif not start_date and end_date -%}
date_{{grain}} >= (
select
min(case when has_data then date_{{grain}} end)
from {{group_name}}__aggregate
)
{% elif start_date and not end_date -%}
date_{{grain}} <= (
select
max(case when has_data then date_{{grain}} end)
from {{group_name}}__aggregate
)
{%- endif %}
)
{%- endif %}

{% if not start_date or not end_date -%}
where (
{% if not start_date and not end_date -%}
date_{{grain}} >= (
select
min(case when has_data then date_{{grain}} end)
from {{metric_name}}__aggregate
)
and date_{{grain}} <= (
select
max(case when has_data then date_{{grain}} end)
from {{metric_name}}__aggregate
)
{% elif not start_date and end_date -%}
date_{{grain}} >= (
select
min(case when has_data then date_{{grain}} end)
from {{metric_name}}__aggregate
)
{% elif start_date and not end_date -%}
date_{{grain}} <= (
select
max(case when has_data then date_{{grain}} end)
from {{metric_name}}__aggregate
)
{%- endif %}
)
{% endif %}
{% endif -%}

{%- else %}
from {{group_name}}__aggregate as parent_metric_cte
{%- endif %}
)

{% endmacro %}

0 comments on commit 55e43bc

Please sign in to comment.