From 3c6e1e75cd52346e291e4e8a29b83db38bc48db1 Mon Sep 17 00:00:00 2001 From: Jeremy Guiselin Date: Wed, 10 May 2023 18:41:59 +0200 Subject: [PATCH] feat: add support for dbt-metrics 1.4.* --- macros/dbt_metrics/gen_metric_cte.sql | 110 ++++++++++++-------------- 1 file changed, 49 insertions(+), 61 deletions(-) diff --git a/macros/dbt_metrics/gen_metric_cte.sql b/macros/dbt_metrics/gen_metric_cte.sql index 3d900ab..fa63e05 100644 --- a/macros/dbt_metrics/gen_metric_cte.sql +++ b/macros/dbt_metrics/gen_metric_cte.sql @@ -1,16 +1,9 @@ - -{%- 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 %} @@ -18,7 +11,7 @@ {%- endfor -%} {%- endif -%} {%- endif -%} - + {%- for calendar_dim in calendar_dimensions %} {{ calendar_dim }}, {%- endfor %} @@ -26,59 +19,54 @@ {%- 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 %}