From ed4483c091b132b2836d1a0c93cce41d26a54ed7 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Wed, 5 Apr 2023 11:11:57 -0400 Subject: [PATCH 1/4] tmp snowflake min or max --- macros/utils/aggregations/_min_or_max.sql | 56 ++++------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/macros/utils/aggregations/_min_or_max.sql b/macros/utils/aggregations/_min_or_max.sql index 7041719..958befd 100644 --- a/macros/utils/aggregations/_min_or_max.sql +++ b/macros/utils/aggregations/_min_or_max.sql @@ -1,54 +1,18 @@ {% macro _min_or_max(min_or_max, qualified_col) %} -{% set aggregation = "min" if min_or_max == "min" else "max" %} -{% set column_name = qualified_col.split(".")[-1].strip() %} -{% set qualified_ts_col = "{}.{}".format(dbt_activity_schema.appended(), dbt_activity_schema.columns().ts )%} -{% set columns = dbt_activity_schema.columns() %} - - -{# Set type to cast back to after aggregation. #} -{# TODO: Refactor column abstraction to contain types. #} -{% if column_name in [ - columns.ts, - columns.activity_repeated_at -] %} - {% set type = dbt.type_timestamp() %} -{% elif column_name in [ - columns.activity_occurrence, - columns.revenue_impact -] %} - {% set type = dbt.type_numeric() %} -{% else %} - {% set type = dbt.type_string() %} -{% endif %} - -{# Prepend ts column and aggregate. See here for details: https://tinyurl.com/mwfz6xm4 #} -{% set ts_concatenated_and_aggregated_col %} - {{ aggregation }}( - {{ dbt.concat([ - dbt.safe_cast(qualified_ts_col, dbt.type_string()), - dbt.safe_cast(qualified_col, dbt.type_string()) - ]) }} - ) -{% endset %} - -{# Aggregate ts column before trimming, so it is not required in GROUP BY. #} -{% set aggregated_ts_col %} - {{ aggregation }}( {{ dbt.safe_cast(qualified_ts_col, dbt.type_string()) }} ) -{% endset %} - -{# Calculate length of column without prepended & aggregated ts column. #} -{% set retain_n_rightmost_characters %} -{{ dbt.length(ts_concatenated_and_aggregated_col) }} - {{ dbt.length(aggregated_ts_col) }} +{% set aggregation = "min_by" if min_or_max == "min" else "max_by" %} +{% set qualified_ts_col = "{}.{}".format( + dbt_activity_schema.appended(), dbt_activity_schema.columns().ts +) %} + +{# Apply min or max by to the selected column #} +{% set aggregated_col %} + {{ aggregation }}({{ qualified_col }}, {{ qualified_ts_col }}) {% endset %} -{# Remove prepended & aggregated ts column. #} +{# Return output. #} {% set output %} -{{ dbt.safe_cast( - dbt.right( - ts_concatenated_and_aggregated_col, - retain_n_rightmost_characters - ), type) }} +{{ aggregated_col }} {% endset %} {% do return(output) %} From f4f6c1090e76b755a5c3dc61b4f914ccef5b1612 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Fri, 7 Apr 2023 10:29:27 -0400 Subject: [PATCH 2/4] add snowflake min or max --- macros/utils/aggregations/_min_or_max.sql | 53 +++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/macros/utils/aggregations/_min_or_max.sql b/macros/utils/aggregations/_min_or_max.sql index 958befd..276bd82 100644 --- a/macros/utils/aggregations/_min_or_max.sql +++ b/macros/utils/aggregations/_min_or_max.sql @@ -1,20 +1,65 @@ {% macro _min_or_max(min_or_max, qualified_col) %} +{% if target.type == "snowflake" %} {% set aggregation = "min_by" if min_or_max == "min" else "max_by" %} {% set qualified_ts_col = "{}.{}".format( dbt_activity_schema.appended(), dbt_activity_schema.columns().ts ) %} -{# Apply min or max by to the selected column #} +{# Apply min or max by to the selected column. #} {% set aggregated_col %} - {{ aggregation }}({{ qualified_col }}, {{ qualified_ts_col }}) + {{ aggregation }}({{ qualified_col }}, {{ qualified_ts_col }}) {% endset %} {# Return output. #} +{% do return(aggregated_col) %} + +{% else %} +{% set aggregation = "min" if min_or_max == "min" else "max" %} +{% set column_name = qualified_col.split(".")[-1].strip() %} +{% set qualified_ts_col = "{}.{}".format( + dbt_activity_schema.appended(), dbt_activity_schema.columns().ts +) %} +{% set columns = dbt_activity_schema.columns() %} + +{# Set type to cast back to after aggregation. #} +{# TODO: Refactor column abstraction to contain types. #} +{% if column_name in [columns.ts, columns.activity_repeated_at] %} +{% set type = dbt.type_timestamp() %} +{% elif column_name in [columns.activity_occurrence, columns.revenue_impact] %} +{% set type = dbt.type_numeric() %} +{% else %} {% set type = dbt.type_string() %} +{% endif %} + +{# Prepend ts column and aggregate. See here for details: https://tinyurl.com/mwfz6xm4 #} +{% set ts_concatenated_and_aggregated_col %} + {{ aggregation }}( + {{ dbt.concat([ + dbt.safe_cast(qualified_ts_col, dbt.type_string()), + dbt.safe_cast(qualified_col, dbt.type_string()) + ]) }} + ) +{% endset %} + +{# Aggregate ts column before trimming, so it is not required in GROUP BY. #} +{% set aggregated_ts_col %} + {{ aggregation }}( {{ dbt.safe_cast(qualified_ts_col, dbt.type_string()) }} ) +{% endset %} + +{# Calculate length of column without prepended & aggregated ts column. #} +{% set retain_n_rightmost_characters %} + {{ dbt.length(ts_concatenated_and_aggregated_col) }} - {{ dbt.length(aggregated_ts_col) }} +{% endset %} + +{# Remove prepended & aggregated ts column. #} {% set output %} -{{ aggregated_col }} + {{ dbt.safe_cast( + dbt.right( + ts_concatenated_and_aggregated_col, + retain_n_rightmost_characters + ), type) }} {% endset %} {% do return(output) %} - +{% endif %} {% endmacro %} From 982270ee9b8c3afb39139aeab772da7d93db3acc Mon Sep 17 00:00:00 2001 From: Will Brown Date: Fri, 21 Apr 2023 15:34:49 -0400 Subject: [PATCH 3/4] update if then to dispatch --- macros/utils/aggregations/_min_or_max.sql | 82 +++++++++++++---------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/macros/utils/aggregations/_min_or_max.sql b/macros/utils/aggregations/_min_or_max.sql index 276bd82..1b9fcfa 100644 --- a/macros/utils/aggregations/_min_or_max.sql +++ b/macros/utils/aggregations/_min_or_max.sql @@ -1,65 +1,75 @@ {% macro _min_or_max(min_or_max, qualified_col) %} + {{ return(adapter.dispatch('_min_or_max','dbt_activity_schema')(min_or_max, qualified_col)) }} +{%- endmacro -%} -{% if target.type == "snowflake" %} -{% set aggregation = "min_by" if min_or_max == "min" else "max_by" %} -{% set qualified_ts_col = "{}.{}".format( - dbt_activity_schema.appended(), dbt_activity_schema.columns().ts -) %} - -{# Apply min or max by to the selected column. #} -{% set aggregated_col %} - {{ aggregation }}({{ qualified_col }}, {{ qualified_ts_col }}) -{% endset %} +{% macro default___min_or_max(min_or_max, qualified_col) -%} -{# Return output. #} -{% do return(aggregated_col) %} - -{% else %} {% set aggregation = "min" if min_or_max == "min" else "max" %} {% set column_name = qualified_col.split(".")[-1].strip() %} -{% set qualified_ts_col = "{}.{}".format( - dbt_activity_schema.appended(), dbt_activity_schema.columns().ts -) %} +{% set qualified_ts_col = "{}.{}".format(dbt_activity_schema.appended(), dbt_activity_schema.columns().ts )%} {% set columns = dbt_activity_schema.columns() %} + {# Set type to cast back to after aggregation. #} {# TODO: Refactor column abstraction to contain types. #} -{% if column_name in [columns.ts, columns.activity_repeated_at] %} -{% set type = dbt.type_timestamp() %} -{% elif column_name in [columns.activity_occurrence, columns.revenue_impact] %} -{% set type = dbt.type_numeric() %} -{% else %} {% set type = dbt.type_string() %} +{% if column_name in [ + columns.ts, + columns.activity_repeated_at +] %} + {% set type = dbt.type_timestamp() %} +{% elif column_name in [ + columns.activity_occurrence, + columns.revenue_impact +] %} + {% set type = dbt.type_numeric() %} +{% else %} + {% set type = dbt.type_string() %} {% endif %} {# Prepend ts column and aggregate. See here for details: https://tinyurl.com/mwfz6xm4 #} {% set ts_concatenated_and_aggregated_col %} - {{ aggregation }}( - {{ dbt.concat([ - dbt.safe_cast(qualified_ts_col, dbt.type_string()), - dbt.safe_cast(qualified_col, dbt.type_string()) - ]) }} - ) + {{ aggregation }}( + {{ dbt.concat([ + dbt.safe_cast(qualified_ts_col, dbt.type_string()), + dbt.safe_cast(qualified_col, dbt.type_string()) + ]) }} + ) {% endset %} {# Aggregate ts column before trimming, so it is not required in GROUP BY. #} {% set aggregated_ts_col %} - {{ aggregation }}( {{ dbt.safe_cast(qualified_ts_col, dbt.type_string()) }} ) + {{ aggregation }}( {{ dbt.safe_cast(qualified_ts_col, dbt.type_string()) }} ) {% endset %} {# Calculate length of column without prepended & aggregated ts column. #} {% set retain_n_rightmost_characters %} - {{ dbt.length(ts_concatenated_and_aggregated_col) }} - {{ dbt.length(aggregated_ts_col) }} +{{ dbt.length(ts_concatenated_and_aggregated_col) }} - {{ dbt.length(aggregated_ts_col) }} {% endset %} {# Remove prepended & aggregated ts column. #} {% set output %} - {{ dbt.safe_cast( - dbt.right( - ts_concatenated_and_aggregated_col, - retain_n_rightmost_characters - ), type) }} +{{ dbt.safe_cast( + dbt.right( + ts_concatenated_and_aggregated_col, + retain_n_rightmost_characters + ), type) }} {% endset %} {% do return(output) %} -{% endif %} + {% endmacro %} + +{% macro snowflake___min_or_max(min_or_max, qualified_col) -%} +{% set aggregation = "min_by" if min_or_max == "min" else "max_by" %} +{% set qualified_ts_col = "{}.{}".format( + dbt_activity_schema.appended(), dbt_activity_schema.columns().ts +) %} + +{# Apply min or max by to the selected column. #} +{% set aggregated_col %} + {{ aggregation }}({{ qualified_col }}, {{ qualified_ts_col }}) +{% endset %} + +{# Return output. #} +{% do return(aggregated_col) %} +{%- endmacro %} \ No newline at end of file From fa808f206cfc9f59cb8b6b5dce48cc01afa17860 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Fri, 21 Apr 2023 15:40:04 -0400 Subject: [PATCH 4/4] formatting --- macros/utils/aggregations/_min_or_max.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macros/utils/aggregations/_min_or_max.sql b/macros/utils/aggregations/_min_or_max.sql index 1b9fcfa..26ff817 100644 --- a/macros/utils/aggregations/_min_or_max.sql +++ b/macros/utils/aggregations/_min_or_max.sql @@ -72,4 +72,4 @@ {# Return output. #} {% do return(aggregated_col) %} -{%- endmacro %} \ No newline at end of file +{%- endmacro %}