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

Safe subtract #748

Merged
merged 6 commits into from
Feb 8, 2023
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1053,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))
Expand All @@ -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.
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful 😍

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 -%}