diff --git a/macros/activity.sql b/macros/activity.sql index 4a7f45f..9914c3e 100644 --- a/macros/activity.sql +++ b/macros/activity.sql @@ -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" %} diff --git a/macros/dataset.sql b/macros/dataset.sql index 7f43013..79dc5b9 100644 --- a/macros/dataset.sql +++ b/macros/dataset.sql @@ -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. #} @@ -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 %} diff --git a/macros/relationships/all_ever.sql b/macros/relationships/all_ever.sql index 4f504a9..c06c6ee 100644 --- a/macros/relationships/all_ever.sql +++ b/macros/relationships/all_ever.sql @@ -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() )) %} diff --git a/macros/relationships/append_only/first_after.sql b/macros/relationships/append_only/first_after.sql index 49c673d..3c0cfd9 100644 --- a/macros/relationships/append_only/first_after.sql +++ b/macros/relationships/append_only/first_after.sql @@ -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 )) %} diff --git a/macros/relationships/append_only/first_before.sql b/macros/relationships/append_only/first_before.sql index 0c05cdf..c880972 100644 --- a/macros/relationships/append_only/first_before.sql +++ b/macros/relationships/append_only/first_before.sql @@ -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 )) %} diff --git a/macros/relationships/append_only/first_in_between.sql b/macros/relationships/append_only/first_in_between.sql index 9ad6349..d79a519 100644 --- a/macros/relationships/append_only/first_in_between.sql +++ b/macros/relationships/append_only/first_in_between.sql @@ -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 )) %} diff --git a/macros/relationships/append_only/last_after.sql b/macros/relationships/append_only/last_after.sql index f53ea5c..14d1ca6 100644 --- a/macros/relationships/append_only/last_after.sql +++ b/macros/relationships/append_only/last_after.sql @@ -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 )) %} diff --git a/macros/relationships/append_only/last_before.sql b/macros/relationships/append_only/last_before.sql index 1c9efbb..1905303 100644 --- a/macros/relationships/append_only/last_before.sql +++ b/macros/relationships/append_only/last_before.sql @@ -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 )) %} diff --git a/macros/relationships/append_only/last_in_between.sql b/macros/relationships/append_only/last_in_between.sql index 76406e5..174fe3c 100644 --- a/macros/relationships/append_only/last_in_between.sql +++ b/macros/relationships/append_only/last_in_between.sql @@ -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 )) %} diff --git a/macros/relationships/first_ever.sql b/macros/relationships/first_ever.sql index c6f29c9..aa043a3 100644 --- a/macros/relationships/first_ever.sql +++ b/macros/relationships/first_ever.sql @@ -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() )) %} diff --git a/macros/relationships/last_ever.sql b/macros/relationships/last_ever.sql index e5fc14e..655432f 100644 --- a/macros/relationships/last_ever.sql +++ b/macros/relationships/last_ever.sql @@ -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() )) %} diff --git a/macros/relationships/nth_ever.sql b/macros/relationships/nth_ever.sql index 8da9a26..0f7c3d4 100644 --- a/macros/relationships/nth_ever.sql +++ b/macros/relationships/nth_ever.sql @@ -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) diff --git a/macros/utils/aggregations/max.sql b/macros/utils/aggregations/max.sql new file mode 100644 index 0000000..9aec550 --- /dev/null +++ b/macros/utils/aggregations/max.sql @@ -0,0 +1,3 @@ +{% macro max() %} +max({{ caller() }}) +{% endmacro %} diff --git a/macros/utils/aggregations/min.sql b/macros/utils/aggregations/min.sql new file mode 100644 index 0000000..5d4e092 --- /dev/null +++ b/macros/utils/aggregations/min.sql @@ -0,0 +1,3 @@ +{% macro min() %} +min({{ caller() }}) +{% endmacro %} diff --git a/macros/utils/render_aggregation.sql b/macros/utils/render_aggregation.sql index 066e94c..14ea9e1 100644 --- a/macros/utils/render_aggregation.sql +++ b/macros/utils/render_aggregation.sql @@ -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. #} @@ -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 %}