|
| 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.SparkFunSuite |
| 21 | +import org.apache.spark.sql.{SparkSession, SparkSessionExtensionsProvider} |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan |
| 23 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 24 | +import org.apache.spark.sql.execution.{ColumnarRule, RangeExec, SparkPlan, SparkStrategy} |
| 25 | +import org.apache.spark.sql.execution.aggregate.HashAggregateExec |
| 26 | +import org.apache.spark.sql.execution.exchange.ShuffleExchangeExec |
| 27 | + |
| 28 | +class AdaptiveRuleContextSuite extends SparkFunSuite with AdaptiveSparkPlanHelper { |
| 29 | + |
| 30 | + private def stop(spark: SparkSession): Unit = { |
| 31 | + spark.stop() |
| 32 | + SparkSession.clearActiveSession() |
| 33 | + SparkSession.clearDefaultSession() |
| 34 | + } |
| 35 | + |
| 36 | + private def withSession( |
| 37 | + builders: Seq[SparkSessionExtensionsProvider])(f: SparkSession => Unit): Unit = { |
| 38 | + val builder = SparkSession.builder().master("local[1]") |
| 39 | + builders.foreach(builder.withExtensions) |
| 40 | + val spark = builder.getOrCreate() |
| 41 | + try f(spark) finally { |
| 42 | + stop(spark) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + test("test adaptive rule context") { |
| 47 | + withSession( |
| 48 | + Seq(_.injectRuntimeOptimizerRule(_ => MyRuleContextForRuntimeOptimization), |
| 49 | + _.injectPlannerStrategy(_ => MyRuleContextForPlannerStrategy), |
| 50 | + _.injectQueryPostPlannerStrategyRule(_ => MyRuleContextForPostPlannerStrategyRule), |
| 51 | + _.injectQueryStagePrepRule(_ => MyRuleContextForPreQueryStageRule), |
| 52 | + _.injectQueryStageOptimizerRule(_ => MyRuleContextForQueryStageRule), |
| 53 | + _.injectColumnar(_ => MyRuleContextForColumnarRule))) { spark => |
| 54 | + val df = spark.range(1, 10, 1, 3).selectExpr("id % 3 as c").groupBy("c").count() |
| 55 | + df.collect() |
| 56 | + assert(collectFirst(df.queryExecution.executedPlan) { |
| 57 | + case s: ShuffleExchangeExec if s.numPartitions == 2 => s |
| 58 | + }.isDefined) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + test("test adaptive rule context with subquery") { |
| 63 | + withSession( |
| 64 | + Seq(_.injectQueryStagePrepRule(_ => MyRuleContextForQueryStageWithSubquery))) { spark => |
| 65 | + spark.sql("select (select count(*) from range(10)), id from range(10)").collect() |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +object MyRuleContext { |
| 71 | + def checkAndGetRuleContext(): AdaptiveRuleContext = { |
| 72 | + val ruleContextOpt = AdaptiveRuleContext.get() |
| 73 | + assert(ruleContextOpt.isDefined) |
| 74 | + ruleContextOpt.get |
| 75 | + } |
| 76 | + |
| 77 | + def checkRuleContextForQueryStage(plan: SparkPlan): SparkPlan = { |
| 78 | + val ruleContext = checkAndGetRuleContext() |
| 79 | + assert(!ruleContext.isSubquery) |
| 80 | + val stage = plan.find(_.isInstanceOf[ShuffleQueryStageExec]) |
| 81 | + if (stage.isDefined && stage.get.asInstanceOf[ShuffleQueryStageExec].isMaterialized) { |
| 82 | + assert(ruleContext.isFinalStage) |
| 83 | + assert(!ruleContext.configs().get("spark.sql.shuffle.partitions").contains("2")) |
| 84 | + } else { |
| 85 | + assert(!ruleContext.isFinalStage) |
| 86 | + assert(ruleContext.configs().get("spark.sql.shuffle.partitions").contains("2")) |
| 87 | + } |
| 88 | + plan |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +object MyRuleContextForRuntimeOptimization extends Rule[LogicalPlan] { |
| 93 | + override def apply(plan: LogicalPlan): LogicalPlan = { |
| 94 | + MyRuleContext.checkAndGetRuleContext() |
| 95 | + plan |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +object MyRuleContextForPlannerStrategy extends SparkStrategy { |
| 100 | + override def apply(plan: LogicalPlan): Seq[SparkPlan] = { |
| 101 | + plan match { |
| 102 | + case _: LogicalQueryStage => |
| 103 | + val ruleContext = MyRuleContext.checkAndGetRuleContext() |
| 104 | + assert(!ruleContext.configs().get("spark.sql.shuffle.partitions").contains("2")) |
| 105 | + Nil |
| 106 | + case _ => Nil |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +object MyRuleContextForPostPlannerStrategyRule extends Rule[SparkPlan] { |
| 112 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 113 | + val ruleContext = MyRuleContext.checkAndGetRuleContext() |
| 114 | + if (plan.find(_.isInstanceOf[RangeExec]).isDefined) { |
| 115 | + ruleContext.setConfig("spark.sql.shuffle.partitions", "2") |
| 116 | + } |
| 117 | + plan |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +object MyRuleContextForPreQueryStageRule extends Rule[SparkPlan] { |
| 122 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 123 | + val ruleContext = MyRuleContext.checkAndGetRuleContext() |
| 124 | + assert(!ruleContext.isFinalStage) |
| 125 | + plan |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +object MyRuleContextForQueryStageRule extends Rule[SparkPlan] { |
| 130 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 131 | + MyRuleContext.checkRuleContextForQueryStage(plan) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +object MyRuleContextForColumnarRule extends ColumnarRule { |
| 136 | + override def preColumnarTransitions: Rule[SparkPlan] = { |
| 137 | + plan: SparkPlan => { |
| 138 | + if (plan.isInstanceOf[AdaptiveSparkPlanExec]) { |
| 139 | + // skip if we are not inside AQE |
| 140 | + assert(AdaptiveRuleContext.get().isEmpty) |
| 141 | + plan |
| 142 | + } else { |
| 143 | + MyRuleContext.checkRuleContextForQueryStage(plan) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + override def postColumnarTransitions: Rule[SparkPlan] = { |
| 149 | + plan: SparkPlan => { |
| 150 | + if (plan.isInstanceOf[AdaptiveSparkPlanExec]) { |
| 151 | + // skip if we are not inside AQE |
| 152 | + assert(AdaptiveRuleContext.get().isEmpty) |
| 153 | + plan |
| 154 | + } else { |
| 155 | + MyRuleContext.checkRuleContextForQueryStage(plan) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +object MyRuleContextForQueryStageWithSubquery extends Rule[SparkPlan] { |
| 162 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 163 | + val ruleContext = MyRuleContext.checkAndGetRuleContext() |
| 164 | + if (plan.exists(_.isInstanceOf[HashAggregateExec])) { |
| 165 | + assert(ruleContext.isSubquery) |
| 166 | + if (plan.exists(_.isInstanceOf[RangeExec])) { |
| 167 | + assert(!ruleContext.isFinalStage) |
| 168 | + } else { |
| 169 | + assert(ruleContext.isFinalStage) |
| 170 | + } |
| 171 | + } else { |
| 172 | + assert(!ruleContext.isSubquery) |
| 173 | + } |
| 174 | + plan |
| 175 | + } |
| 176 | +} |
0 commit comments