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

Refactor(optimizer): improve handling of DDL optimization #1972

Merged
merged 5 commits into from
Jul 28, 2023

Conversation

georgesittas
Copy link
Collaborator

@georgesittas georgesittas commented Jul 28, 2023

cc @gtoonstra

Maybe the name DDL isn't the best for this, given that INSERT is actually in the DML category..

@georgesittas georgesittas requested a review from tobymao July 28, 2023 15:31
@@ -990,7 +990,7 @@ class Uncache(Expression):
arg_types = {"this": True, "exists": False}


class TableChange(Expression):
class DDL(Expression):
Copy link
Collaborator Author

@georgesittas georgesittas Jul 28, 2023

Choose a reason for hiding this comment

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

I didn't have UPDATE inherit from this on purpose because it doesn't have a Subqueryable expression like CREATE and INSERT, so it'd need more effort to qualify / optimize its SET .. FROM .. syntax. Perhaps we could tackle this in a future PR.

Comment on lines +589 to +590
with_ = scope.expression.args.get("with")
if with_ and with_.recursive:
Copy link
Collaborator Author

@georgesittas georgesittas Jul 28, 2023

Choose a reason for hiding this comment

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

In WITH ... CREATE ... AS SELECT ... the CTEs are not attached to the SELECT, so args["with"] would raise a KeyError here.

Comment on lines +753 to +755
query_scope = scope.branch(
scope.expression.expression, scope_type=ScopeType.DERIVED_TABLE, chain_sources=scope.sources
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Picked DERIVED_TABLE here because ROOT is already taken for the DDL statement itself. Might not be the cleanest approach but I think it's fine for now?

@georgesittas
Copy link
Collaborator Author

georgesittas commented Jul 28, 2023

Regarding the comment I left here #1949 (comment), it seems like this is a known "issue" based on:

# title: cte can't be qualified
WITH a AS (SELECT 1 FROM z) SELECT 1 FROM a;
WITH a AS (SELECT 1 FROM c.db.z AS z) SELECT 1 FROM a;

I assume this is because when we do SELECT ... FROM cte the cte is treated as a Scope source and hence we don't visit it in qualify_table's loop where tables are qualified.

@georgesittas georgesittas merged commit 8a44cc2 into main Jul 28, 2023
@georgesittas georgesittas deleted the jo/optimize_ddls branch July 28, 2023 19:46
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.

2 participants