Skip to content

Commit c506925

Browse files
committed
fix: walk through sorts when pairing sort aggregate buffers
Spark puts a SortExec between each SortAggregateExec and the exchange below it. The passes that pair a buffer-consuming aggregate with the Partial that produces its buffers stopped at that sort, so a native collect_list Partial could stay native under a Spark Final or merge stage. Spark then read Comet's array buffer where it expected binary, and a distinct collect_list chain slipped past the issue #4724 guard and failed natively. findPartialAggInPlan, revertUnsafePartialAggregates and hasUnrepairedNativeBuffer now walk through SortExec and CometSortExec.
1 parent 77259ab commit c506925

3 files changed

Lines changed: 114 additions & 12 deletions

File tree

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,9 @@ case class CometExecRule(session: SparkSession)
12621262
* Inspect a failed repair's buffer path without rewriting it or materializing any stage. Report
12631263
* only a native Partial/PartialMerge whose emitted state is not known to be Spark-compatible.
12641264
* Spark Partials and completed aggregates establish new buffers, so stop there rather than
1265-
* finding an unrelated native producer below them. Only known aggregate and exchange wrappers
1266-
* forward the same buffer path; an arbitrary operator is not evidence of a mixed boundary.
1265+
* finding an unrelated native producer below them. Only known aggregate, sort and exchange
1266+
* wrappers forward the same buffer path; an arbitrary operator is not evidence of a mixed
1267+
* boundary.
12671268
*/
12681269
private def hasUnrepairedNativeBuffer(plan: SparkPlan): Boolean = plan match {
12691270
case agg: CometBaseAggregateExec if agg.aggregateExpressions.isEmpty =>
@@ -1279,6 +1280,9 @@ case class CometExecRule(session: SparkSession)
12791280
agg.aggregateExpressions.forall(e => e.mode == Partial || e.mode == PartialMerge) &&
12801281
hasUnrepairedNativeBuffer(agg.child)
12811282
case placeholder: CometSinkPlaceHolder => hasUnrepairedNativeBuffer(placeholder.child)
1283+
// A sort aggregate reads its buffers through the sort that orders them.
1284+
case sort: SortExec => hasUnrepairedNativeBuffer(sort.child)
1285+
case sort: CometSortExec => hasUnrepairedNativeBuffer(sort.child)
12821286
case read: AQEShuffleReadExec => hasUnrepairedNativeBuffer(read.child)
12831287
case stage: ShuffleQueryStageExec => hasUnrepairedNativeBuffer(stage.plan)
12841288
case reused: ReusedExchangeExec => hasUnrepairedNativeBuffer(reused.child)
@@ -1290,9 +1294,10 @@ case class CometExecRule(session: SparkSession)
12901294
/**
12911295
* The early tagging pass cannot know whether a Final's child will become native. Check the
12921296
* actual conversion result before serialization or AQE stage creation, restoring the feeding
1293-
* aggregate/exchange chain while keeping native work below its Partial. Return the repaired
1294-
* plan, or preserve an unrepairable path and record one warning on its Spark Final if an unsafe
1295-
* native producer remains. Existing stages and their buffers are never rewritten by this pass.
1297+
* aggregate/sort/exchange chain while keeping native work below its Partial. Return the
1298+
* repaired plan, or preserve an unrepairable path and record one warning on its Spark Final if
1299+
* an unsafe native producer remains. Existing stages and their buffers are never rewritten by
1300+
* this pass.
12961301
*/
12971302
private[rules] def revertUnsafePartialAggregates(plan: SparkPlan): SparkPlan = {
12981303
def revertChain(node: SparkPlan): Option[SparkPlan] = node match {
@@ -1318,6 +1323,12 @@ case class CometExecRule(session: SparkSession)
13181323
if agg.aggregateExpressions.forall(e => e.mode == Partial || e.mode == PartialMerge) =>
13191324
revertChain(agg.child).map(child => agg.withNewChildren(Seq(child)))
13201325

1326+
// A sort aggregate reads its buffers through the sort that orders them.
1327+
case sort: CometSortExec =>
1328+
revertChain(sort.child).map(child => sort.originalPlan.withNewChildren(Seq(child)))
1329+
case sort: SortExec =>
1330+
revertChain(sort.child).map(child => sort.withNewChildren(Seq(child)))
1331+
13211332
case CometSinkPlaceHolder(_, _, shuffle: CometShuffleExchangeExec) =>
13221333
revertChain(shuffle)
13231334
case shuffle: CometShuffleExchangeExec =>
@@ -1357,11 +1368,12 @@ case class CometExecRule(session: SparkSession)
13571368

13581369
/**
13591370
* Look for the bottom Partial-mode aggregate that feeds into the given plan (the child of a
1360-
* Final). Walks through exchanges and AQE stages, and continues down through intermediate
1361-
* aggregate stages whose modes are all Partial / PartialMerge - these are the PartialMerge (and
1362-
* mixed Partial/PartialMerge) stages that Spark's distinct-aggregate rewrite inserts between
1363-
* the Partial and the Final. Stops at anything else. Requires `aggregateExpressions.nonEmpty`
1364-
* so that group-by-only dedup stages are traversed rather than mistaken for the partial.
1371+
* Final). Walks through exchanges, AQE stages and the sorts that Spark inserts below sort
1372+
* aggregates, and continues down through intermediate aggregate stages whose modes are all
1373+
* Partial / PartialMerge - these are the PartialMerge (and mixed Partial/PartialMerge) stages
1374+
* that Spark's distinct-aggregate rewrite inserts between the Partial and the Final. Stops at
1375+
* anything else. Requires `aggregateExpressions.nonEmpty` so that group-by-only dedup stages
1376+
* are traversed rather than mistaken for the partial.
13651377
*/
13661378
private def findPartialAggInPlan(plan: SparkPlan): Option[BaseAggregateExec] = plan match {
13671379
case agg: BaseAggregateExec
@@ -1376,6 +1388,7 @@ case class CometExecRule(session: SparkSession)
13761388
case a: AQEShuffleReadExec => findPartialAggInPlan(a.child)
13771389
case s: ShuffleQueryStageExec => findPartialAggInPlan(s.plan)
13781390
case e: ShuffleExchangeExec => findPartialAggInPlan(e.child)
1391+
case s: SortExec => findPartialAggInPlan(s.child)
13791392
case other =>
13801393
logDebug(s"findPartialAggInPlan: stopping at ${other.nodeName}; not a known passthrough")
13811394
None

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.apache.spark.sql.catalyst.expressions.Cast
3131
import org.apache.spark.sql.catalyst.expressions.aggregate.{Final, Partial, PartialMerge}
3232
import org.apache.spark.sql.catalyst.optimizer.EliminateSorts
3333
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, RangePartitioning}
34-
import org.apache.spark.sql.comet.{CometFilterExec, CometHashAggregateExec, CometNativeExec, CometProjectExec}
34+
import org.apache.spark.sql.comet.{CometFilterExec, CometHashAggregateExec, CometNativeExec, CometProjectExec, CometSortAggregateExec}
3535
import org.apache.spark.sql.comet.execution.shuffle.CometShuffleExchangeExec
3636
import org.apache.spark.sql.execution.SQLExecution
3737
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, AdaptiveSparkPlanHelper, ShuffleQueryStageExec}
@@ -3404,6 +3404,40 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
34043404
assertSortAggregateRunsNatively("SELECT sort_array(collect_set(v)) FROM tbl")
34053405
}
34063406

3407+
// Spark puts a SortExec between each sort aggregate and the exchange below it, so the passes
3408+
// that pair a buffer-consuming aggregate with its Partial must walk through it. If they stop
3409+
// there, a native collect_list Partial stays native under a Spark consumer.
3410+
private def assertSortAggregateStaysInSpark(query: String, confs: (String, String)*): Unit = {
3411+
Seq("false", "true").foreach { aqe =>
3412+
withSQLConf(
3413+
Seq(
3414+
SQLConf.USE_OBJECT_HASH_AGG.key -> "false",
3415+
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe,
3416+
CometConf.COMET_SHUFFLE_ENABLED.key -> "true") ++ confs: _*) {
3417+
withParquetTable((0 until 8).map(i => (i % 2, i)), "tbl") {
3418+
val df = sql(query)
3419+
checkSparkAnswer(df)
3420+
val plan = df.queryExecution.executedPlan
3421+
assert(collect(plan) { case agg: SortAggregateExec => agg }.nonEmpty, plan)
3422+
assert(collect(plan) { case agg: CometSortAggregateExec => agg }.isEmpty, plan)
3423+
}
3424+
}
3425+
}
3426+
}
3427+
3428+
test("SortAggregate keeps a collect_list partial in Spark when its final cannot convert") {
3429+
assertSortAggregateStaysInSpark(
3430+
"SELECT _1, sort_array(collect_list(_2)) FROM tbl GROUP BY _1",
3431+
CometConf.COMET_ENABLE_FINAL_HASH_AGGREGATE.key -> "false")
3432+
}
3433+
3434+
test("SortAggregate runs a distinct collect_list chain in Spark") {
3435+
// The PartialMerge stages of the distinct rewrite cannot carry the collect_list buffer
3436+
// natively (issue #4724), so the whole chain falls back.
3437+
assertSortAggregateStaysInSpark(
3438+
"SELECT _1, sort_array(collect_list(_2)), count(DISTINCT _2) FROM tbl GROUP BY _1")
3439+
}
3440+
34073441
// Regression: Catalyst prunes `HashAggregateExec.resultExpressions` to
34083442
// empty for EXISTS / row-existence-only subqueries. The native HashAggregate's natural
34093443
// output (the grouping keys) then disagrees with the pruned JVM `output`, leaking through

‎spark/src/test/scala/org/apache/comet/rules/CometExecRuleSuite.scala‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import org.apache.spark.sql.comet._
3939
import org.apache.spark.sql.comet.execution.shuffle.{CometNativeShuffle, CometShuffleExchangeExec}
4040
import org.apache.spark.sql.execution._
4141
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, AQEShuffleReadExec, BroadcastQueryStageExec, LogicalQueryStage, QueryStageExec, ShuffleQueryStageExec, SimpleCost, SimpleCostEvaluator}
42-
import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec}
42+
import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec}
4343
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
4444
import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ShuffleExchangeExec}
4545
import org.apache.spark.sql.internal.SQLConf
@@ -1233,6 +1233,61 @@ class CometExecRuleSuite extends CometTestBase {
12331233
}
12341234
}
12351235

1236+
test("buffer repair walks through the sort below a sort aggregate") {
1237+
// Spark puts a SortExec between a sort aggregate and its exchange, converted or not. Repair
1238+
// must rebuild through it to restore the native Partial, and when repair stops at a stage it
1239+
// must still find the native Partial below the sort and warn.
1240+
withSQLConf(
1241+
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
1242+
SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> "false",
1243+
SQLConf.USE_OBJECT_HASH_AGG.key -> "false",
1244+
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true",
1245+
CometConf.COMET_SHUFFLE_ENABLED.key -> "true",
1246+
CometConf.COMET_SHUFFLE_MODE.key -> "native") {
1247+
withTempView("test_data") {
1248+
createTestDataFrame.createOrReplaceTempView("test_data")
1249+
val plan = applyCometExecRule(
1250+
createSparkPlan(spark, "SELECT collect_list(id) FROM test_data GROUP BY (id % 3)"))
1251+
val nativeFinal = plan
1252+
.collectFirst { case agg: CometSortAggregateExec if agg.modes == Seq(Final) => agg }
1253+
.getOrElse(fail(s"Expected a native final sort aggregate in:\n$plan"))
1254+
val partial = plan
1255+
.collectFirst { case agg: CometSortAggregateExec if agg.modes == Seq(Partial) => agg }
1256+
.getOrElse(fail(s"Expected a native partial sort aggregate in:\n$plan"))
1257+
val sparkFinal = nativeFinal.originalPlan.asInstanceOf[SortAggregateExec]
1258+
val cometSort = nativeFinal.child.asInstanceOf[CometSortExec]
1259+
val sorts: Seq[SparkPlan => SparkPlan] = Seq(
1260+
child => cometSort.copy(child = child),
1261+
child => cometSort.originalPlan.withNewChildren(Seq(child)))
1262+
val rule = CometExecRule(spark)
1263+
1264+
sorts.foreach { sortOver =>
1265+
val repaired =
1266+
rule.revertUnsafePartialAggregates(sparkFinal.copy(child = sortOver(cometSort.child)))
1267+
assert(repaired.collect { case agg: CometSortAggregateExec => agg }.isEmpty, repaired)
1268+
val sparkPartial = repaired.collectFirst {
1269+
case agg: SortAggregateExec if agg.aggregateExpressions.forall(_.mode == Partial) =>
1270+
agg
1271+
}.get
1272+
assert(sparkPartial.getTagValue(CometExecRule.COMET_UNSAFE_PARTIAL).isDefined)
1273+
1274+
val exchange = ShuffleExchangeExec(
1275+
org.apache.spark.sql.catalyst.plans.physical.SinglePartition,
1276+
partial)
1277+
val stage = ShuffleQueryStageExec(0, exchange, exchange.canonicalized)
1278+
val placeholder = CometSinkPlaceHolder(
1279+
org.apache.comet.serde.OperatorOuterClass.Operator.getDefaultInstance,
1280+
stage,
1281+
stage)
1282+
val consumer = sparkFinal.copy(child = sortOver(placeholder))
1283+
assert(rule.revertUnsafePartialAggregates(consumer) eq consumer)
1284+
val reasons = new ExtendedExplainInfo().getFallbackReasons(consumer)
1285+
assert(reasons.exists(_.contains("could not restore a native intermediate buffer")))
1286+
}
1287+
}
1288+
}
1289+
}
1290+
12361291
test("CometExecRule should not allow decimal SUM mixed execution") {
12371292
withTempView("test_data") {
12381293
createTestDataFrame.createOrReplaceTempView("test_data")

0 commit comments

Comments
 (0)