-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-31350][SQL] Coalesce bucketed tables for sort merge join if applicable #28123
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
Changes from all commits
94720e0
a15af6b
40b0707
cdc83f8
01e980e
0b9abee
c93322b
347c2de
c78e2a2
26cfb63
ebe062f
19b8cd8
148a6fc
eeb0ec7
56ca53d
c1e0d26
13aaa55
e54d34b
eee4884
ed2d316
1b5e34f
1df3b9f
cf5b835
592a783
e231268
38c7d6e
62a04a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * 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 org.apache.spark.sql.catalyst.catalog.BucketSpec | ||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| 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.SortMergeJoinExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * This rule coalesces one side of the `SortMergeJoin` if the following conditions are met: | ||
| * - Two bucketed tables are joined. | ||
imback82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * - 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_SORT_MERGE_JOIN_ENABLED is set to true. | ||
| * - The ratio of the number of buckets is less than the value set in | ||
| * COALESCE_BUCKETS_IN_SORT_MERGE_JOIN_MAX_BUCKET_RATIO. | ||
| */ | ||
| case class CoalesceBucketsInSortMergeJoin(conf: SQLConf) extends Rule[SparkPlan] { | ||
| private def mayCoalesce(numBuckets1: Int, numBuckets2: Int, conf: SQLConf): Option[Int] = { | ||
| assert(numBuckets1 != numBuckets2) | ||
| 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. | ||
| if (large % small == 0 && | ||
| large / small <= conf.getConf(SQLConf.COALESCE_BUCKETS_IN_SORT_MERGE_JOIN_MAX_BUCKET_RATIO)) { | ||
| Some(small) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| private def updateNumCoalescedBuckets(plan: SparkPlan, numCoalescedBuckets: Int): SparkPlan = { | ||
| plan.transformUp { | ||
| case f: FileSourceScanExec => | ||
| f.copy(optionalNumCoalescedBuckets = Some(numCoalescedBuckets)) | ||
| } | ||
| } | ||
|
|
||
| def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!conf.getConf(SQLConf.COALESCE_BUCKETS_IN_SORT_MERGE_JOIN_ENABLED)) { | ||
| return plan | ||
| } | ||
|
|
||
| plan transform { | ||
| case ExtractSortMergeJoinWithBuckets(smj, numLeftBuckets, numRightBuckets) | ||
| if numLeftBuckets != numRightBuckets => | ||
| mayCoalesce(numLeftBuckets, numRightBuckets, conf).map { numCoalescedBuckets => | ||
| if (numCoalescedBuckets != numLeftBuckets) { | ||
| smj.copy(left = updateNumCoalescedBuckets(smj.left, numCoalescedBuckets)) | ||
| } else { | ||
| smj.copy(right = updateNumCoalescedBuckets(smj.right, numCoalescedBuckets)) | ||
| } | ||
| }.getOrElse(smj) | ||
| case other => other | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An extractor that extracts `SortMergeJoinExec` where both sides of the join have the bucketed | ||
| * tables and are consisted of only the scan operation. | ||
| */ | ||
| object ExtractSortMergeJoinWithBuckets { | ||
| private def isScanOperation(plan: SparkPlan): Boolean = plan match { | ||
| case f: FilterExec => isScanOperation(f.child) | ||
| case p: ProjectExec => isScanOperation(p.child) | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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`. | ||
cloud-fan 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(s: SortMergeJoinExec): Boolean = { | ||
| isScanOperation(s.left) && | ||
| isScanOperation(s.right) && | ||
| satisfiesOutputPartitioning(s.leftKeys, s.left.outputPartitioning) && | ||
| satisfiesOutputPartitioning(s.rightKeys, s.right.outputPartitioning) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR but just an idea: we don't need to do bucket scan at all if it can't save shuffles. This can increase parallelism. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a follow-up issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can give it a shot after this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@cloud-fan IMO there's other benefit to do bucket scan even though it can't save shuffle, e.g. bucket filter push down. So we probably need to take that into consideration before disabling bucketing. |
||
| } | ||
|
|
||
| def unapply(plan: SparkPlan): Option[(SortMergeJoinExec, Int, Int)] = { | ||
| plan match { | ||
| case s: SortMergeJoinExec if isApplicable(s) => | ||
| val leftBucket = getBucketSpec(s.left) | ||
| val rightBucket = getBucketSpec(s.right) | ||
| if (leftBucket.isDefined && rightBucket.isDefined) { | ||
| Some(s, leftBucket.get.numBuckets, rightBucket.get.numBuckets) | ||
| } else { | ||
| None | ||
| } | ||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.