Skip to content

Commit 2ce73e5

Browse files
committed
Emit PartialMerge states a column at a time for row-only aggregates
MergeAsPartial now wraps an inner aggregate that has no GroupsAccumulator (first, last and any_value without an ordering) in a GroupsAccumulatorAdapter itself. Under Single mode, DataFusion otherwise wrapped the wrapper's row accumulators and evaluated every group to a one-row struct. Also write the planner's two output wrappers the same way, check the planner test's output with assert_batches_sorted_eq and cover first there, and drop two settings that the memory-pressure tests don't read.
1 parent 2f35370 commit 2ce73e5

3 files changed

Lines changed: 113 additions & 35 deletions

File tree

‎native/core/src/execution/merge_as_partial.rs‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::hash::{Hash, Hasher};
3333
use std::sync::Arc;
3434

3535
use arrow::array::{ArrayRef, BooleanArray, StructArray};
36-
use arrow::datatypes::{DataType, Field, FieldRef, Fields};
36+
use arrow::datatypes::{DataType, Field, FieldRef, Fields, Schema};
3737
use datafusion::common::{internal_err, Result};
3838
use datafusion::logical_expr::function::AccumulatorArgs;
3939
use datafusion::logical_expr::function::StateFieldsArgs;
@@ -42,7 +42,9 @@ use datafusion::logical_expr::{
4242
Signature, Volatility,
4343
};
4444
use datafusion::physical_expr::aggregate::AggregateFunctionExpr;
45-
use datafusion::physical_expr::{expressions::Column, PhysicalExpr};
45+
use datafusion::physical_expr::{
46+
expressions::Column, GroupsAccumulatorAdapter, PhysicalExpr, PhysicalSortExpr,
47+
};
4648
use datafusion::physical_plan::{projection::ProjectionExec, ExecutionPlan};
4749
use datafusion::scalar::ScalarValue;
4850
use datafusion_comet_spark_expr::GetStructField;
@@ -190,18 +192,26 @@ impl AggregateUDFImpl for MergeAsPartialUDF {
190192
}))
191193
}
192194

193-
fn groups_accumulator_supported(&self, args: AccumulatorArgs) -> bool {
194-
self.inner_udf
195-
.groups_accumulator_supported(self.inner_accumulator_args(args))
195+
fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
196+
// An inner aggregate without a GroupsAccumulator gets an adapter below, so that Single
197+
// mode emits its states a column at a time rather than evaluating each group to a struct.
198+
true
196199
}
197200

198201
fn create_groups_accumulator(
199202
&self,
200203
args: AccumulatorArgs,
201204
) -> Result<Box<dyn GroupsAccumulator>> {
202-
let inner_acc = self
205+
let inner_args = self.inner_accumulator_args(args);
206+
let inner_acc: Box<dyn GroupsAccumulator> = if self
203207
.inner_udf
204-
.create_groups_accumulator(self.inner_accumulator_args(args))?;
208+
.groups_accumulator_supported(inner_args.clone())
209+
{
210+
self.inner_udf.create_groups_accumulator(inner_args)?
211+
} else {
212+
let factory = InnerAccumulatorFactory::new(&self.inner_udf, &inner_args);
213+
Box::new(GroupsAccumulatorAdapter::new(move || factory.create()))
214+
};
205215
Ok(Box::new(MergeAsPartialGroupsAccumulator {
206216
inner: inner_acc,
207217
state_decoder: self.state_decoder.clone(),
@@ -222,6 +232,52 @@ impl AggregateUDFImpl for MergeAsPartialUDF {
222232
}
223233
}
224234

235+
/// Owned copies of the arguments that build an inner row accumulator, which a
236+
/// `GroupsAccumulatorAdapter` does for each new group, after `AccumulatorArgs` has gone.
237+
struct InnerAccumulatorFactory {
238+
udf: AggregateUDF,
239+
return_field: FieldRef,
240+
schema: Schema,
241+
ignore_nulls: bool,
242+
order_bys: Vec<PhysicalSortExpr>,
243+
is_reversed: bool,
244+
name: String,
245+
is_distinct: bool,
246+
exprs: Vec<Arc<dyn PhysicalExpr>>,
247+
expr_fields: Vec<FieldRef>,
248+
}
249+
250+
impl InnerAccumulatorFactory {
251+
fn new(udf: &AggregateUDF, args: &AccumulatorArgs) -> Self {
252+
Self {
253+
udf: udf.clone(),
254+
return_field: Arc::clone(&args.return_field),
255+
schema: args.schema.clone(),
256+
ignore_nulls: args.ignore_nulls,
257+
order_bys: args.order_bys.to_vec(),
258+
is_reversed: args.is_reversed,
259+
name: args.name.to_string(),
260+
is_distinct: args.is_distinct,
261+
exprs: args.exprs.to_vec(),
262+
expr_fields: args.expr_fields.to_vec(),
263+
}
264+
}
265+
266+
fn create(&self) -> Result<Box<dyn Accumulator>> {
267+
self.udf.accumulator(AccumulatorArgs {
268+
return_field: Arc::clone(&self.return_field),
269+
schema: &self.schema,
270+
ignore_nulls: self.ignore_nulls,
271+
order_bys: &self.order_bys,
272+
is_reversed: self.is_reversed,
273+
name: &self.name,
274+
is_distinct: self.is_distinct,
275+
exprs: &self.exprs,
276+
expr_fields: &self.expr_fields,
277+
})
278+
}
279+
}
280+
225281
/// Projects the output of a Single-mode `aggregate` whose aggregates are all [`MergeAsPartialUDF`]s
226282
/// into the output of a Partial-mode one: its `num_grouping` grouping columns, then one column per
227283
/// state field, unpacked from the struct each aggregate evaluates to.

‎native/core/src/execution/planner.rs‎

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,16 +1684,14 @@ impl PhysicalPlanner {
16841684
)?,
16851685
);
16861686

1687-
// A PartialMerge runs as Single, so unpack the states it evaluates to into the
1688-
// state columns that Spark expects of it.
1689-
let mut output = Arc::clone(&aggregate);
1687+
let mut output = aggregate;
16901688
let mut additional_native_plans = vec![];
16911689
if proto_mode == ProtoAggregateMode::PartialMerge {
1690+
additional_native_plans.push(Arc::clone(&output));
16921691
output = crate::execution::merge_as_partial::unpack_state_structs(
16931692
output,
16941693
agg.grouping_exprs.len(),
16951694
)?;
1696-
additional_native_plans.push(Arc::clone(&aggregate));
16971695
}
16981696

16991697
// Spark's SortAggregateExec reports its output as ordered by the grouping keys,
@@ -6328,16 +6326,22 @@ mod tests {
63286326

63296327
#[tokio::test]
63306328
async fn partial_merge_runs_as_single_and_emits_merged_states() {
6331-
use arrow::array::Int64Array;
6329+
use arrow::array::{BooleanArray, Int64Array};
6330+
use datafusion::assert_batches_sorted_eq;
63326331
use datafusion::common::tree_node::{Transformed, TreeNode};
63336332
use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode};
63346333
use spark_expression::agg_expr::ExprStruct as AggExprStruct;
63356334

6336-
// A Spark PartialMerge of max(v) and count(v) grouped by k, over [k, max state, count state].
6335+
// A Spark PartialMerge of max(v), count(v) and first(v) grouped by k, over k and their
6336+
// states. DataFusion's first has no GroupsAccumulator without an ordering.
63376337
let int64 = spark_expression::DataType {
63386338
type_id: 4,
63396339
type_info: None,
63406340
};
6341+
let boolean = spark_expression::DataType {
6342+
type_id: 0,
6343+
type_info: None,
6344+
};
63416345
let v = Expr {
63426346
expr_struct: Some(Unbound(spark_expression::UnboundReference {
63436347
name: "v".to_string(),
@@ -6350,7 +6354,13 @@ mod tests {
63506354
plan_id: 1,
63516355
children: vec![Operator {
63526356
op_struct: Some(OpStruct::Scan(spark_operator::Scan {
6353-
fields: vec![int64.clone(); 3],
6357+
fields: vec![
6358+
int64.clone(),
6359+
int64.clone(),
6360+
int64.clone(),
6361+
int64.clone(),
6362+
boolean.clone(),
6363+
],
63546364
source: String::new(),
63556365
})),
63566366
..Default::default()
@@ -6377,9 +6387,17 @@ mod tests {
63776387
})),
63786388
..Default::default()
63796389
},
6390+
spark_expression::AggExpr {
6391+
expr_struct: Some(AggExprStruct::First(spark_expression::First {
6392+
child: Some(v.clone()),
6393+
datatype: Some(int64.clone()),
6394+
ignore_nulls: false,
6395+
})),
6396+
..Default::default()
6397+
},
63806398
],
63816399
mode,
6382-
expr_modes: vec![partial_merge; 2],
6400+
expr_modes: vec![partial_merge; 3],
63836401
initial_input_buffer_offset: 1,
63846402
..Default::default()
63856403
})),
@@ -6422,6 +6440,14 @@ mod tests {
64226440
Some(1),
64236441
])),
64246442
Arc::new(Int64Array::from(vec![1, 2, 3, 0, 4])),
6443+
Arc::new(Int64Array::from(vec![
6444+
Some(100),
6445+
None,
6446+
Some(101),
6447+
Some(200),
6448+
None,
6449+
])),
6450+
Arc::new(BooleanArray::from(vec![true, false, true, true, false])),
64256451
],
64266452
)
64276453
.unwrap();
@@ -6440,21 +6466,18 @@ mod tests {
64406466
let results = collect(native_plan.execute(0, task_ctx).unwrap())
64416467
.await
64426468
.unwrap();
6443-
let output = arrow::compute::concat_batches(&results[0].schema(), &results).unwrap();
6444-
let column = |idx: usize| {
6445-
output
6446-
.column(idx)
6447-
.as_any()
6448-
.downcast_ref::<Int64Array>()
6449-
.unwrap()
6450-
.clone()
6451-
};
6452-
let (keys, maxes, counts) = (column(0), column(1), column(2));
6453-
let mut rows: Vec<_> = (0..output.num_rows())
6454-
.map(|row| (keys.value(row), maxes.value(row), counts.value(row)))
6455-
.collect();
6456-
rows.sort();
6457-
assert_eq!(vec![(1, 10, 4), (2, 5, 2), (3, 1, 4)], rows);
6469+
assert_batches_sorted_eq!(
6470+
[
6471+
"+-------+------------+--------------+--------------------+---------------------------+",
6472+
"| col_0 | max[value] | count[count] | first[first_value] | first[first_value_is_set] |",
6473+
"+-------+------------+--------------+--------------------+---------------------------+",
6474+
"| 1 | 10 | 4 | 100 | true |",
6475+
"| 2 | 5 | 2 | 200 | true |",
6476+
"| 3 | 1 | 4 | | false |",
6477+
"+-------+------------+--------------+--------------------+---------------------------+",
6478+
],
6479+
&results
6480+
);
64586481
}
64596482

64606483
#[tokio::test]

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,7 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
254254
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
255255
SQLConf.SHUFFLE_PARTITIONS.key -> "1",
256256
CometConf.COMET_BATCH_SIZE.key -> "1024",
257-
CometConf.COMET_OFFHEAP_MEMORY_POOL_FRACTION.key -> "0.002",
258-
CometConf.COMET_RESPECT_DATAFUSION_CONFIGS.key -> "true",
259-
"spark.comet.datafusion.execution.sort_spill_reservation_bytes" -> "65536")(f)
257+
CometConf.COMET_OFFHEAP_MEMORY_POOL_FRACTION.key -> "0.002")(f)
260258
}
261259

262260
test("PartialMerge that runs short of memory keeps one row per key (issue #6857)") {
@@ -313,12 +311,13 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
313311
val expected = (0 until 4096).map { k =>
314312
Row(k, 2L, (0 until 8).map(j => padded(k + 4096 * j)).sorted)
315313
}
314+
val localRows = rows.toDF("k", "d", "s")
316315
withTempPath { dir =>
317316
val path = dir.getCanonicalPath
318-
rows.toDF("k", "d", "s").write.parquet(path)
317+
localRows.write.parquet(path)
319318
for (sparkPartial <- Seq(false, true)) {
320319
withDistinctMergeMemoryPressure {
321-
val source = if (sparkPartial) rows.toDF("k", "d", "s") else spark.read.parquet(path)
320+
val source = if (sparkPartial) localRows else spark.read.parquet(path)
322321
val df =
323322
source.groupBy("k").agg(count_distinct(col("d")), sort_array(collect_list("s")))
324323
checkCometAnswer(df, expected)

0 commit comments

Comments
 (0)