fix: Allow a correlated scalar subquery to group without aggregating - #24783
Draft
Dandandan wants to merge 1 commit into
Draft
fix: Allow a correlated scalar subquery to group without aggregating#24783Dandandan wants to merge 1 commit into
Dandandan wants to merge 1 commit into
Conversation
A correlated scalar subquery must return at most one row per set of outer values. `check_aggregation_in_scalar_subquery` required an aggregate expression for that, and rejected select o.id, (select k from dim where dim.k = o.k group by k) from o with "Correlated scalar subquery must be aggregated to return at most one row". The aggregate expression is not what makes the subquery scalar. The grouping is. An empty GROUP BY returns one row, and a GROUP BY on only correlated columns returns at most one group per set of outer values. The function already checks the latter, so the aggregate check is redundant and only rejects valid queries. These subqueries decorrelate to a plain LEFT JOIN like any other, so no changes are needed outside the check. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SgqwvctZdvR1ZCz2hbkEJC
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24783 +/- ##
==========================================
- Coverage 81.52% 81.52% -0.01%
==========================================
Files 1123 1123
Lines 405970 406038 +68
Branches 405970 406038 +68
==========================================
+ Hits 330978 331026 +48
- Misses 55627 55645 +18
- Partials 19365 19367 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
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.
Which issue does this PR close?
Rationale for this change
A correlated scalar subquery must return at most one row per set of outer
values.
check_aggregation_in_scalar_subqueryrequired an aggregate expressionto establish that, so this query fails to plan:
with
Correlated scalar subquery must be aggregated to return at most one row.The aggregate expression is not what makes the subquery scalar. The grouping
is. An empty
GROUP BYreturns one row, and aGROUP BYon only correlatedcolumns returns at most one group per set of outer values. The function already
checks the second condition, so the aggregate check is redundant and rejects
valid queries.
What changes are included in this PR?
Removes the
aggr_expr.is_empty()check. TheGROUP BYcheck below it isunchanged, so a
GROUP BYon a non-correlated column is still rejected.These subqueries decorrelate to a plain
LEFT JOINlike any other, so nochanges are needed outside the check:
Are these changes tested?
Yes. An existing case in
subquery.sltasserted the old error; it now checksthe results and the plan. The negative case for a
GROUP BYon anon-correlated column is unchanged and still passes.
Are there any user-facing changes?
Correlated scalar subqueries that group by only correlated columns and have no
aggregate expression now plan instead of returning an error. No existing plan
changes.