peter-toth commented on a change in pull request #28885: URL: https://github.com/apache/spark/pull/28885#discussion_r446632000
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/reuse/Reuse.scala ########## @@ -0,0 +1,95 @@ +/* + * 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.execution.reuse + +import scala.collection.mutable.Map + +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.execution.{BaseSubqueryExec, ExecSubqueryExpression, ReusedSubqueryExec, SparkPlan} +import org.apache.spark.sql.execution.exchange.{Exchange, ReusedExchangeExec} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.StructType + +/** + * Find out duplicated exchanges and subqueries in the whole spark plan including subqueries, then + * use the same exhange or subquery for all the references. + */ +case class WholePlanReuse(conf: SQLConf) extends Rule[SparkPlan] { + + def apply(plan: SparkPlan): SparkPlan = { + if (conf.exchangeReuseEnabled || conf.subqueryReuseEnabled) { + // To avoid costly canonicalization of an exchange or a subquery: Review comment: I think the `Map[StructType, ...` way of caching has been there for quite some time. A simple map of canonicalized plans naturally comes to my mind too and I feel that it would do the thing without any performance degradation for most of the queries. But I'm afraid that there can be edge cases where it could introduce degradation so just to be on the safe side I wouldn't touch this preliminary schema matching when looking up in the cache. On the other hand I think the old `ArrayBuffer[...` can be easily replaced to a map of canonicalized plans to speed up look ups in the cache when schema matches. I saw your other comment: https://github.com/apache/spark/pull/28885#discussion_r446516852 on this topic and I think the `Canonicalized[T]` wrapper would be exactly same as the old `Map[StructType, ArrayBuffer[T]]` cache map, just a bit more complicated. What I did in my latest commit: https://github.com/apache/spark/pull/28885/commits/c49a0f99b25522dd7ed89e8b41d2c5c80dbaf170 is that I extracted the cache code and I think it became quite easy to follow. But I'm open for suggestions and will change the implementation if you think it is still too complicated. ---------------------------------------------------------------- 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]
