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

Safe subtract #748

merged 6 commits into from
Feb 8, 2023

Conversation

dchess
Copy link
Contributor

@dchess dchess commented Dec 17, 2022

resolves #745

This is a:

  • documentation update
  • bug fix with no breaking changes
  • new functionality
  • a breaking change

All pull requests from community contributors should target the main branch (default).

Description & motivation

As discussed in #745, this new macro will provide a method for taking the difference across nullable fields similar in functionality to safe_add(). Currently it is necessary to multiply fields by -1 with the safe_add() macro or use coalesce statements in sql directly to achieve the same result.

Checklist

  • This code is associated with an Issue which has been triaged and accepted for development.
  • I have verified that these changes work locally on the following warehouses (Note: it's okay if you do not have access to all warehouses, this helps us understand what has been covered)
    • BigQuery
    • Postgres
    • Redshift
    • Snowflake
  • I followed guidelines to ensure that my changes will work on "non-core" adapters by:
    • dispatching any new macro(s) so non-core adapters can also use them (e.g. the star() source)
    • using the limit_zero() macro in place of the literal string: limit 0
    • using dbt.type_* macros instead of explicit datatypes (e.g. dbt.type_timestamp() instead of TIMESTAMP
  • I have updated the README.md (if applicable)
  • I have added tests & descriptions to my models (and macros if applicable)
  • I have added an entry to CHANGELOG.md

Copy link
Contributor

@joellabes joellabes left a comment

Choose a reason for hiding this comment

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

this is looking great! sorry for the delay in getting back to you with a review 🌟

A couple of changes but overall very excited to get this in

@@ -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 😍

Comment on lines 7 to 14
{%- 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.

README.md Outdated Show resolved Hide resolved
@dchess
Copy link
Contributor Author

dchess commented Jan 25, 2023

@joellabes looks like the redshift integration tests failed after my updates. Can you share the errors or any insights you may have about the reason? I'd also love to get this in.

@dchess
Copy link
Contributor Author

dchess commented Jan 25, 2023

@joellabes Sorry not used to CI being publicly visible! I was able to access the details myself.

It looks like the seed files aren't loading due to a permission error:

permission denied for schema dbt_utils_integration_tests_redshift

@joellabes
Copy link
Contributor

@dchess I'm looking into this!

Copy link
Contributor

@joellabes joellabes left a comment

Choose a reason for hiding this comment

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

Redshift is passing again, so we're in business! Thanks for getting this over the line @dchess

(And thanks to @dave-connors-3 and @dbeatty10 for the assist with CI here!)

@joellabes joellabes merged commit ba99ffb into dbt-labs:main Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

It would helpful to have a subtraction equivalent of the safe_add() macro
2 participants