Skip to content

Commit 741d221

Browse files
andygrovekosiew
andauthored
[branch-55] Fix retained-capacity accounting for grouped byte MIN/MAX (#26154) (#26767)
## Which issue does this PR close? - Part of #23393 on the `branch-55` release line. - Backports the merged fix in #26154. ## Rationale for this change In 55.x, a grouped `MIN`/`MAX` over strings or binary values reports less memory than it holds. Its `size()` counts the current length of each group's value rather than the capacity that the value's buffer keeps. So when a long value is replaced by a shorter one, the buffer keeps its old capacity but the reported size drops. Unused capacity in the outer per-group vector is not counted either. Under a memory limit, an aggregate can then hold more memory than the pool knows about, and it spills later than it should, or not at all. #26154 fixed this on `main`, but it is not in 55.1.0 or 55.2.0 and first ships in 56.0.0. This backport is for a 55.3.0 patch release, if one is made. ## What changes are included in this PR? - A cherry-pick with `git cherry-pick -x` of the commit that #26154 merged (0d79252). This PR changes nothing else. - `MinMaxBytesState` tracks the retained capacity of each group's value incrementally, keeping `size()` O(1). `size()` reports that capacity plus the outer vector's capacity. The change is confined to `datafusion/functions-aggregate/src/min_max/min_max_bytes.rs`. - The cherry-pick conflicts only in the test module. On `main`, the file already had a `tests` module containing `preserving_selected_min_values`, which uses `GroupSelection` and `evaluate_preserving`, and neither exists on `branch-55`. The resolution adds the `tests` module with only the test from #26154, `size_tracks_growth_and_emit_reuse`. The non-test changes apply as they are on `main`. ## What is the testing strategy for this PR? Covered by `size_tracks_growth_and_emit_reuse`, the test #26154 added. On `branch-55`: - `cargo fmt --all -- --check` - `cargo clippy -p datafusion-functions-aggregate --all-targets -- -D warnings` - `cargo test -p datafusion-functions-aggregate`: 196 unit tests and 2 doc tests passed, including `size_tracks_growth_and_emit_reuse` ## Are there any user-facing changes? No. Query results and public APIs are unchanged. Grouped byte `MIN`/`MAX` now reports its memory more accurately, so under a memory limit it may spill or fail to reserve memory earlier than before. Co-authored-by: kosiew <[email protected]>
1 parent 6fceca2 commit 741d221

1 file changed

Lines changed: 88 additions & 11 deletions

File tree

‎datafusion/functions-aggregate/src/min_max/min_max_bytes.rs‎

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,10 @@ struct MinMaxBytesState {
383383
min_max: Vec<Option<Vec<u8>>>,
384384
/// The data type of the array
385385
data_type: DataType,
386-
/// The total bytes of the string data (for pre-allocating the final array,
387-
/// and tracking memory usage)
386+
/// The total length of the string data, for pre-allocating the final array.
388387
total_data_bytes: usize,
388+
/// The retained inner vector capacities, cached to keep `size()` O(1).
389+
total_data_capacity: usize,
389390
}
390391

391392
/// Implement the MinMaxBytesAccumulator with a comparison function
@@ -400,22 +401,27 @@ impl MinMaxBytesState {
400401
min_max: vec![],
401402
data_type,
402403
total_data_bytes: 0,
404+
total_data_capacity: 0,
403405
}
404406
}
405407

406408
/// Set the specified group to the given value, updating memory usage appropriately
407409
fn set_value(&mut self, group_index: usize, new_val: &[u8]) {
408410
match self.min_max[group_index].as_mut() {
409411
None => {
410-
self.min_max[group_index] = Some(new_val.to_vec());
412+
let value = new_val.to_vec();
413+
self.total_data_capacity += value.capacity();
414+
self.min_max[group_index] = Some(value);
411415
self.total_data_bytes += new_val.len();
412416
}
413417
Some(existing_val) => {
414418
// Copy data over to avoid re-allocating
415419
self.total_data_bytes -= existing_val.len();
416420
self.total_data_bytes += new_val.len();
421+
self.total_data_capacity -= existing_val.capacity();
417422
existing_val.clear();
418423
existing_val.extend_from_slice(new_val);
424+
self.total_data_capacity += existing_val.capacity();
419425
}
420426
}
421427
}
@@ -476,31 +482,102 @@ impl MinMaxBytesState {
476482

477483
/// Emits the specified min_max values
478484
///
479-
/// Returns (data_capacity, min_maxes), updating the current value of total_data_bytes
485+
/// Returns (data_bytes, min_maxes), updating the current value of total_data_bytes
480486
///
481-
/// - `data_capacity`: the total length of all strings and their contents,
487+
/// - `data_bytes`: the total byte length of the emitted values,
482488
/// - `min_maxes`: the actual min/max values for each group
483489
fn emit_to(&mut self, emit_to: EmitTo) -> (usize, Vec<Option<Vec<u8>>>) {
484490
match emit_to {
485491
EmitTo::All => {
492+
self.total_data_capacity = 0;
486493
(
487494
std::mem::take(&mut self.total_data_bytes), // reset total bytes and min_max
488495
std::mem::take(&mut self.min_max),
489496
)
490497
}
491498
EmitTo::First(n) => {
492499
let first_min_maxes = split_vec_min_alloc(&mut self.min_max, n);
493-
let first_data_capacity: usize = first_min_maxes
500+
let (first_data_bytes, first_data_capacity) = first_min_maxes
494501
.iter()
495-
.map(|opt| opt.as_ref().map(|s| s.len()).unwrap_or(0))
496-
.sum();
497-
self.total_data_bytes -= first_data_capacity;
498-
(first_data_capacity, first_min_maxes)
502+
.flatten()
503+
.fold((0, 0), |(bytes, capacity), value| {
504+
(bytes + value.len(), capacity + value.capacity())
505+
});
506+
self.total_data_bytes -= first_data_bytes;
507+
self.total_data_capacity -= first_data_capacity;
508+
(first_data_bytes, first_min_maxes)
499509
}
500510
}
501511
}
502512

503513
fn size(&self) -> usize {
504-
self.total_data_bytes + self.min_max.len() * size_of::<Option<Vec<u8>>>()
514+
// Only retained heap allocations: the outer slots already include the
515+
// inline Option<Vec<u8>> descriptors.
516+
self.total_data_capacity + self.min_max.capacity() * size_of::<Option<Vec<u8>>>()
517+
}
518+
}
519+
520+
#[cfg(test)]
521+
mod tests {
522+
use super::*;
523+
524+
fn expected_size(state: &MinMaxBytesState) -> usize {
525+
state.min_max.capacity() * size_of::<Option<Vec<u8>>>()
526+
+ state
527+
.min_max
528+
.iter()
529+
.flatten()
530+
.map(Vec::capacity)
531+
.sum::<usize>()
532+
}
533+
534+
#[test]
535+
fn size_tracks_growth_and_emit_reuse() -> Result<()> {
536+
let mut state = MinMaxBytesState::new(DataType::Binary);
537+
assert_eq!(state.size(), 0);
538+
539+
state.min_max = Vec::with_capacity(16);
540+
assert_eq!(state.size(), expected_size(&state));
541+
state.min_max.resize(4, None);
542+
assert_eq!(state.size(), expected_size(&state));
543+
544+
state.set_value(0, b"a");
545+
state.set_value(0, &[b'z'; 128]);
546+
state.set_value(1, b"b");
547+
state.set_value(2, b"c");
548+
state.set_value(3, b"d");
549+
assert_eq!(state.size(), expected_size(&state));
550+
let size_before_replacement = state.size();
551+
552+
state.set_value(0, b"x");
553+
assert_eq!(state.size(), size_before_replacement);
554+
assert!(state.min_max[0].as_ref().unwrap().capacity() >= 128);
555+
assert_eq!(state.total_data_bytes, 4);
556+
assert_eq!(state.size(), expected_size(&state));
557+
558+
let (bytes, emitted) = state.emit_to(EmitTo::First(1));
559+
assert_eq!(emitted[0].as_deref(), Some(b"x".as_slice()));
560+
assert_eq!(bytes, 1);
561+
assert_eq!(state.size(), expected_size(&state));
562+
assert_eq!(state.total_data_bytes, 3);
563+
564+
let (bytes, emitted) = state.emit_to(EmitTo::First(2));
565+
assert_eq!(emitted[0].as_deref(), Some(b"b".as_slice()));
566+
assert_eq!(emitted[1].as_deref(), Some(b"c".as_slice()));
567+
assert_eq!(bytes, 2);
568+
assert_eq!(state.size(), expected_size(&state));
569+
assert_eq!(state.total_data_bytes, 1);
570+
571+
let (bytes, emitted) = state.emit_to(EmitTo::All);
572+
assert_eq!(emitted[0].as_deref(), Some(b"d".as_slice()));
573+
assert_eq!(bytes, 1);
574+
assert_eq!(state.size(), 0);
575+
assert_eq!(state.total_data_bytes, 0);
576+
577+
state.update_batch([Some(b"x".as_slice())], &[0], 1, |a, b| a < b)?;
578+
assert_eq!(state.min_max[0].as_deref(), Some(b"x".as_slice()));
579+
assert_eq!(state.total_data_bytes, 1);
580+
assert_eq!(state.size(), expected_size(&state));
581+
Ok(())
505582
}
506583
}

0 commit comments

Comments
 (0)