-
Notifications
You must be signed in to change notification settings - Fork 179
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
[Bug] Use fully qualified names in rename for tables and views #1060
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f9e8b16
update rename macro to include fully qualified target name
mikealfare 948f21f
changelog
mikealfare f39276c
jinja typo
mikealfare de6ab1c
update incorporate to use path dict
mikealfare 062679f
add test cases for expected rename statements
mikealfare 3be5716
provide implementation options
mikealfare 807ac26
highlighted the changed lines
mikealfare 3f9ea80
add a fourth approach
mikealfare 81ff4de
override create_backup and rename_intermediate to use fully qualified…
mikealfare aaaf423
update tests to only test backup and intermediate renames
mikealfare eccf298
Merge branch 'main' into bug/1031-fully-qualified-name-in-rename
mikealfare fe02214
Merge branch 'main' into bug/1031-fully-qualified-name-in-rename
mikealfare ffa74fe
Merge branch 'main' into bug/1031-fully-qualified-name-in-rename
mikealfare 380c848
Merge branch 'main' into bug/1031-fully-qualified-name-in-rename
mikealfare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: 'Rename targets for tables and views use fully qualified names' | ||
time: 2024-05-22T16:05:38.602074-04:00 | ||
custom: | ||
Author: mikealfare | ||
Issue: "1031" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{%- macro snowflake__get_rename_table_sql(relation, new_name) -%} | ||
alter table {{ relation }} rename to {{ new_name }} | ||
alter table {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} | ||
alter view {{ relation }} rename to {{ new_name }} | ||
alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} | ||
{%- endmacro -%} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a user pointed out in this comment, it isn't clear right now whether the type signature of
get_rename_x_sql
is:This change will break if someone is currently passing a
Relation
object into the second argument. Do you think we should add conditional handling based on the type ofnew_name
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My short answer is that I'd rather not overload the argument.
While I specifically chose the suffix
name
to distinguish fromrelation
, that's an implicit assumption. We could adopt the practice of adding the equivalent of a docstring to macros that we expect users to use (or maybe just all of them for our own sake). I like this idea better than what we're currently doing. If we instead add a conditional that handles both a relation and a string, then we're advertising that both are acceptable when we really don't expect a relation.I have a stronger concern from the user's second comment, that this takes away the ability to use this macro to move an object to a different schema by supplying a fully qualified name. The Snowflake docs on ALTER TABLE suggest that the default for <new_table_name> is just the identifier, which aligns with my experience in other databases. They go on further in the parameters section to mention that you could use an ALTER/RENAME statement to move an object from one database/schema to another. That's possible with the current implementation, but not so with this version.
So talking that out, I actually think we should not make this change at all. However, it's certainly worth adding the docstring to make it clearer to users what this macro expects for arguments and how it's expected to be used. I would also keep the tests that were added and update them to whatever we choose to go with. I'm definitely interested in your thoughts on this before moving forward as this is ultimately your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikealfare Thanks for thinking this through!
str
orRelation
(andstr
to avoid a breaking change with current expected type)use
statements before every single query)I might have been confused about the specific bug that surfaced with dynamic tables, which motivated me to open #1031. I agree the actual underlying issue was the
2024_03
bundle change toshow terse objects
, which led dbt to think a DT was actually a table, and try to drop + recreate it accordingly. But it was during that drop + recreate (= create-alter-rename-swap-drop), the backup DT was supposed to be getting dropped, but dbt wasn't dropping it correctly, because it wasn't actually renaming them correctly.Basically, something like this:
This fails because the table was renamed to
analytics.dbt_jcohen_dev.my_table__dbt_backup
, NOTanalytics.dbt_jcohen_other.my_table__dbt_backup
. In the meantime, the renamed DT keeps "hanging out" and is never actually dropped.Making the proposed change actually resolved that surface-level issue for me (with
2024_03
bundle + previousshow terse objects
logic), though it left the underlying problem about misclassifying DTs (which we resolved in #1049).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notes from live conversation with @mikealfare
Two options here:
get_rename_sql
is only to update theidentifier
of an object (view/table/etc). It does not support moving objects into different databases/schemas. If we need to do the latter in the future, we'll implement some other macro (get_rename_relation_sql
).get_rename_sql
acceptsUnion[str, Relation]
as the type fornew_name
, and handles either with conditional logic. This feels like too low-level a place to be doing the type checking, and we'll be doing it several times in different places.Proposal: The top-level
get_rename_sql
macro should accept two arguments:relation
(which is aRelation
) andnew_name
, which is either:identifier
forrelation
(first argument). We expect this is >95% of the expected usage of this macro, and so it should be easy to use it in this way.Relation
object, which it will convert to a string (viarender()
) representing that relation's fully qualified name.string
like"different_database.different_schema.different_identifier"
. If a user wants to leverageget_rename_sql
for moving objects to different database/schema, they need to build aRelation
object and pass that in.In this example, whether we make the second argument
backup_relation.identifier
("happy path") OR justbackup_relation
, we get the same result. In that way, this is a backward-compatible change and yields what the user would expect.What feels less good? The lower-level rename macros to which it dispatches should accept
relation
(which is aRelation
) andnew_name
(which is always a string, representing a fully qualified relation name).We're going to need to make some choice here that feels less-than-ideal (or has some trade-off), so we're going to spend a little (not too much) time comparing two potential paths forward and then pick one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtcohen6 Please see the new files that I pushed to this PR:
_rename_approach_0.sql
- current state_rename_approach_1.sql
- this PR's approach_rename_approach_2.sql
- the approach from our discussion_rename_approach_3.sql
- a compromise of 1 and 2_rename_usage.sql
- two impacted use cases of these changesI combined macros from different files and packages in one spot so that we don't need to flip back and forth to follow the flow. I also highlighted the change with a comment. Most of the code really isn't changing, but it helps to see where the changed code is. My new proposal is option 3.
I have two more outcomes from this exercise:
_rename_approach_0.sql
) somewhere for future discussionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a fourth approach which I think I actually prefer more than 3. We basically only impact the backup/deploy renames in
dbt-snowflake
, which I think is where we're seeing the issue anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikealfare Thanks for the thorough walkthrough! So the idea behind approach 4 is:
dbt-snowflake
, to pass the entire rendered relation name (fully qualified) from the backup/intermediate rename macrosI think I buy your reasoning, that this approach is:
dbt-snowflake
because of this unique(?) behavior on SnowflakeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's the rationale. And this leaves the rename macro flexible, so that users can use it for other purposes. I'll update this PR with approach 4 then, and remove the temp files. Thanks for the feedback!