c21 commented on a change in pull request #29804:
URL: https://github.com/apache/spark/pull/29804#discussion_r492919052
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala
##########
@@ -165,6 +166,7 @@ case class FileSourceScanExec(
partitionFilters: Seq[Expression],
optionalBucketSet: Option[BitSet],
optionalNumCoalescedBuckets: Option[Int],
+ optionalDynamicDecideBucketing: Option[Boolean],
Review comment:
@maropu - sure, updated. Added a new field `DisableBucketedScan` for
explain. This will be printed out when bucketed scan is disable. In addition,
changed to not print out the other field `SelectedBucketsCount` if this is not
bucketed scan.
@cloud-fan - sure, updated. Was just trying to keep consistent with other
bucketing parameters here, but an `Option` is not necessary.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/PlanBucketing.scala
##########
@@ -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.
+ * 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
+ * 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] {
Review comment:
@cloud-fan - updated.
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
##########
@@ -2764,6 +2764,14 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val DYNAMIC_DECIDE_BUCKETING_ENABLED =
+ buildConf("spark.sql.sources.dynamic.decide.bucketing.enabled")
Review comment:
@maropu , @cloud-fan - sure, updated. Change the config name to be
`spark.sql.sources.bucketing.autoBucketedScan.enabled` as most of our configs
are with `.enabled` at the end.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/PlanBucketing.scala
##########
@@ -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.
Review comment:
@cloud-fan - updated.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/PlanBucketing.scala
##########
@@ -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.
+ * 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
+ * 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] {
+ private def disableBucketWithInterestingPartition(plan: SparkPlan):
SparkPlan = {
+ var hasPlanWithInterestingPartition = false
+
+ val newPlan = plan.transformUp {
+ case p if hasInterestingPartition(p) =>
+ hasPlanWithInterestingPartition = true
Review comment:
@cloud-fan - sure, I feel the same way and sorry for the laziness. I
updated to use `TreeNode.mapChildren` with only one round of pre-order
traversal of plan (`disableBucketWithInterestingPartition`). Wondering does it
look better? Thanks.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/PlanBucketing.scala
##########
@@ -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.
+ * 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
Review comment:
@cloud-fan - sorry if I misunderstand anything, I have check to only
allow certain `UnaryExecNode` operators in sub-plan, so the single lineage
property is guaranteed. I updated the comment to call it out explicitly.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]