feat: Single join for correlated scalar subqueries - #24784
Open
Dandandan wants to merge 2 commits into
Open
Conversation
A correlated scalar subquery must return at most one row per set of outer values. The analyzer enforced this by requiring an aggregate on top of the subquery, so a plain attribute lookup did not plan: select o_orderkey, (select c_name from customer where c_custkey = o_custkey) from orders failed with "Correlated scalar subquery must be aggregated to return at most one row". To run it you had to wrap the column in min, max or any_value. That aggregate does no useful work, it only proves a row count, and it costs a hash aggregation over the subquery side. This adds the single join from Neumann and Kemper's unnesting paper as JoinType::LeftSingle and JoinType::RightSingle. It behaves like a left or right join, but returns "Scalar subquery returned more than one row" when a second row matches, which is the error ScalarSubqueryExec already returns for uncorrelated scalar subqueries. ScalarSubqueryToJoin uses it for correlated scalar subqueries that are not already known to return at most one row, so those queries decorrelate into a join with no aggregate. Subqueries that are known to return at most one row keep their plain LEFT JOIN. This matters for more than the run-time check: the optimizer can make a LEFT JOIN inner when a predicate above it rejects nulls, fold a comparison into a second join key, and turn the result into a semi join, none of which are correct for a single join. Besides the aggregate the rule can see at the top of the subquery, it now also asks the decorrelated subquery's functional dependencies whether it is unique on the join keys, which covers a declared constraint or an aggregate further down. HashJoinExec implements both directions, so JoinSelection can still swap the inputs to choose a build side. NestedLoopJoinExec implements both for correlations with no equijoin key. Both reuse the matched bitmaps to detect duplicates, so the check costs one bit test per matched row and covers matches spread over batches and partitions. SortMergeJoinExec and PiecewiseMergeJoin reject single joins, and the physical planner routes them elsewhere. No query in TPC-H or TPC-DS produces a single join, so no plan in either suite changes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SgqwvctZdvR1ZCz2hbkEJC
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #24784 +/- ##
========================================
Coverage 81.52% 81.53%
========================================
Files 1123 1123
Lines 405970 406261 +291
Branches 405970 406261 +291
========================================
+ Hits 330978 331227 +249
- Misses 55627 55667 +40
- Partials 19365 19367 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thank you for opening this pull request! Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch). Details |
Dandandan
marked this pull request as ready for review
August 30, 2026 15:14
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?
Closes: ##16425
Rationale for this change
A correlated scalar subquery must return at most one row per set of outer
values. DataFusion enforced this in the analyzer by requiring an aggregate on
top of the subquery. Without one, planning failed with
So a plain attribute lookup did not plan at all:
To run it you had to wrap the column in
min,maxorany_value.What changes are included in this PR?
Implement single join
Benchmarks
TPC-H SF10, comparing the form you had to write before against the form that
now plans. Median of 7 runs, interleaved and repeated.
Are these changes tested?
Are there any user-facing changes?
JoinTypegains two variants, so exhaustive matches on it in downstream codeneed a new arm. This is an API change.