Skip to content

Commit f1e5868

Browse files
committed
fix: distinguish aggregate buffer compatibility by direction
1 parent cfa3113 commit f1e5868

7 files changed

Lines changed: 127 additions & 45 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ case class CometExecRule(session: SparkSession)
974974
// PartialMerge stages of a distinct-aggregate rewrite. See issues #1389 and #4813.
975975
val modes = agg.aggregateExpressions.map(_.mode).distinct
976976
if (modes == Seq(Final) &&
977-
!QueryPlanSerde.allAggsSupportMixedExecution(agg.aggregateExpressions) &&
977+
!QueryPlanSerde.allAggsSupportNativePartialToSparkFinal(agg.aggregateExpressions) &&
978978
!canAggregateBeConverted(agg, Final)) {
979979
findPartialAggInPlan(agg.child).foreach { partial =>
980980
// Only tag if the Partial would otherwise have been converted. If the Partial itself
@@ -1064,7 +1064,7 @@ case class CometExecRule(session: SparkSession)
10641064
plan.transformUp {
10651065
case agg: BaseAggregateExec
10661066
if agg.aggregateExpressions.map(_.mode).distinct == Seq(Final) &&
1067-
!QueryPlanSerde.allAggsSupportMixedExecution(agg.aggregateExpressions) =>
1067+
!QueryPlanSerde.allAggsSupportNativePartialToSparkFinal(agg.aggregateExpressions) =>
10681068
revertChain(agg.child)
10691069
// Rebuild native consumers and shuffles from their original Spark operators. Merely
10701070
// replacing their children would leave a native protobuf reading the old buffers.

‎spark/src/main/scala/org/apache/comet/serde/CometAggregateExpressionSerde.scala‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,23 @@ trait CometAggregateExpressionSerde[T <: AggregateFunction] {
8282
def getSupportLevel(expr: T): SupportLevel = Compatible(None)
8383

8484
/**
85-
* Whether this aggregate's intermediate buffer format is compatible between Spark and Comet for
86-
* the given function instance, making it safe to run the Partial in one engine and the Final in
87-
* the other. Aggregates with simple single-value buffers (MIN, MAX, bitwise) are always safe;
88-
* SUM and non-decimal AVG match Spark's buffer and are safe except where noted per instance
89-
* (e.g. TRY-mode SUM uses a Comet-internal flag column). COUNT is intentionally excluded
90-
* despite a matching buffer: mixed COUNT partial/final regressed AQE's
85+
* Whether a Comet aggregate can consume this function's Spark intermediate buffer. This covers
86+
* Spark Partial to Comet Final, including intermediate PartialMerge stages. COUNT is excluded
87+
* despite a matching buffer: a Comet Final above a Spark Partial regressed AQE's
9188
* PropagateEmptyRelationAfterAQE pattern (which matches BaseAggregateExec only) and the Spark
9289
* 4.0 count-bug decorrelation for correlated IN subqueries.
9390
*/
94-
def supportsMixedPartialFinal(fn: T): Boolean = false
91+
def supportsSparkPartialToNativeFinal(fn: T): Boolean = false
92+
93+
/**
94+
* Whether Spark can consume this function's Comet intermediate buffer. Keep this separate from
95+
* the reverse direction: planner restrictions on a Comet Final do not necessarily prohibit a
96+
* Comet Partial feeding a Spark Final. Existing bidirectional implementations share the
97+
* default; handlers may admit an additional forward direction only after validating it
98+
* independently.
99+
*/
100+
def supportsNativePartialToSparkFinal(fn: T): Boolean =
101+
supportsSparkPartialToNativeFinal(fn)
95102

96103
/**
97104
* Convert a Spark expression into a protocol buffer representation that can be passed into

‎spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -421,31 +421,35 @@ object QueryPlanSerde extends Logging with CometExprShim with CometTypeShim {
421421
classOf[VarianceSamp] -> CometVarianceSamp)
422422

423423
/**
424-
* Returns true if all aggregate expressions in the list have intermediate buffer formats that
425-
* are compatible between Spark and Comet, making it safe to run Partial in one engine and Final
426-
* in the other.
424+
* Returns true if Spark can consume all the intermediate buffers produced by Comet. Used when a
425+
* Spark Final would otherwise consume a native Partial, including after shuffle fallback.
427426
*/
428-
def allAggsSupportMixedExecution(aggExprs: Seq[AggregateExpression]): Boolean = {
429-
aggExprs.forall(aggExpr => supportsMixedExecution(aggExpr.aggregateFunction))
427+
def allAggsSupportNativePartialToSparkFinal(aggExprs: Seq[AggregateExpression]): Boolean = {
428+
aggExprs.forall { aggExpr =>
429+
val fn = aggExpr.aggregateFunction
430+
aggrSerdeMap.get(fn.getClass).exists { handler =>
431+
handler
432+
.asInstanceOf[CometAggregateExpressionSerde[AggregateFunction]]
433+
.supportsNativePartialToSparkFinal(fn)
434+
}
435+
}
430436
}
431437

432438
/**
433-
* Returns the aggregate functions in the list whose intermediate buffer formats are not known
434-
* to be compatible between Spark and Comet. These are the functions that prevent a Spark Final
435-
* aggregate (without a Comet Partial) from running, since the buffer produced by one engine
436-
* cannot be safely consumed by the other.
439+
* Returns functions whose Spark intermediate buffers cannot safely be consumed by a Comet Final
440+
* or PartialMerge. This is independent of native Partial to Spark Final compatibility.
437441
*/
438-
def aggsNotSupportingMixedExecution(
442+
def aggsNotSupportingSparkPartialToNativeFinal(
439443
aggExprs: Seq[AggregateExpression]): Seq[AggregateFunction] = {
440-
aggExprs.map(_.aggregateFunction).filterNot(supportsMixedExecution)
444+
aggExprs.map(_.aggregateFunction).filterNot(supportsSparkPartialToNativeFinal)
441445
}
442446

443-
private def supportsMixedExecution(fn: AggregateFunction): Boolean = {
447+
private def supportsSparkPartialToNativeFinal(fn: AggregateFunction): Boolean = {
444448
aggrSerdeMap.get(fn.getClass) match {
445449
case Some(handler) =>
446450
handler
447451
.asInstanceOf[CometAggregateExpressionSerde[AggregateFunction]]
448-
.supportsMixedPartialFinal(fn)
452+
.supportsSparkPartialToNativeFinal(fn)
449453
case None => false
450454
}
451455
}

‎spark/src/main/scala/org/apache/comet/serde/aggregates.scala‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import org.apache.comet.shims.{CometCollectShim, CometEvalModeUtil}
3535

3636
object CometMin extends CometAggregateExpressionSerde[Min] {
3737

38-
override def supportsMixedPartialFinal(fn: Min): Boolean = true
38+
override def supportsSparkPartialToNativeFinal(fn: Min): Boolean = true
3939

4040
override def getSupportLevel(expr: Min): SupportLevel =
4141
AggSerde.minMaxSupportLevel(expr.dataType)
@@ -71,7 +71,7 @@ object CometMin extends CometAggregateExpressionSerde[Min] {
7171

7272
object CometMax extends CometAggregateExpressionSerde[Max] {
7373

74-
override def supportsMixedPartialFinal(fn: Max): Boolean = true
74+
override def supportsSparkPartialToNativeFinal(fn: Max): Boolean = true
7575

7676
override def getSupportLevel(expr: Max): SupportLevel =
7777
AggSerde.minMaxSupportLevel(expr.dataType)
@@ -106,6 +106,10 @@ object CometMax extends CometAggregateExpressionSerde[Max] {
106106
}
107107

108108
object CometCount extends CometAggregateExpressionSerde[Count] {
109+
// Both buffers are a single non-null Long. The AQE/count-bug restrictions documented on the
110+
// reverse direction concern a Comet Final; retaining Spark's Final preserves those rewrites.
111+
override def supportsNativePartialToSparkFinal(fn: Count): Boolean = true
112+
109113
override def convert(
110114
aggExpr: AggregateExpression,
111115
expr: Count,
@@ -129,7 +133,7 @@ object CometCount extends CometAggregateExpressionSerde[Count] {
129133

130134
object CometAverage extends CometAggregateExpressionSerde[Average] {
131135

132-
override def supportsMixedPartialFinal(fn: Average): Boolean =
136+
override def supportsSparkPartialToNativeFinal(fn: Average): Boolean =
133137
// Non-decimal AVG has a (sum: double, count: long) buffer matching Spark. Decimal AVG is
134138
// deferred (overflow nulls count differently) and stays unsafe for mixed execution.
135139
!fn.child.dataType.isInstanceOf[DecimalType]
@@ -189,7 +193,7 @@ object CometAverage extends CometAggregateExpressionSerde[Average] {
189193

190194
object CometSum extends CometAggregateExpressionSerde[Sum] {
191195

192-
override def supportsMixedPartialFinal(fn: Sum): Boolean =
196+
override def supportsSparkPartialToNativeFinal(fn: Sum): Boolean =
193197
// Decimal SUM is excluded: overflow detection (ANSI throw / Legacy null) does not survive a
194198
// Spark-partial / Comet-final split, so the required ArithmeticException is never raised.
195199
// TRY-mode integer SUM carries a Comet-internal has_all_nulls column that Spark cannot read.
@@ -306,7 +310,7 @@ object CometLast extends CometAggregateExpressionSerde[Last] {
306310
}
307311

308312
object CometBitAndAgg extends CometAggregateExpressionSerde[BitAndAgg] {
309-
override def supportsMixedPartialFinal(fn: BitAndAgg): Boolean = true
313+
override def supportsSparkPartialToNativeFinal(fn: BitAndAgg): Boolean = true
310314

311315
override def getSupportLevel(expr: BitAndAgg): SupportLevel =
312316
if (AggSerde.bitwiseAggTypeSupported(expr.dataType)) {
@@ -344,7 +348,7 @@ object CometBitAndAgg extends CometAggregateExpressionSerde[BitAndAgg] {
344348
}
345349

346350
object CometBitOrAgg extends CometAggregateExpressionSerde[BitOrAgg] {
347-
override def supportsMixedPartialFinal(fn: BitOrAgg): Boolean = true
351+
override def supportsSparkPartialToNativeFinal(fn: BitOrAgg): Boolean = true
348352

349353
override def getSupportLevel(expr: BitOrAgg): SupportLevel =
350354
if (AggSerde.bitwiseAggTypeSupported(expr.dataType)) {
@@ -382,7 +386,7 @@ object CometBitOrAgg extends CometAggregateExpressionSerde[BitOrAgg] {
382386
}
383387

384388
object CometBitXOrAgg extends CometAggregateExpressionSerde[BitXorAgg] {
385-
override def supportsMixedPartialFinal(fn: BitXorAgg): Boolean = true
389+
override def supportsSparkPartialToNativeFinal(fn: BitXorAgg): Boolean = true
386390

387391
override def getSupportLevel(expr: BitXorAgg): SupportLevel =
388392
if (AggSerde.bitwiseAggTypeSupported(expr.dataType)) {
@@ -765,7 +769,7 @@ object CometCorr extends CometAggregateExpressionSerde[Corr] {
765769

766770
object CometBloomFilterAggregate extends CometAggregateExpressionSerde[BloomFilterAggregate] {
767771

768-
override def supportsMixedPartialFinal(fn: BloomFilterAggregate): Boolean = true
772+
override def supportsSparkPartialToNativeFinal(fn: BloomFilterAggregate): Boolean = true
769773

770774
override def getSupportLevel(expr: BloomFilterAggregate): SupportLevel =
771775
expr.child.dataType match {
@@ -934,7 +938,7 @@ object CometApproxCountDistinct extends CometAggregateExpressionSerde[HyperLogLo
934938
// The register buffer uses Spark's identical packed-`Long` layout (`numWords` `Long` columns),
935939
// matching Spark's `aggBufferSchema`, so a Comet partial and Spark final (or the reverse) can
936940
// be mixed in one plan.
937-
override def supportsMixedPartialFinal(fn: HyperLogLogPlusPlus): Boolean = true
941+
override def supportsSparkPartialToNativeFinal(fn: HyperLogLogPlusPlus): Boolean = true
938942

939943
// Types that Comet's native `xxhash64` hashes identically to Spark's `XxHash64Function`.
940944
// `StringType` here is the default UTF8_BINARY collation; a collated `StringType(collationId)`

‎spark/src/main/scala/org/apache/spark/sql/comet/operators.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ trait CometBaseAggregate {
16561656

16571657
if (missingCometProducer) {
16581658
val incompatibleAggs =
1659-
QueryPlanSerde.aggsNotSupportingMixedExecution(aggregate.aggregateExpressions)
1659+
QueryPlanSerde.aggsNotSupportingSparkPartialToNativeFinal(aggregate.aggregateExpressions)
16601660
if (incompatibleAggs.nonEmpty) {
16611661
val names = incompatibleAggs.map(_.prettyName).distinct.sorted.mkString(", ")
16621662
withFallbackReason(

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

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,78 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
368368
}
369369
}
370370

371+
for (adaptive <- Seq(false, true)) {
372+
test(s"COUNT preserves safe native partials across a Spark shuffle (AQE=$adaptive)") {
373+
val data = Seq((0, None), (0, None), (1, Some(3)), (1, None), (1, Some(4)))
374+
withParquetTable(data, "count_fallback", false) {
375+
for (finalEnabled <- Seq(false, true)) {
376+
withSQLConf(
377+
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> adaptive.toString,
378+
SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key -> "false",
379+
CometConf.COMET_SHUFFLE_ENABLED.key -> "false",
380+
CometConf.COMET_ENABLE_FINAL_HASH_AGGREGATE.key -> finalEnabled.toString) {
381+
for (query <- Seq(
382+
"SELECT _1, COUNT(_2), COUNT(*) FROM count_fallback GROUP BY _1",
383+
"SELECT COUNT(_2), COUNT(*) FROM count_fallback WHERE _1 = 0",
384+
"SELECT COUNT(_2), COUNT(*) FROM count_fallback WHERE _1 < 0")) {
385+
val df = sql(query)
386+
val initialPlan = stripAQEPlan(df.queryExecution.executedPlan)
387+
assert(collect(initialPlan) {
388+
case agg: CometHashAggregateExec if agg.modes == Seq(Partial) => agg
389+
}.size == 1)
390+
assert(collect(initialPlan) {
391+
case agg: BaseAggregateExec
392+
if agg.aggregateExpressions.map(_.mode).distinct == Seq(Final) =>
393+
agg
394+
}.size == 1)
395+
checkSparkAnswer(df)
396+
}
397+
}
398+
}
399+
}
400+
}
401+
402+
for (fn <- Seq("collect_list", "collect_set")) {
403+
test(s"$fn falls back when enabled native shuffle is ineligible (AQE=$adaptive)") {
404+
val data = (0 until 30).map(i => (i % 3, if (i % 7 == 0) None else Some(i % 5)))
405+
withParquetTable(data, "collect_fallback", false) {
406+
withSQLConf(
407+
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> adaptive.toString,
408+
SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key -> "false",
409+
SQLConf.USE_OBJECT_HASH_AGG.key -> "true",
410+
CometConf.COMET_SHUFFLE_ENABLED.key -> "true",
411+
CometConf.COMET_SHUFFLE_MODE.key -> "native",
412+
CometConf.COMET_SHUFFLE_NATIVE_HASH_PARTITIONING_ENABLED.key -> "false") {
413+
// Integer keys isolate this from the wide-decimal shuffle restriction in #5420.
414+
// The native Partial emits an Array buffer, but Spark's Final expects Binary.
415+
val query = s"SELECT _1, sort_array($fn(_2)), COUNT(*) " +
416+
"FROM collect_fallback WHERE _1 >= 0 GROUP BY _1"
417+
val df = sql(query)
418+
val initialPlan = stripAQEPlan(df.queryExecution.executedPlan)
419+
checkSparkAnswer(df)
420+
for (plan <- Seq(initialPlan, df.queryExecution.executedPlan)) {
421+
assert(collect(plan) { case agg: CometHashAggregateExec => agg }.isEmpty)
422+
val partials = collect(plan) {
423+
case agg: BaseAggregateExec
424+
if agg.aggregateExpressions.map(_.mode).distinct == Seq(Partial) =>
425+
agg
426+
}
427+
assert(partials.size == 1)
428+
assert(partials.head.getTagValue(CometExecRule.COMET_UNSAFE_PARTIAL).isDefined)
429+
assert(collect(plan) { case filter: CometFilterExec => filter }.nonEmpty)
430+
}
431+
// A fully native producer/consumer pair can still use its native buffer format.
432+
withSQLConf(CometConf.COMET_SHUFFLE_NATIVE_HASH_PARTITIONING_ENABLED.key -> "true") {
433+
val native = sql(query)
434+
checkSparkAnswer(native)
435+
assert(getNumCometHashAggregate(native) == 2)
436+
}
437+
}
438+
}
439+
}
440+
}
441+
}
442+
371443
test("stddev_pop should return NaN for some cases") {
372444
withSQLConf(CometConf.COMET_SHUFFLE_ENABLED.key -> "true") {
373445
Seq(true, false).foreach { nullOnDivideByZero =>
@@ -718,10 +790,7 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
718790
dictionaryEnabled) {
719791
val n = if (nativeShuffleEnabled) 2 else 1
720792
checkSparkAnswerAndNumOfAggregates("SELECT _2, SUM(_1) FROM tbl GROUP BY _2", n)
721-
// COUNT is not declared safe for mixed execution, unlike the other aggregates here.
722-
checkSparkAnswerAndNumOfAggregates(
723-
"SELECT _2, COUNT(_1) FROM tbl GROUP BY _2",
724-
if (nativeShuffleEnabled) 2 else 0)
793+
checkSparkAnswerAndNumOfAggregates("SELECT _2, COUNT(_1) FROM tbl GROUP BY _2", n)
725794
checkSparkAnswerAndNumOfAggregates("SELECT _2, MIN(_1) FROM tbl GROUP BY _2", n)
726795
checkSparkAnswerAndNumOfAggregates("SELECT _2, MAX(_1) FROM tbl GROUP BY _2", n)
727796
checkSparkAnswerAndNumOfAggregates("SELECT _2, AVG(_1) FROM tbl GROUP BY _2", n)

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ class CometExecRuleSuite extends CometTestBase {
234234
}
235235
}
236236

237-
// Regression test for https://github.com/apache/datafusion-comet/issues/1389
238-
test("CometExecRule should not allow Comet partial and Spark final hash aggregate") {
237+
test("CometExecRule should allow COUNT Comet partial and Spark final hash aggregate") {
239238
withTempView("test_data") {
240239
createTestDataFrame.createOrReplaceTempView("test_data")
241240

@@ -251,11 +250,10 @@ class CometExecRuleSuite extends CometTestBase {
251250
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
252251
val transformedPlan = applyCometExecRule(sparkPlan)
253252

254-
// COUNT is intentionally excluded from mixed execution (AQE / count-bug reasons), so if
255-
// the final aggregate cannot be converted to Comet, neither should the partial.
256-
assert(
257-
countOperators(transformedPlan, classOf[HashAggregateExec]) == originalHashAggCount)
258-
assert(countOperators(transformedPlan, classOf[CometHashAggregateExec]) == 0)
253+
// COUNT's buffer is compatible in this direction. Keeping the Final in Spark also keeps
254+
// the AQE/count-bug rewrites that prevent the reverse direction from being admitted.
255+
assert(countOperators(transformedPlan, classOf[HashAggregateExec]) == 1)
256+
assert(countOperators(transformedPlan, classOf[CometHashAggregateExec]) == 1)
259257
}
260258
}
261259
}
@@ -276,8 +274,8 @@ class CometExecRuleSuite extends CometTestBase {
276274
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
277275
val transformedPlan = applyCometExecRule(sparkPlan)
278276

279-
// COUNT blocks mixed execution, so if the partial cannot be converted, neither should
280-
// the final.
277+
// COUNT still blocks Spark Partial to Comet Final, independently of the safe reverse
278+
// direction, so if the partial cannot be converted, neither should the final.
281279
assert(
282280
countOperators(transformedPlan, classOf[HashAggregateExec]) == originalHashAggCount)
283281
assert(countOperators(transformedPlan, classOf[CometHashAggregateExec]) == 0)

0 commit comments

Comments
 (0)