apache / apache/arrow-rs

Parsing zero scale numbers truncates instead of rounding the number

Open
#9,422 2 comments 0 reactions 1 assignee Claimed by @neilconway View on GitHub
bug
Dominant language
Rust
Stars
3.6k
Forks
1.3k
Avg merge
2d 18h
Merged PRs (30d)
169

Description

**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::("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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.