kosiew commented on code in PR #27196:
URL: https://github.com/apache/datafusion/pull/27196#discussion_r4731848688
##########
datafusion/physical-plan/src/aggregates/order/partial.rs:
##########
@@ -87,15 +88,10 @@ enum State {
}
impl State {
- fn heap_size(&self) -> usize {
+ fn heap_size(&self, ctx: &mut DFHeapSizeCtx) -> usize {
match self {
- State::Taken => 0,
- State::Start => 0,
- State::InProgress { group_key, .. } => group_key
- .iter()
- .map(|scalar_value| scalar_value.size())
- .sum(),
- State::Complete => 0,
+ State::InProgress { group_key, .. } => group_key.heap_size(ctx),
Review Comment:
Arc and Arrow's get_array_memory_size() both charge the outer array object,
producing 104 excess bytes for a struct key. Please fix the concrete array
estimator or preserve the previous scalar payload accounting, and add an
extracted struct-key regression with independent byte expectations.
##########
datafusion/physical-plan/src/aggregates/order/partial.rs:
##########
@@ -346,4 +345,41 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn heap_size_counts_retained_sort_key_capacity() -> Result<()> {
+ // An in-progress sort key with spare capacity and a heap-owning
scalar.
+ let mut group_key = Vec::with_capacity(4);
+ let key = String::from("retained sort key");
+ let key_capacity = key.capacity();
+ group_key.push(ScalarValue::Utf8(Some(key)));
+ let group_key_capacity = group_key.capacity();
+ assert!(group_key_capacity > group_key.len());
+
+ let mut grouping_indices = Vec::with_capacity(8);
+ grouping_indices.push(0);
+ let grouping_indices_capacity = grouping_indices.capacity();
+
+ let mut partial = GroupClusteringPartial::try_new(grouping_indices)?;
+ partial.state = State::InProgress {
+ current_run_start: 0,
+ group_key,
+ current: 0,
+ };
+
+ // Every scalar slot, including the spare ones, is charged; the
+ // scalar's own heap payload is charged on top of its slot.
+ let expected = grouping_indices_capacity * size_of::<usize>()
+ + group_key_capacity * size_of::<ScalarValue>()
+ + key_capacity;
+ assert_eq!(partial.heap_size(), expected);
+
+ // Accounting only the live element would miss the spare slots.
Review Comment:
Could we drop the live_only comparison? The spare-capacity assertion and
exact byte equality already detect both under-counting and over-counting.
##########
datafusion/physical-plan/src/aggregates/order/mod.rs:
##########
@@ -215,12 +216,18 @@ mod tests {
GroupClustering::Partial(GroupClusteringPartial::try_new(grouping_indices)?);
assert_eq!(group_clustering.size(), expected);
- // A variable-width key checks both the scalar descriptor and its
payload.
+ // A variable-width key checks retained sort-key capacity, the scalar
+ // slots, and the scalar payload.
let key = "retained group key";
let batch_group_values: Vec<ArrayRef> =
vec![Arc::new(StringArray::from(vec!["a", key]))];
group_clustering.new_groups(&batch_group_values, &[0, 1], 2)?;
- let in_progress = expected +
ScalarValue::Utf8(Some(key.to_owned())).size();
+ let group_key = get_row_at_idx(&batch_group_values, 1)?;
+ // `size_of_vec` includes the inline Vec descriptor, which
+ // `GroupClustering::size` already charges via `size_of::<Self>()`.
+ let retained_sort_key =
+ ScalarValue::size_of_vec(&group_key) -
size_of::<Vec<ScalarValue>>();
+ let in_progress = expected + retained_sort_key;
assert_eq!(group_clustering.size(), in_progress);
Review Comment:
Optional: call reset() with a live key and verify that only grouping-index
capacity remains charged. Refill the key before the existing completion
assertion to cover both transitions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]