-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-51109][SQL] CTE in subquery expression as grouping column #49829
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
Closed
Conversation
This file contains hidden or 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
Contributor
Author
peter-toth
approved these changes
Feb 7, 2025
Contributor
peter-toth
left a comment
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.
LGTM, pending tests.
Contributor
Author
|
thanks for the review! merging to master/4.0! |
cloud-fan
added a commit
that referenced
this pull request
Feb 10, 2025
### What changes were proposed in this pull request? This is a long-standing problem. With the GROUP BY ordinal feature, it's quite easy for users to write a complicated expression as the GROUP BY expression and also put it in the SELECT list. It's usually OK as the complicated expressions in the GROUP BY expression and SELECT list remain the same, but problems may occur with subquery expressions, duplicated relations, and CTE inline. Let's look at this example: ``` CREATE VIEW v AS WITH r AS (SELECT c1 + c2 AS c FROM t) SELECT * FROM r; SELECT (SELECT max(c) FROM v WHERE c > id) FROM range(1) GROUP BY 1; ``` A scalar subquery appears in both the GROUP BY expression and SELECT list. The scalar subquery scans table `t`, and because this scalar subquery appears twice, `DeduplicateRelations` will trigger. This makes the output attributes of CTE def and ref out of sync in the second scalar subquery and `InlineCTE` will add a cosmetic `Project` to adjust the output attr ids. `CheckAnalysis` will inline CTE in the beginning, and this extra cosmetic `Project` in the second scalar subquery makes Spark think it's not semantically equal to the first scalar subquery and fails the query. The proposal here is to remove cosmetic Projects during plan canonicalization. ### Why are the changes needed? bug fix ### Does this PR introduce _any_ user-facing change? Yes, some queries fail before and work now. ### How was this patch tested? new test ### Was this patch authored or co-authored using generative AI tooling? no Closes #49829 from cloud-fan/cte. Authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit c7edcae) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
zifeif2
pushed a commit
to zifeif2/spark
that referenced
this pull request
Nov 14, 2025
### What changes were proposed in this pull request? This is a long-standing problem. With the GROUP BY ordinal feature, it's quite easy for users to write a complicated expression as the GROUP BY expression and also put it in the SELECT list. It's usually OK as the complicated expressions in the GROUP BY expression and SELECT list remain the same, but problems may occur with subquery expressions, duplicated relations, and CTE inline. Let's look at this example: ``` CREATE VIEW v AS WITH r AS (SELECT c1 + c2 AS c FROM t) SELECT * FROM r; SELECT (SELECT max(c) FROM v WHERE c > id) FROM range(1) GROUP BY 1; ``` A scalar subquery appears in both the GROUP BY expression and SELECT list. The scalar subquery scans table `t`, and because this scalar subquery appears twice, `DeduplicateRelations` will trigger. This makes the output attributes of CTE def and ref out of sync in the second scalar subquery and `InlineCTE` will add a cosmetic `Project` to adjust the output attr ids. `CheckAnalysis` will inline CTE in the beginning, and this extra cosmetic `Project` in the second scalar subquery makes Spark think it's not semantically equal to the first scalar subquery and fails the query. The proposal here is to remove cosmetic Projects during plan canonicalization. ### Why are the changes needed? bug fix ### Does this PR introduce _any_ user-facing change? Yes, some queries fail before and work now. ### How was this patch tested? new test ### Was this patch authored or co-authored using generative AI tooling? no Closes apache#49829 from cloud-fan/cte. Authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit b3be934) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes were proposed in this pull request?
This is a long-standing problem. With the GROUP BY ordinal feature, it's quite easy for users to write a complicated expression as the GROUP BY expression and also put it in the SELECT list. It's usually OK as the complicated expressions in the GROUP BY expression and SELECT list remain the same, but problems may occur with subquery expressions, duplicated relations, and CTE inline. Let's look at this example:
A scalar subquery appears in both the GROUP BY expression and SELECT list. The scalar subquery scans table
t, and because this scalar subquery appears twice,DeduplicateRelationswill trigger. This makes the output attributes of CTE def and ref out of sync in the second scalar subquery andInlineCTEwill add a cosmeticProjectto adjust the output attr ids.CheckAnalysiswill inline CTE in the beginning, and this extra cosmeticProjectin the second scalar subquery makes Spark think it's not semantically equal to the first scalar subquery and fails the query.The proposal here is to remove cosmetic Projects during plan canonicalization.
Why are the changes needed?
bug fix
Does this PR introduce any user-facing change?
Yes, some queries fail before and work now.
How was this patch tested?
new test
Was this patch authored or co-authored using generative AI tooling?
no