|
| 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.execution.adaptive |
| 19 | + |
| 20 | +import org.apache.spark.sql.execution.SparkPlan |
| 21 | + |
| 22 | +/** |
| 23 | + * This class provides utility methods related to tree traversal of an [[AdaptiveSparkPlanExec]] |
| 24 | + * plan. Unlike their counterparts in [[org.apache.spark.sql.catalyst.trees.TreeNode]] or |
| 25 | + * [[org.apache.spark.sql.catalyst.plans.QueryPlan]], these methods traverse down leaf nodes of |
| 26 | + * adaptive plans, i.e., [[AdaptiveSparkPlanExec]] and [[QueryStageExec]]. |
| 27 | + */ |
| 28 | +trait AdaptiveSparkPlanHelper { |
| 29 | + |
| 30 | + /** |
| 31 | + * Find the first [[SparkPlan]] that satisfies the condition specified by `f`. |
| 32 | + * The condition is recursively applied to this node and all of its children (pre-order). |
| 33 | + */ |
| 34 | + def find(p: SparkPlan)(f: SparkPlan => Boolean): Option[SparkPlan] = if (f(p)) { |
| 35 | + Some(p) |
| 36 | + } else { |
| 37 | + allChildren(p).foldLeft(Option.empty[SparkPlan]) { (l, r) => l.orElse(find(r)(f)) } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Runs the given function on this node and then recursively on children. |
| 42 | + * @param f the function to be applied to each node in the tree. |
| 43 | + */ |
| 44 | + def foreach(p: SparkPlan)(f: SparkPlan => Unit): Unit = { |
| 45 | + f(p) |
| 46 | + allChildren(p).foreach(foreach(_)(f)) |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Runs the given function recursively on children then on this node. |
| 51 | + * @param f the function to be applied to each node in the tree. |
| 52 | + */ |
| 53 | + def foreachUp(p: SparkPlan)(f: SparkPlan => Unit): Unit = { |
| 54 | + allChildren(p).foreach(foreachUp(_)(f)) |
| 55 | + f(p) |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns a Seq containing the result of applying the given function to each |
| 60 | + * node in this tree in a preorder traversal. |
| 61 | + * @param f the function to be applied. |
| 62 | + */ |
| 63 | + def map[A](p: SparkPlan)(f: SparkPlan => A): Seq[A] = { |
| 64 | + val ret = new collection.mutable.ArrayBuffer[A]() |
| 65 | + foreach(p)(ret += f(_)) |
| 66 | + ret |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a Seq by applying a function to all nodes in this tree and using the elements of the |
| 71 | + * resulting collections. |
| 72 | + */ |
| 73 | + def flatMap[A](p: SparkPlan)(f: SparkPlan => TraversableOnce[A]): Seq[A] = { |
| 74 | + val ret = new collection.mutable.ArrayBuffer[A]() |
| 75 | + foreach(p)(ret ++= f(_)) |
| 76 | + ret |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a Seq containing the result of applying a partial function to all elements in this |
| 81 | + * tree on which the function is defined. |
| 82 | + */ |
| 83 | + def collect[B](p: SparkPlan)(pf: PartialFunction[SparkPlan, B]): Seq[B] = { |
| 84 | + val ret = new collection.mutable.ArrayBuffer[B]() |
| 85 | + val lifted = pf.lift |
| 86 | + foreach(p)(node => lifted(node).foreach(ret.+=)) |
| 87 | + ret |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns a Seq containing the leaves in this tree. |
| 92 | + */ |
| 93 | + def collectLeaves(p: SparkPlan): Seq[SparkPlan] = { |
| 94 | + collect(p) { case plan if allChildren(plan).isEmpty => plan } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Finds and returns the first [[SparkPlan]] of the tree for which the given partial function |
| 99 | + * is defined (pre-order), and applies the partial function to it. |
| 100 | + */ |
| 101 | + def collectFirst[B](p: SparkPlan)(pf: PartialFunction[SparkPlan, B]): Option[B] = { |
| 102 | + val lifted = pf.lift |
| 103 | + lifted(p).orElse { |
| 104 | + allChildren(p).foldLeft(Option.empty[B]) { (l, r) => l.orElse(collectFirst(r)(pf)) } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns a sequence containing the result of applying a partial function to all elements in this |
| 110 | + * plan, also considering all the plans in its (nested) subqueries |
| 111 | + */ |
| 112 | + def collectInPlanAndSubqueries[B](p: SparkPlan)(f: PartialFunction[SparkPlan, B]): Seq[B] = { |
| 113 | + (p +: subqueriesAll(p)).flatMap(collect(_)(f)) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns a sequence containing the subqueries in this plan, also including the (nested) |
| 118 | + * subquries in its children |
| 119 | + */ |
| 120 | + def subqueriesAll(p: SparkPlan): Seq[SparkPlan] = { |
| 121 | + val subqueries = flatMap(p)(_.subqueries) |
| 122 | + subqueries ++ subqueries.flatMap(subqueriesAll) |
| 123 | + } |
| 124 | + |
| 125 | + private def allChildren(p: SparkPlan): Seq[SparkPlan] = p match { |
| 126 | + case a: AdaptiveSparkPlanExec => Seq(a.executedPlan) |
| 127 | + case s: QueryStageExec => Seq(s.plan) |
| 128 | + case _ => p.children |
| 129 | + } |
| 130 | +} |
0 commit comments