You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parsing zero scale numbers truncates instead of rounding the number #9422
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.
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
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:fnround_decimal_parse(digits:&[u8],truncation_point:usize,scale:i8) -> i128{letmut 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:
Parse integer part: 1
First truncated fractional digit: 9 >= 5, so round up
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.
Activity
Whatsonyourmind commentedon Apr 3, 2026
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.
For the specific case of scale=0, parsing "1.9" should give:
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 checkresult < 10^precisionafter rounding and return an overflow error if violated.fix(arrow-cast): round instead of truncate when parsing decimals
neilconway commentedon Aug 21, 2026
take
Consolidate string-to-decimal parsing into a single code path
fix: Consolidate string-to-decimal parsing into a single code path (#…
fix: Consolidate string-to-decimal parsing into a single code path (a…
fix: Consolidate string-to-decimal parsing into a single code path (a…
fix: Consolidate string-to-decimal parsing into a single code path (a…
sdf-jkl commentedon Sep 30, 2026
Closed via #10850.