sigmod commented on a change in pull request #32298: URL: https://github.com/apache/spark/pull/32298#discussion_r630412347
########## 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: > I would pursue (1) in this PR first and maybe (2) in a separate one. Does this sound acceptable? Yeah, that sounds great. Thanks a lot, @peter-toth! > There are 2 aggregates in both subqueries so without dedup both (2) and this PR could cause regressions. IIUC, I think it sounds like an existing bug (or missing feature) for struct subfield pruning, which could be blocking (2) but is orthogonal to (2). For instance, if I write your example join query manually, I'd expect the struct subfield pruning to happen to the struct constructor, regardless of the existence of subqueries. > I've never seen such transformations in SparkStrategys. It's not uncommon in exploration Strategies such as index selection, common subplan dedup, when we substitute the subtree of a tree node T with another subtree (from somewhere else in the plan or a different access path) that may contain unneeded columns for T. Spark doesn't have those strategies for now, but I'll not be surprised if we add them down the road. -- 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