-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EMR] Support fallback whole stage according to the number of window …
…sort in stage
- Loading branch information
1 parent
c989524
commit 4610784
Showing
4 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...strait/src/main/scala/org/apache/gluten/extension/columnar/WindowSortFallbackPolicy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.extension.columnar | ||
|
||
import org.apache.gluten.GlutenConfig | ||
import org.apache.gluten.execution.{BroadcastHashJoinExecTransformerBase, SortExecTransformer, WindowExecTransformer} | ||
import org.apache.gluten.extension.columnar.heuristic.FallbackNode | ||
import org.apache.gluten.extension.columnar.transition.{ColumnarToRowLike, Transitions} | ||
|
||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution._ | ||
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, BroadcastQueryStageExec, QueryStageExec} | ||
import org.apache.spark.sql.execution.command.ExecutedCommandExec | ||
import org.apache.spark.sql.execution.exchange.Exchange | ||
|
||
/** | ||
* @param isAdaptiveContext | ||
* If is inside AQE | ||
* @param originalPlan | ||
* The vanilla SparkPlan without apply gluten transform rules | ||
*/ | ||
case class WindowSortFallbackPolicy(isAdaptiveContext: Boolean, originalPlan: SparkPlan) | ||
extends Rule[SparkPlan] { | ||
|
||
private def getWindowSortCount(plan: SparkPlan): Int = { | ||
var windowSortCount = 0 | ||
def countFallbackInternal(plan: SparkPlan): Unit = { | ||
plan match { | ||
case _: QueryStageExec => // Another stage. | ||
case _: CommandResultExec | _: ExecutedCommandExec => // ignore | ||
// we plan exchange to columnar exchange in columnar rules and the exchange does not | ||
// support columnar, so the output columnar is always false in AQE postStageCreationRules | ||
case ColumnarToRowLike(s: Exchange) if isAdaptiveContext => | ||
countFallbackInternal(s) | ||
case _ @WindowExecTransformer(_, _, _, s: SortExecTransformer) => | ||
windowSortCount += 1 | ||
countFallbackInternal(s.child) | ||
case p => p.children.foreach(countFallbackInternal) | ||
} | ||
} | ||
countFallbackInternal(plan) | ||
windowSortCount | ||
} | ||
|
||
private def hasColumnarBroadcastExchangeWithJoin(plan: SparkPlan): Boolean = { | ||
def isColumnarBroadcastExchange(p: SparkPlan): Boolean = p match { | ||
case BroadcastQueryStageExec(_, _: ColumnarBroadcastExchangeExec, _) => true | ||
case _ => false | ||
} | ||
|
||
plan.find { | ||
case j: BroadcastHashJoinExecTransformerBase | ||
if isColumnarBroadcastExchange(j.left) || | ||
isColumnarBroadcastExchange(j.right) => | ||
true | ||
case _ => false | ||
}.isDefined | ||
} | ||
|
||
private def fallback(plan: SparkPlan): FallbackInfo = { | ||
val fallbackThreshold = if (isAdaptiveContext) { | ||
GlutenConfig.getConf.wholeStageWindowSortFallbackThreshold | ||
} else if (plan.find(_.isInstanceOf[AdaptiveSparkPlanExec]).isDefined) { | ||
// if we are here, that means we are now at `QueryExecution.preparations` and | ||
// AQE is actually not applied. We do nothing for this case, and later in | ||
// AQE we can check `wholeStageFallbackThreshold`. | ||
return FallbackInfo.DO_NOT_FALLBACK() | ||
} else { | ||
// AQE is not applied, so we use the whole query threshold to check if should fallback | ||
GlutenConfig.getConf.queryFallbackThreshold | ||
} | ||
if (fallbackThreshold < 0) { | ||
return FallbackInfo.DO_NOT_FALLBACK() | ||
} | ||
|
||
// not safe to fallback row-based BHJ as the broadcast exchange is already columnar | ||
if (hasColumnarBroadcastExchangeWithJoin(plan)) { | ||
return FallbackInfo.DO_NOT_FALLBACK() | ||
} | ||
|
||
val windowSortCount = getWindowSortCount(plan) | ||
|
||
if (windowSortCount >= fallbackThreshold) { | ||
FallbackInfo( | ||
Some( | ||
s"Sort Window Fallback policy is taking effect, Sort Window count: $windowSortCount, " + | ||
s"threshold: $fallbackThreshold"), | ||
windowSortCount | ||
) | ||
} else { | ||
FallbackInfo(windowSortCount = windowSortCount) | ||
} | ||
} | ||
|
||
private def fallbackToRowBasedPlan(outputsColumnar: Boolean): SparkPlan = { | ||
val planWithTransitions = Transitions.insertTransitions(originalPlan, outputsColumnar) | ||
planWithTransitions | ||
} | ||
|
||
override def apply(plan: SparkPlan): SparkPlan = { | ||
val outputsColumnar = plan.supportsColumnar | ||
val fallbackInfo = fallback(plan) | ||
if (fallbackInfo.shouldFallback) { | ||
val vanillaSparkPlan = fallbackToRowBasedPlan(outputsColumnar) | ||
FallbackTags.addRecursively( | ||
vanillaSparkPlan, | ||
FallbackTag.Exclusive(fallbackInfo.reason.getOrElse("Unknown reason"))) | ||
FallbackNode(vanillaSparkPlan) | ||
} else { | ||
plan | ||
} | ||
} | ||
|
||
case class FallbackInfo(reason: Option[String] = None, windowSortCount: Int = 0) { | ||
def shouldFallback: Boolean = reason.isDefined | ||
} | ||
|
||
object FallbackInfo { | ||
def DO_NOT_FALLBACK(): FallbackInfo = FallbackInfo() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters