Skip to content

Commit 97d60af

Browse files
committed
feat: shrink_to_fit in multi-group-by
1 parent ab2219e commit 97d60af

4 files changed

Lines changed: 12 additions & 11 deletions

File tree

datafusion/expr-common/src/groups_accumulator.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Vectorized [`GroupsAccumulator`]
1919
2020
use arrow::array::{ArrayRef, BooleanArray};
21-
use datafusion_common::{Result, not_impl_err};
21+
use datafusion_common::{Result, not_impl_err, utils::split_vec_min_alloc};
2222

2323
/// Describes how many rows should be emitted during grouping.
2424
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -45,13 +45,7 @@ impl EmitTo {
4545
// Take the entire vector, leave new (empty) vector
4646
std::mem::take(v)
4747
}
48-
Self::First(n) => {
49-
// get end n+1,.. values into t
50-
let mut t = v.split_off(*n);
51-
// leave n+1,.. in v
52-
std::mem::swap(v, &mut t);
53-
t
54-
}
48+
Self::First(n) => split_vec_min_alloc(v, *n),
5549
}
5650
}
5751
}

datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ impl<const NULLABLE: bool> GroupColumn for BooleanGroupValueBuilder<NULLABLE> {
188188

189189
// take only first n values from the original builder
190190
new_builder.truncate(n);
191+
let mut buffer = new_builder.finish();
192+
buffer.shrink_to_fit();
191193

192-
Arc::new(BooleanArray::new(new_builder.finish(), first_n_nulls))
194+
Arc::new(BooleanArray::new(buffer, first_n_nulls))
193195
}
194196
}
195197

datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ where
401401
// Find out a way to avoid copying buffer but split the original one into two.
402402
remaining_buffer.append_slice(&self.buffer.as_slice()[first_remaining_offset..]);
403403
self.buffer.truncate(first_remaining_offset);
404-
let values = self.buffer.finish();
404+
let mut values = self.buffer.finish();
405+
values.shrink_to_fit();
405406
self.buffer = remaining_buffer;
406407

407408
match self.output_type {

datafusion/physical-plan/src/aggregates/group_values/null_builder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ impl MaybeNullBufferBuilder {
8787

8888
// take only first n values from the original builder
8989
new_builder.truncate(n);
90-
new_builder.finish()
90+
let mut buffer = new_builder.finish();
91+
if let Some(b) = buffer.as_mut() {
92+
b.shrink_to_fit()
93+
}
94+
buffer
9195
}
9296

9397
/// Returns true if this builder might have any nulls

0 commit comments

Comments
 (0)