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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to