Skip to content

Document decimal AVG wrapping arithmetic - #23685

Merged
alamb merged 2 commits into
apache:mainfrom
sakshichitnis27:issue-23665-decimal-avg-helpers
Aug 15, 2026
Merged

Document decimal AVG wrapping arithmetic#23685
alamb merged 2 commits into
apache:mainfrom
sakshichitnis27:issue-23665-decimal-avg-helpers

Conversation

@sakshichitnis27

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Decimal AVG intentionally uses wrapping arithmetic with a widened intermediate sum type. The existing direct add_wrapping and sub_wrapping calls made that invariant difficult to audit.

What changes are included in this PR?

  • Added small private helpers documenting the widened/headroom contract.
  • Updated scalar, grouped, merge, retract, and AVG(DISTINCT) decimal paths to use the helpers.
  • Kept the existing state types, return types, and arithmetic behavior unchanged.

Are these changes tested?

  • cargo fmt --all -- --check
  • cargo test -p datafusion-functions-aggregate avg --lib
  • cargo test -p datafusion-functions-aggregate-common avg_distinct --lib
  • cargo clippy -p datafusion-functions-aggregate-common --all-targets -- -D warnings

Are there any user-facing changes?

No.

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 18, 2026
@github-actions github-actions Bot added the auto detected api change Auto detected API change label Jul 27, 2026
@codecov-commenter

codecov-commenter commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.19%. Comparing base (1b67f2e) to head (033b14a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23685      +/-   ##
==========================================
- Coverage   81.19%   81.19%   -0.01%     
==========================================
  Files        1110     1110              
  Lines      388616   388637      +21     
  Branches   388616   388637      +21     
==========================================
+ Hits       315529   315537       +8     
- Misses      54506    54515       +9     
- Partials    18581    18585       +4     

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

@github-actions github-actions Bot removed the auto detected api change Auto detected API change label Aug 14, 2026

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

LGTM

@AdamGS

AdamGS commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

@sakshichitnis27 thank you for your contribution! sorry for taking so long, I lost track of this PR.

@alamb
alamb added this pull request to the merge queue Aug 15, 2026
@alamb

alamb commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Thanks @AdamGS and @sakshichitnis27

Merged via the queue into apache:main with commit 67502a3 Aug 15, 2026
37 checks passed
imtherealnaska pushed a commit to imtherealnaska/datafusion that referenced this pull request Aug 16, 2026
## Which issue does this PR close?
  - Closes apache#23665

  ## Rationale for this change
Decimal AVG intentionally uses wrapping arithmetic with a widened
intermediate sum type. The existing direct `add_wrapping` and
`sub_wrapping` calls made that invariant difficult to audit.

  ## What changes are included in this PR?
- Added small private helpers documenting the widened/headroom contract.
- Updated scalar, grouped, merge, retract, and `AVG(DISTINCT)` decimal
paths to use the helpers.
- Kept the existing state types, return types, and arithmetic behavior
unchanged.

  ## Are these changes tested?
  - `cargo fmt --all -- --check`
  - `cargo test -p datafusion-functions-aggregate avg
  --lib`
  - `cargo test -p datafusion-functions-aggregate-common
  avg_distinct --lib`
  - `cargo clippy -p datafusion-functions-aggregate-common
  --all-targets -- -D warnings`

  ## Are there any user-facing changes?
  No.

Co-authored-by: Adam Gutglick <adamgsal@gmail.com>
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.

Document decimal AVG wrapping arithmetic behind explicit helpers

4 participants