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 4 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
11 changes: 11 additions & 0 deletions 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 @@ -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'[,...]) }}
dchess marked this conversation as resolved.
Show resolved Hide resolved
```

#### 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 now takes a single list argument instead of \
string arguments. The {}.{} model triggered this warning. \
'.format(model.package_name, model.name) -%}

{%- do exceptions.warn(error_message) -%}
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, I don't think this note is necessary - we had it for safe_add as part of its behaviour migration, but this is a net-new macro that is being built the right way.

If someone does {{ dbt_utils.safe_subtract('first', 'second') }} it will render as coalesce(f, 0) - coalesce(i, 0), ... which will send them back to double check the docs anyway.

So two options:

  • Remove altogether
  • Change it to an exception and don't say "now takes", because it never didn't take that form

Copy link
Contributor Author

@dchess dchess Jan 25, 2023

Choose a reason for hiding this comment

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

@joellabes That makes total sense to me. I think since the shift is still fairly new it makes sense to throw an exception and I have updated it accordingly, but let me know if you would prefer to remove altogether instead.


{%- endif -%}

{% set fields = [] %}

{%- for field in field_list -%}

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

{%- endfor -%}

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

{%- endmacro -%}