Skip to content

Commit

Permalink
Feature/safe divide (dbt-labs#697)
Browse files Browse the repository at this point in the history
* 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 <graciegoheen@gmail.com>
  • Loading branch information
deanna-minnick and graciegoheen committed Oct 10, 2022
1 parent 7eab492 commit f368cec
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions integration_tests/data/sql/data_safe_divide.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
numerator,denominator,output
6,0,
10,5,2
,,
,0,
17,,
0,,
,9,
0,5,0
6 changes: 6 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/models/sql/test_safe_divide.sql
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions macros/sql/safe_divide.sql
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit f368cec

Please sign in to comment.