-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: increase ROUND decimal precision to prevent overflow truncation #19926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Other options considered:
I chose option 1 as the most user friendly, I would like to hear if there are other suggestion for this fix. |
Option 1 sounds good. Option 2 would be too strict, not sure I fully understand what option 3 means here. One thing to keep in mind is we still need to consider edge case where precision is already maxed out. Would this bug occur again? |
You were right to point that, i did some more test and found out it was overflowing after 38 digits, I handled that as an error. |
|
|
||
| // Validate the result fits within the precision | ||
| // The max value for a given precision is 10^precision - 1 | ||
| let max_value = ten.pow_checked(precision as u32).map_err(|_| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use these constants instead
- https://docs.rs/arrow/latest/arrow/datatypes/constant.MAX_DECIMAL128_FOR_EACH_PRECISION.html
- https://docs.rs/arrow/latest/arrow/datatypes/constant.MIN_DECIMAL128_FOR_EACH_PRECISION.html
(Same constants exist for each decimal type)
Which issue does this PR close?
rounddecimal incorrect result #19921.Rationale for this change
SELECT ROUND('999.9'::DECIMAL(4,1))incorrectly returned100.0instead of1000.When rounding a decimal value causes a carry-over that increases the number of digits (e.g., 999.9 → 1000.0), the result overflows the original precision constraint. The overflow was silently truncated during display, producing incorrect results.
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?
ROUND('999.9'::DECIMAL(4,1))100.0(wrong)1000(correct)Decimal128(4, 1)Decimal128(5, 1)The return type precision is now increased by 1 to prevent overflow. This matches PostgreSQL behavior where
ROUNDcan increase the number of digits before the decimal point when carry-over occurs.Users who depend on exact output precision may need to explicitly cast: