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

ADAP-869: Support atomic replace in replace macro #8539

Merged
merged 46 commits into from
Sep 11, 2023

Conversation

mikealfare
Copy link
Contributor

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

Problem

The current replace macro does not support replacing an existing relation with a target relation of the same type using an atomic "create and replace". Instead it tries to do the stage/backup/swap method which uses at least two statements (one to drop the existing and one to create the target).

Solution

Update the replace macro to first check if the types are the same, and if so, if the type is a replaceable type. Some relation types are not replaceable (e.g. materialized views in Postgres). If this check fails, then head down the stage/backup/swap route.

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

# Conflicts:
#	core/dbt/include/global_project/macros/relations/drop.sql
#	core/dbt/include/global_project/macros/relations/rename.sql
#	plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql
…relations` so that generics are close to the relation specific macros that they reference; also aligns with adapter macro files structure, to look more familiar
…r adapters that have a unique list of relation types, e.g. dbt-snowflake
…nd can be addressed properly at a later date
@mikealfare mikealfare requested review from a team as code owners August 31, 2023 23:57
@mikealfare mikealfare requested review from Fleid and aranke August 31, 2023 23:57
@mikealfare
Copy link
Contributor Author

Since postgres does not use replace in this way yet (it's not implemented for tables and views and materialized views don't support create or replace), it was tested against dbt-snowflake. The tests that used to use drop/create methods are now using create or replace.

# Conflicts:
#	core/dbt/adapters/base/relation.py
#	core/dbt/include/global_project/macros/relations/materialized_view/_replace.sql
#	core/dbt/include/global_project/macros/relations/materialized_view/alter.sql
#	core/dbt/include/global_project/macros/relations/replace.sql
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to relations/materialized_view/alter.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.

Replaced with relations/materialized_view/replace.sql, should have shown as a rename. That file was then updated to what shows below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Contents were moved to relations/view/replace.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.

Replaced with relations/materialized_view/replace.sql, should have shown as a rename. The contents of that file were then updated to the below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was taken from the default create view statement in dbt-core. Postgres did not overwrite it. It was then updated to run a create or replace instead of just replace and whitespace was adjusted for readability; otherwise it was left untouched.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Contents of this file were moved to relations/materialized_view/alter.sql.

@mikealfare mikealfare requested review from nathaniel-may and removed request for Fleid September 1, 2023 00:57
Copy link
Member

@aranke aranke left a comment

Choose a reason for hiding this comment

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

LGTM, but obviously needs Adapters 👍🏽 as well.
One nit on whether we can make the logic a bit more dynamic instead of having these large switch statements everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants