Skip to content

Commit

Permalink
Safe subtract (#748)
Browse files Browse the repository at this point in the history
* Add macro definition

* Add seed file for tests

* Add integration test

* Update readme

* Update exception message

* Correct docs for safe_add and safe_subtract
  • Loading branch information
dchess committed Feb 8, 2023
1 parent eaa0e41 commit ba99ffb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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)
Expand Down Expand Up @@ -1055,7 +1056,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))
Expand All @@ -1073,6 +1074,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.
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/data/sql/data_safe_subtract.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
field_1,field_2,field_3,expected
3,2,1,0
4,,3,1
,,2,-2
,,,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 @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/models/sql/test_safe_subtract.sql
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions macros/sql/safe_subtract.sql
Original file line number Diff line number Diff line change
@@ -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 takes a single list argument instead of \
string arguments. The {}.{} model triggered this warning. \
'.format(model.package_name, model.name) -%}

{%- do exceptions.raise_compiler_error(error_message) -%}

{%- endif -%}

{% set fields = [] %}

{%- for field in field_list -%}

{% do fields.append("coalesce(" ~ field ~ ", 0)") %}

{%- endfor -%}

{{ fields|join(' -\n ') }}

{%- endmacro -%}

0 comments on commit ba99ffb

Please sign in to comment.