Skip to content

fix(physical-plan): fix aggregation_time metric inflation on multi-aggregate GROUP BY - #24743

Closed
arnavahire19 wants to merge 1 commit into
apache:mainfrom
arnavahire19:fix/aggregation-time-metric-inflation
Closed

fix(physical-plan): fix aggregation_time metric inflation on multi-aggregate GROUP BY#24743
arnavahire19 wants to merge 1 commit into
apache:mainfrom
arnavahire19:fix/aggregation-time-metric-inflation

Conversation

@arnavahire19

Copy link
Copy Markdown

Closes #24735.

Problem

In GroupedHashAggregateStream::process_batch, agg_start_time is recorded once before iterating over the accumulators. However, self.group_by_metrics.aggregation_time.add_elapsed(agg_start_time) was placed inside the accumulator loop.

For queries with $N$ aggregate functions, every iteration re-adds the accumulated duration from the start of the loop (sum_{k=1..N} (t_1 + ... + t_k)), inflating the reported aggregation_time metric by up to $(N+1)/2$x.

Solution

Move add_elapsed(agg_start_time) outside and after the accumulator loop so that the total time spent updating/merging accumulators for the batch is recorded exactly once per batch. This matches the behavior in aggregate_hash_table/common.rs and aggregate_hash_table/common_ordered.rs.

Testing

Ran cargo test --package datafusion-physical-plan --lib aggregates (all 193 tests passing).

… per accumulator

In GroupedHashAggregateStream, add_elapsed was called inside the accumulator loop using agg_start_time recorded before the loop. On queries with multiple aggregate functions, this compounded elapsed time across all accumulators in each batch and inflated aggregation_time up to (N+1)/2 times.

Moving add_elapsed after the loop aligns grouped_hash_stream with the scoped timer pattern used across the other aggregate hash table implementations.
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Aug 28, 2026
@kumarUjjawal

Copy link
Copy Markdown
Contributor

Closing this in favour of #24736

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.47%. Comparing base (ee59f62) to head (c9710da).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24743      +/-   ##
==========================================
+ Coverage   81.42%   81.47%   +0.04%     
==========================================
  Files        1121     1122       +1     
  Lines      402142   403629    +1487     
  Branches   402142   403629    +1487     
==========================================
+ Hits       327460   328860    +1400     
- Misses      55484    55510      +26     
- Partials    19198    19259      +61     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aggregation_time metric is inflated up to (N+1)/2 times on GROUP BY nodes with N aggregate functions

3 participants