zinking commented on a change in pull request #32298:
URL: https://github.com/apache/spark/pull/32298#discussion_r708917462



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,413 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ListBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, 
CommonScalarSubqueries, Filter, Join, LogicalPlan, Project, Subquery}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{SCALAR_SUBQUERY, 
SCALAR_SUBQUERY_REFERENCE, TreePattern}
+import org.apache.spark.sql.types.DataType
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s to 
compute multiple scalar
+ * values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried 
to merge into the cache
+ *   of already seen subquery plans. If merge is possible then cache is 
updated with the merged
+ *   subquery plan, if not then the new subquery plan is added to the cache.
+ *   During this first traversal each [[ScalarSubquery]] expression is 
replaced to a
+ *   [[ScalarSubqueryReference]] pointing to its cached version.
+ *   The cache uses a flag to keep track of if a cache entry is a results of 
merging 2 or more
+ *   plans, or it is a plan that was seen only once.
+ *   Merged plans in the cache get a "header" that is is basically
+ *   `CreateNamedStructure(name1, attribute1, name2, attribute2, ...)`
+ *   expression in new root [[Project]] node. This expression ensures that the 
merged plan is a
+ *   valid scalar subquery that returns only one value.
+ * - A second traversal checks if a [[ScalarSubqueryReference]] is pointing to 
a merged subquery
+ *   plan or not and either keeps the reference or restores the original 
[[ScalarSubquery]].
+ *   If there are [[ScalarSubqueryReference]] nodes remained a 
[[CommonScalarSubqueries]] root node
+ *   is added to the plan with the referenced scalar subqueries.
+ * - [[PlanSubqueries]] or [[PlanAdaptiveSubqueries]] rule does the physical 
planning of scalar
+ *   subqueries including the ones under [[CommonScalarSubqueriesExec]] node 
and replaces
+ *   each [[ScalarSubqueryReference]] to their referenced physical plan in
+ *   `GetStructField(ScalarSubquery(merged plan with CreateNamedStruct() 
header))` form.
+ *   It is important that references pointing to the same merged subquery are 
replaced to the same
+ *   planned instance to make sure that each merged subquery runs only once 
(even without a wrapping
+ *   [[ReuseSubquery]] node).
+ *   Finally, the [[CommonScalarSubqueriesExec]] node is removed from the 
physical plan.
+ * - The [[ReuseExchangeAndSubquery]] rule wraps the second, third, ... 
instances of the same
+ *   subquery into a [[ReuseSubquery]] node, but this just a cosmetic change 
in the plan.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ *   (SELECT avg(a) FROM t GROUP BY b),
+ *   (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ *          scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * :  :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * :  :  +- Relation default.t[a#233,b#234] parquet
+ * :  +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * :     +- Project [b#240]
+ * :        +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * CommonScalarSubqueries [scalar-subquery#250 []]
+ * :  +- Project [named_struct(avg(a), avg(a)#236, sum(b), sum(b)#238L) AS 
mergedValue#249]
+ * :     +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS 
sum(b)#238L]
+ * :        +- Project [a#233, b#234]
+ * :           +- Relation default.t[a#233,b#234] parquet
+ * +- Project [scalarsubqueryreference(0, 0, DoubleType, 231) AS 
scalarsubquery()#241,
+ *             scalarsubqueryreference(0, 1, LongType, 232) AS 
scalarsubquery()#242L]
+ *    +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+  def apply(plan: LogicalPlan): LogicalPlan = {
+    if (conf.subqueryReuseEnabled) {
+      plan match {
+        case Subquery(_: CommonScalarSubqueries, _) => plan
+        case s: Subquery => s.copy(child = 
extractCommonScalarSubqueries(s.child))
+        case _: CommonScalarSubqueries => plan
+        case _ => extractCommonScalarSubqueries(plan)
+      }
+    } else {
+      plan
+    }
+  }
+
+  private def extractCommonScalarSubqueries(plan: LogicalPlan) = {
+    // Plan of subqueries and a flag is the plan is merged
+    val cache = ListBuffer.empty[(Project, Boolean)]
+    val newPlan = removeReferences(insertReferences(plan, cache), cache)
+    if (cache.nonEmpty) {
+      val scalarSubqueries = cache.map { case (header, _) => 
ScalarSubquery(header) }.toSeq
+      CommonScalarSubqueries(scalarSubqueries, newPlan)
+    } else {
+      newPlan
+    }
+  }
+
+  // First traversal builds up the cache and inserts 
`ScalarSubqueryReference`s to the plan.
+  private def insertReferences(
+      plan: LogicalPlan,
+      cache: ListBuffer[(Project, Boolean)]): LogicalPlan = {
+    
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY)) {
+      case s: ScalarSubquery if s.children.isEmpty =>
+        val (subqueryIndex, headerIndex) = cacheSubquery(s.plan, cache)
+        ScalarSubqueryReference(subqueryIndex, headerIndex, s.dataType, 
s.exprId)
+    }
+  }
+
+  // Caching returns the index of the subquery in the cache and the index of 
scalar member in the
+  // `CreateNamedStruct` header.
+  private def cacheSubquery(
+      plan: LogicalPlan,
+      cache: ListBuffer[(Project, Boolean)]): (Int, Int) = {
+    val firstOutput = plan.output.head
+    cache.zipWithIndex.collectFirst(Function.unlift { case ((header, merged), 
subqueryIndex) =>
+      checkIdenticalPlans(plan, header.child)
+        .map((subqueryIndex, header, header.child, _, merged))
+        .orElse(tryMergePlans(plan, header.child).map {
+          case (mergedPlan, outputMap) => (subqueryIndex, header, mergedPlan, 
outputMap, true)
+        })
+    }).map { case (subqueryIndex, header, mergedPlan, outputMap, merged) =>
+      val mappedFirstOutput = mapAttributes(firstOutput, outputMap)
+      val headerElements = getHeaderElements(header)
+      var headerIndex = headerElements.indexWhere {
+        case (_, attribute) => attribute.exprId == mappedFirstOutput.exprId
+      }
+      if (headerIndex == -1) {
+        val newHeaderElements = headerElements :+ (Literal(firstOutput.name) 
-> mappedFirstOutput)
+        cache(subqueryIndex) = createHeader(newHeaderElements, mergedPlan) -> 
merged
+        headerIndex = headerElements.size
+      }
+      subqueryIndex -> headerIndex
+    }.getOrElse {
+      cache += createHeader(Seq(Literal(firstOutput.name) -> firstOutput), 
plan) -> false
+      cache.length - 1 -> 0
+    }
+  }
+
+  // If 2 plans are identical return the attribute mapping from the new to the 
cached version.
+  private def checkIdenticalPlans(newPlan: LogicalPlan, cachedPlan: 
LogicalPlan) = {
+    if (newPlan.canonicalized == cachedPlan.canonicalized) {
+      Some(AttributeMap(newPlan.output.zip(cachedPlan.output)))
+    } else {
+      None
+    }
+  }
+
+  // Recursively traverse down and try merging 2 plans. If merge is possible 
then return the merged
+  // plan with the attribute mapping from the new to the merged version.
+  // Please note that merging arbitrary plans can be complicated, the current 
version supports only
+  // some of the most important nodes.
+  private def tryMergePlans(
+      newPlan: LogicalPlan,
+      cachedPlan: LogicalPlan): Option[(LogicalPlan, AttributeMap[Attribute])] 
= {
+    checkIdenticalPlans(newPlan, cachedPlan).map(cachedPlan -> _).orElse(
+      (newPlan, cachedPlan) match {
+        case (np: Project, cp: Project) =>
+          tryMergePlans(np.child, cp.child).map { case (mergedChild, 
outputMap) =>
+            val (mergedProjectList, newOutputMap) =
+              mergeNamedExpressions(np.projectList, outputMap, cp.projectList)
+            val mergedPlan = Project(mergedProjectList, mergedChild)
+            mergedPlan -> newOutputMap
+          }
+        case (np, cp: Project) =>
+          tryMergePlans(np, cp.child).map { case (mergedChild, outputMap) =>
+            val (mergedProjectList, newOutputMap) =
+              mergeNamedExpressions(np.output, outputMap, cp.projectList)
+            val mergedPlan = Project(mergedProjectList, mergedChild)
+            mergedPlan -> newOutputMap
+          }
+        case (np: Project, cp) =>
+          tryMergePlans(np.child, cp).map { case (mergedChild, outputMap) =>
+            val (mergedProjectList, newOutputMap) =
+              mergeNamedExpressions(np.projectList, outputMap, cp.output)
+            val mergedPlan = Project(mergedProjectList, mergedChild)
+            mergedPlan -> newOutputMap
+          }
+        case (np: Aggregate, cp: Aggregate) if supportedAggregateMerge(np, cp) 
=>
+          tryMergePlans(np.child, cp.child).flatMap { case (mergedChild, 
outputMap) =>
+            val mappedNewGroupingExpression =
+              np.groupingExpressions.map(mapAttributes(_, outputMap))
+            if (ExpressionSet(mappedNewGroupingExpression) ==
+              ExpressionSet(cp.groupingExpressions)) {
+              val (mergedAggregateExpressions, newOutputMap) =
+                mergeNamedExpressions(np.aggregateExpressions, outputMap, 
cp.aggregateExpressions)
+              val mergedPlan =
+                Aggregate(cp.groupingExpressions, mergedAggregateExpressions, 
mergedChild)
+              Some(mergedPlan -> newOutputMap)
+            } else {
+              None
+            }
+          }
+
+        // Merging general nodes is complicated and this implementation 
supports only those nodes in
+        // which the order and the number of output attributes are not 
relevant (see
+        // `supportedMerge()` whitelist).
+        // Also, this implementation supports only those nodes in which 
children can be merged in
+        // the same order.
+        case (np, cp) if supportedMerge(np) && np.getClass == cp.getClass &&
+          np.children.size == cp.children.size =>
+          val merged = np.children.zip(cp.children).map {
+            case (npChild, cpChild) => tryMergePlans(npChild, cpChild)
+          }
+          if (merged.forall(_.isDefined)) {
+            val (mergedChildren, outputMaps) = merged.map(_.get).unzip
+            val outputMap = AttributeMap(outputMaps.map(_.iterator).reduce(_ 
++ _).toSeq)
+            val mappedNewPlan = 
mapAttributes(np.withNewChildren(mergedChildren), outputMap)
+            val mergedPlan = cp.withNewChildren(mergedChildren)
+            if (mappedNewPlan.canonicalized == mergedPlan.canonicalized) {
+              Some(mergedPlan -> outputMap)
+            } else {
+              None
+            }
+          } else {
+            None
+          }
+
+        // As a follow-up, it would be possible to merge 
`CommonScalarSubqueries` nodes, which would

Review comment:
       perhaps I am not catch all. wouldn't it be straight forward if you put 
the `Common` under the root node and merge reference plans there ? 




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



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

Reply via email to