You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "leopoldch (via GitHub)" <gi...@apache.org> on 2026/02/16 13:30:45 UTC
[PR] fix(arrow-cast): implement rounding instead of truncation in decimal parsing [arrow-rs]
leopoldch opened a new pull request, #9421:
URL: https://github.com/apache/arrow-rs/pull/9421
# Which issue does this PR close?
- Closes #9410 .
# Rationale for this change
This PR addresses the inconsistency between decimal parsing and decimal casting. Previously, `parse_decimal` would truncate values that exceeded the defined scale (e.g., parsing `"1.99"` with a scale of `1` resulted in `1.9`). However, casting operations typically perform rounding.
This change aligns the parsing behavior with standard expectations by implementing "Round Half Up" logic. It ensures that values like `"1.99"` are correctly parsed as `2.0` when the scale requires it.
# What changes are included in this PR?
- **`parse_decimal` update:** Modified the parsing loop to inspect the first digit immediately following the scale limit. If this digit is `>= 5`, the result is incremented to perform a round-up.
- **`parse_e_notation` update:** Adjusted the logic for negative exponents (division). Instead of simple integer division (which truncates), the code now adds half of the divisor before dividing `(result + divisor / 2) / divisor`. This simulates rounding during the division step.
- **Unit Tests:** Added specific test cases to cover:
- Rounding up with carry-over (e.g., `1.99` -> `2.0`).
- Rounding up simple cases (e.g., `1.39` -> `1.4`).
- Rounding down/Truncation (e.g., `1.33` -> `1.3`).
- Exact values (no change when precision matches).
# Are these changes tested?
Yes.
I have added new test cases in `arrow-cast/src/parse.rs` inside the `tests` module:
- `test_parse_decimal_should_round_up_carry_over`
- `test_parse_decimal_should_keep_exact_value`
- `test_parse_decimal_should_round_up_simple`
- `test_parse_decimal_should_round_down`
Additionally, I verified that existing tests (including scientific notation tests in `test_parse_decimal_with_parameter`) pass with the new rounding logic.
# Are there any user-facing changes?
Yes, this is a behavioral change for parsing Decimal types.
- **Before:** Input strings with precision exceeding the target scale were **truncated**.
- **After:** Input strings are now **rounded** (Round Half Up).
This does not change the public API signature, but the resulting values for specific string inputs will change to be more accurate.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-cast): implement rounding instead of truncation in decimal parsing [arrow-rs]
Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #9421:
URL: https://github.com/apache/arrow-rs/pull/9421#discussion_r2831258022
##########
arrow-cast/src/parse.rs:
##########
@@ -826,7 +826,9 @@ fn parse_e_notation<T: DecimalType>(
}
if exp < 0 {
- result = result.div_wrapping(base.pow_wrapping(-exp as _));
+ let divisor = base.pow_wrapping(-exp as _);
+ let half_divisor = divisor.div_wrapping(T::Native::usize_as(2));
+ result = result.add_wrapping(half_divisor).div_wrapping(divisor);
Review Comment:
It seems like this could cause a near-max value to overflow before the division could succeed? e.g. for u8, 255/10 = 25 but (255+5)/10 overflows on add. I have vague memories that other parts of the decimal code have a trick for avoiding the problem. I don't remember what trick or where, but it's likely in decimal casting/scaling code.
Either way, we should have a unit test that verifies we don't introduce new "intermediate" overflows like that. A true overflow due to rounding up with carry-over would be allowed, e.g. parsing 99.9 to a (2, 0) decimal would overflow because 100 isn't representable.
##########
arrow-cast/src/parse.rs:
##########
@@ -964,6 +974,8 @@ pub fn parse_decimal<T: DecimalType>(
return Err(ArrowError::ParseError(format!(
"parse decimal overflow ({s})"
)));
+ } else if round_up {
+ result = result.add_wrapping(T::Native::usize_as(1));
Review Comment:
nit: [T::Native::ONE](https://docs.rs/arrow/latest/arrow/array/trait.ArrowNativeTypeOp.html#associatedconstant.ONE)?
##########
arrow-cast/src/parse.rs:
##########
@@ -915,6 +919,12 @@ pub fn parse_decimal<T: DecimalType>(
// We have processed all the digits that we need. All that
// is left is to validate that the rest of the string contains
// valid digits.
Review Comment:
Worth updating the comment?
##########
arrow-cast/src/parse.rs:
##########
@@ -915,6 +919,12 @@ pub fn parse_decimal<T: DecimalType>(
// We have processed all the digits that we need. All that
// is left is to validate that the rest of the string contains
// valid digits.
+ if !rounding_checked {
+ if *b >= b'5' {
+ round_up = true;
+ }
Review Comment:
nit: the branch likely costs more than it's worth (both in LoC and in CPU cycles)
```suggestion
round_up = *b >= b'5';
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-cast): implement rounding instead of truncation in decimal parsing [arrow-rs]
Posted by "leopoldch (via GitHub)" <gi...@apache.org>.
leopoldch commented on PR #9421:
URL: https://github.com/apache/arrow-rs/pull/9421#issuecomment-3917159750
I updated tests that used to use truncation
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-cast): implement rounding instead of truncation in decimal parsing [arrow-rs]
Posted by "gruuya (via GitHub)" <gi...@apache.org>.
gruuya commented on PR #9421:
URL: https://github.com/apache/arrow-rs/pull/9421#issuecomment-3989717753
> Before we merge tho, we should verify that there's consensus that the current behavior is a bug -- I didn't see any comments on the bug report.
I think this makes sense to fix.
I've been thinking about a potential solution that would solve not only this, but also https://github.com/apache/arrow-rs/issues/5762, as well as avoid the double pass that sometimes occurs in `parse_e_notation`.
This solution would perform a single backwards pass (the alternative mentioned here https://github.com/apache/arrow-rs/issues/9170) instead of a forwards pass on the input string, which means that
- if present, we parse e-notation first
- then we know whether we should include more or less of the fractionals for a given scale
- we should be able to avoid the overflow with negative e-notation
- we should be able to round appropriately
I still need to think about it a bit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-cast): implement rounding instead of truncation in decimal parsing [arrow-rs]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #9421:
URL: https://github.com/apache/arrow-rs/pull/9421#issuecomment-4785346663
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org