Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datafusion/core/tests/sql/explain_analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ async fn explain_analyze_baseline_metrics() {
"AggregateExec: mode=Partial, gby=[]",
"output_bytes="
);

assert_metrics!(
&formatted,
"AggregateExec: mode=Partial, gby=[c1@0 as c1]",
"reduction_factor=5.1% (5/99)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really nice

);

assert_metrics!(
&formatted,
"AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1]",
Expand Down
24 changes: 24 additions & 0 deletions datafusion/physical-plan/src/aggregates/row_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ pub(crate) struct GroupedHashAggregateStream {

/// Aggregation-specific metrics
group_by_metrics: GroupByMetrics,

/// Reduction factor metric, calculated as `output_rows/input_rows` (only for partial aggregation)
reduction_factor: Option<metrics::RatioMetrics>,
}

impl GroupedHashAggregateStream {
Expand Down Expand Up @@ -600,6 +603,16 @@ impl GroupedHashAggregateStream {
None
};

let reduction_factor = if agg.mode == AggregateMode::Partial {
Some(
MetricBuilder::new(&agg.metrics)
.with_type(metrics::MetricType::SUMMARY)
.ratio_metrics("reduction_factor", partition),
)
} else {
None
};

Ok(GroupedHashAggregateStream {
schema: agg_schema,
input,
Expand All @@ -620,6 +633,7 @@ impl GroupedHashAggregateStream {
spill_state,
group_values_soft_limit: agg.limit,
skip_aggregation_probe,
reduction_factor,
})
}
}
Expand Down Expand Up @@ -662,6 +676,11 @@ impl Stream for GroupedHashAggregateStream {
let timer = elapsed_compute.timer();
let input_rows = batch.num_rows();

if let Some(reduction_factor) = self.reduction_factor.as_ref()
{
reduction_factor.add_total(input_rows);
}

// Do the grouping
self.group_aggregate_batch(batch)?;

Expand Down Expand Up @@ -799,6 +818,11 @@ impl Stream for GroupedHashAggregateStream {
let output = batch.slice(0, size);
(ExecutionState::ProducingOutput(remaining), output)
};

if let Some(reduction_factor) = self.reduction_factor.as_ref() {
reduction_factor.add_part(output_batch.num_rows());
}

// Empty record batches should not be emitted.
// They need to be treated as [`Option<RecordBatch>`]es and handled separately
debug_assert!(output_batch.num_rows() > 0);
Expand Down