peter-toth commented on a change in pull request #32298:
URL: https://github.com/apache/spark/pull/32298#discussion_r627441601



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode, 
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY, 
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] 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.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference 
pointing to its cached
+ *   version in this form: 
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a 
subquery plan that
+ *   returns multiple values and either replaces only [[SubqueryReference]] to 
the cached plan or
+ *   restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed 
once.
+ *
+ * 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:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ *   multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * :  :- 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
+ * :  +- 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
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+  def apply(plan: LogicalPlan): LogicalPlan = {
+    if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+      val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+      removeReferences(mergeAndInsertReferences(plan, mergedSubqueries), 
mergedSubqueries)
+    } else {
+      plan
+    }
+  }
+
+  private def mergeAndInsertReferences(
+      plan: LogicalPlan,
+      mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+    
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY), 
ruleId) {
+      case s: ScalarSubquery if s.children.isEmpty =>
+        val (mergedPlan, ordinal) = mergeAndGetReference(s.plan, 
mergedSubqueries)
+        GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+    }
+  }
+
+  case class SubqueryReference(
+      index: Int,
+      mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+    override def stringArgs: Iterator[Any] = Iterator(index)
+
+    override def output: Seq[Attribute] = mergedSubqueries(index).output
+  }
+
+  private def mergeAndGetReference(
+      plan: LogicalPlan,
+      mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+    mergedSubqueries.zipWithIndex.collectFirst {
+      Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+    }.map { case ((mergedPlan, outputMap), i) =>
+      mergedSubqueries(i) = mergedPlan
+      SubqueryReference(i, mergedSubqueries) ->
+        mergedPlan.output.indexOf(outputMap(plan.output.head))
+    }.getOrElse {
+      mergedSubqueries += plan
+      SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+    }
+  }
+
+  private def mergePlans(
+      newPlan: LogicalPlan,
+      existingPlan: LogicalPlan): Option[(LogicalPlan, 
AttributeMap[Attribute])] = {
+    (newPlan, existingPlan) match {
+      case (np, ep) if np.canonicalized == ep.canonicalized =>
+        Some(ep -> AttributeMap(np.output.zip(ep.output)))
+      case (np: Project, ep: Project) =>
+        mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+          val newProjectList = replaceAttributes(np.projectList, outputMap)
+          val newOutputMap = createOutputMap(np.projectList, newProjectList)
+          Project(distinctExpressions(ep.projectList ++ newProjectList), 
mergedChild) ->
+            newOutputMap
+        }
+      case (np, ep: Project) =>
+        mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+          Project(distinctExpressions(ep.projectList ++ outputMap.values), 
mergedChild) -> outputMap
+        }
+      case (np: Project, ep) =>
+        mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+          val newProjectList = replaceAttributes(np.projectList, outputMap)
+          val newOutputMap = createOutputMap(np.projectList, newProjectList)
+          Project(distinctExpressions(ep.output ++ newProjectList), 
mergedChild) -> newOutputMap
+        }
+      case (np: Aggregate, ep: Aggregate) =>

Review comment:
       > We can always assume merging two (or more) aggregates makes 
performance better? For example, we have two aggregates in a plan, one side is 
a hash-aggregate and the other side is an object hash-aggregate. In this case, 
the merged plan node seems to be an object-hash aggregate. If this is true, 
this rewrite can easily cause high memory pressure.
   
   Thanks, this is a very good question, let me look into this...
   
   > IMHO, since this rewrite itself is not an optimization, but a pre-process 
to reuse sub-queries, so it might be better to implement this logic inside the 
ReuseSubquery side, and merge them if physical plans are the same.
   
   The reason why I implemented this feature as an `Optimizer` rule is that 
merging `LogicalPlans` seems much easier than merging physical ones. The 
example in the description has the following physical subquery plans:
   ``` 
   *(1) Project [Subquery scalar-subquery#231, [id=#110] AS 
scalarsubquery()#241, Subquery scalar-subquery#232, [id=#132] AS 
scalarsubquery()#242L]
   :  :- Subquery scalar-subquery#231, [id=#110]
   :  :  +- *(2) HashAggregate(keys=[b#234], functions=[avg(a#233)], 
output=[avg(a)#236])
   :  :     +- Exchange hashpartitioning(b#234, 5), ENSURE_REQUIREMENTS, 
[id=#106]
   :  :        +- *(1) HashAggregate(keys=[b#234], 
functions=[partial_avg(a#233)], output=[b#234, sum#247, count#248L])
   :  :           +- *(1) ColumnarToRow
   :  :              +- FileScan parquet default.t[a#233,b#234] Batched: true, 
DataFilters: [], Format: Parquet, Location: InMemoryFileIndex(1 
paths)[file:/Users/petertoth/git/apache/spark/spark-warehouse/org.apache.spar...,
 PartitionFilters: [], PushedFilters: [], ReadSchema: struct<a:int,b:int>
   :  +- Subquery scalar-subquery#232, [id=#132]
   :     +- *(2) HashAggregate(keys=[b#240], functions=[sum(b#240)], 
output=[sum(b)#238L])
   :        +- Exchange hashpartitioning(b#240, 5), ENSURE_REQUIREMENTS, 
[id=#128]
   :           +- *(1) HashAggregate(keys=[b#240], 
functions=[partial_sum(b#240)], output=[b#240, sum#250L])
   :              +- *(1) ColumnarToRow
   :                 +- FileScan parquet default.t[b#240] Batched: true, 
DataFilters: [], Format: Parquet, Location: InMemoryFileIndex(1 
paths)[file:/Users/petertoth/git/apache/spark/spark-warehouse/org.apache.spar...,
 PartitionFilters: [], PushedFilters: [], ReadSchema: struct<b:int>
   ```
   Merging these 2 physical subqueries would require much more complex 
`mergePlans()` function that can handle `Exchange` and `Scan` nodes.
   
    




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

Reply via email to