You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "cloud-fan (via GitHub)" <gi...@apache.org> on 2026/08/29 04:57:30 UTC
[PR] [SPARK-36082][SQL][FOLLOWUP] Keep optimized NAAJ when fallback also broadcasts [spark]
cloud-fan opened a new pull request, #58404:
URL: https://github.com/apache/spark/pull/58404
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'common/utils/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
This follow-up restores the optimized single-column null-aware anti join (NAAJ) path when the
right side exceeds `spark.sql.autoBroadcastJoinThreshold`.
It removes the size check added to `JoinSelection` by #55678 and makes
`getBroadcastHashJoinBuildSide` consistently report `BuildRight` for a recognized single-column
NAAJ. The broadcast-mode compatibility changes from #55678 remain unchanged.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
When the size check rejects the null-aware broadcast hash join, Spark falls through to
`BroadcastNestedLoopJoinExec`. For a left anti join, that fallback still broadcasts the right side;
it changes only the broadcast representation and join algorithm:
```
BroadcastHashJoin: hash lookup, approximately O(M + N)
BroadcastNestedLoopJoin: nested lookup, up to O(M * N)
```
Consequently, the automatic broadcast threshold does not select a non-broadcast alternative. It
can instead cause a severe performance regression while retaining the same fundamental broadcast
memory requirement. Until Spark has a shuffle-capable NAAJ implementation, the optimized hash join
is the better fallback when NAAJ optimization is enabled.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
Yes. Relative to #55678 on unreleased branches, an optimized single-column NAAJ whose right side
exceeds the automatic broadcast threshold uses `BroadcastHashJoinExec` instead of
`BroadcastNestedLoopJoinExec`. This restores the behavior from released Spark versions.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
Updated `JoinSelectionHelperSuite` and `JoinSuite` to verify that a recognized single-column NAAJ
uses the null-aware broadcast hash join even when its right side exceeds the automatic broadcast
threshold.
The following focused tests were attempted twice, but the local environment could not resolve
Maven Central while loading the SBT project, so no test code was executed:
```
build/sbt \
'catalyst/testOnly org.apache.spark.sql.catalyst.optimizer.JoinSelectionHelperSuite -- \
-z "canPlanAsBroadcastHashJoin should detect single-column null-aware anti join"' \
'sql/testOnly org.apache.spark.sql.JoinSuite -- -z "SPARK-36082"'
```
The project load failed with:
```
repo.maven.apache.org: Name or service not known
```
`git diff --check` passed.
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
Generated-by: OpenAI Codex (GPT-5)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #58404: [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior
URL: https://github.com/apache/spark/pull/58404
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58404:
URL: https://github.com/apache/spark/pull/58404#issuecomment-5536643371
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/f8b673c5a5302c97d3b4fc129b494e891abd6a2f
- merged into branch-4.x https://github.com/apache/spark/commit/d49def3d735477e8471611115c5e49985fc8cc03
- merged into branch-4.3 https://github.com/apache/spark/commit/cf496786ed34fc0e27d3cfc6db962af59a890739
*Posted by `merge_spark_pr.py`*
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58404:
URL: https://github.com/apache/spark/pull/58404#discussion_r3897664546
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushDownLeftSemiAntiJoin.scala:
##########
@@ -75,7 +74,11 @@ object PushDownLeftSemiAntiJoin extends Rule[LogicalPlan]
val makeJoinCondition = (predicates: Seq[Expression]) => {
replaceAlias(predicates.reduce(And), aliasMap)
}
- pushDownJoin(join, canPushDownPredicate, makeJoinCondition)
+ pushDownJoin(
+ join,
+ canPushDownPredicate,
+ makeJoinCondition,
+ canPlanAsBroadcastHashJoin(_, conf))
Review Comment:
Addressed in the current revision. Aggregate pushdown now keeps the original `canPlanAsBroadcastHashJoin(join, conf)` guard and also validates `pushedJoin`. The new CBO stats test covers the case where the original join has a `BuildLeft` fallback that must block pushdown.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -289,6 +291,20 @@ case object BuildLeft extends BuildSide
trait JoinSelectionHelper extends Logging {
+ // Keep this synchronized with BroadcastExchangeExec's non-Long hashed relation limit.
+ private val maxBroadcastHashRows = (BytesToBytesMap.MAX_CAPACITY / 1.5).toLong
+
+ private def canBuildNullAwareAntiJoinHashRelation(
+ rightKeys: Seq[Expression],
+ right: LogicalPlan,
+ conf: SQLConf): Boolean = {
+ val usesLongHashedRelation =
+ rightKeys.length == 1 && rightKeys.head.dataType.isInstanceOf[IntegralType]
+ usesLongHashedRelation || right.stats.rowCount.fold(canBroadcastBySize(right, conf)) {
+ _ < maxBroadcastHashRows
+ }
Review Comment:
Addressed in the current revision. The row-count-based expansion has been removed: the specialized hash path now requires the right side to satisfy `canBroadcastBySize`, while threshold-zero and over-threshold cases retain the identity-broadcast nested-loop fallback.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Keep optimized NAAJ when fallback also broadcasts [spark]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #58404:
URL: https://github.com/apache/spark/pull/58404#discussion_r3887065977
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -341,7 +341,7 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
}
case j @ ExtractSingleColumnNullAwareAntiJoin(leftKeys, rightKeys)
- if canBroadcastBySize(j.right, conf) =>
+ if canPlanAsBroadcastHashJoin(j, conf) =>
Review Comment:
[P1] Normalize floating keys before enabling the expanded hash path
With nullable DOUBLE keys, a known RHS row count, `spark.sql.autoBroadcastJoinThreshold=0`, and AQE disabled, this changes a previously correct nested-loop join into a null-aware hash join. For left values `[-0.0, 2.0, NULL]` and right values `[+0.0, 1.0]`, `NOT IN` should return only `2.0`, but the hash lookup also returns `-0.0`.
`NormalizeFloatingNumbers` only handles `ExtractEquiJoinKeys`, so it skips the NAAJ `Or(EqualTo, IsNull(EqualTo))` shape. `HashJoin.normalizeJoinKeys` only injects collation keys, leaving these floating keys unchanged, and `UnsafeHashedRelation` compares their raw bytes. The missing normalization predates this PR, but the formerly correct threshold-disabled execution changes here. Please normalize these keys before expanding hash eligibility, or retain the nested-loop path for affected types.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -289,6 +291,20 @@ case object BuildLeft extends BuildSide
trait JoinSelectionHelper extends Logging {
+ // Keep this synchronized with BroadcastExchangeExec's non-Long hashed relation limit.
+ private val maxBroadcastHashRows = (BytesToBytesMap.MAX_CAPACITY / 1.5).toLong
+
+ private def canBuildNullAwareAntiJoinHashRelation(
+ rightKeys: Seq[Expression],
+ right: LogicalPlan,
+ conf: SQLConf): Boolean = {
+ val usesLongHashedRelation =
+ rightKeys.length == 1 && rightKeys.head.dataType.isInstanceOf[IntegralType]
+ usesLongHashedRelation || right.stats.rowCount.fold(canBroadcastBySize(right, conf)) {
+ _ < maxBroadcastHashRows
+ }
Review Comment:
[P2] Account for hash storage exceeding the broadcast byte limit
Row capacity does not establish that the hashed representation fits the broadcast byte limit. With `spark.sql.autoBroadcastJoinThreshold=0` and `spark.sql.maxBroadcastTableSize=1MB`, even a tiny nonempty RHS with nullable integral keys and no actual null values now fails: `LongHashedRelation` allocates at least a 1 MiB page plus its index, whereas the previous identity broadcast contains only the raw rows and fits.
`BroadcastExchangeExec` checks `HashedRelation.estimatedSize` for hashing but sums raw `UnsafeRow` sizes for identity broadcasts, so selecting the same build side does not preserve byte-limit behavior. The same representation overhead matters for larger inputs under the default limit. Please account for this capacity difference or preserve a fallback when hash storage exceeds the byte limit.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushDownLeftSemiAntiJoin.scala:
##########
@@ -75,7 +74,11 @@ object PushDownLeftSemiAntiJoin extends Rule[LogicalPlan]
val makeJoinCondition = (predicates: Seq[Expression]) => {
replaceAlias(predicates.reduce(And), aliasMap)
}
- pushDownJoin(join, canPushDownPredicate, makeJoinCondition)
+ pushDownJoin(
+ join,
+ canPushDownPredicate,
+ makeJoinCondition,
+ canPlanAsBroadcastHashJoin(_, conf))
Review Comment:
[P2] Preserve the original aggregate join eligibility check
Checking only the rewritten join loses the original `BuildLeft` fallback when CBO estimates that an aggregate reduces a large input to a few groups. For an explicit `LEFT ANTI JOIN` above the aggregate with a nullable integral condition `g.k = r.k OR isnull(g.k = r.k)`, the aggregate can be below the broadcast threshold while both its child and the RHS exceed it. The original plan broadcasts the small aggregate; replacing it with its large child makes the rewritten join choose `BuildRight`, so this callback permits pushdown and forces the large RHS into a hash broadcast.
At 512,000,000 RHS rows, that broadcast necessarily fails the exchange row limit even though the original `BuildLeft` join can stream the RHS. Smaller inputs can exceed the byte limit or driver memory. Please retain the original eligibility guard as well as validating the rewritten join.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on PR #58404:
URL: https://github.com/apache/spark/pull/58404#issuecomment-5534689688
LGTM, thanks @cloud-fan !
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Keep optimized NAAJ when fallback also broadcasts [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58404:
URL: https://github.com/apache/spark/pull/58404#issuecomment-5460458301
cc @jbharadw-oai @sunchao
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58404:
URL: https://github.com/apache/spark/pull/58404#issuecomment-5536595154
thanks for review, merging to master/4.x/4.3/4.2!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58404:
URL: https://github.com/apache/spark/pull/58404#issuecomment-5521507528
Hi @sunchao , thanks for your review, do you have more concerns?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-36082][SQL][FOLLOWUP] Preserve NAAJ broadcast fallback behavior [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58404:
URL: https://github.com/apache/spark/pull/58404#discussion_r3897659814
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -341,7 +341,7 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
}
case j @ ExtractSingleColumnNullAwareAntiJoin(leftKeys, rightKeys)
- if canBroadcastBySize(j.right, conf) =>
+ if canPlanAsBroadcastHashJoin(j, conf) =>
Review Comment:
Addressed in the current revision. `canPlanAsBroadcastHashJoin` now requires `canBroadcastBySize` on the right side, so a nonempty RHS with threshold zero stays on `BroadcastNestedLoopJoinExec`. I also added signed-zero result coverage for `-0.0` versus `0.0`.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org