Skip to content

Commit 73c727c

Browse files
committed
fix: put a converted shuffle back on the JVM columnar shuffle when its stage is reverted
RevertNativeForTransitionHeavyStages strips CometSparkToColumnarExec with the rest of a reverted stage, which left a native shuffle reading the batches of Spark's RowToColumnarExec, and the task failed casting them to CometVector. The shuffle now goes back to the JVM columnar shuffle that the conversion replaced, which reads the stage's rows.
1 parent 16bdaf0 commit 73c727c

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎spark/src/main/scala/org/apache/comet/rules/RevertNativeForTransitionHeavyStages.scala‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.apache.spark.sql.SparkSession
2424
import org.apache.spark.sql.catalyst.expressions.aggregate.{Final, Partial, PartialMerge}
2525
import org.apache.spark.sql.catalyst.rules.Rule
2626
import org.apache.spark.sql.comet.{CometColumnarToRowExec, CometExec, CometHashAggregateExec, CometIcebergNativeScanExec, CometLocalTopKExec, CometNativeColumnarToRowExec, CometNativeScanExec, CometSparkToColumnarExec}
27+
import org.apache.spark.sql.comet.execution.shuffle.{CometColumnarShuffle, CometNativeShuffle, CometShuffleExchangeExec}
2728
import org.apache.spark.sql.execution.{ColumnarToRowExec, ColumnarToRowTransition, RowToColumnarExec, SparkPlan}
2829
import org.apache.spark.sql.execution.adaptive.QueryStageExec
2930
import org.apache.spark.sql.execution.exchange.{BroadcastExchangeLike, ShuffleExchangeLike}
@@ -62,9 +63,7 @@ case class RevertNativeForTransitionHeavyStages(session: SparkSession, wholePlan
6263
plan match {
6364
case _: BroadcastExchangeLike => plan
6465
case exchange: ShuffleExchangeLike =>
65-
revertStageIfNeeded(exchange.child, exchange.supportsColumnar)
66-
.map(reverted => exchange.withNewChildren(Seq(reverted)))
67-
.getOrElse(plan)
66+
revertShuffleStageIfNeeded(exchange).getOrElse(plan)
6867
case _ =>
6968
// Result stage: its output is collected as rows, so no consumer requires columnar input
7069
// and the reverted stage needs no trailing R2C.
@@ -74,14 +73,31 @@ case class RevertNativeForTransitionHeavyStages(session: SparkSession, wholePlan
7473

7574
private def applyForNonAQE(plan: SparkPlan): SparkPlan = {
7675
val withRevertedStages = plan.transformUp { case exchange: ShuffleExchangeLike =>
77-
revertStageIfNeeded(exchange.child, exchange.supportsColumnar)
78-
.map(reverted => exchange.withNewChildren(Seq(reverted)))
79-
.getOrElse(exchange)
76+
revertShuffleStageIfNeeded(exchange).getOrElse(exchange)
8077
}
8178
revertStageIfNeeded(withRevertedStages, outputColumnar = false)
8279
.getOrElse(withRevertedStages)
8380
}
8481

82+
/**
83+
* Reverts the stage below a shuffle if needed. A native shuffle over rows that
84+
* `spark.comet.convert.shuffleInput.enabled` converted loses the conversion with the rest of
85+
* the stage, so it goes back to the JVM columnar shuffle that the conversion replaced, which
86+
* reads the stage's rows.
87+
*/
88+
private def revertShuffleStageIfNeeded(exchange: ShuffleExchangeLike): Option[SparkPlan] =
89+
revertStageIfNeeded(exchange.child, exchange.supportsColumnar).map { reverted =>
90+
exchange match {
91+
case s: CometShuffleExchangeExec
92+
if s.shuffleType == CometNativeShuffle &&
93+
s.child.isInstanceOf[CometSparkToColumnarExec] =>
94+
val columnar = s.copy(child = reverted, shuffleType = CometColumnarShuffle)
95+
columnar.copyTagsFrom(s)
96+
columnar
97+
case _ => exchange.withNewChildren(Seq(reverted))
98+
}
99+
}
100+
85101
/**
86102
* Reverts the stage if C2R count exceeds threshold. Wraps in R2C if exchange needs columnar.
87103
*/

‎spark/src/test/scala/org/apache/comet/exec/CometShuffleInputConversionSuite.scala‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.apache.comet.exec
2121

2222
import org.apache.spark.sql.{CometTestBase, DataFrame, Row}
23-
import org.apache.spark.sql.comet.CometSparkToColumnarExec
23+
import org.apache.spark.sql.comet.{CometPlan, CometSparkToColumnarExec}
2424
import org.apache.spark.sql.comet.execution.shuffle.{CometColumnarShuffle, CometNativeShuffle, CometShuffleExchangeExec}
2525
import org.apache.spark.sql.execution.{ColumnarToRowExec, ColumnarToRowTransition, SparkPlan}
2626
import org.apache.spark.sql.functions.{array, avg, col, count, hash, length, max, min, size, sum}
@@ -184,6 +184,31 @@ class CometShuffleInputConversionSuite extends CometTestBase {
184184
}
185185
}
186186

187+
convertTest("a stage that the transition revert puts back on Spark keeps the JVM shuffle") {
188+
// The typed operation reads the Comet scan through a transition. With no transitions allowed,
189+
// the revert puts the stage below the shuffle back on Spark, the conversion included, and the
190+
// shuffle goes back to the JVM columnar shuffle, which reads the stage's rows.
191+
withParquetTable((0 until 200).map(i => (i, (i % 13).toString)), "tbl") {
192+
Seq("true", "false").foreach { aqe =>
193+
withSQLConf(
194+
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe,
195+
CometConf.COMET_EXEC_TRANSITION_REVERT_ENABLED.key -> "true",
196+
CometConf.COMET_EXEC_TRANSITION_REVERT_MAX_TRANSITIONS.key -> "0") {
197+
val ds = spark.sql("SELECT _1 AS a, _2 AS b FROM tbl").as[ShuffleInputRec]
198+
val (_, plan) = checkSparkAnswer(
199+
ds.map(r => ShuffleInputRec(r.a % 13, r.b))
200+
.repartition(7, col("a"))
201+
.groupBy("a")
202+
.agg(max("b")))
203+
assert(conversions(plan).isEmpty, s"AQE $aqe:\n$plan")
204+
val reverted = cometShuffles(plan).filter(_.shuffleType == CometColumnarShuffle)
205+
assert(reverted.length == 1, s"AQE $aqe:\n$plan")
206+
assert(!reverted.head.child.exists(_.isInstanceOf[CometPlan]), s"AQE $aqe:\n$plan")
207+
}
208+
}
209+
}
210+
}
211+
187212
convertTest("a join with an input that stays on the JVM columnar shuffle") {
188213
// The left input converts and the right one, with its array<int> column, does not. Native
189214
// shuffle hashes an int key as Spark does, so matching keys still meet.

0 commit comments

Comments
 (0)