Skip to content

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

@gruuya

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)         │
├─────────────────────────────┼─────────────────────────────┤
│                           12 │
└─────────────────────────────┴─────────────────────────────┘

Additional context
Note that the behavior is as expected when the decimal part is less than .5, since truncation and rounding then match up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions