Skip to content

Commit cd8cef9

Browse files
authored
Add per-aggregate accumulator phase metrics (apache#24423)
## Which issue does this PR close? * Part of apache#23570 ## Rationale for this change Aggregate argument evaluation metrics only measure the cost of producing input arrays and do not show how much time individual aggregate expressions spend performing accumulator work. This PR adds per-aggregate-expression timing for accumulator phases in migrated grouped hash aggregation, making it possible to attribute time spent updating, merging, emitting state, converting input to state, and evaluating accumulators to the aggregate expression performing that work. Existing shared group-by aggregation and emission timing remains in place. ## What changes are included in this PR? * Adds `AggregateAccumulatorMetrics`, which creates per-aggregate timers for accumulator phases. * Adds phase-specific metrics for `update`, `merge`, `state`, `convert_to_state`, and `evaluate`. * Creates accumulator metrics once per aggregate table and shares them with the migrated hash table implementations. * Instruments accumulator operations at the per-aggregate/per-batch boundary rather than per row or group. * Adds instrumentation to both unordered and ordered migrated aggregate tables. * Defines the accumulator phases expected for each `AggregateMode`. * Preserves the existing `GroupByMetrics` and aggregate argument metrics. * Instruments the skip-partial path's `convert_to_state` operation. * Carries the accumulator metrics through partial-to-skip table transitions and spill/replay paths. ## Are these changes tested? Yes. The patch adds or updates tests that: * Verify the accumulator phases associated with each `AggregateMode`. * Verify two aggregate expressions (`SUM(a)` / `SUM(b)` and `SUM(b)` / `SUM(c)` in the respective tests) receive separate metric names and `aggregate` labels. * Verify partial aggregation records positive `update_time` and `state_time` metrics. * Verify final aggregation records positive `merge_time` and `evaluate_time` metrics. * Verify migrated ordered aggregation paths record the expected accumulator phase timings. * Verify skip-partial aggregation records `convert_to_state` timing. * Verify a migrated single aggregate that spills records `update`, `state`, `merge`, and `evaluate` timings. * Explicitly enable `datafusion.execution.enable_migration_aggregate` in the relevant tests. The issue suggests the targeted verification command: ```bash cargo test -p datafusion-physical-plan aggregates ``` The patch itself does not show that command being run, so this PR description does not claim that it passed. ## Are there any user-facing changes? Yes. Execution metrics for migrated grouped hash aggregation now expose per-aggregate-expression accumulator timing, with metrics such as: ```text agg_expr_0_update_time agg_expr_0_merge_time agg_expr_0_state_time agg_expr_0_convert_to_state_time agg_expr_0_evaluate_time ``` Each metric also carries the corresponding `aggregate` label. There are no public accumulator API or aggregate semantics changes shown in this patch. ## LLM-generated code disclosure This PR includes LLM-generated code and comments. All LLM-generated content has been manually reviewed.
1 parent 0fc962f commit cd8cef9

13 files changed

Lines changed: 487 additions & 80 deletions

File tree

‎datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ use datafusion_physical_expr::aggregate::AggregateFunctionExpr;
2828

2929
use crate::PhysicalExpr;
3030
use crate::aggregates::group_values::{
31-
AggregateArgumentMetrics, GroupByMetrics, GroupValues, new_group_values,
31+
AccumulatorPhase, AggregateAccumulatorMetrics, AggregateArgumentMetrics,
32+
GroupByMetrics, GroupValues, new_group_values,
3233
};
3334
use crate::aggregates::grouped_hash_stream::create_group_accumulator;
3435
use crate::aggregates::order::GroupOrdering;
3536
use crate::aggregates::{
36-
AggregateExec, PhysicalGroupBy, aggregate_expressions, aggregate_metric_label,
37-
evaluate_group_by,
37+
AggregateExec, PhysicalGroupBy, aggregate_expressions, evaluate_group_by,
3838
};
3939

40+
use super::AggregateTableMetrics;
41+
4042
/// Marker for raw rows -> partial state aggregation.
4143
pub(in crate::aggregates) struct PartialMarker;
4244
/// Marker for raw rows -> final value aggregation.
@@ -81,6 +83,9 @@ pub(in crate::aggregates) struct AggregateHashTable<AggrMode> {
8183
/// Per-aggregate timing metrics for evaluating aggregate arguments.
8284
pub(super) aggregate_argument_metrics: AggregateArgumentMetrics,
8385

86+
/// Per-aggregate timing metrics for accumulator operations.
87+
pub(super) aggregate_accumulator_metrics: Arc<AggregateAccumulatorMetrics>,
88+
8489
/// Raw input schema, used to evaluate expressions and synthesize empty
8590
/// grouping-set rows.
8691
pub(super) input_schema: SchemaRef,
@@ -140,17 +145,12 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
140145
let group_schema = agg.group_by.group_schema(&input_schema)?;
141146
let group_values = new_group_values(group_schema, &GroupOrdering::None)?;
142147

143-
let aggregate_argument_metrics = AggregateArgumentMetrics::new(
144-
&agg.metrics,
145-
partition,
146-
agg.aggr_expr
147-
.iter()
148-
.map(|agg_expr| aggregate_metric_label(agg_expr)),
149-
);
148+
let metrics = AggregateTableMetrics::new(agg, partition);
150149

151150
Ok(Self {
152-
group_by_metrics: GroupByMetrics::new(&agg.metrics, partition),
153-
aggregate_argument_metrics,
151+
group_by_metrics: metrics.group_by,
152+
aggregate_argument_metrics: metrics.aggregate_arguments,
153+
aggregate_accumulator_metrics: metrics.accumulator,
154154
input_schema,
155155
output_schema,
156156
state_schema,
@@ -208,8 +208,10 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
208208
&mut self,
209209
batch: &RecordBatch,
210210
aggregate_fn: AggregateBatchFn,
211+
accumulator_phase: AccumulatorPhase,
211212
) -> Result<()> {
212213
let evaluated_batch = self.evaluate_batch(batch)?;
214+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
213215
let state = self.state.building_mut();
214216

215217
let _timer = self.group_by_metrics.aggregation_time.timer();
@@ -220,12 +222,15 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
220222
let group_indices = &state.batch_group_indices;
221223
let total_num_groups = state.group_values.len();
222224

223-
for (acc, values) in state
225+
for (idx, (acc, values)) in state
224226
.accumulators
225227
.iter_mut()
226228
.zip(evaluated_batch.accumulator_args.iter())
229+
.enumerate()
227230
{
228-
aggregate_fn(acc, values, group_indices, total_num_groups)?;
231+
accumulator_metrics.time(idx, accumulator_phase, || {
232+
aggregate_fn(acc, values, group_indices, total_num_groups)
233+
})?;
229234
}
230235
}
231236

@@ -244,9 +249,11 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
244249
pub(super) fn next_output_batch_inner(
245250
&mut self,
246251
materialize_accumulator_fn: MaterializeAccumulatorFn,
252+
accumulator_phase: AccumulatorPhase,
247253
) -> Result<Option<RecordBatch>> {
248254
let output_schema = Arc::clone(&self.output_schema);
249255
let batch_size = self.batch_size;
256+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
250257

251258
let mut output =
252259
match std::mem::replace(&mut self.state, AggregateHashTableState::Done) {
@@ -260,8 +267,12 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
260267
let emit_to = EmitTo::All;
261268
let timer = self.group_by_metrics.emitting_time.timer();
262269
let mut columns = state.group_values.emit(emit_to)?;
263-
for acc in state.accumulators.iter_mut() {
264-
columns.extend(materialize_accumulator_fn(acc, emit_to)?);
270+
for (idx, acc) in state.accumulators.iter_mut().enumerate() {
271+
columns.extend(accumulator_metrics.time(
272+
idx,
273+
accumulator_phase,
274+
|| materialize_accumulator_fn(acc, emit_to),
275+
)?);
265276
}
266277
drop(timer);
267278

@@ -322,14 +333,19 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
322333
&mut self,
323334
) -> Result<Option<RecordBatch>> {
324335
let state_schema = Arc::clone(&self.state_schema);
336+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
325337
let state = self.state.building_mut();
326338
if state.group_values.is_empty() {
327339
return Ok(None);
328340
}
329341

330342
let mut output = state.group_values.emit(EmitTo::All)?;
331-
for acc in &mut state.accumulators {
332-
output.extend(acc.state(EmitTo::All)?);
343+
for (idx, acc) in state.accumulators.iter_mut().enumerate() {
344+
output.extend(accumulator_metrics.time(
345+
idx,
346+
AccumulatorPhase::State,
347+
|| acc.state(EmitTo::All),
348+
)?);
333349
}
334350

335351
let batch = RecordBatch::try_new(state_schema, output)?;

‎datafusion/physical-plan/src/aggregates/aggregate_hash_table/common_ordered.rs‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ use datafusion_expr::EmitTo;
3131
use crate::InputOrderMode;
3232
use crate::PhysicalExpr;
3333
use crate::aggregates::group_values::{
34-
AggregateArgumentMetrics, GroupByMetrics, GroupValues, new_group_values,
34+
AccumulatorPhase, AggregateAccumulatorMetrics, AggregateArgumentMetrics,
35+
GroupByMetrics, GroupValues, new_group_values,
3536
};
3637
use crate::aggregates::grouped_hash_stream::create_group_accumulator;
3738
use crate::aggregates::order::GroupOrdering;
3839
use crate::aggregates::{
3940
AggregateExec, AggregateMode, PhysicalGroupBy, aggregate_expressions,
40-
aggregate_metric_label, evaluate_group_by,
41+
evaluate_group_by,
4142
};
4243

44+
use super::AggregateTableMetrics;
4345
use super::common::{
4446
AggregateAccumulator, AggregateBatchFn, AggregateHashTable, EvaluatedAggregateBatch,
4547
MaterializeAccumulatorFn,
@@ -49,20 +51,16 @@ use super::common::{
4951
pub(in crate::aggregates) struct OrderedAggregateTableMetrics {
5052
pub(super) group_by: GroupByMetrics,
5153
pub(super) aggregate_arguments: AggregateArgumentMetrics,
54+
pub(super) accumulator: Arc<AggregateAccumulatorMetrics>,
5255
}
5356

5457
impl OrderedAggregateTableMetrics {
5558
pub(in crate::aggregates) fn new(agg: &AggregateExec, partition: usize) -> Self {
56-
let aggregate_arguments = AggregateArgumentMetrics::new(
57-
&agg.metrics,
58-
partition,
59-
agg.aggr_expr
60-
.iter()
61-
.map(|agg_expr| aggregate_metric_label(agg_expr)),
62-
);
59+
let metrics = AggregateTableMetrics::new(agg, partition);
6360
Self {
64-
group_by: GroupByMetrics::new(&agg.metrics, partition),
65-
aggregate_arguments,
61+
group_by: metrics.group_by,
62+
aggregate_arguments: metrics.aggregate_arguments,
63+
accumulator: metrics.accumulator,
6664
}
6765
}
6866

@@ -72,6 +70,7 @@ impl OrderedAggregateTableMetrics {
7270
Self {
7371
group_by: table.group_by_metrics.clone(),
7472
aggregate_arguments: table.aggregate_argument_metrics.clone(),
73+
accumulator: Arc::clone(&table.aggregate_accumulator_metrics),
7574
}
7675
}
7776
}
@@ -136,6 +135,9 @@ pub(in crate::aggregates) struct OrderedAggregateTable<OrderedAggrMode> {
136135
/// Per-aggregate timing metrics for evaluating aggregate arguments.
137136
pub(super) aggregate_argument_metrics: AggregateArgumentMetrics,
138137

138+
/// Per-aggregate timing metrics for accumulator operations.
139+
pub(super) aggregate_accumulator_metrics: Arc<AggregateAccumulatorMetrics>,
140+
139141
/// Group keys, ordering state, and accumulator states.
140142
pub(super) buffer: OrderedAggregateTableBuffer,
141143

@@ -222,6 +224,7 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
222224
batch_size,
223225
group_by_metrics: metrics.group_by,
224226
aggregate_argument_metrics: metrics.aggregate_arguments,
227+
aggregate_accumulator_metrics: metrics.accumulator,
225228
buffer: OrderedAggregateTableBuffer {
226229
group_by: Arc::clone(&agg.group_by),
227230
group_ordering,
@@ -304,6 +307,7 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
304307
OrderedAggregateTableMetrics {
305308
group_by: self.group_by_metrics.clone(),
306309
aggregate_arguments: self.aggregate_argument_metrics.clone(),
310+
accumulator: Arc::clone(&self.aggregate_accumulator_metrics),
307311
}
308312
}
309313

@@ -321,9 +325,14 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
321325
return Ok(None);
322326
}
323327

328+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
324329
let mut output = self.buffer.group_values.emit(EmitTo::All)?;
325-
for acc in &mut self.buffer.accumulators {
326-
output.extend(acc.state(EmitTo::All)?);
330+
for (idx, acc) in self.buffer.accumulators.iter_mut().enumerate() {
331+
output.extend(accumulator_metrics.time(
332+
idx,
333+
AccumulatorPhase::State,
334+
|| acc.state(EmitTo::All),
335+
)?);
327336
}
328337

329338
let batch = RecordBatch::try_new(Arc::clone(&self.state_schema), output)?;
@@ -369,7 +378,9 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
369378
&mut self,
370379
evaluated_batch: &EvaluatedAggregateBatch,
371380
aggregate_fn: AggregateBatchFn,
381+
accumulator_phase: AccumulatorPhase,
372382
) -> Result<()> {
383+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
373384
for group_values in &evaluated_batch.grouping_set_args {
374385
let starting_num_groups = self.buffer.group_values.len();
375386
self.buffer
@@ -385,13 +396,21 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
385396
}
386397

387398
let timer = self.group_by_metrics.aggregation_time.timer();
388-
for (acc, values) in self
399+
for (idx, (acc, values)) in self
389400
.buffer
390401
.accumulators
391402
.iter_mut()
392403
.zip(evaluated_batch.accumulator_args.iter())
404+
.enumerate()
393405
{
394-
aggregate_fn(acc, values, &self.buffer.group_indices, total_num_groups)?;
406+
accumulator_metrics.time(idx, accumulator_phase, || {
407+
aggregate_fn(
408+
acc,
409+
values,
410+
&self.buffer.group_indices,
411+
total_num_groups,
412+
)
413+
})?;
395414
}
396415
drop(timer);
397416
}
@@ -409,6 +428,7 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
409428
pub(super) fn next_output_batch_inner(
410429
&mut self,
411430
materialize_accumulator_fn: MaterializeAccumulatorFn,
431+
accumulator_phase: AccumulatorPhase,
412432
) -> Result<Option<RecordBatch>> {
413433
if self.buffer.group_values.is_empty() {
414434
return Ok(None);
@@ -420,6 +440,7 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
420440
let (emit_to, should_remove_groups) =
421441
self.clamp_emit_to(self.buffer.group_values.len(), emit_to);
422442

443+
let accumulator_metrics = Arc::clone(&self.aggregate_accumulator_metrics);
423444
let timer = self.group_by_metrics.emitting_time.timer();
424445
let mut output = self.buffer.group_values.emit(emit_to)?;
425446
if should_remove_groups {
@@ -432,8 +453,10 @@ impl<AggrMode> OrderedAggregateTable<AggrMode> {
432453
}
433454
}
434455

435-
for acc in &mut self.buffer.accumulators {
436-
output.extend(materialize_accumulator_fn(acc, emit_to)?);
456+
for (idx, acc) in self.buffer.accumulators.iter_mut().enumerate() {
457+
output.extend(accumulator_metrics.time(idx, accumulator_phase, || {
458+
materialize_accumulator_fn(acc, emit_to)
459+
})?);
437460
}
438461
drop(timer);
439462

‎datafusion/physical-plan/src/aggregates/aggregate_hash_table/final_table.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use arrow::record_batch::RecordBatch;
2222
use datafusion_common::Result;
2323

2424
use crate::aggregates::AggregateExec;
25+
use crate::aggregates::group_values::AccumulatorPhase;
2526

2627
use super::common::{AggregateHashTable, FinalMarker, HashAggregateAccumulator};
2728

@@ -58,7 +59,10 @@ impl AggregateHashTable<FinalMarker> {
5859
pub(in crate::aggregates) fn next_output_batch(
5960
&mut self,
6061
) -> Result<Option<RecordBatch>> {
61-
self.next_output_batch_inner(HashAggregateAccumulator::evaluate_to_columns)
62+
self.next_output_batch_inner(
63+
HashAggregateAccumulator::evaluate_to_columns,
64+
AccumulatorPhase::Evaluate,
65+
)
6266
}
6367

6468
/// Final aggregation consumes partial aggregate states and merges them into
@@ -67,7 +71,11 @@ impl AggregateHashTable<FinalMarker> {
6771
&mut self,
6872
batch: &RecordBatch,
6973
) -> Result<()> {
70-
self.aggregate_batch_inner(batch, HashAggregateAccumulator::merge_batch)
74+
self.aggregate_batch_inner(
75+
batch,
76+
HashAggregateAccumulator::merge_batch,
77+
AccumulatorPhase::Merge,
78+
)
7179
}
7280

7381
pub(in crate::aggregates) fn start_output(&mut self) -> Result<()> {

0 commit comments

Comments
 (0)