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

Persist predicates to get_merge_sql and wrap in parentheses #5679

Closed
wants to merge 3 commits into from
Closed

Persist predicates to get_merge_sql and wrap in parentheses #5679

wants to merge 3 commits into from

Conversation

NiallRees
Copy link
Contributor

@NiallRees NiallRees commented Aug 18, 2022

Resolves #5680 and dbt-labs/dbt-snowflake#231

This enables the already available incremental_predicates to be persisted to get_merge_sql(). It also wraps all predicates in parentheses to force the correct evaluation order when they're anded.

I can now add this config to an incremental model:

        incremental_predicates=[
            'dbt_internal_source.tenant_id = dbt_internal_dest.tenant_id or dbt_internal_source.tenant_id is null or dbt_internal_dest.tenant_id is null',
            'dbt_internal_source.tenant_id2 = dbt_internal_dest.tenant_id2 or dbt_internal_source.tenant_id2 is null or dbt_internal_dest.tenant_id2 is null'
        ],

Resulting in:

merge into analytics.core.model as DBT_INTERNAL_DEST
        using analytics.core.model__dbt_tmp as DBT_INTERNAL_SOURCE
        on (dbt_internal_source.tenant_id = dbt_internal_dest.tenant_id or dbt_internal_source.tenant_id is null or dbt_internal_dest.tenant_id is null) and (dbt_internal_source.tenant_id2 = dbt_internal_dest.tenant_id2 or dbt_internal_source.tenant_id2 is null or dbt_internal_dest.tenant_id2 is null) and 
                DBT_INTERNAL_SOURCE.pk = DBT_INTERNAL_DEST.pk

improving pruning performance.

I've changed the default value of predicates to be an empty list from none, that may have been done for a reason I'm unaware of though! Also not sure if/where to add tests so appreciate any pointers.

Description

Checklist

@NiallRees NiallRees requested a review from a team August 18, 2022 09:45
@NiallRees NiallRees requested a review from a team as a code owner August 18, 2022 09:45
@cla-bot cla-bot bot added the cla:yes label Aug 18, 2022
@NiallRees NiallRees requested a review from a team as a code owner August 18, 2022 10:11
@jtcohen6 jtcohen6 added Team:Adapters Issues designated for the adapter area of the code ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering labels Aug 18, 2022
Comment on lines +56 to +57
{#-- Wrap predicates in parentheses to ensure correct evaluation when later anded --#}
{% do predicates.append('(' ~ predicate ~ ')') %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh! I see what you mean, since predicates could have their own nested conditional logic (and/or):

(predicate_a) and (predicate_b) and (predicate_c)

Would it make more sense to do that wrapping here, or within the templating logic in get_merge_sql?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I concluded here was best, as it avoids the need to replicate this logic into every get_x_sql which might use the predicates. I'll defer to you though!

Copy link
Contributor

@jtcohen6 jtcohen6 left a comment

Choose a reason for hiding this comment

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

Thanks @NiallRees! I'd be very excited to (finally) have this functionality. I've got a bit of bookkeeping to do, to clean up the issues tracking this work.

The right place for tests is probably in the "incremental" subsection of the "adapter zone" functional tests: https://github.com/dbt-labs/dbt-core/tree/main/tests/adapter/dbt/tests/adapter/incremental. We'd want the ability to pull these tests in and run them on adapter plugins, too.

One thought: We could take this one step further, and make it available to the other incremental strategies as well, by pulling in predicates to each of those macros, too. @dave-connors-3 had done work in that vein back in #4546. If you want to keep this limited in scope, to just default__get_merge_sql and the adapters which use it, that's fine too — we should open a follow-on ticket to track complete availability across adapters + strategies.

@NiallRees
Copy link
Contributor Author

Thanks @NiallRees! I'd be very excited to (finally) have this functionality. I've got a bit of bookkeeping to do, to clean up the issues tracking this work.

The right place for tests is probably in the "incremental" subsection of the "adapter zone" functional tests: https://github.com/dbt-labs/dbt-core/tree/main/tests/adapter/dbt/tests/adapter/incremental. We'd want the ability to pull these tests in and run them on adapter plugins, too.

One thought: We could take this one step further, and make it available to the other incremental strategies as well, by pulling in predicates to each of those macros, too. @dave-connors-3 had done work in that vein back in #4546. If you want to keep this limited in scope, to just default__get_merge_sql and the adapters which use it, that's fine too — we should open a follow-on ticket to track complete availability across adapters + strategies.

I think I'll keep this limited in scope if that's cool, just to increase my chances of getting it across the finish line relatively quickly!

@jtcohen6
Copy link
Contributor

jtcohen6 commented Jan 2, 2023

Closing this PR, in favor of #5702 (merged for inclusion in v1.4!)

@jtcohen6 jtcohen6 closed this Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes ready_for_review Externally contributed PR has functional approval, ready for code review from Core engineering Team:Adapters Issues designated for the adapter area of the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-1054] [Feature] Add predicates to default__get_incremental_merge_sql
2 participants