Skip to content

fix: Allow a correlated scalar subquery to group without aggregating - #24783

Draft
Dandandan wants to merge 1 commit into
apache:mainfrom
Dandandan:feat/scalar-subquery-grouped-key
Draft

fix: Allow a correlated scalar subquery to group without aggregating#24783
Dandandan wants to merge 1 commit into
apache:mainfrom
Dandandan:feat/scalar-subquery-grouped-key

Conversation

@Dandandan

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

A correlated scalar subquery must return at most one row per set of outer
values. check_aggregation_in_scalar_subquery required an aggregate expression
to establish that, so this query fails to plan:

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 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. The GROUP BY check below it is
unchanged, so a GROUP BY on a non-correlated column is still rejected.

These subqueries decorrelate to a plain LEFT JOIN like any other, so no
changes are needed outside the check:

Projection: fact.id, __scalar_sq_1.k AS g
--Left Join: fact.k = __scalar_sq_1.k
----TableScan: fact projection=[id, k]
----SubqueryAlias: __scalar_sq_1
------Aggregate: groupBy=[[dim.k]], aggr=[[]]
--------TableScan: dim projection=[k]

Are these changes tested?

Yes. An existing case in subquery.slt asserted the old error; it now checks
the results and the plan. The negative case for a GROUP BY on a
non-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.

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
@github-actions github-actions Bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Aug 30, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.52%. Comparing base (61bf6b9) to head (778dd5e).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants