Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/safe divide #697

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}