Skip to content

Parsing zero scale numbers truncates instead of rounding the number #9422

Closed

Description

@gruuya

Describe the bug
The fix 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.

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

postgres@localhost:postgres> select '1.1'::numeric(3, 0), '1.9'::numeric(3, 0);
+---------+---------+
| numeric | numeric |
|---------+---------|
| 1       | 2       |
+---------+---------+
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.

Activity

  1. added
    bug
    on Feb 16, 2026
  2. Whatsonyourmind commented on Apr 3, 2026

    @Whatsonyourmind

    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.

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

  3. added a commit that references this issue on Jun 25, 2026
    ad6300c
  4. neilconway commented on Aug 21, 2026

    @neilconway
    Contributor

    take

  5. sdf-jkl commented on Sep 30, 2026

    @sdf-jkl
    Member

    Closed via #10850.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions