From 30dbaadf924e5aafe901e42ab4581a23e13a08f5 Mon Sep 17 00:00:00 2001 From: MaxxBusch Date: Thu, 21 Aug 2025 09:47:01 +0200 Subject: [PATCH 1/3] Added `expect_multicolumn_sum_to_be_between` --- .../expect_multicolumn_sum_to_be_between.sql | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 macros/schema_tests/multi-column/expect_multicolumn_sum_to_be_between.sql diff --git a/macros/schema_tests/multi-column/expect_multicolumn_sum_to_be_between.sql b/macros/schema_tests/multi-column/expect_multicolumn_sum_to_be_between.sql new file mode 100644 index 0000000..c28592f --- /dev/null +++ b/macros/schema_tests/multi-column/expect_multicolumn_sum_to_be_between.sql @@ -0,0 +1,54 @@ + +{% test expect_multicolumn_sum_to_be_between(model, + column_list, + min_value=None, + max_value=None, + group_by=None, + row_condition=None, + strictly=False + ) %} + +{% set expression %} +{% for column in column_list %} +sum({{ column }}){% if not loop.last %} + {% endif %} +{# the if just allows for column names or literal numbers #} +{% endfor %} +{% endset %} + +{%- if min_value is none and max_value is none -%} +{{ exceptions.raise_compiler_error( + "You have to provide either a min_value, max_value or both." +) }} +{%- endif -%} + +{%- set strict_operator = "" if strictly else "=" -%} + +{% set expression_min_max %} +( 1=1 +{%- if min_value is not none %} + and {{ expression | trim }} >{{ strict_operator }} + {% if min_value is number %} + {{min_value}} + {% else %} + sum({{ min_value }}) + {% endif %} +{% endif %} + +{%- if max_value is not none %} + and {{ expression | trim }} <{{ strict_operator }} + {% if max_value is number %} + {{max_value}} + {% else %} + sum({{ max_value }}) + {% endif %} +{% endif %} +) +{% endset %} + +{{ dbt_expectations.expression_is_true(model, + expression=expression_min_max, + group_by_columns=group_by_columns, + row_condition=row_condition) + }} + +{% endtest %} From 40609bcaea2b9c2ab267af9b8dbda94f9a280b6b Mon Sep 17 00:00:00 2001 From: MaxxBusch Date: Thu, 21 Aug 2025 10:22:02 +0200 Subject: [PATCH 2/3] Added integration tests for `expect_multicolumn_sum_to_be_between` --- integration_tests/models/schema_tests/schema.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/integration_tests/models/schema_tests/schema.yml b/integration_tests/models/schema_tests/schema.yml index 4f5124f..1cc56ff 100644 --- a/integration_tests/models/schema_tests/schema.yml +++ b/integration_tests/models/schema_tests/schema.yml @@ -484,7 +484,21 @@ models: column_list: ["col_numeric_a", "col_numeric_b"] sum_total: col_numeric_a_plus_b group_by: [idx] - + - dbt_expectations.expect_multicolumn_sum_to_be_between: + column_list: ["col_numeric_a", "col_numeric_b"] + min_value: 3 + max_value: 5 + strictly: true + - dbt_expectations.expect_multicolumn_sum_to_be_between: + column_list: ["col_numeric_a", "col_numeric_b"] + min_value: 4 + max_value: 4 + strictly: false + - dbt_expectations.expect_multicolumn_sum_to_be_between: + column_list: ["col_numeric_a", "col_numeric_b"] + min_value: col_numeric_a_plus_b + max_value: col_numeric_a_plus_b + strictly: false columns: - name: idx From 0a43314b091dd2971b4fa38f51c4e0a394bfc35b Mon Sep 17 00:00:00 2001 From: MaxxBusch Date: Thu, 21 Aug 2025 10:52:45 +0200 Subject: [PATCH 3/3] Added docs --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 71a21e3..0c22dc9 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ For example, use `America/New_York` for East Coast Time. - [expect_column_pair_values_to_be_in_set](#expect_column_pair_values_to_be_in_set) - [expect_compound_columns_to_be_unique](#expect_compound_columns_to_be_unique) - [expect_multicolumn_sum_to_equal](#expect_multicolumn_sum_to_equal) +- [expect_multicolumn_sum_to_be_between](#expect_multicolumn_sum_to_be_between) - [expect_select_column_values_to_be_unique_within_record](#expect_select_column_values_to_be_unique_within_record) ### Distributional functions @@ -1101,6 +1102,23 @@ tests: row_condition: "id is not null" # (Optional) ``` +### [expect_multicolumn_sum_to_be_between](macros/schema_tests/multi-column/expect_multicolumn_sum_to_be_between.sql) + +Expects that sum of all rows for a set of columns is between two values. + +*Applies to:* Model, Seed, Source + +```yaml +tests: + - dbt_expectations.expect_multicolumn_sum_to_be_between: + column_list: ["col_numeric_a", "col_numeric_b"] + min_value: 3 + max_value: 5 + group_by: [group_id, other_group_id, ...] # (Optional) + row_condition: "id is not null" # (Optional) + strictly: false # (Optional) +``` + ### [expect_compound_columns_to_be_unique](macros/schema_tests/multi-column/expect_compound_columns_to_be_unique.sql) Expect that the columns are unique together, e.g. a multi-column primary key.