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

Fix merge fails when partition is required #986

Merged
merged 12 commits into from
Feb 16, 2024

Conversation

tnk-ysk
Copy link
Contributor

@tnk-ysk tnk-ysk commented Oct 27, 2023

resolves #792
docs dbt-labs/docs.getdbt.com/#

Problem

Trying to migrate a BQ stored procedure to DBT where the partition on the target table is required. I receive this error on the table the requires a partition:

BadRequest("
Cannot query over table 'test' without a filter over column(s) 'date' that can be used for partition elimination")

Solution

Avoid require partition filter by adding partition is null or partition is not null to the merge condition.
This condition does not change the target record.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX

@tnk-ysk tnk-ysk requested a review from a team as a code owner October 27, 2023 17:07
@cla-bot
Copy link

cla-bot bot commented Oct 27, 2023

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @tnk-ysk

@tnk-ysk tnk-ysk force-pushed the fix-require-partition-error branch from 8651295 to da07bff Compare October 27, 2023 17:08
@cla-bot
Copy link

cla-bot bot commented Oct 27, 2023

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @tnk-ysk

@tnk-ysk tnk-ysk force-pushed the fix-require-partition-error branch from da07bff to 0c17b47 Compare October 27, 2023 17:19
@cla-bot cla-bot bot added the cla:yes label Oct 27, 2023
@tnk-ysk
Copy link
Contributor Author

tnk-ysk commented Oct 28, 2023

I'm wondering if I should add the partition_filld parameter to get_merge_sql and modify default__get_merge_sql instead of adding bigquery__get_merge_sql.
Please let us know what you think.

@github-christophe-oudar
Copy link
Contributor

Great contribution @tnk-ysk!
Could it be possible to extract

{% if partition_config and config.get('require_partition_filter') -%}
            {% set avoid_require_partition %}
                DBT_INTERNAL_DEST.{{ partition_config.field }} IS NULL OR DBT_INTERNAL_DEST.{{ partition_config.field }} IS NOT NULL
            {% endset %}
            {% do predicates.append(avoid_require_partition) %}
        {%- endif -%}

And add that condition to the predicates so that you can combine it with https://github.com/dbt-labs/dbt-bigquery/pull/994/files?

@tnk-ysk
Copy link
Contributor Author

tnk-ysk commented Nov 5, 2023

@github-christophe-oudar
Thank you for your review.
I extracted the process and made it into a macro, so please check it.

I will to fix #994 after this PR is merged.

@github-christophe-oudar
Copy link
Contributor

@tnk-ysk it looks better!
Then wouldn't you be able to keep the default macro and pass the predicates array as the incremental_predicates param from the get_merge_sql macro?
My point is that if someone has overridden the get_merge_sql macro inside its dbt project for any reason, he won't benefit from the change if we redefine it that way.

@tnk-ysk
Copy link
Contributor Author

tnk-ysk commented Nov 6, 2023

@github-christophe-oudar
Thank you for your wonderful comment.
I fixed it. The predicate is also added if unique_key is not set, but there is no negative effect.

Also made the macro return the predicate instead of making destructive change.

Copy link
Contributor

@github-christophe-oudar github-christophe-oudar left a comment

Choose a reason for hiding this comment

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

LGTM, I'll let dbt Labs team takes over the review as I don't have any rights.

@mikealfare mikealfare merged commit f4cf22c into dbt-labs:main Feb 16, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ADAP-659] [CT-2745] [Bug] BigQuery merge fails when partition is required
4 participants