sigmod commented on a change in pull request #32298: URL: https://github.com/apache/spark/pull/32298#discussion_r629516965
########## 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: >> Doesn't that mean that (2) also assumes that >> no subsequent transformation changes the 2 instances differently and >> ReuseSubquery does the dedup? The difference is that transformation (2) is a self-contained transformation that results in a better plan, while this PR's MergeScalarSubqueries is not. If for some reason ReuseSubquery does not trigger, applying (2) does not make the plan worse than not applying (2). However, iiuc, in the same situation, MergeScalarSubqueries would make the plan worse because each aggregate pipeline runs additional computations. -- 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