Skip to content

Commit c17e315

Browse files
committed
perf: avoid unnecessary aggregate spill rewrites
Limit an eligible intermediate merge to the inputs needed to target the ordinary admitted fan-in for final replay. Keep the original reservation, read-ahead, and next-pass admission. Only the migrated aggregate replay path opts in; legacy aggregation keeps its existing selection. Apply count-based sizing only when admitted and pending runs have equal recorded maximum batch memory and batch-size limits. A heterogeneous IPC regression demonstrates that trimming ahead of a wider run otherwise adds intermediate rewrites. Remove this PR's widening, source/output estimator, input-boundary flushing, retry ownership, and their dedicated tests. Cover sizing and its fallback with exact output, rewritten-row, and cleanup assertions.
1 parent 7078ef7 commit c17e315

7 files changed

Lines changed: 321 additions & 1593 deletions

File tree

‎datafusion/core/tests/fuzz_cases/spilling_fuzz_in_memory_constrained_env.rs‎

Lines changed: 6 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use std::sync::Arc;
2222

2323
use crate::fuzz_cases::aggregate_fuzz::assert_spill_count_metric;
2424
use crate::fuzz_cases::once_exec::OnceExec;
25-
use arrow::array::{AsArray, UInt64Array};
26-
use arrow::datatypes::UInt64Type;
25+
use arrow::array::UInt64Array;
2726
use arrow::row::{RowConverter, SortField};
2827
use arrow::{array::StringArray, compute::SortOptions, record_batch::RecordBatch};
2928
use arrow_schema::{DataType, Field, Schema};
@@ -35,7 +34,7 @@ use datafusion::physical_plan::sorts::sort::SortExec;
3534
use datafusion::prelude::SessionConfig;
3635
use datafusion_common::units::{KB, MB};
3736
use datafusion_execution::memory_pool::{
38-
FairSpillPool, GreedyMemoryPool, MemoryConsumer, MemoryReservation,
37+
FairSpillPool, MemoryConsumer, MemoryReservation,
3938
};
4039
use datafusion_execution::{SendableRecordBatchStream, TaskContext};
4140
use datafusion_functions_aggregate::array_agg::array_agg_udaf;
@@ -49,8 +48,7 @@ use datafusion_physical_plan::aggregates::{
4948
use datafusion_physical_plan::metrics::MetricValue;
5049
use datafusion_physical_plan::spill::get_record_batch_memory_size;
5150
use datafusion_physical_plan::stream::RecordBatchStreamAdapter;
52-
use futures::{StreamExt, TryStreamExt};
53-
use rand::{Rng, SeedableRng, rngs::StdRng};
51+
use futures::StreamExt;
5452

5553
use arrow::array::Int32Array;
5654
use datafusion::datasource::memory::MemorySourceConfig;
@@ -828,125 +826,9 @@ async fn test_aggregate_with_high_cardinality_with_limited_memory_and_large_reco
828826
Ok(())
829827
}
830828

831-
#[rstest::rstest]
832-
#[case::greedy_seed_7(7, false)]
833-
#[case::greedy_seed_91(91, false)]
834-
#[case::fair_seed_7(7, true)]
835-
#[case::fair_seed_91(91, true)]
836-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
837-
async fn aggregate_spill_with_seeded_batches_and_concurrent_peer(
838-
#[case] seed: u64,
839-
#[case] fair: bool,
840-
) -> Result<()> {
841-
const POOL_SIZE: usize = 2 * MB as usize;
842-
const INPUT_BATCHES: usize = 96;
843-
let inner: Arc<dyn MemoryPool> = if fair {
844-
Arc::new(FairSpillPool::new(POOL_SIZE))
845-
} else {
846-
Arc::new(GreedyMemoryPool::new(POOL_SIZE))
847-
};
848-
let pool = Arc::new(PeakRecordingPool::new(inner));
849-
let runtime = RuntimeEnvBuilder::new()
850-
.with_memory_pool(Arc::clone(&pool) as Arc<dyn MemoryPool>)
851-
.build_arc()?;
852-
let mut rng = StdRng::seed_from_u64(seed);
853-
let mut runs = Vec::new();
854-
// Construct both pipelines before polling either, so their reservations
855-
// overlap. Each peer reads seeded narrow and oversized string batches,
856-
// spills, and replays against the same pool with default merge fan-in.
857-
for _ in 0..2 {
858-
let batch_size = [64, 128][rng.random_range(0..2)];
859-
let sizes = Arc::new(
860-
(0..INPUT_BATCHES)
861-
.map(|index| {
862-
if index % 19 == 0 {
863-
rng.random_range(POOL_SIZE / 12..POOL_SIZE / 6)
864-
} else {
865-
rng.random_range(POOL_SIZE / 128..POOL_SIZE / 32)
866-
}
867-
})
868-
.collect::<Vec<_>>(),
869-
);
870-
let task_ctx = Arc::new(
871-
TaskContext::default()
872-
.with_session_config(SessionConfig::new().with_batch_size(batch_size))
873-
.with_runtime(Arc::clone(&runtime)),
874-
);
875-
let generated_sizes = Arc::clone(&sizes);
876-
let (args, plan, stream) =
877-
build_high_cardinality_aggregate(RunTestWithLimitedMemoryArgs {
878-
pool_size: POOL_SIZE,
879-
task_ctx,
880-
number_of_record_batches: INPUT_BATCHES,
881-
get_size_of_record_batch_to_generate: Box::pin(move |index| {
882-
generated_sizes[index]
883-
}),
884-
memory_behavior: MemoryBehavior::AsIs,
885-
assert_all_output_batches_roughly_match_batch_size_conf: false,
886-
})?;
887-
let schema = stream.schema();
888-
let mut seen = vec![false; INPUT_BATCHES * batch_size];
889-
let checked = stream.map_ok(move |batch| {
890-
let keys = batch.column(0).as_primitive::<UInt64Type>();
891-
let lists = batch.column(1).as_list::<i32>();
892-
for row in 0..batch.num_rows() {
893-
let key = keys.value(row) as usize;
894-
assert!(key < INPUT_BATCHES * batch_size, "seed={seed}, key={key}");
895-
assert!(
896-
!std::mem::replace(&mut seen[key], true),
897-
"duplicate key {key}, seed={seed}"
898-
);
899-
let expected_length = sizes[key / batch_size]
900-
.saturating_sub(size_of::<u64>() * batch_size)
901-
/ batch_size;
902-
let values = lists.value(row);
903-
let strings = values.as_string::<i32>();
904-
assert_eq!(values.len(), 1, "seed={seed}, key={key}");
905-
assert_eq!(
906-
strings.value(0).len(),
907-
expected_length,
908-
"seed={seed}, key={key}"
909-
);
910-
assert!(strings.value(0).bytes().all(|byte| byte == b'a'));
911-
}
912-
batch
913-
});
914-
let stream = Box::pin(RecordBatchStreamAdapter::new(schema, checked));
915-
runs.push(run_test(args, plan, stream));
916-
}
917-
tokio::time::timeout(
918-
std::time::Duration::from_secs(30),
919-
futures::future::try_join_all(runs),
920-
)
921-
.await
922-
.expect("concurrent seeded aggregates must complete")?;
923-
assert!(
924-
pool.peak_reserved() <= POOL_SIZE,
925-
"seed={seed}, fair={fair}"
926-
);
927-
assert_eq!(pool.reserved(), 0);
928-
assert_eq!(runtime.disk_manager.used_disk_space(), 0);
929-
assert_eq!(
930-
runtime.disk_manager.spilling_progress().active_files_count,
931-
0
932-
);
933-
Ok(())
934-
}
935-
936829
async fn run_test_aggregate_with_high_cardinality(
937-
args: RunTestWithLimitedMemoryArgs,
938-
) -> Result<MetricsSet> {
939-
let (args, aggregate_final, result) = build_high_cardinality_aggregate(args)?;
940-
run_test(args, aggregate_final, result).await
941-
}
942-
943-
fn build_high_cardinality_aggregate(
944830
mut args: RunTestWithLimitedMemoryArgs,
945-
) -> Result<(
946-
RunTestWithLimitedMemoryArgs,
947-
Arc<AggregateExec>,
948-
SendableRecordBatchStream,
949-
)> {
831+
) -> Result<MetricsSet> {
950832
let get_size_of_record_batch_to_generate = std::mem::replace(
951833
&mut args.get_size_of_record_batch_to_generate,
952834
Box::pin(move |_| unreachable!("should not be called after take")),
@@ -1027,7 +909,8 @@ fn build_high_cardinality_aggregate(
1027909
)?);
1028910

1029911
let result = aggregate_final.execute(0, Arc::clone(&args.task_ctx))?;
1030-
Ok((args, aggregate_final, result))
912+
913+
run_test(args, aggregate_final, result).await
1031914
}
1032915

1033916
async fn run_test(

‎datafusion/physical-plan/src/aggregates/spill.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ impl AggregateSpill {
276276
.with_batch_size(batch_size)
277277
.with_reservation(merge_reservation)
278278
.with_replay_headroom()
279+
.with_intermediate_merge_sizing()
279280
.build()?;
280281
let replay = OrderedFinalAggregateStream::new_with_input_and_metrics(
281282
&replay_agg,

0 commit comments

Comments
 (0)