Skip to content

[SPARK-58485] [SQL] Broadcast hash joins with scalar subquery as join key - #57811

Open
VindhyaG wants to merge 1 commit into
apache:masterfrom
VindhyaG:SPARK-58485
Open

[SPARK-58485] [SQL] Broadcast hash joins with scalar subquery as join key#57811
VindhyaG wants to merge 1 commit into
apache:masterfrom
VindhyaG:SPARK-58485

Conversation

@VindhyaG

@VindhyaG VindhyaG commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Broadcast hash join fails with "Subquery has not finished" when the join key contains an uncorrelated scalar subquery.
Rootcause provided bu the the reporter :
ExtractEquiJoinKeys accepts a join key containing an uncorrelated scalar subquery (it has no references, so canEvaluate passes for either side), and the key lands in HashedRelationBroadcastMode(key: Seq[Expression]).
Scalar subqueries are normally started and awaited by SparkPlan.prepareSubqueries and waitForSubqueries, which discover them through QueryPlan.expressions. That walk only surfaces Expression, Option and Iterable constructor arguments (src).
BroadcastMode is an opaque case class, so the subquery inside mode.key is never registered or awaited on the exchange node.
The broadcast thread then evaluates/code-generates the un-updated execution.ScalarSubquery and hits the require(updated) guard (src). The join node's own waitForSubqueries does see its leftKeys/rightKeys, but has no happens-before relationship with the broadcast thread, which doPrepare launches eagerly.

What changes were proposed in this pull request?

Register the HashedRelationBroadcastMode key and wait for the subqueries to be finished before triggering transform.
Since, registering the key to the expressions was the most simple with least changes in the architecture went with that solution out of all the solutions suggested.

Why are the changes needed?

Does this PR introduce any user-facing change?

No

How was this patch tested?

Tested the changes locally with the code provided in the bug and UT.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: GPT 5.5

@VindhyaG
VindhyaG marked this pull request as draft August 6, 2026 04:28
@VindhyaG
VindhyaG force-pushed the SPARK-58485 branch 4 times, most recently from 8f2b199 to 6d393ea Compare August 6, 2026 09:42
@VindhyaG
VindhyaG marked this pull request as ready for review August 6, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant