Skip to content

Commit e1d7fe2

Browse files
authored
fix: charge accumulators' construction-time state to the memory pool in ungrouped aggregation (apache#24692)
## Which issue does this PR close? - Closes apache#24691. ## Rationale for this change An ungrouped aggregation can hold arbitrarily large accumulator state that the memory pool never sees. `AggregateStream` grows its reservation only by per-batch `size()` deltas (`size_post.saturating_sub(size_pre)` in `aggregate_batch`), so an accumulator that allocates its retained state in its constructor and never resizes it is charged nothing for the stream's lifetime. The grouped path already handles this: `GroupsAccumulatorAdapter` charges `state.size()` when it creates each accumulator. Embedding DataFusion 54.1.0 in a Spark accelerator we measured Spark's runtime-filter `bloom_filter_agg` (an 8 MiB bit array zero-filled at construction, one per concurrent task) running entirely unaccounted, so a fair-spill pool could neither fail admission nor pressure other consumers to spill. ## What changes are included in this PR? One charge in `AggregateStream::new`: after registering the reservation, `try_grow` the sum of the accumulators' initial `size()`. Per-batch deltas compose on top of it, so there is no double counting. `test_oom`'s ungrouped arm is updated for the earlier failure point: with its 1-byte limit the median accumulator's initial 48 bytes now fail admission at `execute_typed`, so that arm asserts the construction-time `ResourcesExhausted` instead of collecting first; the grouped arms are unchanged. ## Are these changes tested? The updated `test_oom` is the regression pin: on `main` without the fix the ungrouped stream constructs successfully and the arm fails, with the fix it passes. The full `datafusion-physical-plan` lib suite passes (1784 tests). ## Are there any user-facing changes? Yes, behavioral: an ungrouped aggregation whose accumulators' construction-time state exceeds the memory limit now fails with `ResourcesExhausted` at `execute` instead of silently running past the configured limit, and memory-limited setups that relied on that overshoot may need limits raised to what the query actually uses. No API changes.
1 parent 0f55902 commit e1d7fe2

2 files changed

Lines changed: 148 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ impl AggregateStream {
324324

325325
let reservation = MemoryConsumer::new(format!("AggregateStream[{partition}]"))
326326
.register(context.memory_pool());
327+
// Accumulators that allocate their state at construction are invisible to the
328+
// per-batch size deltas in `aggregate_batch`, so charge their initial sizes up
329+
// front (mirroring `GroupsAccumulatorAdapter`, which charges `state.size()` when
330+
// it creates each accumulator).
331+
reservation.try_grow(accumulators.iter().map(|accum| accum.size()).sum())?;
327332

328333
// Enable dynamic filter if:
329334
// 1. AggregateExec did the check and ensure it supports the dynamic filter

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

Lines changed: 143 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,7 +3235,7 @@ mod tests {
32353235
use crate::test::TestMemoryExec;
32363236
use crate::test::assert_is_pending;
32373237
use crate::test::exec::{
3238-
BlockingExec, StatisticsExec, assert_strong_count_converges_to_zero,
3238+
BlockingExec, PanicExec, StatisticsExec, assert_strong_count_converges_to_zero,
32393239
};
32403240

32413241
use arrow::array::{
@@ -4011,21 +4011,32 @@ mod tests {
40114011
Arc::clone(&input_schema),
40124012
)?);
40134013

4014-
let stream = partial_aggregate.execute_typed(0, &task_ctx)?;
4014+
let stream = partial_aggregate.execute_typed(0, &task_ctx);
40154015

40164016
// ensure that we really got the version we wanted
4017-
match version {
4017+
let stream = match version {
40184018
0 => {
4019-
assert!(matches!(stream, StreamType::AggregateStream(_)));
4019+
// the ungrouped stream charges its accumulators' construction-time
4020+
// state up front, so admission fails before any input is read
4021+
let err = stream.err().unwrap();
4022+
assert!(
4023+
matches!(err.find_root(), DataFusionError::ResourcesExhausted(_)),
4024+
"Wrong error type: {err}",
4025+
);
4026+
continue;
40204027
}
40214028
1 => {
4029+
let stream = stream?;
40224030
assert!(matches!(stream, StreamType::GroupedHash(_)));
4031+
stream
40234032
}
40244033
2 => {
4034+
let stream = stream?;
40254035
assert!(matches!(stream, StreamType::SingleHash(_)));
4036+
stream
40264037
}
40274038
_ => panic!("Unknown version: {version}"),
4028-
}
4039+
};
40294040

40304041
let stream: SendableRecordBatchStream = stream.into();
40314042
let err = collect(stream).await.unwrap_err();
@@ -4041,6 +4052,133 @@ mod tests {
40414052
Ok(())
40424053
}
40434054

4055+
/// An accumulator that allocates its retained state in its constructor and keeps that
4056+
/// size across `update_batch` (Spark's `bloom_filter_agg` shape) is charged at
4057+
/// `execute`, so it is rejected before any input is polled and, when it fits, is
4058+
/// visible to the pool for the stream's lifetime.
4059+
#[tokio::test]
4060+
async fn test_ungrouped_agg_charges_constructor_allocated_state() -> Result<()> {
4061+
const STATE_BYTES: usize = 8 * 1024 * 1024;
4062+
4063+
let schema: SchemaRef =
4064+
Arc::new(Schema::new(vec![Field::new("b", DataType::Int32, true)]));
4065+
// panics when polled, so nothing here can pass by reading the input first
4066+
let input: Arc<dyn ExecutionPlan> =
4067+
Arc::new(PanicExec::new(Arc::clone(&schema), 1));
4068+
let udaf = Arc::new(AggregateUDF::from(ConstructorAllocatingUdaf::new(
4069+
STATE_BYTES,
4070+
)));
4071+
let aggregates: Vec<Arc<AggregateFunctionExpr>> = vec![Arc::new(
4072+
AggregateExprBuilder::new(udaf, vec![col("b", &schema)?])
4073+
.schema(Arc::clone(&schema))
4074+
.alias("constructor_allocating(b)")
4075+
.build()?,
4076+
)];
4077+
let aggregate = Arc::new(AggregateExec::try_new(
4078+
AggregateMode::Single,
4079+
PhysicalGroupBy::default(),
4080+
aggregates,
4081+
vec![None],
4082+
input,
4083+
Arc::clone(&schema),
4084+
)?);
4085+
4086+
let too_small = RuntimeEnvBuilder::new()
4087+
.with_memory_limit(STATE_BYTES / 2, 1.0)
4088+
.build_arc()?;
4089+
let task_ctx = Arc::new(TaskContext::default().with_runtime(too_small));
4090+
let err = aggregate
4091+
.execute_typed(0, &task_ctx)
4092+
.err()
4093+
.expect("initial accumulator state should not fit the pool");
4094+
assert!(
4095+
matches!(err.find_root(), DataFusionError::ResourcesExhausted(_)),
4096+
"Wrong error type: {err}",
4097+
);
4098+
4099+
let fits = RuntimeEnvBuilder::new()
4100+
.with_memory_limit(4 * STATE_BYTES, 1.0)
4101+
.build_arc()?;
4102+
let pool = Arc::clone(&fits.memory_pool);
4103+
let task_ctx = Arc::new(TaskContext::default().with_runtime(fits));
4104+
let stream = aggregate.execute_typed(0, &task_ctx)?;
4105+
assert!(matches!(stream, StreamType::AggregateStream(_)));
4106+
assert!(
4107+
pool.reserved() >= STATE_BYTES,
4108+
"initial accumulator state not charged: {} bytes reserved",
4109+
pool.reserved(),
4110+
);
4111+
4112+
Ok(())
4113+
}
4114+
4115+
/// UDAF whose accumulator allocates all of its state at construction, the shape the
4116+
/// per-batch `size()` deltas in `aggregate_batch` cannot observe.
4117+
#[derive(Debug, PartialEq, Eq, Hash)]
4118+
struct ConstructorAllocatingUdaf {
4119+
signature: Signature,
4120+
state_bytes: usize,
4121+
}
4122+
4123+
impl ConstructorAllocatingUdaf {
4124+
fn new(state_bytes: usize) -> Self {
4125+
Self {
4126+
signature: Signature::any(1, Volatility::Immutable),
4127+
state_bytes,
4128+
}
4129+
}
4130+
}
4131+
4132+
impl AggregateUDFImpl for ConstructorAllocatingUdaf {
4133+
fn name(&self) -> &str {
4134+
"constructor_allocating"
4135+
}
4136+
4137+
fn signature(&self) -> &Signature {
4138+
&self.signature
4139+
}
4140+
4141+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
4142+
Ok(DataType::Int64)
4143+
}
4144+
4145+
fn accumulator(
4146+
&self,
4147+
_acc_args: AccumulatorArgs,
4148+
) -> Result<Box<dyn Accumulator>> {
4149+
Ok(Box::new(ConstructorAllocatingAccumulator {
4150+
state: vec![0; self.state_bytes],
4151+
}))
4152+
}
4153+
}
4154+
4155+
#[derive(Debug)]
4156+
struct ConstructorAllocatingAccumulator {
4157+
state: Vec<u8>,
4158+
}
4159+
4160+
impl Accumulator for ConstructorAllocatingAccumulator {
4161+
fn update_batch(&mut self, _values: &[ArrayRef]) -> Result<()> {
4162+
Ok(())
4163+
}
4164+
4165+
fn merge_batch(&mut self, _states: &[ArrayRef]) -> Result<()> {
4166+
Ok(())
4167+
}
4168+
4169+
fn evaluate(&mut self) -> Result<ScalarValue> {
4170+
Ok(ScalarValue::Int64(Some(self.state.len() as i64)))
4171+
}
4172+
4173+
fn state(&mut self) -> Result<Vec<ScalarValue>> {
4174+
self.evaluate().map(|value| vec![value])
4175+
}
4176+
4177+
fn size(&self) -> usize {
4178+
size_of_val(self) + self.state.capacity()
4179+
}
4180+
}
4181+
40444182
#[tokio::test]
40454183
async fn partial_grouped_aggregate_uses_raw_partial_stream() -> Result<()> {
40464184
let (schema, batches) = some_data();

0 commit comments

Comments
 (0)