fix(functions-aggregate): guard against slice indexing underflow and allocation hazards in first_last and array_agg - #25751
Draft
IgnatiusPang wants to merge 2 commits into
Conversation
added 2 commits
September 25, 2026 21:55
…irst_last and array_agg
…evaluate, and first_last take_need
Contributor
|
Marking as draft per #25741 (comment) |
alamb
marked this pull request as draft
September 25, 2026 14:44
Author
Rationale for this changeIn distributed query execution (such as Apache Ballista or partitioned stream reducers), intermediate aggregation states are serialized into RecordBatches and exchanged across workers:
What changes are included in this PR?Replace unchecked slice indexing and length subtraction with safe checked and saturating operations:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
When aggregating empty batches or streaming partitioned state arrays across distributed executors, several aggregate accumulators in
datafusion-functions-aggregatelacked boundary checks before indexingstates:1.
FirstValueAccumulatorandLastValueAccumulator(first_last.rs)In
TrivialFirstValueAccumulator::merge_batchandTrivialLastValueAccumulator::merge_batch:If
states.len() < 2, direct indexing panics withindex out of bounds: the len is ... but the index is 1.In
FirstValueAccumulator::merge_batchandLastValueAccumulator::merge_batch:If an empty slice
&[]is received,states.len() - 1underflowsusizetousize::MAX, immediately crashing with an out-of-bounds panic.2.
OrderSensitiveArrayAggAccumulatorandArrayAggAccumulator(array_agg.rs)In
ensure_sorted_indices:If
sorted_len > self.entries.len(),self.entries.len() - sorted_lenunderflows, causing an immediate Out-Of-Memory (OOM) abort when attempting to allocateusize::MAXcapacity.Similarly in
ArrayAggAccumulator::retract_batch:and in
ArrayAggAccumulator::evaluate:If
self.front_offset > front.len(), arithmetic underflow occurs.3.
NthValueAccumulator(nth_value.rs)In
NthValueAccumulator::merge_batch:If
states.len() == 1,is_empty()check passes, but accessingstates[1]panics out-of-bounds. Replaced withif states.len() < 2.4.
take_need(first_last/state.rs)In
take_need:If
n > bool_buf.len(),bool_buf.slice(0, n)panics andbool_buf.len() - nunderflows. Clamped withlet n = n.min(bool_buf.len());.What changes are included in this PR?
first_last.rs:if states.is_empty() { return Ok(()); }andif states.len() < 2 { return Ok(()); }.states.len() - 1withstates.len().saturating_sub(1).array_agg.rs:self.entries.len() - sorted_lenwithself.entries.len().saturating_sub(sorted_len).front.len() - self.front_offsetwithfront.len().saturating_sub(self.front_offset).front_offsetinevaluate()toa.len().group_rows.len() - 1withgroup_rows.len().saturating_sub(1).nth_value.rs:if states.is_empty()withif states.len() < 2before accessingstates[1].first_last/state.rs:ntobool_buf.len()intake_need.Are these changes tested?
Yes, tested across unit and integration suites ensuring zero regression on standard inputs and panic-free handling on empty/sub-slice boundaries.
Are there any user-facing changes?
No user-facing SQL API changes. Improves engine robustness against empty state partitions.