Skip to content

Commit d00b725

Browse files
kumarUjjawalOmega359
authored andcommitted
test: bound merge fan-in in ordered aggregate spill tests (apache#25252)
## Which issue does this PR close? Closes apache#25047. ## Rationale for this change `ordered_aggregate_spill.slt` can fail when spill-merge buffers leave too little memory for aggregate replay while another partition retains aggregate state in the shared greedy pool. Limit merge fan-in to two to reduce this contention while preserving the 600 KiB memory limit, two partitions, and existing query results. ## What changes are included in this PR? - Set and reset the SQL test's spill merge fan-in. - Add controlled two-partition tests for grouped results, spilling, and reservation cleanup on completion, input error, and cancellation. - Check spilled rows without depending on the formatted byte unit, and remove exact spill-count claims from comments. ## What is the testing strategy for this PR? Local validation: - 320 SQL test executions (16 concurrent copies per run) and 50 repetitions of the focused tests. - Full extended workspace tests, including all 518 SQL test files. - Workspace Clippy with all targets and features, formatting, and submission checks. ## Are there any user-facing changes? No. This changes tests only; production execution and defaults are unchanged.
1 parent 10ba04e commit d00b725

2 files changed

Lines changed: 295 additions & 4 deletions

File tree

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

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,3 +900,286 @@ impl RecordBatchStream for OrderedFinalAggregateStream {
900900
Arc::clone(&self.schema)
901901
}
902902
}
903+
904+
#[cfg(test)]
905+
mod tests {
906+
use super::*;
907+
use crate::ExecutionPlan;
908+
use crate::aggregates::PhysicalGroupBy;
909+
use crate::common::collect;
910+
use crate::stream::RecordBatchStreamAdapter;
911+
use crate::test::TestMemoryExec;
912+
use arrow::array::{Int64Array, StringViewArray};
913+
use arrow::datatypes::{DataType, Field, Schema};
914+
use datafusion_execution::config::SessionConfig;
915+
use datafusion_execution::memory_pool::{GreedyMemoryPool, MemoryPool};
916+
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
917+
use datafusion_functions_aggregate::{min_max::min_udaf, sum::sum_udaf};
918+
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
919+
use datafusion_physical_expr::expressions::col;
920+
use futures::FutureExt;
921+
use futures::channel::mpsc;
922+
use std::collections::BTreeMap;
923+
924+
#[derive(Clone, Copy)]
925+
enum Finish {
926+
Collect,
927+
DropDuringMerge,
928+
DropDuringReplay,
929+
InputError,
930+
}
931+
932+
#[tokio::test]
933+
async fn spill_replay_with_another_ordered_partition() -> Result<()> {
934+
for input_batches in [28, 36, 55, 63] {
935+
run_shared_pool_case(input_batches, 600 * 1024, Finish::Collect).await?;
936+
}
937+
// The same input also produces the reference results without spilling.
938+
run_shared_pool_case(63, 10 * 1024 * 1024, Finish::Collect).await
939+
}
940+
941+
#[tokio::test]
942+
async fn spill_replay_releases_memory_on_drop() -> Result<()> {
943+
run_shared_pool_case(36, 600 * 1024, Finish::DropDuringMerge).await?;
944+
run_shared_pool_case(36, 600 * 1024, Finish::DropDuringReplay).await
945+
}
946+
947+
#[tokio::test]
948+
async fn ordered_spill_releases_memory_on_input_error() -> Result<()> {
949+
run_shared_pool_case(36, 600 * 1024, Finish::InputError).await
950+
}
951+
952+
async fn run_shared_pool_case(
953+
input_batches: i64,
954+
limit: usize,
955+
finish: Finish,
956+
) -> Result<()> {
957+
let schema = Arc::new(Schema::new(vec![
958+
Field::new("a", DataType::Int64, false),
959+
Field::new("b", DataType::Int64, false),
960+
Field::new("v", DataType::Int64, false),
961+
Field::new("s", DataType::Utf8View, false),
962+
]));
963+
let groups = PhysicalGroupBy::new_single(vec![
964+
(col("a", &schema)?, "a".into()),
965+
(col("b", &schema)?, "b".into()),
966+
]);
967+
let expressions = vec![
968+
Arc::new(
969+
AggregateExprBuilder::new(sum_udaf(), vec![col("v", &schema)?])
970+
.schema(Arc::clone(&schema))
971+
.alias("sum")
972+
.build()?,
973+
),
974+
Arc::new(
975+
AggregateExprBuilder::new(min_udaf(), vec![col("s", &schema)?])
976+
.schema(Arc::clone(&schema))
977+
.alias("min")
978+
.build()?,
979+
),
980+
];
981+
let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), None)?;
982+
let partial = AggregateExec::try_new(
983+
AggregateMode::Partial,
984+
groups.clone(),
985+
expressions.clone(),
986+
vec![None; 2],
987+
empty,
988+
Arc::clone(&schema),
989+
)?;
990+
let partial_schema = partial.schema();
991+
let ordering =
992+
LexOrdering::new([PhysicalSortExpr::new_default(col("a", &partial_schema)?)])
993+
.unwrap();
994+
let input = TestMemoryExec::try_new(
995+
&[vec![], vec![]],
996+
Arc::clone(&partial_schema),
997+
None,
998+
)?
999+
.try_with_sort_information(vec![ordering])?;
1000+
let aggregate = AggregateExec::try_new(
1001+
AggregateMode::FinalPartitioned,
1002+
groups.as_final(),
1003+
expressions,
1004+
vec![None; 2],
1005+
Arc::new(TestMemoryExec::update_cache(&Arc::new(input))),
1006+
schema,
1007+
)?;
1008+
assert_eq!(
1009+
aggregate.input_order_mode(),
1010+
&InputOrderMode::PartiallySorted(vec![0])
1011+
);
1012+
1013+
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(limit));
1014+
let context = Arc::new(
1015+
TaskContext::default()
1016+
.with_session_config(
1017+
SessionConfig::new().with_batch_size(128).set_bool(
1018+
"datafusion.execution.enable_migration_aggregate",
1019+
true,
1020+
),
1021+
)
1022+
.with_runtime(
1023+
RuntimeEnvBuilder::new()
1024+
.with_max_spill_merge_fan_in(2)
1025+
.with_memory_pool(Arc::clone(&pool))
1026+
.build_arc()?,
1027+
),
1028+
);
1029+
let mut streams = vec![];
1030+
let mut senders = vec![];
1031+
for partition in 0..2 {
1032+
let (sender, receiver) = mpsc::unbounded();
1033+
let input = Box::pin(RecordBatchStreamAdapter::new(
1034+
Arc::clone(&partial_schema),
1035+
receiver,
1036+
));
1037+
let stream = OrderedFinalAggregateStream::new_with_input(
1038+
&aggregate,
1039+
&context,
1040+
partition,
1041+
input,
1042+
aggregate.input_order_mode(),
1043+
)?;
1044+
senders.push(sender);
1045+
streams.push(stream);
1046+
}
1047+
let mut expected = BTreeMap::new();
1048+
let mut make_batch = |partition: i64, start: i64| {
1049+
for value in start..start + 128 {
1050+
let entry = expected
1051+
.entry((
1052+
partition,
1053+
if partition == 0 && value % 128 == 0 {
1054+
0
1055+
} else {
1056+
value
1057+
},
1058+
))
1059+
.or_insert_with(|| (0, (value % 2).to_string()));
1060+
entry.0 += value * 2;
1061+
}
1062+
RecordBatch::try_new(
1063+
Arc::clone(&partial_schema),
1064+
vec![
1065+
Arc::new(Int64Array::from(vec![partition; 128])),
1066+
Arc::new(Int64Array::from_iter_values((start..start + 128).map(
1067+
|value| {
1068+
if partition == 0 && value % 128 == 0 {
1069+
0
1070+
} else {
1071+
value
1072+
}
1073+
},
1074+
))),
1075+
Arc::new(Int64Array::from_iter_values(
1076+
(start..start + 128).map(|v| v * 2),
1077+
)),
1078+
Arc::new(StringViewArray::from_iter_values(
1079+
(start..start + 128).map(|v| if v % 2 == 0 { "0" } else { "1" }),
1080+
)),
1081+
],
1082+
)
1083+
.unwrap()
1084+
};
1085+
// Keep partition 1's incomplete ordered run live while partition 0 spills
1086+
// and replays. Channel inputs return Pending after each supplied batch,
1087+
// making this interleaving independent of task scheduling.
1088+
for batch in 0..55 {
1089+
senders[1]
1090+
.unbounded_send(Ok(make_batch(1, batch * 128)))
1091+
.unwrap();
1092+
assert!(streams[1].next().now_or_never().is_none());
1093+
}
1094+
let held = streams[1].reservation.size();
1095+
assert!(held > 500 * 1024);
1096+
for batch in 0..input_batches {
1097+
// Repeated keys cross spill runs, so replay must merge their sums.
1098+
senders[0]
1099+
.unbounded_send(Ok(make_batch(0, batch * 128)))
1100+
.unwrap();
1101+
assert!(streams[0].next().now_or_never().is_none());
1102+
}
1103+
if limit == 600 * 1024 {
1104+
assert!(aggregate.metrics().unwrap().spill_count().unwrap() > 0);
1105+
}
1106+
let mut first = streams.remove(0);
1107+
match finish {
1108+
Finish::Collect => {
1109+
senders[0].close_channel();
1110+
let mut output = collect(Box::pin(first)).await?;
1111+
assert_eq!(pool.reserved(), held);
1112+
senders[1].close_channel();
1113+
output.extend(collect(Box::pin(streams.remove(0))).await?);
1114+
let mut actual = BTreeMap::new();
1115+
for batch in output {
1116+
let a = batch
1117+
.column(0)
1118+
.as_any()
1119+
.downcast_ref::<Int64Array>()
1120+
.unwrap();
1121+
let b = batch
1122+
.column(1)
1123+
.as_any()
1124+
.downcast_ref::<Int64Array>()
1125+
.unwrap();
1126+
let sum = batch
1127+
.column(2)
1128+
.as_any()
1129+
.downcast_ref::<Int64Array>()
1130+
.unwrap();
1131+
let min = batch
1132+
.column(3)
1133+
.as_any()
1134+
.downcast_ref::<StringViewArray>()
1135+
.unwrap();
1136+
for row in 0..batch.num_rows() {
1137+
assert!(
1138+
actual
1139+
.insert(
1140+
(a.value(row), b.value(row)),
1141+
(sum.value(row), min.value(row).to_string())
1142+
)
1143+
.is_none()
1144+
);
1145+
}
1146+
}
1147+
assert_eq!(actual, expected);
1148+
assert_eq!(
1149+
aggregate.metrics().unwrap().spill_count().unwrap() > 0,
1150+
limit == 600 * 1024
1151+
);
1152+
}
1153+
Finish::DropDuringMerge => {
1154+
senders[0].close_channel();
1155+
let _ = first.next().now_or_never();
1156+
assert!(matches!(
1157+
first.state.as_ref(),
1158+
Some(OrderedFinalAggregateState::MergingSpills { .. })
1159+
));
1160+
drop(first);
1161+
}
1162+
Finish::DropDuringReplay => {
1163+
senders[0].close_channel();
1164+
first.next().await.unwrap()?;
1165+
assert!(pool.reserved() > held);
1166+
drop(first);
1167+
assert_eq!(pool.reserved(), held);
1168+
}
1169+
Finish::InputError => {
1170+
senders[0]
1171+
.unbounded_send(datafusion_common::exec_err!(
1172+
"injected input failure"
1173+
))
1174+
.unwrap();
1175+
let error = first.next().await.unwrap().unwrap_err();
1176+
assert!(error.to_string().contains("injected input failure"));
1177+
assert_eq!(pool.reserved(), held);
1178+
drop(first);
1179+
}
1180+
}
1181+
drop(streams);
1182+
assert_eq!(pool.reserved(), 0);
1183+
Ok(())
1184+
}
1185+
}

‎datafusion/sqllogictest/test_files/ordered_aggregate_spill.slt‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ SET datafusion.optimizer.prefer_existing_sort = true
3535
statement ok
3636
SET datafusion.execution.enable_migration_aggregate = true
3737

38+
# Limit merge buffers so replay can allocate while another partition retains
39+
# its aggregate state in the shared greedy pool.
40+
statement ok
41+
SET datafusion.runtime.max_spill_merge_fan_in = 2
42+
3843
statement ok
3944
SET datafusion.runtime.memory_limit = '1M'
4045

@@ -108,7 +113,7 @@ GROUP BY round(v1, -4), v1 % 5000
108113
----
109114
45000 values hashing to e6ece4b4b86e6152a1c785e90ab5ba12
110115

111-
# Round 2: The same query spills five times with a 600 KB limit.
116+
# Round 2: The same query spills with a 600 KB limit.
112117
statement ok
113118
SET datafusion.runtime.memory_limit = '600K'
114119

@@ -132,7 +137,7 @@ GROUP BY round(v1, -4), v1 % 5000
132137
----
133138
45000 values hashing to e6ece4b4b86e6152a1c785e90ab5ba12
134139

135-
# Round 3: The same query spills six times with a 500 KB limit.
140+
# Round 3: The same query spills with a 500 KB limit.
136141
statement ok
137142
SET datafusion.runtime.memory_limit = '500K'
138143

@@ -162,7 +167,7 @@ GROUP BY round(v1, -4), v1 % 5000
162167
statement ok
163168
SET datafusion.runtime.memory_limit = '600K'
164169

165-
# Ensures final aggregate has spill_count > 0
170+
# Check spilled rows because extra merge passes can change the byte unit.
166171
query TT
167172
EXPLAIN ANALYZE
168173
SELECT round(v1, -4), v1 % 5000,
@@ -171,7 +176,7 @@ FROM generate_series(20000) AS t1(v1)
171176
GROUP BY round(v1, -4), v1 % 5000
172177
----
173178
Plan with Metrics
174-
01)AggregateExec: mode=FinalPartitioned,<slt:ignore>aggr=[sum(t1.v1 * Int64(2)), min(t1.v1 % Int64(2))], ordering_mode=PartiallySorted([0]), metrics=[<slt:ignore>spilled_bytes=<slt:ignore>KB,<slt:ignore>]
179+
01)AggregateExec: mode=FinalPartitioned,<slt:ignore>aggr=[sum(t1.v1 * Int64(2)), min(t1.v1 % Int64(2))], ordering_mode=PartiallySorted([0]), metrics=[<slt:ignore>spilled_rows=<slt:ignore> K,<slt:ignore>]
175180
02)--RepartitionExec:<slt:ignore>input_partitions=1, maintains_sort_order=true<slt:ignore>
176181
03)----AggregateExec: mode=Partial,<slt:ignore>aggr=[sum(t1.v1 * Int64(2)), min(t1.v1 % Int64(2))], ordering_mode=PartiallySorted([0]), metrics=[<slt:ignore>spill_count=0,<slt:ignore>]
177182
<slt:ignore>
@@ -249,5 +254,8 @@ RESET datafusion.execution.batch_size
249254
statement ok
250255
SET datafusion.execution.target_partitions = 4
251256

257+
statement ok
258+
RESET datafusion.runtime.max_spill_merge_fan_in
259+
252260
statement ok
253261
RESET datafusion.catalog.create_default_catalog_and_schema

0 commit comments

Comments
 (0)