|
| 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.catalyst.analysis |
| 19 | + |
| 20 | +import scala.collection.{immutable, mutable} |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.CatalystConf |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical._ |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Substitute Hints. |
| 29 | + * - BROADCAST/BROADCASTJOIN/MAPJOIN match the closest table with the given name parameters. |
| 30 | + * |
| 31 | + * This rule substitutes `UnresolvedRelation`s in `Substitute` batch before `ResolveRelations` |
| 32 | + * rule is applied. Here are two reasons. |
| 33 | + * - To support `MetastoreRelation` in Hive module. |
| 34 | + * - To reduce the effect of `Hint` on the other rules. |
| 35 | + * |
| 36 | + * After this rule, it is guaranteed that there exists no unknown `Hint` in the plan. |
| 37 | + * All new `Hint`s should be transformed into concrete Hint classes `BroadcastHint` here. |
| 38 | + */ |
| 39 | +class SubstituteHints(conf: CatalystConf) extends Rule[LogicalPlan] { |
| 40 | + private val BROADCAST_HINT_NAMES = immutable.Set("BROADCAST", "BROADCASTJOIN", "MAPJOIN") |
| 41 | + |
| 42 | + def resolver: Resolver = conf.resolver |
| 43 | + |
| 44 | + private def appendAllDescendant(set: mutable.Set[LogicalPlan], plan: LogicalPlan): Unit = { |
| 45 | + set += plan |
| 46 | + plan.children.foreach { child => appendAllDescendant(set, child) } |
| 47 | + } |
| 48 | + |
| 49 | + def apply(plan: LogicalPlan): LogicalPlan = plan transform { |
| 50 | + case logical: LogicalPlan => logical transformDown { |
| 51 | + case h @ Hint(name, parameters, child) if BROADCAST_HINT_NAMES.contains(name.toUpperCase) => |
| 52 | + var resolvedChild = child |
| 53 | + for (table <- parameters) { |
| 54 | + var stop = false |
| 55 | + val skipNodeSet = scala.collection.mutable.Set.empty[LogicalPlan] |
| 56 | + resolvedChild = resolvedChild.transformDown { |
| 57 | + case n if skipNodeSet.contains(n) => |
| 58 | + skipNodeSet -= n |
| 59 | + n |
| 60 | + case p @ Project(_, _) if p != resolvedChild => |
| 61 | + appendAllDescendant(skipNodeSet, p) |
| 62 | + skipNodeSet -= p |
| 63 | + p |
| 64 | + case r @ BroadcastHint(UnresolvedRelation(t, _)) |
| 65 | + if !stop && resolver(t.table, table) => |
| 66 | + stop = true |
| 67 | + r |
| 68 | + case r @ UnresolvedRelation(t, alias) if !stop && resolver(t.table, table) => |
| 69 | + stop = true |
| 70 | + if (alias.isDefined) { |
| 71 | + SubqueryAlias(alias.get, BroadcastHint(r.copy(alias = None)), None) |
| 72 | + } else { |
| 73 | + BroadcastHint(r) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + resolvedChild |
| 78 | + |
| 79 | + // Remove unrecognized hints |
| 80 | + case Hint(name, _, child) => child |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments