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

Add option to pushdown CTE aliases earlier in qualify #2726

Merged
merged 7 commits into from Dec 28, 2023

Conversation

dangoldin
Copy link
Contributor

Based on Slack conversation with @barakalon we wanted to support the following valid Snowflake query:

WITH y (c) AS (
  SELECT 
    SUM(a)
  FROM (
    SELECT 1 a
  ) AS x
  HAVING c > 0
)
SELECT * FROM y

where the HAVING c > 0 is referencing the CTE alias c

Copy link
Collaborator

@barakalon barakalon left a comment

Choose a reason for hiding this comment

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

tests/test_optimizer.py Outdated Show resolved Hide resolved
sqlglot/optimizer/qualify_columns.py Show resolved Hide resolved
@dangoldin
Copy link
Contributor Author

dangoldin commented Dec 28, 2023

Nice PR!

Here's some examples of using the dialect attributes to customize optimizer behavior: https://github.com/tobymao/sqlglot/blob/main/sqlglot/optimizer/simplify.py#L1095 https://github.com/tobymao/sqlglot/blob/main/sqlglot/optimizer/qualify_columns.py#L43

@barakalon Just took a stab at it - let me know if this is what you intended here but I made it optional and default to what is set in the Dialect if not explicitly passed in.

Copy link
Collaborator

@barakalon barakalon left a comment

Choose a reason for hiding this comment

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

A couple more minor comments 👍

@dangoldin
Copy link
Contributor Author

Addressed latest round of feedback - let me know if the docs are good. I may have over done it a bit.

Copy link
Collaborator

@barakalon barakalon 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 yield to Toby.

@tobymao tobymao merged commit 4629410 into tobymao:main Dec 28, 2023
5 checks passed
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.

None yet

3 participants