fix: avoid dividing by zero in avg for decimals with a negative scale - #24899
Open
edubraqd wants to merge 1 commit into
Open
fix: avoid dividing by zero in avg for decimals with a negative scale#24899edubraqd wants to merge 1 commit into
avg for decimals with a negative scale#24899edubraqd wants to merge 1 commit into
Conversation
`DecimalAverager::try_new` computed `10^sum_scale` and `10^target_scale`
separately with `pow_wrapping(scale as u32)`. For a negative scale the
exponent wraps to a huge value and the result is 0, so `avg` later divided
by zero:
SELECT avg(x) FROM (VALUES (arrow_cast(1, 'Decimal128(10, -2)'))) t(x);
thread 'main' panicked ... attempt to divide by zero
The two factors were only ever used as the ratio `10^(target_scale -
sum_scale)`, so compute that directly from the scale difference. The
existing "Arithmetic Overflow in AvgAccumulator" error for a target scale
smaller than the sum scale is kept.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24899 +/- ##
==========================================
- Coverage 81.63% 81.62% -0.01%
==========================================
Files 1123 1123
Lines 409546 409554 +8
Branches 409546 409554 +8
==========================================
- Hits 334319 334310 -9
- Misses 55591 55601 +10
- Partials 19636 19643 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
avgover a decimal with a negative scale panics with "attempt to divide by zero" #24898.Rationale for this change
DecimalAverager::try_newcomputed10^sum_scaleand10^target_scaleseparately withpow_wrapping(scale as u32). For a negative scale the exponent wraps and the result is 0, soavgdivided by zero and panicked for any decimal input with a negative scale.What changes are included in this PR?
The two factors were only ever used as the ratio
10^(target_scale - sum_scale), soDecimalAveragernow stores that single multiplier, computed from the scale difference. The existingArithmetic Overflow in AvgAccumulatorerror for a target scale smaller than the sum scale is kept (it is now thescale_diff < 0check).Are these changes tested?
Yes.
avg_decimal_negative_scaleaveragesDecimal128(10, -2)values and checks theDecimal128(14, 2)result; it used to panic. The existingaverageandfunctions-aggregate-commonunit tests pass.Are there any user-facing changes?
avgover decimals with a negative scale returns a result instead of panicking.