From 0dfe0d15bf187444c8e69316bb2a5723e7b6b1e8 Mon Sep 17 00:00:00 2001 From: dchess Date: Sat, 17 Dec 2022 15:08:05 -0600 Subject: [PATCH 1/6] Add macro definition --- macros/sql/safe_subtract.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 macros/sql/safe_subtract.sql diff --git a/macros/sql/safe_subtract.sql b/macros/sql/safe_subtract.sql new file mode 100644 index 00000000..5d4eb438 --- /dev/null +++ b/macros/sql/safe_subtract.sql @@ -0,0 +1,28 @@ +{%- macro safe_subtract(field_list) -%} + {{ return(adapter.dispatch('safe_subtract', 'dbt_utils')(field_list)) }} +{% endmacro %} + +{%- macro default__safe_subtract(field_list) -%} + +{%- if field_list is not iterable or field_list is string or field_list is mapping -%} + +{%- set error_message = ' +Warning: the `safe_subtract` macro now takes a single list argument instead of \ +string arguments. The {}.{} model triggered this warning. \ +'.format(model.package_name, model.name) -%} + +{%- do exceptions.warn(error_message) -%} + +{%- endif -%} + +{% set fields = [] %} + +{%- for field in field_list -%} + + {% do fields.append("coalesce(" ~ field ~ ", 0)") %} + +{%- endfor -%} + +{{ fields|join(' -\n ') }} + +{%- endmacro -%} From 601c18eda992269efe09e2e223065808659a5204 Mon Sep 17 00:00:00 2001 From: dchess Date: Sat, 17 Dec 2022 15:08:16 -0600 Subject: [PATCH 2/6] Add seed file for tests --- integration_tests/data/sql/data_safe_subtract.csv | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 integration_tests/data/sql/data_safe_subtract.csv diff --git a/integration_tests/data/sql/data_safe_subtract.csv b/integration_tests/data/sql/data_safe_subtract.csv new file mode 100644 index 00000000..1d5652a8 --- /dev/null +++ b/integration_tests/data/sql/data_safe_subtract.csv @@ -0,0 +1,5 @@ +field_1,field_2,field_3,expected +3,2,1,0 +4,,3,1 +,,2,-2 +,,,0 From d272e0ba4482057a77c75c35877ed40d41e1205b Mon Sep 17 00:00:00 2001 From: dchess Date: Sat, 17 Dec 2022 15:08:26 -0600 Subject: [PATCH 3/6] Add integration test --- integration_tests/models/sql/schema.yml | 6 ++++++ integration_tests/models/sql/test_safe_subtract.sql | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 integration_tests/models/sql/test_safe_subtract.sql diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index fab8a20b..e79e782f 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -120,6 +120,12 @@ models: - assert_equal: actual: actual expected: expected + + - name: test_safe_subtract + tests: + - assert_equal: + actual: actual + expected: expected - name: test_safe_divide tests: diff --git a/integration_tests/models/sql/test_safe_subtract.sql b/integration_tests/models/sql/test_safe_subtract.sql new file mode 100644 index 00000000..0ce816d0 --- /dev/null +++ b/integration_tests/models/sql/test_safe_subtract.sql @@ -0,0 +1,12 @@ + +with data as ( + + select * from {{ ref('data_safe_subtract') }} + +) + +select + {{ dbt_utils.safe_subtract(['field_1', 'field_2', 'field_3']) }} as actual, + expected + +from data From ab130090277df295f0a07ccef79c3e6831cd8637 Mon Sep 17 00:00:00 2001 From: dchess Date: Sat, 17 Dec 2022 15:11:18 -0600 Subject: [PATCH 4/6] Update readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 5c1953fe..46100e32 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Check [dbt Hub](https://hub.getdbt.com/dbt-labs/dbt_utils/latest/) for the lates - [generate_surrogate_key](#generate_surrogate_key-source) - [safe_add](#safe_add-source) - [safe_divide](#safe_divide-source) + - [safe_subtract](#safe_subtract-source) - [pivot](#pivot-source) - [unpivot](#unpivot-source) - [width_bucket](#width_bucket-source) @@ -1071,6 +1072,16 @@ This macro performs division but returns null if the denominator is 0. {{ dbt_utils.safe_divide('numerator', 'denominator') }} ``` +#### safe_subtract ([source](macros/sql/safe_subtract.sql)) + +This macro implements a cross-database way to take the difference of nullable fields using the fields specified. + +**Usage:** + +``` +{{ dbt_utils.safe_subtract('field_a', 'field_b'[,...]) }} +``` + #### pivot ([source](macros/sql/pivot.sql)) This macro pivots values from rows to columns. From 533c042b30a0121c591e0ee29512b6c5fa4ec6e9 Mon Sep 17 00:00:00 2001 From: dchess Date: Tue, 24 Jan 2023 21:20:15 -0600 Subject: [PATCH 5/6] Update exception message --- macros/sql/safe_subtract.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/macros/sql/safe_subtract.sql b/macros/sql/safe_subtract.sql index 5d4eb438..378424ce 100644 --- a/macros/sql/safe_subtract.sql +++ b/macros/sql/safe_subtract.sql @@ -7,11 +7,11 @@ {%- if field_list is not iterable or field_list is string or field_list is mapping -%} {%- set error_message = ' -Warning: the `safe_subtract` macro now takes a single list argument instead of \ +Warning: the `safe_subtract` macro takes a single list argument instead of \ string arguments. The {}.{} model triggered this warning. \ '.format(model.package_name, model.name) -%} -{%- do exceptions.warn(error_message) -%} +{%- do exceptions.raise_compiler_error(error_message) -%} {%- endif -%} From e193a285aa17f9e9e752b13f70acd51074480546 Mon Sep 17 00:00:00 2001 From: dchess Date: Tue, 24 Jan 2023 21:21:58 -0600 Subject: [PATCH 6/6] Correct docs for safe_add and safe_subtract --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 46100e32..186295f4 100644 --- a/README.md +++ b/README.md @@ -1054,7 +1054,7 @@ This macro implements a cross-database way to sum nullable fields using the fiel **Usage:** ``` -{{ dbt_utils.safe_add('field_a', 'field_b'[,...]) }} +{{ dbt_utils.safe_add(['field_a', 'field_b', ...]) }} ``` #### safe_divide ([source](macros/cross_db_utils/safe_divide.sql)) @@ -1079,7 +1079,7 @@ This macro implements a cross-database way to take the difference of nullable fi **Usage:** ``` -{{ dbt_utils.safe_subtract('field_a', 'field_b'[,...]) }} +{{ dbt_utils.safe_subtract(['field_a', 'field_b', ...]) }} ``` #### pivot ([source](macros/sql/pivot.sql))