Skip to content

Commit 0ddf5e8

Browse files
committed
perf(functions-aggregate): cap ordered ARRAY_AGG coalescing by bytes
Avoid repeatedly copying wide payloads when small update_batch calls are coalesced. Limit concat to batches whose combined buffer size is at most 4 KiB, alongside the existing 64-row threshold. Add an update-only benchmark with 4 KiB Utf8 values and one row per call to track the wide-payload cost. Generalize the benchmark fixture to support the Utf8 payload type.
1 parent 092743d commit 0ddf5e8

2 files changed

Lines changed: 87 additions & 12 deletions

File tree

‎datafusion/functions-aggregate/benches/array_agg.rs‎

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
use std::hint::black_box;
1919
use std::sync::Arc;
2020

21-
use arrow::array::Int64Array;
2221
use arrow::array::{
23-
Array, ArrayRef, ArrowPrimitiveType, AsArray, ListArray, NullBufferBuilder,
24-
StringArray,
22+
Array, ArrayRef, ArrowPrimitiveType, AsArray, Int64Array, ListArray,
23+
NullBufferBuilder, StringArray,
2524
};
2625
use arrow::datatypes::{DataType, Field, FieldRef, Int64Type, Schema};
27-
use criterion::{Criterion, criterion_group, criterion_main};
26+
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
2827
use datafusion_expr::function::AccumulatorArgs;
2928
use datafusion_expr::{Accumulator, AggregateUDFImpl};
3029
use datafusion_functions_aggregate::array_agg::{
@@ -294,8 +293,7 @@ fn distinct_array_agg_benchmark(c: &mut Criterion) {
294293
}
295294

296295
/// Precomputes the schema, physical expressions, sort expression, and aggregate
297-
/// metadata so each benchmark iteration measures only accumulator creation,
298-
/// `update_batch()`, and `evaluate()`.
296+
/// metadata so each benchmark iteration can focus on accumulator operations.
299297
struct OrderedArrayAggBenchFixture {
300298
schema: Schema,
301299
value_expr: Arc<dyn PhysicalExpr>,
@@ -306,9 +304,9 @@ struct OrderedArrayAggBenchFixture {
306304
}
307305

308306
impl OrderedArrayAggBenchFixture {
309-
fn new(input_preordered: bool) -> Self {
307+
fn new(value_type: &DataType, input_preordered: bool) -> Self {
310308
let schema = Schema::new(vec![
311-
Field::new("value", DataType::Int64, false),
309+
Field::new("value", value_type.clone(), false),
312310
Field::new("ordering", DataType::Int64, false),
313311
]);
314312

@@ -332,7 +330,7 @@ impl OrderedArrayAggBenchFixture {
332330

333331
let return_field = Field::new(
334332
"array_agg",
335-
DataType::List(Field::new_list_field(DataType::Int64, true).into()),
333+
DataType::List(Field::new_list_field(value_type.clone(), true).into()),
336334
true,
337335
)
338336
.into();
@@ -398,11 +396,12 @@ fn create_ordered_array_agg_batches(
398396
fn ordered_array_agg_bench(
399397
c: &mut Criterion,
400398
name: &str,
399+
value_type: &DataType,
401400
batches: &[[ArrayRef; 2]],
402401
input_preordered: bool,
403402
) {
404403
c.bench_function(name, |b| {
405-
let fixture = OrderedArrayAggBenchFixture::new(input_preordered);
404+
let fixture = OrderedArrayAggBenchFixture::new(value_type, input_preordered);
406405
b.iter(|| {
407406
let mut accumulator = fixture.create_accumulator();
408407

@@ -428,6 +427,7 @@ fn ordered_array_agg_benchmark(c: &mut Criterion) {
428427
"ordered_array_agg i64 ordered input, \
429428
{rows_per_batch} rows per update_batch"
430429
),
430+
&DataType::Int64,
431431
&ordered_batches,
432432
true,
433433
);
@@ -438,15 +438,83 @@ fn ordered_array_agg_benchmark(c: &mut Criterion) {
438438
"ordered_array_agg i64 random input, \
439439
{rows_per_batch} rows per update_batch"
440440
),
441+
&DataType::Int64,
441442
&shuffled_batches,
442443
false,
443444
);
444445
}
445446
}
447+
448+
/// Creates preordered batches with a 4 KiB Utf8 payload per row.
449+
fn create_wide_utf8_batches(rows_per_batch: usize) -> Vec<[ArrayRef; 2]> {
450+
assert!(
451+
rows_per_batch > 0,
452+
"rows_per_batch must be greater than zero"
453+
);
454+
455+
let values = vec!["x".repeat(4 * 1_024); ORDERED_ARRAY_AGG_ROWS];
456+
457+
values
458+
.chunks(rows_per_batch)
459+
.enumerate()
460+
.map(|(idx, batch_values)| {
461+
let payload =
462+
Arc::new(StringArray::from_iter_values(batch_values.iter())) as ArrayRef;
463+
let start = idx * rows_per_batch;
464+
let ordering_values = Arc::new(Int64Array::from(
465+
(start..start + batch_values.len())
466+
.map(|row| i64::try_from(row).unwrap())
467+
.collect::<Vec<_>>(),
468+
)) as ArrayRef;
469+
470+
[payload, ordering_values]
471+
})
472+
.collect()
473+
}
474+
475+
fn ordered_array_agg_update_only_bench(
476+
c: &mut Criterion,
477+
name: &str,
478+
value_type: &DataType,
479+
batches: &[[ArrayRef; 2]],
480+
input_preordered: bool,
481+
) {
482+
let fixture = OrderedArrayAggBenchFixture::new(value_type, input_preordered);
483+
484+
c.bench_function(name, |b| {
485+
b.iter_batched(
486+
|| fixture.create_accumulator(),
487+
|mut accumulator| {
488+
for batch in batches {
489+
accumulator
490+
.update_batch(batch)
491+
.expect("update_batch should succeed");
492+
}
493+
494+
// Return the accumulator so Criterion drops it after timing the updates.
495+
accumulator
496+
},
497+
BatchSize::PerIteration,
498+
);
499+
});
500+
}
501+
502+
fn ordered_array_agg_wide_utf8_benchmark(c: &mut Criterion) {
503+
let ordered_batches = create_wide_utf8_batches(1);
504+
ordered_array_agg_update_only_bench(
505+
c,
506+
"ordered_array_agg utf8 4 KiB, 1 row per update_batch",
507+
&DataType::Utf8,
508+
&ordered_batches,
509+
true,
510+
);
511+
}
512+
446513
criterion_group!(
447514
benches,
448515
array_agg_benchmark,
449516
distinct_array_agg_benchmark,
450-
ordered_array_agg_benchmark
517+
ordered_array_agg_benchmark,
518+
ordered_array_agg_wide_utf8_benchmark
451519
);
452520
criterion_main!(benches);

‎datafusion/functions-aggregate/src/array_agg.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,10 @@ struct OrderedArrayAggEntry {
12501250
/// per-array memory overhead while limiting repeated concatenation costs.
12511251
const ORDERED_ARRAY_AGG_COALESCE_ROWS: usize = 64;
12521252

1253+
/// Skip coalescing once the tail batch is large enough that the fixed
1254+
/// per-array overhead is negligible relative to the payload.
1255+
const ORDERED_ARRAY_AGG_COALESCE_BYTES: usize = 4096;
1256+
12531257
#[derive(Debug)]
12541258
pub(crate) struct OrderSensitiveArrayAggAccumulator {
12551259
/// Arrow payload arrays. Entries refer to rows in these batches.
@@ -1537,7 +1541,10 @@ impl OrderSensitiveArrayAggAccumulator {
15371541
let start = self.entries.len();
15381542
let (batch_idx, row_offset) = match self.batches.last() {
15391543
Some(last_batch)
1540-
if last_batch.len() + row_count <= ORDERED_ARRAY_AGG_COALESCE_ROWS =>
1544+
if last_batch.len() + row_count <= ORDERED_ARRAY_AGG_COALESCE_ROWS
1545+
&& last_batch.get_buffer_memory_size()
1546+
+ values.get_buffer_memory_size()
1547+
<= ORDERED_ARRAY_AGG_COALESCE_BYTES =>
15411548
{
15421549
let merged =
15431550
arrow::compute::concat(&[last_batch.as_ref(), values.as_ref()])?;

0 commit comments

Comments
 (0)