xiliang678 opened a new pull request, #60376:
URL: https://github.com/apache/spark/pull/60376

   ### What changes were proposed in this pull request?
   
   A LeftSingle join without equi-join keys fails at execution when the planner 
builds its left side. This PR stops the planner from choosing that side.
   
   `JoinSelectionHelper.getNonEquiJoinBuildSide`, which `JoinSelection` calls 
with and without AQE, chooses the side that a join without equi-join keys 
builds in three steps: the hint pass, then the size pass, then a last resort 
when no side qualifies in either pass. (An inner join may get a Cartesian 
product instead of a build side.) A side qualifies in the hint pass if it has a 
broadcast hint, and in the size pass if its estimated size is at most the 
broadcast threshold (for runtime statistics under AQE, the adaptive one) and it 
is not hinted `NO_BROADCAST_AND_REPLICATION`. A pass in which only one side 
qualifies builds that side. A pass in which both sides qualify builds the 
smaller side for an inner or full outer join, the left side for a right outer 
join, and the right side for every other join type, LeftSingle included. The 
last resort builds a side whatever its size, and for a LeftSingle join it 
builds the right side.
   
   With this PR, the left side never qualifies in either pass when the join 
type is LeftSingle: the left side's qualification test in the shared helper of 
both passes now starts with `joinType == LeftSingle`, which makes it false. A 
LeftSingle join then builds its right side: in the first pass in which the 
right side qualifies, and otherwise in the last resort. Joins of every other 
type keep their build side. The scaladoc of `getNonEquiJoinBuildSide` now says 
that a LeftSingle join always builds its right side.
   
   ### Why are the changes needed?
   
   With `spark.sql.optimizer.scalarSubqueryUseSingleJoin` on (the default since 
4.0.0), a correlated scalar subquery that the optimizer can't prove returns at 
most one row becomes a LeftSingle join. Its left side is the outer query and 
its right side is the subquery. When that join has no equi-join keys, for 
example because the correlation is a range predicate such as `id < l.a` below, 
it runs as a `BroadcastNestedLoopJoinExec`, the only operator for such a join. 
`BroadcastNestedLoopJoinExec` throws if it is asked to build the left side of a 
LeftSingle join, yet the left side is built exactly when only the left side 
qualifies in the first pass in which any side qualifies. The query then fails 
at execution. On master, with AQE on and off:
   
   ```sql
   SET spark.sql.autoBroadcastJoinThreshold=1000;
   SET spark.sql.adaptive.autoBroadcastJoinThreshold=1000;
   SELECT a, (SELECT id FROM range(0, 1000) WHERE id < l.a) AS s FROM VALUES 
(0), (1) AS l(a);
   -- expected: (0, NULL), (1, 0)
   -- actual: java.lang.IllegalArgumentException: BroadcastNestedLoopJoin 
should not use the left side as build when executing a LeftSingle join
   ```
   
   In this query, the size pass builds the left side: the outer side is 
estimated at 24 bytes, under the 1000-byte thresholds, and `range(0, 1000)` at 
8000 bytes, over them. The small thresholds only keep the example's data small. 
With the default thresholds (10MB) and no hints, the same happens when the 
outer side is estimated under 10MB and the subquery side over it. The hint pass 
also builds the left side when only the left side has a broadcast hint.
   
   `BroadcastNestedLoopJoinExec` runs every other join type with either build 
side, so only LeftSingle joins fail. The right side is the only build side it 
supports for a LeftSingle join, which must emit NULL for an outer row with no 
match and raise `SCALAR_SUBQUERY_TOO_MANY_ROWS` for an outer row with more than 
one. Its left-build path for outer joins tracks only whether each left row 
matched, so it can't tell one match from several. Choosing the right side in 
the planner is the smaller change.
   
   SPARK-49653 (#48145) added the LeftSingle join and the check in 
`BroadcastNestedLoopJoinExec` that throws, and made the last resort build the 
right side of a LeftSingle join, but did not change the hint and size passes. 
On master, SPARK-60941 (#60144) then moved the build-side choice for joins 
without equi-join keys, unchanged, from `JoinSelection` into 
`getNonEquiJoinBuildSide`.
   
   The released 4.0.0 and 4.3.0 fail the same way: the query above, run with 
pyspark 4.0.0 and 4.3.0 from PyPI with AQE on and off, throws the same 
`IllegalArgumentException`. In those releases the choice is still inline in 
`JoinSelection`. The 4.1.0 and 4.2.0 sources have the same hint and size 
passes, the same check in `BroadcastNestedLoopJoinExec` and the same conf 
default; the query was not run on them. A backport to the 4.x release branches 
would add the same check to the inline passes in `JoinSelection`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a bug fix. The result of `getNonEquiJoinBuildSide` changes only where 
it was `BuildLeft` for a LeftSingle join, and such a join fails whenever it 
runs with that build side. With AQE, re-planning can replace an initial plan 
that builds the left side before the join runs, so such a query could run 
before if re-planning chose the right side. Re-planning calls the same 
function, which now never returns `BuildLeft` for a LeftSingle join, so such a 
query still ends with its join building the right side.
   
   A LeftSingle join that built its left side now builds its right (subquery) 
side, and the query runs: it returns the correct rows, or fails with 
`SCALAR_SUBQUERY_TOO_MANY_ROWS` when more than one subquery row matches an 
outer row. The subquery side is broadcast whatever its size, as it already is 
in the last resort, so a very large subquery side can now fail at broadcast 
time instead: on the broadcast size or row limits, on 
`spark.sql.broadcastTimeout`, or on driver memory. Such queries failed before 
too, with the `IllegalArgumentException`.
   
   A broadcast hint on the left (outer) side of such a join is now not applied, 
and no warning is logged; before, it made the query fail. The right side is 
built even when it is hinted `NO_BROADCAST_AND_REPLICATION`, as the last resort 
already does; Spark itself sets that internal hint only on the target side of a 
rewritten MERGE. Joins of other types keep their build side.
   
   ### How was this patch tested?
   
   New tests that fail without the fix:
   - `JoinSelectionHelperSuite`: six joins without equi-join keys in which only 
the left side qualifies in the first pass in which any side qualifies. In four, 
the size pass decides: the left side is small by its static size; small only by 
its runtime size (over `spark.sql.autoBroadcastJoinThreshold`, under 
`spark.sql.adaptive.autoBroadcastJoinThreshold`); or small, with a 
`SHUFFLE_HASH` or `NO_BROADCAST_AND_REPLICATION` hint on the large right side, 
which sends the join through the hint pass first. In two, the hint pass 
decides: the left side is hinted to broadcast, with neither side small and with 
only the right side small. In each, a LeftSingle join builds its right side, 
and inner, left outer, right outer, full outer, left semi, left anti and 
existence joins keep building their left side. Without the fix, only the six 
LeftSingle checks fail, with `BuildLeft`.
   - `SingleJoinSuite` (an existing suite), with AQE on and off: the query 
above plans a LeftSingle `BroadcastNestedLoopJoinExec` that builds its right 
side, before and after the query runs, and returns (0, NULL), (1, 0). A variant 
in which two subquery rows match one outer row fails with 
`SCALAR_SUBQUERY_TOO_MANY_ROWS`. Without the fix, the plan builds the left 
side, and the variant throws the `IllegalArgumentException`.
   
   A new test in `JoinSelectionHelperSuite` that passes with and without the 
fix: in seven more joins without equi-join keys, a LeftSingle join builds its 
right side. They are: only the right side small; both sides small; neither side 
small; the right side hinted to broadcast; both sides hinted; neither side 
small with a `SHUFFLE_REPLICATE_NL` hint on the left side, which doesn't apply 
to a LeftSingle join; and the left side small but hinted 
`NO_BROADCAST_AND_REPLICATION`.
   
   The query above, with its `SET` statements, was also run on master through a 
temporary test, with AQE on and off: without the fix it throws the 
`IllegalArgumentException`, and with the fix it returns (0, NULL), (1, 0).
   
   Also ran `JoinHintSuite`, `JoinSuite`, `SubquerySuite` and the 
`subquery/scalar-subquery` files of `SQLQueryTestSuite`, which pass. The Python 
UDF tests of `JoinSuite` and `SubquerySuite` were canceled locally for lack of 
Python.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to