Skip to content

feat: Fix output bytes metric in hash agg - #25086

Open
ariel-miculas wants to merge 4 commits into
apache:mainfrom
ariel-miculas:fix-output-bytes-metric-in-hash-agg-2
Open

ariel-miculas wants to merge 4 commits into
apache:mainfrom
ariel-miculas:fix-output-bytes-metric-in-hash-agg-2

Conversation

@ariel-miculas

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Alternative POC to #23777

Rationale for this change

Summing together the produced sliced RecordBatches in hash aggregation leads to inflated output_bytes metric

What changes are included in this PR?

What is the testing strategy for this PR?

sqllogic test and unit tests

Are there any user-facing changes?

@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) common Related to common crate physical-plan Changes to the physical-plan crate labels Sep 8, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.73006% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.74%. Comparing base (57740ca) to head (a1c65f0).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/physical-plan/src/aggregates/mod.rs 87.39% 2 Missing and 13 partials ⚠️
...usion/physical-expr-common/src/metrics/baseline.rs 57.14% 3 Missing ⚠️
...fusion/physical-plan/src/aggregates/hash_stream.rs 77.77% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25086      +/-   ##
==========================================
+ Coverage   81.71%   81.74%   +0.03%     
==========================================
  Files        1127     1128       +1     
  Lines      416072   416782     +710     
  Branches   416072   416782     +710     
==========================================
+ Hits       339993   340708     +715     
+ Misses      56091    56001      -90     
- Partials    19988    20073      +85     

☔ 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.

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ariel-miculas,

Thanks for working on this. The approach looks good overall, and I don't see anything blocking this PR. I left a couple of suggestions around the SQLLogicTest documentation and coverage of the migrated hash aggregate stream paths.

# Using `RecordBatch::record_output` (as the streams used to) recomputes
# memory usage from scratch on every slice, with no memory of buffers
# already counted -- so all N slices of one materialization each count the
# shared buffers again, inflating `output_bytes` by ~Nx. `RecordBatchMemoryMetrics`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small documentation nit: the new implementation doesn't use RecordBatchMemoryMetrics, and I don't think that type exists. The fix records the unsliced materialized batch once via BaselineMetrics::record_output_bytes. Could you update this test header to describe the mechanism that's actually being tested?

/// range, giving many independently allocated emits. `batch_size` 8192
/// leaves them whole; `batch_size` 100 slices each 512-row emit into 6.
#[tokio::test]
async fn output_bytes_metric_is_exact_for_sliced_and_dropped_batches() -> Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add some focused metric regression coverage for the migrated hash streams changed by this PR: PartialHashAggregateStream, FinalHashAggregateStream, SingleHashAggregateStream, and PartialReduceHashAggregateStream? The migration flag already defaults to true, but this test provides sorted input, so it selects OrderedPartialAggregateStream rather than PartialHashAggregateStream. It would be useful to use linear input and assert which stream path is selected, or construct the streams directly, while still exercising sliced output. Similar legacy fallback coverage would also be useful while GroupedHashAggregateStream remains supported.

@ariel-miculas

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look, @kosiew
I think the blocker is choosing the right design between the current PR's proposal and #23777, which has some issue with deduplicating distinct Buffers due to address reuse (new allocations can get the same pointer address from the Allocator).
So I'm waiting for @2010YOUY01 to have a look, since he reviewed the original PR for solving this issue.

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

Labels

common Related to common crate physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect output_bytes metric in hash aggregation

3 participants