|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.util |
| 19 | + |
| 20 | +import scala.collection.mutable.Map |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.plans.QueryPlan |
| 23 | +import org.apache.spark.sql.types.StructType |
| 24 | + |
| 25 | +/** |
| 26 | + * Map of canonicalized plans that can be used to find reuse possibilities. |
| 27 | + * |
| 28 | + * To avoid costly canonicalization of a plan: |
| 29 | + * - we use its schema first to check if it can be replaced to a reused one at all |
| 30 | + * - we insert it into the map of canonicalized plans only when at least 2 have the same schema |
| 31 | + */ |
| 32 | +class ReuseMap[T <: QueryPlan[_]] { |
| 33 | + // scalastyle:off structural.type |
| 34 | + private val map = Map[StructType, (T, Map[T2 forSome { type T2 >: T }, T])]() |
| 35 | + // scalastyle:on structural.type |
| 36 | + |
| 37 | + /** |
| 38 | + * Find a matching plan with the same canonicalized form in the map or add the new plan to the |
| 39 | + * map otherwise. |
| 40 | + * |
| 41 | + * @param plan the input plan |
| 42 | + * @return the matching plan or the input plan |
| 43 | + */ |
| 44 | + def lookup(plan: T): T = { |
| 45 | + val (firstSameSchemaPlan, sameResultPlans) = map.getOrElseUpdate(plan.schema, plan -> Map()) |
| 46 | + if (firstSameSchemaPlan ne plan) { |
| 47 | + if (sameResultPlans.isEmpty) { |
| 48 | + sameResultPlans += firstSameSchemaPlan.canonicalized -> firstSameSchemaPlan |
| 49 | + } |
| 50 | + sameResultPlans.getOrElseUpdate(plan.canonicalized, plan) |
| 51 | + } else { |
| 52 | + plan |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Find a matching plan with the same canonicalized form in the map and apply `f` on it or add |
| 58 | + * the new plan to the map otherwise. |
| 59 | + * |
| 60 | + * @param plan the input plan |
| 61 | + * @param f the function to apply |
| 62 | + * @return the matching plan with `f` applied or the input plan |
| 63 | + */ |
| 64 | + def addOrElse[T2 >: T](plan: T, f: T => T2): T2 = { |
| 65 | + val found = lookup(plan) |
| 66 | + if (found eq plan) { |
| 67 | + plan |
| 68 | + } else { |
| 69 | + f(found) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments