-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-32286][SQL] Coalesce bucketed table for shuffled hash join if applicable #29079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
549c096
Coalesce bucketed table for shuffled hash join if applicable
c21 5cf56d7
Separate max bucket ratio for SMJ and SHJ and add OOM related documen…
c21 cbd2fa1
Set smaller default config value for SHJ
c21 d6c9d88
Address all comments beside the separate configs discussion
c21 2e9aff9
Change back to single ratio config for SMJ and SHJ, and rebase to lat…
c21 7b20049
Address comments in unit test and rebase
c21 b1a8a92
Change method name to hasScanOperation
c21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
177 changes: 177 additions & 0 deletions
177
sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/CoalesceBucketsInJoin.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.bucketing | ||
|
||
import scala.annotation.tailrec | ||
|
||
import org.apache.spark.sql.catalyst.catalog.BucketSpec | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight} | ||
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution.{FileSourceScanExec, FilterExec, ProjectExec, SparkPlan} | ||
import org.apache.spark.sql.execution.joins.{BaseJoinExec, ShuffledHashJoinExec, SortMergeJoinExec} | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
/** | ||
* This rule coalesces one side of the `SortMergeJoin` and `ShuffledHashJoin` | ||
* if the following conditions are met: | ||
* - Two bucketed tables are joined. | ||
* - Join keys match with output partition expressions on their respective sides. | ||
* - The larger bucket number is divisible by the smaller bucket number. | ||
* - COALESCE_BUCKETS_IN_JOIN_ENABLED is set to true. | ||
* - The ratio of the number of buckets is less than the value set in | ||
* COALESCE_BUCKETS_IN_JOIN_MAX_BUCKET_RATIO. | ||
*/ | ||
case class CoalesceBucketsInJoin(conf: SQLConf) extends Rule[SparkPlan] { | ||
private def updateNumCoalescedBucketsInScan( | ||
plan: SparkPlan, | ||
numCoalescedBuckets: Int): SparkPlan = { | ||
plan transformUp { | ||
case f: FileSourceScanExec => | ||
f.copy(optionalNumCoalescedBuckets = Some(numCoalescedBuckets)) | ||
} | ||
} | ||
|
||
private def updateNumCoalescedBuckets( | ||
join: BaseJoinExec, | ||
numLeftBuckets: Int, | ||
numRightBucket: Int, | ||
numCoalescedBuckets: Int): BaseJoinExec = { | ||
if (numCoalescedBuckets != numLeftBuckets) { | ||
val leftCoalescedChild = | ||
updateNumCoalescedBucketsInScan(join.left, numCoalescedBuckets) | ||
join match { | ||
case j: SortMergeJoinExec => j.copy(left = leftCoalescedChild) | ||
case j: ShuffledHashJoinExec => j.copy(left = leftCoalescedChild) | ||
} | ||
} else { | ||
val rightCoalescedChild = | ||
updateNumCoalescedBucketsInScan(join.right, numCoalescedBuckets) | ||
join match { | ||
case j: SortMergeJoinExec => j.copy(right = rightCoalescedChild) | ||
case j: ShuffledHashJoinExec => j.copy(right = rightCoalescedChild) | ||
} | ||
} | ||
} | ||
|
||
private def isCoalesceSHJStreamSide( | ||
join: ShuffledHashJoinExec, | ||
numLeftBuckets: Int, | ||
numRightBucket: Int, | ||
numCoalescedBuckets: Int): Boolean = { | ||
if (numCoalescedBuckets == numLeftBuckets) { | ||
join.buildSide != BuildRight | ||
} else { | ||
join.buildSide != BuildLeft | ||
} | ||
} | ||
|
||
def apply(plan: SparkPlan): SparkPlan = { | ||
if (!conf.coalesceBucketsInJoinEnabled) { | ||
return plan | ||
} | ||
|
||
plan transform { | ||
case ExtractJoinWithBuckets(join, numLeftBuckets, numRightBuckets) | ||
if math.max(numLeftBuckets, numRightBuckets) / math.min(numLeftBuckets, numRightBuckets) <= | ||
conf.coalesceBucketsInJoinMaxBucketRatio => | ||
val numCoalescedBuckets = math.min(numLeftBuckets, numRightBuckets) | ||
join match { | ||
case j: SortMergeJoinExec => | ||
updateNumCoalescedBuckets(j, numLeftBuckets, numRightBuckets, numCoalescedBuckets) | ||
case j: ShuffledHashJoinExec | ||
// Only coalesce the buckets for shuffled hash join stream side, | ||
// to avoid OOM for build side. | ||
if isCoalesceSHJStreamSide(j, numLeftBuckets, numRightBuckets, numCoalescedBuckets) => | ||
updateNumCoalescedBuckets(j, numLeftBuckets, numRightBuckets, numCoalescedBuckets) | ||
case other => other | ||
} | ||
case other => other | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* An extractor that extracts `SortMergeJoinExec` and `ShuffledHashJoin`, | ||
* where both sides of the join have the bucketed tables, | ||
* are consisted of only the scan operation, | ||
* and numbers of buckets are not equal but divisible. | ||
*/ | ||
object ExtractJoinWithBuckets { | ||
@tailrec | ||
private def hasScanOperation(plan: SparkPlan): Boolean = plan match { | ||
case f: FilterExec => hasScanOperation(f.child) | ||
case p: ProjectExec => hasScanOperation(p.child) | ||
case _: FileSourceScanExec => true | ||
case _ => false | ||
} | ||
|
||
private def getBucketSpec(plan: SparkPlan): Option[BucketSpec] = { | ||
plan.collectFirst { | ||
case f: FileSourceScanExec if f.relation.bucketSpec.nonEmpty && | ||
f.optionalNumCoalescedBuckets.isEmpty => | ||
f.relation.bucketSpec.get | ||
} | ||
} | ||
|
||
/** | ||
* The join keys should match with expressions for output partitioning. Note that | ||
* the ordering does not matter because it will be handled in `EnsureRequirements`. | ||
viirya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private def satisfiesOutputPartitioning( | ||
keys: Seq[Expression], | ||
partitioning: Partitioning): Boolean = { | ||
partitioning match { | ||
case HashPartitioning(exprs, _) if exprs.length == keys.length => | ||
exprs.forall(e => keys.exists(_.semanticEquals(e))) | ||
case _ => false | ||
} | ||
} | ||
|
||
private def isApplicable(j: BaseJoinExec): Boolean = { | ||
(j.isInstanceOf[SortMergeJoinExec] || | ||
j.isInstanceOf[ShuffledHashJoinExec]) && | ||
hasScanOperation(j.left) && | ||
hasScanOperation(j.right) && | ||
satisfiesOutputPartitioning(j.leftKeys, j.left.outputPartitioning) && | ||
satisfiesOutputPartitioning(j.rightKeys, j.right.outputPartitioning) | ||
} | ||
|
||
private def isDivisible(numBuckets1: Int, numBuckets2: Int): Boolean = { | ||
val (small, large) = (math.min(numBuckets1, numBuckets2), math.max(numBuckets1, numBuckets2)) | ||
// A bucket can be coalesced only if the bigger number of buckets is divisible by the smaller | ||
// number of buckets because bucket id is calculated by modding the total number of buckets. | ||
numBuckets1 != numBuckets2 && large % small == 0 | ||
} | ||
|
||
def unapply(plan: SparkPlan): Option[(BaseJoinExec, Int, Int)] = { | ||
plan match { | ||
case j: BaseJoinExec if isApplicable(j) => | ||
val leftBucket = getBucketSpec(j.left) | ||
val rightBucket = getBucketSpec(j.right) | ||
if (leftBucket.isDefined && rightBucket.isDefined && | ||
isDivisible(leftBucket.get.numBuckets, rightBucket.get.numBuckets)) { | ||
Some(j, leftBucket.get.numBuckets, rightBucket.get.numBuckets) | ||
} else { | ||
None | ||
} | ||
case _ => None | ||
} | ||
} | ||
} |
132 changes: 0 additions & 132 deletions
132
.../main/scala/org/apache/spark/sql/execution/bucketing/CoalesceBucketsInSortMergeJoin.scala
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.