From f368cecca34a6488e14029d60d5116d7cc5fca51 Mon Sep 17 00:00:00 2001 From: deanna-minnick <41010575+deanna-minnick@users.noreply.github.com> Date: Mon, 10 Oct 2022 14:29:27 -0400 Subject: [PATCH 1/3] Feature/safe divide (#697) * add safe_divide documentation * add safe_divide macro * add integration test for safe_divide macro * moved macro and documentation to new SQL generator section Co-authored-by: Grace Goheen --- README.md | 16 ++++++++++++++++ integration_tests/data/sql/data_safe_divide.csv | 9 +++++++++ integration_tests/models/sql/schema.yml | 6 ++++++ .../models/sql/test_safe_divide.sql | 12 ++++++++++++ macros/sql/safe_divide.sql | 7 +++++++ 5 files changed, 50 insertions(+) create mode 100644 integration_tests/data/sql/data_safe_divide.csv create mode 100644 integration_tests/models/sql/test_safe_divide.sql create mode 100644 macros/sql/safe_divide.sql diff --git a/README.md b/README.md index 9e47e071..fd50c867 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Check [dbt Hub](https://hub.getdbt.com/dbt-labs/dbt_utils/latest/) for the lates - [generate_series](#generate_series-source) - [surrogate_key](#surrogate_key-source) - [safe_add](#safe_add-source) + - [safe_divide](#safe_divide-source) - [pivot](#pivot-source) - [unpivot](#unpivot-source) - [width_bucket](#width_bucket-source) @@ -1024,6 +1025,21 @@ Implements a cross-database way to sum nullable fields using the fields specifie {{ dbt_utils.safe_add('field_a', 'field_b'[,...]) }} ``` +#### safe_divide ([source](macros/cross_db_utils/safe_divide.sql)) + +This macro performs division but returns null if the denominator is 0. + +**Args:** + +- `numerator` (required): The number you want to divide. +- `denominator` (required): The number you want to divide by. + +**Usage:** + +``` +{{ dbt_utils.safe_divide('numerator', 'denominator') }} +``` + #### pivot ([source](macros/sql/pivot.sql)) This macro pivots values from rows to columns. diff --git a/integration_tests/data/sql/data_safe_divide.csv b/integration_tests/data/sql/data_safe_divide.csv new file mode 100644 index 00000000..aec2ee1d --- /dev/null +++ b/integration_tests/data/sql/data_safe_divide.csv @@ -0,0 +1,9 @@ +numerator,denominator,output +6,0, +10,5,2 +,, +,0, +17,, +0,, +,9, +0,5,0 \ No newline at end of file diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index d46d9283..3fc685fe 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -91,6 +91,12 @@ models: actual: actual expected: expected + - name: test_safe_divide + tests: + - assert_equal: + actual: actual + expected: expected + - name: test_pivot tests: - dbt_utils.equality: diff --git a/integration_tests/models/sql/test_safe_divide.sql b/integration_tests/models/sql/test_safe_divide.sql new file mode 100644 index 00000000..da5aeb0c --- /dev/null +++ b/integration_tests/models/sql/test_safe_divide.sql @@ -0,0 +1,12 @@ + +with data as ( + + select * from {{ ref('data_safe_divide') }} + +) + +select + {{ dbt_utils.safe_divide('numerator', 'denominator') }} as actual, + output as expected + +from data \ No newline at end of file diff --git a/macros/sql/safe_divide.sql b/macros/sql/safe_divide.sql new file mode 100644 index 00000000..ab3dfb94 --- /dev/null +++ b/macros/sql/safe_divide.sql @@ -0,0 +1,7 @@ +{% macro safe_divide(numerator, denominator) -%} + {{ return(adapter.dispatch('safe_divide', 'dbt_utils')(numerator, denominator)) }} +{%- endmacro %} + +{% macro default__safe_divide(numerator, denominator) %} + {{ numerator }} / nullif( {{ denominator }}, 0) +{% endmacro %} \ No newline at end of file From 6a5dd1eb97700301eaeba14c23c3682ceac9fb2b Mon Sep 17 00:00:00 2001 From: deanna-minnick <41010575+deanna-minnick@users.noreply.github.com> Date: Tue, 11 Oct 2022 14:04:12 -0400 Subject: [PATCH 2/3] Revert "Feature/safe divide (#697)" (#702) This reverts commit f368cecca34a6488e14029d60d5116d7cc5fca51. --- README.md | 16 ---------------- integration_tests/data/sql/data_safe_divide.csv | 9 --------- integration_tests/models/sql/schema.yml | 6 ------ .../models/sql/test_safe_divide.sql | 12 ------------ macros/sql/safe_divide.sql | 7 ------- 5 files changed, 50 deletions(-) delete mode 100644 integration_tests/data/sql/data_safe_divide.csv delete mode 100644 integration_tests/models/sql/test_safe_divide.sql delete mode 100644 macros/sql/safe_divide.sql diff --git a/README.md b/README.md index fd50c867..9e47e071 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ Check [dbt Hub](https://hub.getdbt.com/dbt-labs/dbt_utils/latest/) for the lates - [generate_series](#generate_series-source) - [surrogate_key](#surrogate_key-source) - [safe_add](#safe_add-source) - - [safe_divide](#safe_divide-source) - [pivot](#pivot-source) - [unpivot](#unpivot-source) - [width_bucket](#width_bucket-source) @@ -1025,21 +1024,6 @@ Implements a cross-database way to sum nullable fields using the fields specifie {{ dbt_utils.safe_add('field_a', 'field_b'[,...]) }} ``` -#### safe_divide ([source](macros/cross_db_utils/safe_divide.sql)) - -This macro performs division but returns null if the denominator is 0. - -**Args:** - -- `numerator` (required): The number you want to divide. -- `denominator` (required): The number you want to divide by. - -**Usage:** - -``` -{{ dbt_utils.safe_divide('numerator', 'denominator') }} -``` - #### pivot ([source](macros/sql/pivot.sql)) This macro pivots values from rows to columns. diff --git a/integration_tests/data/sql/data_safe_divide.csv b/integration_tests/data/sql/data_safe_divide.csv deleted file mode 100644 index aec2ee1d..00000000 --- a/integration_tests/data/sql/data_safe_divide.csv +++ /dev/null @@ -1,9 +0,0 @@ -numerator,denominator,output -6,0, -10,5,2 -,, -,0, -17,, -0,, -,9, -0,5,0 \ No newline at end of file diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index 3fc685fe..d46d9283 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -91,12 +91,6 @@ models: actual: actual expected: expected - - name: test_safe_divide - tests: - - assert_equal: - actual: actual - expected: expected - - name: test_pivot tests: - dbt_utils.equality: diff --git a/integration_tests/models/sql/test_safe_divide.sql b/integration_tests/models/sql/test_safe_divide.sql deleted file mode 100644 index da5aeb0c..00000000 --- a/integration_tests/models/sql/test_safe_divide.sql +++ /dev/null @@ -1,12 +0,0 @@ - -with data as ( - - select * from {{ ref('data_safe_divide') }} - -) - -select - {{ dbt_utils.safe_divide('numerator', 'denominator') }} as actual, - output as expected - -from data \ No newline at end of file diff --git a/macros/sql/safe_divide.sql b/macros/sql/safe_divide.sql deleted file mode 100644 index ab3dfb94..00000000 --- a/macros/sql/safe_divide.sql +++ /dev/null @@ -1,7 +0,0 @@ -{% macro safe_divide(numerator, denominator) -%} - {{ return(adapter.dispatch('safe_divide', 'dbt_utils')(numerator, denominator)) }} -{%- endmacro %} - -{% macro default__safe_divide(numerator, denominator) %} - {{ numerator }} / nullif( {{ denominator }}, 0) -{% endmacro %} \ No newline at end of file From 88afd9fe88efe892a4da9b338dea1a08b39856a7 Mon Sep 17 00:00:00 2001 From: ian-fahey-dbt <107962364+ian-fahey-dbt@users.noreply.github.com> Date: Tue, 22 Nov 2022 03:49:17 -0500 Subject: [PATCH 3/3] Quick nitpicks (#718) I was doing some studying on these and spotted some stuff. One verb conjugation and a consistency in macro description --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9e47e071..d4b66e1e 100644 --- a/README.md +++ b/README.md @@ -910,7 +910,7 @@ Optionally takes a `unit` string argument ('km' or 'mi') which defaults to miles #### group_by ([source](macros/sql/groupby.sql)) -This macro build a group by statement for fields 1...N +This macro builds a group by statement for fields 1...N **Usage:** @@ -1006,7 +1006,7 @@ This macro implements a cross-database mechanism to generate an arbitrarily long #### surrogate_key ([source](macros/sql/surrogate_key.sql)) -Implements a cross-database way to generate a hashed surrogate key using the fields specified. +This macro implements a cross-database way to generate a hashed surrogate key using the fields specified. **Usage:** @@ -1016,7 +1016,7 @@ Implements a cross-database way to generate a hashed surrogate key using the fie #### safe_add ([source](macros/sql/safe_add.sql)) -Implements a cross-database way to sum nullable fields using the fields specified. +This macro implements a cross-database way to sum nullable fields using the fields specified. **Usage:**