Skip to content

Commit

Permalink
Feat: Use caller() Decorators for Aggregations (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnightengale authored Mar 14, 2023
1 parent 6e1af63 commit b8841a0
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion macros/activity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ params:
{# The columns.feature_json requires columns.ts to be present. #}
{% if (dbt_activity_schema.columns().feature_json in columns) and
(dbt_activity_schema.columns().ts not in columns) %}
{% set columns = columns.append(dbt_activity_schema.columns().ts) %}
{% do columns.append(dbt_activity_schema.columns().ts) %}
{% endif %}

{% set additional_join_condition = additional_join_condition if additional_join_condition else "true" %}
Expand Down
6 changes: 3 additions & 3 deletions macros/dataset.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ params:
variables in ./dataclasses/columns.sql to set the columns of the activity
stream.

primary_activity: primary_activity (/activities)
primary_activity: activity (class)
The primary activity of the derived dataset.

appended_activities: List[ appended_activity (/activities) ]
appended_activities: List[ activity (class) ]
The list of appended activities to self-join to the primary activity.
#}

Expand Down Expand Up @@ -99,7 +99,7 @@ aggregate_appended_activities as (
{% for activity in appended_activities %}{% set i = loop.index %}{% set last_outer_loop = loop.last %}
{% for col in activity.columns %}

{{ render_agg(activity.relationship.aggregation_func, col, activity) }}
{{ render_agg(col, activity) }}

{% if not (last_outer_loop and loop.last) %},{% endif %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/all_ever.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% do return(namespace(
name="all_ever",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.all_ever_join_clause,
where_clause=dbt_activity_schema.all_ever_join_clause()
)) %}
Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/first_after.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% do return(namespace(
name="first_after",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.first_after_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/first_before.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{% do return(namespace(
name="first_before",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.first_before_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/first_in_between.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

{% do return(namespace(
name="first_in_between",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.first_in_between_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/last_after.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% do return(namespace(
name="last_after",
aggregation_func="max",
aggregation_func=dbt_activity_schema.max,
join_clause=dbt_activity_schema.last_after_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/last_before.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% do return(namespace(
name="last_before",
aggregation_func="max",
aggregation_func=dbt_activity_schema.max,
join_clause=dbt_activity_schema.last_before_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/append_only/last_in_between.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

{% do return(namespace(
name="last_in_between",
aggregation_func="max",
aggregation_func=dbt_activity_schema.max,
join_clause=dbt_activity_schema.last_in_between_join_clause
)) %}

Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/first_ever.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% do return(namespace(
name="first_ever",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.first_ever_join_clause,
where_clause=dbt_activity_schema.first_ever_join_clause()
)) %}
Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/last_ever.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% do return(namespace(
name="last_ever",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
join_clause=dbt_activity_schema.last_ever_join_clause,
where_clause=dbt_activity_schema.last_ever_join_clause()
)) %}
Expand Down
2 changes: 1 addition & 1 deletion macros/relationships/nth_ever.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% do return(namespace(
name="nth_ever",
aggregation_func="min",
aggregation_func=dbt_activity_schema.min,
nth_occurance=nth_occurance,
join_clause=dbt_activity_schema.nth_ever_join_clause,
where_clause=dbt_activity_schema.nth_ever_join_clause(nth_occurance)
Expand Down
3 changes: 3 additions & 0 deletions macros/utils/aggregations/max.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro max() %}
max({{ caller() }})
{% endmacro %}
3 changes: 3 additions & 0 deletions macros/utils/aggregations/min.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro min() %}
min({{ caller() }})
{% endmacro %}
29 changes: 14 additions & 15 deletions macros/utils/render_aggregation.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
{%- macro render_aggregation(aggregation_func, col, activity) -%}
{{ return(adapter.dispatch("render_aggregation", "dbt_activity_schema")(aggregation_func, col, activity)) }}
{%- macro render_aggregation(col, activity) -%}
{{ return(adapter.dispatch("render_aggregation", "dbt_activity_schema")(col, activity)) }}
{%- endmacro -%}


{%- macro default__render_aggregation(aggregation_func, col, activity) -%}
{%- macro default__render_aggregation(col, activity) -%}

{# Render the aggregation, handling special cases for non-cardinal columns.

params:

aggregation_func: str
A valid SQL aggregation function.

col: str
The column to aggregate.

activity: str
The activity to aggregate.
activity: activity (class)
The activity class which contains a name and aggregation function.

#}

Expand All @@ -28,21 +25,23 @@ params:
{% if col in [columns.feature_json] %}

{% set ts_concat_feature_json %}
(
{{ aggregation_func }}(
{{ dbt.concat([aliased_activity_ts_col, aliased_col]) }}
)
)
{% call activity.relationship.aggregation_func() %}
{{ dbt.concat([aliased_activity_ts_col, aliased_col]) }}
{% endcall %}
{% endset %}

{% set ts_aggregated %}
{{ aggregation_func }}({{ aliased_activity_ts_col }})
{% call activity.relationship.aggregation_func() %}
{{ aliased_activity_ts_col }}
{% endcall %}
{% endset %}

{{ dbt_activity_schema.ltrim(ts_concat_feature_json, ts_aggregated) }} as {{ aliased_col }}

{% else %}
{{ aggregation_func }}( {{ aliased_col }} ) as {{ aliased_col }}
{% call activity.relationship.aggregation_func() %}
{{ aliased_col }}
{% endcall %} as {{ aliased_col }}
{% endif %}

{% endmacro %}

0 comments on commit b8841a0

Please sign in to comment.