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

Feat(optimizer): add support for resolving CTEs in CREATE statements #1949

Merged
merged 4 commits into from
Jul 28, 2023

Conversation

gtoonstra
Copy link
Contributor

@gtoonstra gtoonstra commented Jul 22, 2023

Pull request to resolve CTEs when CREATE statement is at the root of the AST.

I've dived a lot more into the scopes and resolvers and came up with this implementation so far.

Looking forward to feedback.

sqlglot/expressions.py Outdated Show resolved Hide resolved
@@ -1006,6 +1006,31 @@ class Create(Expression):
"clone": False,
}

@property
def ctes(self):
Copy link
Owner

Choose a reason for hiding this comment

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

should we move these helper methods to a shared base class and have this applied to Insert and Update or other DDLs?

then in the optimizer you can check for the base class instead of just Create

Copy link
Owner

Choose a reason for hiding this comment

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

can we call it DDL

tests/test_optimizer.py Outdated Show resolved Hide resolved
@tobymao
Copy link
Owner

tobymao commented Jul 22, 2023

thanks so much for the PR!

@tobymao
Copy link
Owner

tobymao commented Jul 24, 2023

@gtoonstra are you planning to finish up this pr?

@georgesittas georgesittas changed the title Add support for resolving CTEs in CREATE statements Feat(optimizer): add support for resolving CTEs in CREATE statements Jul 25, 2023
@gtoonstra gtoonstra force-pushed the fix_create_insert_not_qualifying branch from 4ef83a1 to c24acaf Compare July 25, 2023 13:55
@gtoonstra
Copy link
Contributor Author

gtoonstra commented Jul 25, 2023

@tobymao : I have made the necessary changes and CI is processing now. After this PR, I will look into INSERT/UPDATE as well.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

LGTM, left one question.

I agree with Toby about renaming TableChange to DDL. I believe INSERT and UPDATE should just work with a few changes (i.e. do instance checks on the common superclass instead of just CREATE).

Btw, if we're to create a new test file for qualify columns, I'd rather use a bit more generic name like qualify_columns_ddl.sql or something to anticipate for the INSERT, UPDATE tests as well.

Nice 👍

Comment on lines +3 to +4
WITH cte AS (SELECT a FROM db.y) CREATE TABLE s AS SELECT * FROM cte;
WITH cte AS (SELECT a AS a FROM db.y AS y) CREATE TABLE s AS SELECT cte.a AS a FROM cte;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why doesn't cte have an alias in the final SELECT here? Seems a bit off. Ditto for subsequent tests.

@georgesittas georgesittas merged commit 9d67283 into tobymao:main Jul 28, 2023
@georgesittas
Copy link
Collaborator

georgesittas commented Jul 28, 2023

I'll complete this, but most of the work should be already done. Thanks for the PR @gtoonstra!

@gtoonstra gtoonstra deleted the fix_create_insert_not_qualifying branch July 28, 2023 06:30
georgesittas added a commit that referenced this pull request Jul 28, 2023
georgesittas added a commit that referenced this pull request Jul 28, 2023
@gtoonstra gtoonstra restored the fix_create_insert_not_qualifying branch July 30, 2023 11:17
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.

3 participants