-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Jrmyy/main
feat: add support for dbt-metrics 1.4.*
- Loading branch information
Showing
1 changed file
with
49 additions
and
61 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 |
---|---|---|
@@ -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 %} |