cloud-fan commented on a change in pull request #25600: [SPARK-11150][SQL] 
Dynamic Partition Pruning
URL: https://github.com/apache/spark/pull/25600#discussion_r320625154
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/dynamicpruning/PlanDynamicPruningFilters.scala
 ##########
 @@ -0,0 +1,101 @@
+/*
+ * 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.dynamicpruning
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions
+import org.apache.spark.sql.catalyst.expressions.{Alias, BindReferences, 
DynamicPruningExpression, DynamicPruningSubquery, Expression, ListQuery, 
Literal, PredicateHelper}
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.physical.BroadcastMode
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.{InSubqueryExec, QueryExecution, 
SparkPlan, SubqueryBroadcastExec}
+import org.apache.spark.sql.execution.exchange.BroadcastExchangeExec
+import org.apache.spark.sql.execution.joins._
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * This planner rule aims at rewriting dynamic pruning predicates in order to 
reuse the
+ * results of broadcast. For joins that are not planned as broadcast hash 
joins we keep
+ * the fallback mechanism with subquery duplicate.
+*/
+case class PlanDynamicPruningFilters(sparkSession: SparkSession)
+    extends Rule[SparkPlan] with PredicateHelper {
+
+  private def reuseBroadcast: Boolean =
+    SQLConf.get.dynamicPartitionPruningReuseBroadcast && 
SQLConf.get.exchangeReuseEnabled
+
+  /**
+   * Identify the shape in which keys of a given plan are broadcasted.
+   */
+  private def broadcastMode(keys: Seq[Expression], plan: LogicalPlan): 
BroadcastMode = {
+    val packedKeys = 
BindReferences.bindReferences(HashJoin.rewriteKeyExpr(keys), plan.output)
+    HashedRelationBroadcastMode(packedKeys)
+  }
+
+  /**
+   * Verify if a given plan can be broadcasted either because of its size or a 
user's hint.
+   */
+  private def canBroadcast(plan: LogicalPlan): Boolean = {
+    plan.stats.sizeInBytes >= 0 &&
+      plan.stats.sizeInBytes <= SQLConf.get.autoBroadcastJoinThreshold
+  }
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!SQLConf.get.dynamicPartitionPruningEnabled) {
+      return plan
+    }
+
+    plan transformAllExpressions {
+      case DynamicPruningSubquery(
+          value, buildPlan, buildKeys, broadcastKeyIndex, onlyInBroadcast, 
broadcastHint, exprId) =>
+        val qe = new QueryExecution(sparkSession, buildPlan)
+        // Using `sparkPlan` is a little hacky as it is based on the 
assumption that this rule is
+        // the first to be applied (apart from `InsertAdaptiveSparkPlan`).
+        val canReuseExchange = reuseBroadcast &&
+          (broadcastHint || canBroadcast(buildPlan)) &&
 
 Review comment:
   why do we need to check the plan data size and broadcast hint, if we already 
know that the plan is under a broadcast join?

----------------------------------------------------------------
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]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to