|
| 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.streaming |
| 19 | + |
| 20 | +import java.util.UUID |
| 21 | + |
| 22 | +import org.apache.spark.rdd.RDD |
| 23 | +import org.apache.spark.sql.catalyst.InternalRow |
| 24 | +import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute |
| 25 | +import org.apache.spark.sql.catalyst.expressions.Attribute |
| 26 | +import org.apache.spark.sql.catalyst.plans.physical._ |
| 27 | +import org.apache.spark.sql.execution.{SparkPlan, SparkPlanTest, UnaryExecNode} |
| 28 | +import org.apache.spark.sql.execution.exchange.{Exchange, ShuffleExchange} |
| 29 | +import org.apache.spark.sql.execution.streaming.{IncrementalExecution, OffsetSeqMetadata, StatefulOperator, StatefulOperatorStateInfo} |
| 30 | +import org.apache.spark.sql.test.SharedSQLContext |
| 31 | + |
| 32 | +class EnsureStatefulOpPartitioningSuite extends SparkPlanTest with SharedSQLContext { |
| 33 | + |
| 34 | + import testImplicits._ |
| 35 | + super.beforeAll() |
| 36 | + |
| 37 | + private val baseDf = Seq((1, "A"), (2, "b")).toDF("num", "char") |
| 38 | + |
| 39 | + testEnsureStatefulOpPartitioning( |
| 40 | + "ClusteredDistribution generates Exchange with HashPartitioning", |
| 41 | + baseDf.queryExecution.sparkPlan, |
| 42 | + requiredDistribution = keys => ClusteredDistribution(keys), |
| 43 | + expectedPartitioning = |
| 44 | + keys => HashPartitioning(keys, spark.sessionState.conf.numShufflePartitions), |
| 45 | + expectShuffle = true) |
| 46 | + |
| 47 | + testEnsureStatefulOpPartitioning( |
| 48 | + "ClusteredDistribution with coalesce(1) generates Exchange with HashPartitioning", |
| 49 | + baseDf.coalesce(1).queryExecution.sparkPlan, |
| 50 | + requiredDistribution = keys => ClusteredDistribution(keys), |
| 51 | + expectedPartitioning = |
| 52 | + keys => HashPartitioning(keys, spark.sessionState.conf.numShufflePartitions), |
| 53 | + expectShuffle = true) |
| 54 | + |
| 55 | + testEnsureStatefulOpPartitioning( |
| 56 | + "AllTuples generates Exchange with SinglePartition", |
| 57 | + baseDf.queryExecution.sparkPlan, |
| 58 | + requiredDistribution = _ => AllTuples, |
| 59 | + expectedPartitioning = _ => SinglePartition, |
| 60 | + expectShuffle = true) |
| 61 | + |
| 62 | + testEnsureStatefulOpPartitioning( |
| 63 | + "AllTuples with coalesce(1) doesn't need Exchange", |
| 64 | + baseDf.coalesce(1).queryExecution.sparkPlan, |
| 65 | + requiredDistribution = _ => AllTuples, |
| 66 | + expectedPartitioning = _ => SinglePartition, |
| 67 | + expectShuffle = false) |
| 68 | + |
| 69 | + /** |
| 70 | + * For `StatefulOperator` with the given `requiredChildDistribution`, and child SparkPlan |
| 71 | + * `inputPlan`, ensures that the incremental planner adds exchanges, if required, in order to |
| 72 | + * ensure the expected partitioning. |
| 73 | + */ |
| 74 | + private def testEnsureStatefulOpPartitioning( |
| 75 | + testName: String, |
| 76 | + inputPlan: SparkPlan, |
| 77 | + requiredDistribution: Seq[Attribute] => Distribution, |
| 78 | + expectedPartitioning: Seq[Attribute] => Partitioning, |
| 79 | + expectShuffle: Boolean): Unit = { |
| 80 | + test(testName) { |
| 81 | + val operator = TestStatefulOperator(inputPlan, requiredDistribution(inputPlan.output.take(1))) |
| 82 | + val executed = executePlan(operator, OutputMode.Complete()) |
| 83 | + if (expectShuffle) { |
| 84 | + val exchange = executed.children.find(_.isInstanceOf[Exchange]) |
| 85 | + if (exchange.isEmpty) { |
| 86 | + fail(s"Was expecting an exchange but didn't get one in:\n$executed") |
| 87 | + } |
| 88 | + assert(exchange.get === |
| 89 | + ShuffleExchange(expectedPartitioning(inputPlan.output.take(1)), inputPlan), |
| 90 | + s"Exchange didn't have expected properties:\n${exchange.get}") |
| 91 | + } else { |
| 92 | + assert(!executed.children.exists(_.isInstanceOf[Exchange]), |
| 93 | + s"Unexpected exchange found in:\n$executed") |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** Executes a SparkPlan using the IncrementalPlanner used for Structured Streaming. */ |
| 99 | + private def executePlan( |
| 100 | + p: SparkPlan, |
| 101 | + outputMode: OutputMode = OutputMode.Append()): SparkPlan = { |
| 102 | + val execution = new IncrementalExecution( |
| 103 | + spark, |
| 104 | + null, |
| 105 | + OutputMode.Complete(), |
| 106 | + "chk", |
| 107 | + UUID.randomUUID(), |
| 108 | + 0L, |
| 109 | + OffsetSeqMetadata()) { |
| 110 | + override lazy val sparkPlan: SparkPlan = p transform { |
| 111 | + case plan: SparkPlan => |
| 112 | + val inputMap = plan.children.flatMap(_.output).map(a => (a.name, a)).toMap |
| 113 | + plan transformExpressions { |
| 114 | + case UnresolvedAttribute(Seq(u)) => |
| 115 | + inputMap.getOrElse(u, |
| 116 | + sys.error(s"Invalid Test: Cannot resolve $u given input $inputMap")) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + execution.executedPlan |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** Used to emulate a `StatefulOperator` with the given requiredDistribution. */ |
| 125 | +case class TestStatefulOperator( |
| 126 | + child: SparkPlan, |
| 127 | + requiredDist: Distribution) extends UnaryExecNode with StatefulOperator { |
| 128 | + override def output: Seq[Attribute] = child.output |
| 129 | + override def doExecute(): RDD[InternalRow] = child.execute() |
| 130 | + override def requiredChildDistribution: Seq[Distribution] = requiredDist :: Nil |
| 131 | + override def stateInfo: Option[StatefulOperatorStateInfo] = None |
| 132 | +} |
0 commit comments