-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-32859][SQL] Introduce physical rule to decide bucketing dynamically #29804
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 2 commits
a7193cf
7ffcdbc
6a8c7a5
a15f864
b45ee8b
9aa0266
26c4338
336337a
1012334
057c021
e0c76c9
fbe0c06
4027175
f2ceacd
b29f688
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 |
|---|---|---|
|
|
@@ -155,6 +155,7 @@ case class RowDataSourceScanExec( | |
| * @param partitionFilters Predicates to use for partition pruning. | ||
| * @param optionalBucketSet Bucket ids for bucket pruning. | ||
| * @param optionalNumCoalescedBuckets Number of coalesced buckets. | ||
| * @param optionalDynamicDecideBucketing Disable bucketing dynamically based on query plan. | ||
| * @param dataFilters Filters on non-partition columns. | ||
| * @param tableIdentifier identifier for the table in the metastore. | ||
| */ | ||
|
|
@@ -165,6 +166,7 @@ case class FileSourceScanExec( | |
| partitionFilters: Seq[Expression], | ||
| optionalBucketSet: Option[BitSet], | ||
| optionalNumCoalescedBuckets: Option[Int], | ||
| optionalDynamicDecideBucketing: Option[Boolean], | ||
|
Member
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. nit: could you add this param in
Contributor
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. Why does it need to be
Contributor
Author
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. @maropu - sure, updated. Added a new field @cloud-fan - sure, updated. Was just trying to keep consistent with other bucketing parameters here, but an |
||
| dataFilters: Seq[Expression], | ||
| tableIdentifier: Option[TableIdentifier]) | ||
| extends DataSourceScanExec { | ||
|
|
@@ -257,7 +259,8 @@ case class FileSourceScanExec( | |
|
|
||
| // exposed for testing | ||
| lazy val bucketedScan: Boolean = { | ||
| if (relation.sparkSession.sessionState.conf.bucketingEnabled && relation.bucketSpec.isDefined) { | ||
| if (relation.sparkSession.sessionState.conf.bucketingEnabled && relation.bucketSpec.isDefined | ||
| && optionalDynamicDecideBucketing.getOrElse(true)) { | ||
| val spec = relation.bucketSpec.get | ||
| val bucketColumns = spec.bucketColumnNames.flatMap(n => toAttribute(n)) | ||
| bucketColumns.size == spec.bucketColumnNames.size | ||
|
|
@@ -623,6 +626,7 @@ case class FileSourceScanExec( | |
| filterUnusedDynamicPruningExpressions(partitionFilters), output), | ||
| optionalBucketSet, | ||
| optionalNumCoalescedBuckets, | ||
| optionalDynamicDecideBucketing, | ||
| QueryPlan.normalizePredicates(dataFilters, output), | ||
| None) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * 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.expressions.aggregate.{Partial, PartialMerge} | ||
| import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, HashClusteredDistribution} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.{FileSourceScanExec, FilterExec, ProjectExec, SortExec, SparkPlan} | ||
| import org.apache.spark.sql.execution.aggregate.BaseAggregateExec | ||
| import org.apache.spark.sql.execution.exchange.Exchange | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * Plans bucketing dynamically based on actual physical query plan. | ||
|
Contributor
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.
Contributor
Author
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 - updated. |
||
| * NOTE: this rule is designed to be applied right after [[EnsureRequirements]], | ||
| * where all [[ShuffleExchangeExec]] and [[SortExec]] have been added to plan properly. | ||
| * | ||
| * When BUCKETING_ENABLED and DYNAMIC_DECIDE_BUCKETING_ENABLED are set to true, go through | ||
| * query plan to check where bucketed table scan is unnecessary, and disable bucketed table | ||
| * scan if needed. | ||
| * | ||
| * For all operators which [[hasInterestingPartition]] (i.e., require [[ClusteredDistribution]] | ||
| * or [[HashClusteredDistribution]]), check if the sub-plan for operator has [[Exchange]] and | ||
|
Contributor
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.
Can we make it more fine-grained? It's possible that the exchange and the bucket scan are not in the same lineage:
Contributor
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. The above quey is not valid, I just put it here as an example.
Contributor
Author
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 - sorry if I misunderstand anything, I have check to only allow certain |
||
| * bucketed table scan (and only allow certain operators in plan, see details in | ||
| * [[canDisableBucketedScan]]). If yes, disable the bucketed table scan in the sub-plan. | ||
| * | ||
| * Examples: | ||
| * (1).join: | ||
| * SortMergeJoin(t1.i = t2.j) | ||
| * / \ | ||
| * Sort(i) Sort(j) | ||
| * / \ | ||
| * Shuffle(i) Scan(t2: i, j) | ||
| * / (bucketed on column j, enable bucketed scan) | ||
| * Scan(t1: i, j) | ||
| * (bucketed on column j, DISABLE bucketed scan) | ||
| * | ||
| * (2).aggregate: | ||
| * HashAggregate(i, ..., Final) | ||
| * | | ||
| * Shuffle(i) | ||
| * | | ||
| * HashAggregate(i, ..., Partial) | ||
| * | | ||
| * Filter | ||
| * | | ||
| * Scan(t1: i, j) | ||
| * (bucketed on column j, DISABLE bucketed scan) | ||
| * | ||
| * The idea of [[hasInterestingPartition]] is inspired from "interesting order" in | ||
| * the paper "Access Path Selection in a Relational Database Management System" | ||
| * (http://www.inf.ed.ac.uk/teaching/courses/adbs/AccessPath.pdf). | ||
| */ | ||
| case class PlanBucketing(conf: SQLConf) extends Rule[SparkPlan] { | ||
|
Contributor
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. nit:
Contributor
Author
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 - updated. |
||
| private def disableBucketWithInterestingPartition(plan: SparkPlan): SparkPlan = { | ||
| var hasPlanWithInterestingPartition = false | ||
|
|
||
| val newPlan = plan.transformUp { | ||
| case p if hasInterestingPartition(p) => | ||
| hasPlanWithInterestingPartition = true | ||
|
Contributor
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. This looks tricky and fragile. We shouldn't call
Contributor
Author
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 - sure, I feel the same way and sorry for the laziness. I updated to use |
||
|
|
||
| val newChildren = p.children.map(child => { | ||
| if (canDisableBucketedScan(child)) { | ||
| disableBucketedScan(child) | ||
| } else { | ||
| child | ||
| } | ||
| }) | ||
| p.withNewChildren(newChildren) | ||
| case other => other | ||
| } | ||
|
|
||
| if (hasPlanWithInterestingPartition) { | ||
| newPlan | ||
| } else { | ||
| // Disable all bucketed scans if there's no operator with interesting partition | ||
| // found in query plan. | ||
| disableBucketedScan(newPlan) | ||
| } | ||
| } | ||
|
|
||
| private def hasInterestingPartition(plan: SparkPlan): Boolean = { | ||
| plan.requiredChildDistribution.exists { | ||
| case _: ClusteredDistribution | _: HashClusteredDistribution => true | ||
| case _ => false | ||
| } | ||
| } | ||
|
|
||
| private def canDisableBucketedScan(plan: SparkPlan): Boolean = { | ||
| val hasExchange = plan.find(_.isInstanceOf[Exchange]).isDefined | ||
| val hasBucketedScan = plan.find { | ||
| case scan: FileSourceScanExec => isBucketedScanWithoutFilter(scan) | ||
| case _ => false | ||
| }.isDefined | ||
|
|
||
| def isAllowedPlan(plan: SparkPlan): Boolean = plan match { | ||
| case _: SortExec | _: Exchange | _: ProjectExec | _: FilterExec | | ||
| _: FileSourceScanExec => true | ||
| case partialAgg: BaseAggregateExec => | ||
| val modes = partialAgg.aggregateExpressions.map(_.mode) | ||
| modes.nonEmpty && modes.forall(mode => mode == Partial || mode == PartialMerge) | ||
| case _ => false | ||
| } | ||
| val onlyHasAllowedPlans = plan.find(!isAllowedPlan(_)).isEmpty | ||
| hasExchange && hasBucketedScan && onlyHasAllowedPlans | ||
| } | ||
|
|
||
| private def disableBucketedScan(plan: SparkPlan): SparkPlan = { | ||
| plan.transformUp { | ||
| case scan: FileSourceScanExec if isBucketedScanWithoutFilter(scan) => | ||
| scan.copy(optionalDynamicDecideBucketing = Some(false)) | ||
| } | ||
| } | ||
|
|
||
| private def isBucketedScanWithoutFilter(scan: FileSourceScanExec): Boolean = { | ||
| // Do not disable bucketing for the scan if it has filter pruning, | ||
| // because bucketing is still useful here to save CPU/IO cost with | ||
| // only reading selected bucket files. | ||
| scan.bucketedScan && scan.optionalBucketSet.isEmpty | ||
| } | ||
|
|
||
| def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!conf.bucketingEnabled || !conf.dynamicDecideBucketingEnabled) { | ||
| plan | ||
| } else { | ||
| disableBucketWithInterestingPartition(plan) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding this param under
spark.sql.sources.bucketing.xxx?spark.sql.sources.bucketing.enableAutoBucketScanor something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maropu , @cloud-fan - sure, updated. Change the config name to be
spark.sql.sources.bucketing.autoBucketedScan.enabledas most of our configs are with.enabledat the end.