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



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

Review comment:
       >> In this PR the new MergeScalarSubqueries rule runs in a separate 
batch 
   >> after column pruning, close to the end of optimization. 
   >> This is by design to make sure no subsequent rule changes the structure 
   
   I don't see how we can guarantee that. I think the hidden, inter-rule 
dependency can add complexities for future development and maintenance. 
   
   For instance, someone could implement a new Strategy that internally calls 
ColumnPruning after exploring one logical plan alternative. By the time such a 
Strategy is implemented, the authors wouldn't be aware of the fact that 
ColumnPruning should *not* be called after MergeScalarSubqueries. 
   - First of all, such issues can hardly be detected by the author's new 
unit/query tests, as an effective test has to have both effective patterns to 
trigger the Strategy and for MergeScalarSubqueries. However, such a case can 
happen in prod traffic;
   - Second, if they do find that's an issue, they would then have to either 
(a) add some hacks in the Aggregate to mark that MergeScalarSubqueries has been 
applied and hence ColumnPruning should not go through it, or (b) re-implement 
MergeScalarSubqueries per my proposal (1).
     
   I'm wondering whether we can pursue (2) for now, if it meets your need. It's 
less ambitious but may address most of your issue?  If you indeed have to 
extract subqueries over the entire tree, I don't have a clean approach in mind 
other than (1).
   
   >> Add a performance improvement to 1. so as to physical plan a merged 
   >> subquery only once. This is your (1) basically.
   
   The performance was not my initial concern, but rather, I think we'd better 
make MergeScalarSubqueries self-contained and does not depend on *an assumption 
that could later be changed*.
   
   >>  But I don't think we need lateral views
   
   Sub-querying over arrays are important use cases, for which we don't want to 
de-correlate. In this case, a subquery is more like an ordinary expression and 
should be evaluated within the operator node. 
   




-- 
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to