Skip to content

feat: add single flag to hash and nested loop joins for scalar subqueries - #27228

Draft
shehabgamin wants to merge 2 commits into
apache:mainfrom
lakehq:single-join
Draft

shehabgamin wants to merge 2 commits into
apache:mainfrom
lakehq:single-join

Conversation

@shehabgamin

@shehabgamin shehabgamin commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

In SELECT o.id, (SELECT i.price FROM items i WHERE i.order_id = o.id) FROM orders o SQL requires at most one item per order and an error otherwise. DataFusion rejects the query at planning time, and a plain join would duplicate the order row.

What changes are included in this PR?

  • A single flag on the logical Join, HashJoinExec and NestedLoopJoinExec. The join fails with Scalar subquery returned more than one row when an outer row has two matches after all join conditions. A row is emitted only after all its matches are seen.
  • HashJoinExec supports Left and Right single joins in CollectLeft and Partitioned mode. NestedLoopJoinExec supports Right single joins, for correlations without an equality.
  • The planner and optimizer rules keep the flag and never move a single join to an implementation or a shape that loses the check.
  • ScalarSubqueryToJoin plans a non-aggregated correlated scalar subquery as a single join.
  • Proto support. Substrait and the SQL unparser reject single joins.

What is the testing strategy for this PR?

Unit tests for both hash join orientations and modes, the nested loop join, the optimizer rules, JoinSelection and proto round trips, plus single_join.slt.

Are there any user-facing changes?

Non-aggregated correlated scalar subqueries now run, and fail only when a row really has two matches. With enable_physical_uncorrelated_scalar_subquery = false, a multi-row uncorrelated subquery now fails instead of duplicating rows. Code that builds Join with a struct literal must set single.

@github-actions github-actions Bot added sql SQL Planner logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate proto Related to proto crate physical-plan Changes to the physical-plan crate labels Nov 8, 2026
@shehabgamin

Copy link
Copy Markdown
Contributor Author

"Author has reached the open pull request limit", so this stays in draft for now. That's okay because #27227 has to merge first anyway.

@alamb

alamb commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

ScalarSubqueryToJoin plans a non-aggregated correlated scalar subquery as a single join.

I think this is a bad idea, see

@shehabgamin

Copy link
Copy Markdown
Contributor Author

@alamb See here #27229 (comment)

The only piece that touches subquery support is a small change in #27228 that lets ScalarSubqueryToJoin use the flag, and it exists only so the single join can be covered by an sqllogictest integration test. A general pass would replace that rule anyway, so if you'd rather not add any new shapes to it, I'm happy to drop that part.

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

Labels

core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules physical-plan Changes to the physical-plan crate proto Related to proto crate sql SQL Planner sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better join support for correlated subqueries: single join, null-aware RightMark, partitioned null-aware joins

2 participants