feat: Add statistics-based guards to SortMergeJoin-to-HashJoin rewrite [experimental]#3554
Draft
andygrove wants to merge 2 commits intoapache:mainfrom
Draft
feat: Add statistics-based guards to SortMergeJoin-to-HashJoin rewrite [experimental]#3554andygrove wants to merge 2 commits intoapache:mainfrom
andygrove wants to merge 2 commits intoapache:mainfrom
Conversation
Previously, enabling `spark.comet.exec.replaceSortMergeJoin` would unconditionally rewrite all SortMergeJoins to ShuffledHashJoins, which could cause OOM when the build side is too large to fit in memory. This adds two checks mirroring Spark's own JoinSelection logic: 1. Per-partition size check: the build side must fit in memory using Spark's `autoBroadcastJoinThreshold * numShufflePartitions` formula (matching `canBuildLocalHashMapBySize()`). 2. Size ratio check: the build side must be significantly smaller than the probe side, controlled by a new config `spark.comet.exec.replaceSortMergeJoin.sizeRatio` (default: 3), matching Spark's `SHUFFLE_HASH_JOIN_FACTOR`. When either check fails, the SortMergeJoin is kept and the reason is logged via `withInfo` (visible with `explainFallback.enabled=true`). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
Author
|
@sqlbenchmark run tpch |
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.
Summary
RewriteJoin, mirroring Spark's ownJoinSelectionlogic (canBuildLocalHashMapBySize()andmuchSmaller())spark.comet.exec.replaceSortMergeJoinwould unconditionally rewrite all SortMergeJoins to ShuffledHashJoins, risking OOM when the build side is too largespark.comet.exec.replaceSortMergeJoin.sizeRatio(default: 3) matching Spark'sSHUFFLE_HASH_JOIN_FACTORexplainFallback.enabled=true)Details
Per-partition size check:
buildSize < autoBroadcastJoinThreshold * numShufflePartitionsautoBroadcastJoinThreshold = -1(broadcast disabled), this check is skippedSize ratio check:
buildSize * sizeRatio <= probeSizespark.sql.shuffledHashJoinFactorSafe fallback: When no logical plan statistics are available, the rewrite is skipped conservatively.
Test plan
CometJoinSuitetests still passexplainFallback.enabled=trueto verify skip reasons are loggedautoBroadcastJoinThreshold=-1still allows rewrite when size ratio is met🤖 Generated with Claude Code