Skip to content

Fix AvgGroupsAccumulator::size() self-overcount - #25117

Merged
kosiew merged 4 commits into
apache:mainfrom
kosiew:memcalc-10-23393
Sep 11, 2026
Merged

kosiew merged 4 commits into
apache:mainfrom
kosiew:memcalc-10-23393

Conversation

@kosiew

@kosiew kosiew commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

GroupsAccumulator::size() is expected to report retained state owned by the accumulator without counting the accumulator object itself.

AvgGroupsAccumulator::size() was also charging for the inline Vec descriptors for counts and sums, even though those descriptors are part of the accumulator object rather than separately retained heap state. This caused AVG group accumulators to over-report their memory usage.

What changes are included in this PR?

This PR:

  • Removes the size_of::<Vec<_>>() charges for the inline counts and sums vector descriptors from AvgGroupsAccumulator::size().
  • Keeps accounting for the heap capacities of counts and sums.
  • Keeps accounting for retained null-tracking state through null_state.size().
  • Adds coverage verifying that the sum buffer is accounted for using the actual sum native type, including the Decimal128 input / Decimal256 sum case.

Are these changes tested?

Yes. This PR adds the following focused unit tests in datafusion/functions-aggregate/src/average.rs:

  • avg_groups_size_excludes_accumulator_storage
  • avg_groups_size_uses_sum_native_type

The first verifies that an empty accumulator reports zero retained state and that, after group state is allocated, size() equals the capacities of counts and sums plus null_state.size().

The second verifies that memory accounting uses the sum accumulator's native type by checking a Decimal128 AVG whose sum state uses Decimal256.

No test execution results are shown in the patch.

Are there any user-facing changes?

No direct user-facing API or query-result changes are included.

This changes the memory-size accounting reported by AvgGroupsAccumulator so that it follows the GroupsAccumulator::size() contract and no longer includes inline vector descriptors.

LLM-generated code disclosure

This PR includes LLM-generated code and comments. All LLM-generated content has been manually reviewed.

- Fixed AvgGroupsAccumulator::size() → no inline Vec descriptor charges.
- Added empty + grown-state size test.
… Decimal256 conversion

- Added a new test case in `average.rs` that validates Decimal128 inputs are correctly widened to Decimal256 when computing sums using native i256 arithmetic.
- The test verifies correct behavior for typical values, large values, and edge cases where the sum exceeds the Decimal128 range.
- Includes assertions that ensure the resulting Decimal256 value matches the expected precision and accuracy.
- Ensures the Decimal library’s mixed‑precision handling (Decimal128 → Decimal256) works reliably with the native i256 size.
- Extends the existing test suite, improving confidence in the library’s widened decimal operations.
… with Decimal128(30, 4) result

- Updated `avg_groups_size_uses_sum_native_type` to compute **real AVG types** instead of approximating types.
- **Input type** changed to `Decimal128(26, 0)` for precise group size representation.
- **Intermediate `sum` type** upgraded to `Decimal256(76, 0)` to avoid overflow during aggregation.
- **Returned average type** now uses `Decimal128(30, 4)`, providing sufficient precision and scale for accurate average calculations.
- Adjustments ensure that the function respects the native decimal behavior required for downstream analytics and reporting.
@github-actions github-actions Bot added the functions Changes to functions implementation label Sep 9, 2026
@codecov-commenter

codecov-commenter commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.80%. Comparing base (4048898) to head (81880e1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/average.rs 87.50% 1 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25117      +/-   ##
==========================================
- Coverage   81.80%   81.80%   -0.01%     
==========================================
  Files        1130     1130              
  Lines      417754   417788      +34     
  Branches   417754   417788      +34     
==========================================
+ Hits       341754   341778      +24     
- Misses      55875    55877       +2     
- Partials    20125    20133       +8     

☔ 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 requested a review from comphead September 10, 2026 06:16
@kosiew
kosiew marked this pull request as ready for review September 10, 2026 09:11

@kumarUjjawal kumarUjjawal 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.

Thank you @kosiew

LGTM!

@kosiew

kosiew commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

🚀
@kumarUjjawal
Thank you for your review.

@kosiew
kosiew added this pull request to the merge queue Sep 11, 2026
Merged via the queue into apache:main with commit 6263df2 Sep 11, 2026
38 checks passed
@kosiew
kosiew deleted the memcalc-10-23393 branch September 11, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants