You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "gruuya (via GitHub)" <gi...@apache.org> on 2026/02/16 14:21:36 UTC

[I] Parsing zero scale numbers truncates instead of rounding the number [arrow-rs]

gruuya opened a new issue, #9422:
URL: https://github.com/apache/arrow-rs/issues/9422

   **Describe the bug**
   The [fix](https://github.com/apache/arrow-rs/pull/8700) that corrected the decimal position spillover when parsing zero scale numbers also introduced another issue, which is that it always truncates the number instead of rounding it appropriately.
   
   **To Reproduce**
   The following test case fails, instead the parsed number is `1`.
   ```rust
   assert_eq!(
       parse_decimal::<Decimal128Type>("1.9", 3, 0).unwrap(),
       2,
   );
   ```
   
   **Expected behavior**
   It's not instantly clear that this should be the expected behavior in this case, but it would make sense to align with Postgres and DuckDB, where this is true
   ```sql
   postgres@localhost:postgres> select '1.1'::numeric(3, 0), '1.9'::numeric(3, 0);
   +---------+---------+
   | numeric | numeric |
   |---------+---------|
   | 1       | 2       |
   +---------+---------+
   ```
   
   ```sql
   D select '1.1'::numeric(3, 0), '1.9'::numeric(3, 0);
   ┌─────────────────────────────┬─────────────────────────────┐
   │ CAST('1.1' AS DECIMAL(3,0)) │ CAST('1.9' AS DECIMAL(3,0)) │
   │        decimal(3,0)         │        decimal(3,0)         │
   ├─────────────────────────────┼─────────────────────────────┤
   │                           1 │                           2 │
   └─────────────────────────────┴─────────────────────────────┘
   ```
   
   **Additional context**
   Note that the behavior is as expected when the decimal part is less than `.5`, since truncation and rounding then match up.


-- 
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.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Parsing zero scale numbers truncates instead of rounding the number [arrow-rs]

Posted by "Whatsonyourmind (via GitHub)" <gi...@apache.org>.
Whatsonyourmind commented on issue #9422:
URL: https://github.com/apache/arrow-rs/issues/9422#issuecomment-4182733753

   The fix needs to implement half-up rounding (banker's rounding or round-half-to-even would also be defensible, but Postgres and DuckDB both use half-up).
   
   The core logic: when parsing a decimal string with more fractional digits than the target scale allows, you need to look at the first truncated digit to decide whether to round up.
   
   ```rust
   // After parsing the significant digits and determining where truncation occurs:
   fn round_decimal_parse(digits: &[u8], truncation_point: usize, scale: i8) -> i128 {
       let mut result = parse_digits_up_to(digits, truncation_point);
       
       if truncation_point < digits.len() {
           let next_digit = digits[truncation_point] - b'0';
           if next_digit >= 5 {
               // Round away from zero (half-up for positive, half-down for negative)
               if result >= 0 {
                   result += 1;
               } else {
                   result -= 1;
               }
           }
       }
       result
   }
   ```
   
   For the specific case of scale=0, parsing "1.9" should give:
   1. Parse integer part: 1
   2. First truncated fractional digit: 9 >= 5, so round up
   3. Result: 2
   
   One edge case to watch: rounding can cause overflow. Parsing "99.5" as `numeric(2, 0)` should round to 100, which exceeds the precision. Postgres raises an error here. The implementation should check `result < 10^precision` after rounding and return an overflow error if violated.


-- 
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: [I] Parsing zero scale numbers truncates instead of rounding the number [arrow-rs]

Posted by "sdf-jkl (via GitHub)" <gi...@apache.org>.
sdf-jkl closed issue #9422: Parsing zero scale numbers truncates instead of rounding the number
URL: https://github.com/apache/arrow-rs/issues/9422


-- 
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: [I] Parsing zero scale numbers truncates instead of rounding the number [arrow-rs]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on issue #9422:
URL: https://github.com/apache/arrow-rs/issues/9422#issuecomment-5375313142

   take


-- 
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: [I] Parsing zero scale numbers truncates instead of rounding the number [arrow-rs]

Posted by "sdf-jkl (via GitHub)" <gi...@apache.org>.
sdf-jkl commented on issue #9422:
URL: https://github.com/apache/arrow-rs/issues/9422#issuecomment-5916664180

   Closed via #10850.


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