|
1 |
| - |
2 |
| -{%- macro athena__gen_metric_cte(metric_name, grain, dimensions, secondary_calculations, start_date, end_date, relevant_periods, calendar_dimensions, treat_null_values_as_zero) %} |
3 |
| - |
4 |
| -, {{metric_name}}__final as ( |
5 |
| - |
6 |
| - {%- if not treat_null_values_as_zero -%} |
7 |
| - {%- set metric_val = metric_name -%} |
8 |
| - {%- else -%} |
9 |
| - {%- set metric_val = "coalesce(" ~ metric_name ~ ", 0) as " ~ metric_name -%} |
10 |
| - {%- endif %} |
11 |
| - |
| 1 | +{%- macro athena__gen_metric_cte(metrics_dictionary, group_name, group_values, grain, dimensions, secondary_calculations, start_date, end_date, relevant_periods, calendar_dimensions) %} |
| 2 | +{%- set combined_dimensions = calendar_dimensions | list + dimensions | list -%} |
| 3 | +, {{group_name}}__final as ( |
| 4 | + {# #} |
12 | 5 | select
|
13 |
| - {% if grain != 'all_time' %} |
| 6 | + {%- if grain %} |
14 | 7 | date_{{grain}},
|
15 | 8 | {%- if secondary_calculations | length > 0 -%}
|
16 | 9 | {% for period in relevant_periods %}
|
17 | 10 | parent_metric_cte.date_{{ period }},
|
18 | 11 | {%- endfor -%}
|
19 | 12 | {%- endif -%}
|
20 | 13 | {%- endif -%}
|
21 |
| - |
| 14 | + |
22 | 15 | {%- for calendar_dim in calendar_dimensions %}
|
23 | 16 | {{ calendar_dim }},
|
24 | 17 | {%- endfor %}
|
25 | 18 |
|
26 | 19 | {%- for dim in dimensions %}
|
27 | 20 | {{ dim }},
|
28 | 21 | {%- endfor %}
|
29 |
| - {{ metric_val }} |
30 |
| - |
31 |
| - {%- if grain == 'all_time' %} |
32 |
| - |
33 |
| - ,metric_start_date |
34 |
| - ,metric_end_date |
35 | 22 |
|
36 |
| - from {{metric_name}}__aggregate as parent_metric_cte |
| 23 | + {%- for metric_name in group_values.metric_names -%} |
| 24 | + {# TODO: coalesce based on the value. Need to bring this config #} |
| 25 | + {%- if not metrics_dictionary[metric_name].get("config").get("treat_null_values_as_zero", True) %} |
| 26 | + {{ metric_name }} |
| 27 | + {%- else %} |
| 28 | + coalesce({{ metric_name }}, 0) as {{ metric_name }} |
| 29 | + {%- endif %} |
| 30 | + {%- if not loop.last-%},{%endif%} |
| 31 | + {%- endfor %} |
37 | 32 |
|
38 |
| - {% else %} |
| 33 | + {%- if secondary_calculations | length > 0 %} |
| 34 | + from {{group_name}}__spine_time as parent_metric_cte |
| 35 | + left outer join {{group_name}}__aggregate |
| 36 | + using (date_{{grain}} {%- if combined_dimensions | length > 0 -%}, {{ combined_dimensions | join(", ") }} {%-endif-%} ) |
39 | 37 |
|
40 |
| - from {{metric_name}}__spine_time as parent_metric_cte |
41 |
| - left outer join {{metric_name}}__aggregate |
42 |
| - using ( |
43 |
| - date_{{grain}} |
44 |
| - {%- for calendar_dim in calendar_dimensions %} |
45 |
| - , {{ calendar_dim }} |
46 |
| - {%- endfor %} |
47 |
| - {%- for dim in dimensions %} |
48 |
| - , {{ dim }} |
49 |
| - {%- endfor %} |
| 38 | + {% if not start_date or not end_date -%} |
| 39 | + where ( |
| 40 | + {% if not start_date and not end_date -%} |
| 41 | + date_{{grain}} >= ( |
| 42 | + select |
| 43 | + min(case when has_data then date_{{grain}} end) |
| 44 | + from {{group_name}}__aggregate |
50 | 45 | )
|
| 46 | + and date_{{grain}} <= ( |
| 47 | + select |
| 48 | + max(case when has_data then date_{{grain}} end) |
| 49 | + from {{group_name}}__aggregate |
| 50 | + ) |
| 51 | + {% elif not start_date and end_date -%} |
| 52 | + date_{{grain}} >= ( |
| 53 | + select |
| 54 | + min(case when has_data then date_{{grain}} end) |
| 55 | + from {{group_name}}__aggregate |
| 56 | + ) |
| 57 | + {% elif start_date and not end_date -%} |
| 58 | + date_{{grain}} <= ( |
| 59 | + select |
| 60 | + max(case when has_data then date_{{grain}} end) |
| 61 | + from {{group_name}}__aggregate |
| 62 | + ) |
| 63 | + {%- endif %} |
| 64 | + ) |
| 65 | + {%- endif %} |
51 | 66 |
|
52 |
| - {% if not start_date or not end_date -%} |
53 |
| - where ( |
54 |
| - {% if not start_date and not end_date -%} |
55 |
| - date_{{grain}} >= ( |
56 |
| - select |
57 |
| - min(case when has_data then date_{{grain}} end) |
58 |
| - from {{metric_name}}__aggregate |
59 |
| - ) |
60 |
| - and date_{{grain}} <= ( |
61 |
| - select |
62 |
| - max(case when has_data then date_{{grain}} end) |
63 |
| - from {{metric_name}}__aggregate |
64 |
| - ) |
65 |
| - {% elif not start_date and end_date -%} |
66 |
| - date_{{grain}} >= ( |
67 |
| - select |
68 |
| - min(case when has_data then date_{{grain}} end) |
69 |
| - from {{metric_name}}__aggregate |
70 |
| - ) |
71 |
| - {% elif start_date and not end_date -%} |
72 |
| - date_{{grain}} <= ( |
73 |
| - select |
74 |
| - max(case when has_data then date_{{grain}} end) |
75 |
| - from {{metric_name}}__aggregate |
76 |
| - ) |
77 |
| - {%- endif %} |
78 |
| - ) |
79 |
| - {% endif %} |
80 |
| - {% endif -%} |
81 |
| - |
| 67 | + {%- else %} |
| 68 | + from {{group_name}}__aggregate as parent_metric_cte |
| 69 | + {%- endif %} |
82 | 70 | )
|
83 | 71 |
|
84 | 72 | {% endmacro %}
|
0 commit comments