Skip to content

Commit 6ac1e2b

Browse files
bench: cover fully ordered aggregation across key types
1 parent cf4869c commit 6ac1e2b

1 file changed

Lines changed: 186 additions & 2 deletions

File tree

‎datafusion/core/benches/aggregate_query_sql.rs‎

Lines changed: 186 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
mod data_utils;
1919

20+
use arrow::datatypes::{DataType, IntervalUnit, TimeUnit};
2021
use criterion::{Criterion, criterion_group, criterion_main};
2122
use data_utils::create_table_provider;
23+
use datafusion::datasource::MemTable;
2224
use datafusion::error::Result;
23-
use datafusion::execution::context::SessionContext;
25+
use datafusion::execution::context::{SessionConfig, SessionContext};
26+
use datafusion::physical_plan::{collect, displayable};
27+
use datafusion_expr::col;
2428
use parking_lot::Mutex;
2529
use std::hint::black_box;
2630
use std::sync::Arc;
@@ -330,5 +334,185 @@ fn criterion_benchmark(c: &mut Criterion) {
330334
});
331335
}
332336

333-
criterion_group!(benches, criterion_benchmark);
337+
/// Measure the multi-column fully ordered grouping path, with data generation
338+
/// and sorting outside timing. Both keys vary, and the nullable second key
339+
/// changes type while the shape of the input stays the same.
340+
fn fully_ordered(c: &mut Criterion) {
341+
use DataType::*;
342+
use IntervalUnit::{DayTime, MonthDayNano};
343+
use TimeUnit::{Microsecond, Millisecond, Nanosecond};
344+
345+
let cases = [
346+
("int64", "signed_key", Int64, 31),
347+
(
348+
"utf8_view",
349+
"arrow_cast(lpad(CAST(key_value AS VARCHAR), 4, '0'), 'Utf8View')",
350+
Utf8View,
351+
31,
352+
),
353+
(
354+
"timestamp_ns",
355+
"arrow_cast(signed_key, 'Timestamp(Nanosecond)')",
356+
Timestamp(Nanosecond, None),
357+
31,
358+
),
359+
(
360+
"timestamp_us_utc",
361+
"arrow_cast(signed_key, 'Timestamp(Microsecond, Some(\"UTC\"))')",
362+
Timestamp(Microsecond, Some("UTC".into())),
363+
31,
364+
),
365+
(
366+
"date32",
367+
"arrow_cast(CAST(signed_key AS INTEGER), 'Date32')",
368+
Date32,
369+
31,
370+
),
371+
(
372+
"time32_ms",
373+
"arrow_cast(CAST(key_value AS INTEGER), 'Time32(Millisecond)')",
374+
Time32(Millisecond),
375+
31,
376+
),
377+
(
378+
"time64_ns",
379+
"arrow_cast(key_value, 'Time64(Nanosecond)')",
380+
Time64(Nanosecond),
381+
31,
382+
),
383+
(
384+
"duration_ns",
385+
"arrow_cast(signed_key, 'Duration(Nanosecond)')",
386+
Duration(Nanosecond),
387+
31,
388+
),
389+
(
390+
"interval_day_time",
391+
"arrow_cast(concat(CAST(signed_key AS VARCHAR), ' days'), 'Interval(DayTime)')",
392+
Interval(DayTime),
393+
31,
394+
),
395+
(
396+
"interval_month_day_nano",
397+
"arrow_cast(arrow_cast(signed_key, 'Duration(Nanosecond)'), 'Interval(MonthDayNano)')",
398+
Interval(MonthDayNano),
399+
31,
400+
),
401+
(
402+
"decimal128",
403+
"arrow_cast(concat('123456789012345678901', lpad(CAST(key_value AS VARCHAR), 4, '0'), '.1250'), 'Decimal128(30, 4)')",
404+
Decimal128(30, 4),
405+
31,
406+
),
407+
(
408+
"decimal256",
409+
"arrow_cast(concat('123456789012345678901234567890123456789012345678901', lpad(CAST(key_value AS VARCHAR), 4, '0'), '.1250'), 'Decimal256(60, 4)')",
410+
Decimal256(60, 4),
411+
31,
412+
),
413+
(
414+
"fixed_binary16",
415+
"arrow_cast(decode(lpad(to_hex(key_value), 32, '0'), 'hex'), 'FixedSizeBinary(16)')",
416+
FixedSizeBinary(16),
417+
31,
418+
),
419+
(
420+
"fixed_binary7",
421+
"arrow_cast(decode(lpad(to_hex(key_value), 14, '0'), 'hex'), 'FixedSizeBinary(7)')",
422+
FixedSizeBinary(7),
423+
31,
424+
),
425+
(
426+
"timestamp_ns_singleton",
427+
"arrow_cast(signed_key, 'Timestamp(Nanosecond)')",
428+
Timestamp(Nanosecond, None),
429+
1,
430+
),
431+
(
432+
"timestamp_ns_long_runs",
433+
"arrow_cast(signed_key, 'Timestamp(Nanosecond)')",
434+
Timestamp(Nanosecond, None),
435+
512,
436+
),
437+
];
438+
439+
let rt = Runtime::new().unwrap();
440+
let mut group = c.benchmark_group("fully_ordered");
441+
for (name, key_expr, key_type, run_length) in cases {
442+
let ctx = rt
443+
.block_on(fully_ordered_context(key_expr, &key_type, run_length))
444+
.unwrap();
445+
group.bench_function(name, |b| {
446+
b.iter(|| query(ctx.clone(), &rt, FULLY_ORDERED_QUERY))
447+
});
448+
}
449+
group.finish();
450+
}
451+
452+
const FULLY_ORDERED_QUERY: &str = "SELECT k1, k2 FROM ordered_input GROUP BY k1, k2";
453+
454+
async fn fully_ordered_context(
455+
key_expr: &str,
456+
key_type: &DataType,
457+
run_length: usize,
458+
) -> Result<Arc<Mutex<SessionContext>>> {
459+
const ROWS: usize = 2_097_152;
460+
let config = SessionConfig::new()
461+
.with_target_partitions(1)
462+
.with_batch_size(8192)
463+
.with_prefer_existing_sort(true);
464+
let ctx = SessionContext::new_with_config(config);
465+
// Runs of 31 cross batch boundaries; singleton and long-run cases vary
466+
// cardinality without multiplying every type by every input shape.
467+
let input = ctx
468+
.sql(&format!(
469+
"SELECT CAST(group_id / 32 AS INTEGER) AS k1, \
470+
CASE WHEN key_value = 0 THEN NULL ELSE {key_expr} END AS k2 \
471+
FROM (SELECT value / {run_length} AS group_id, \
472+
value / {run_length} % 32 AS key_value, \
473+
value / {run_length} % 32 - 16 AS signed_key \
474+
FROM range({ROWS})) \
475+
ORDER BY k1 ASC NULLS FIRST, k2 ASC NULLS FIRST"
476+
))
477+
.await?
478+
.create_physical_plan()
479+
.await?;
480+
let schema = input.schema();
481+
assert_eq!(schema.field(1).data_type(), key_type);
482+
let batches = collect(input, ctx.task_ctx()).await?;
483+
assert_eq!(
484+
batches.iter().map(|batch| batch.num_rows()).sum::<usize>(),
485+
ROWS
486+
);
487+
// The setup query sorts both keys before materialization. Retain that
488+
// known ordering explicitly when registering its results as a table.
489+
let table = MemTable::try_new(schema, vec![batches])?.with_sort_order(vec![vec![
490+
col("k1").sort(true, true),
491+
col("k2").sort(true, true),
492+
]]);
493+
ctx.register_table("ordered_input", Arc::new(table))?;
494+
495+
let plan = ctx
496+
.sql(FULLY_ORDERED_QUERY)
497+
.await?
498+
.create_physical_plan()
499+
.await?;
500+
let description = displayable(plan.as_ref()).indent(true).to_string();
501+
assert!(
502+
description.contains("ordering_mode=Sorted"),
503+
"{description}"
504+
);
505+
// Input is already sorted in one partition. Fail if the planner adds
506+
// sorting or repartitioning to the measured query.
507+
assert!(!description.contains("SortExec"), "{description}");
508+
assert!(!description.contains("RepartitionExec"), "{description}");
509+
let result = collect(plan, ctx.task_ctx()).await?;
510+
assert_eq!(
511+
result.iter().map(|batch| batch.num_rows()).sum::<usize>(),
512+
ROWS.div_ceil(run_length)
513+
);
514+
Ok(Arc::new(Mutex::new(ctx)))
515+
}
516+
517+
criterion_group!(benches, criterion_benchmark, fully_ordered);
334518
criterion_main!(benches);

0 commit comments

Comments
 (0)