Skip to content

fix(arrow): preserve precision when scaling float64 to decimals - #1663

Open
vera-rubin wants to merge 1 commit into
apache:mainfrom
vera-rubin:fix/float64-decimal-scaling
Open

vera-rubin wants to merge 1 commit into
apache:mainfrom
vera-rubin:fix/float64-decimal-scaling

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

Rationale for this change

Converting float64 values to decimal128 or decimal256 scales them with a float64 multiplication before rounding. At large scales that multiplication loses digits, so exact inputs come back changed without an error. For example, 1.0 converted to decimal(24, 23) gives 0.99999999999999991611392, also through the default safe array cast:

n, err := decimal128.FromFloat64(1, 24, 23)
fmt.Println(n.BigInt(), err)
// want: 100000000000000000000000 <nil>

What changes are included in this PR?

For scales from 0 to the type's maximum scale, fromPositiveFloat64 (decimal128) and fromPositiveFloat64_256 (decimal256) now take the exact binary value of the float64 as a big.Rat, multiply it by the power of ten with integers, round half to even, and then check the result against the precision. Negative scales keep the existing float path.

Are these changes tested?

Yes. TestDecimalFromFloat64ExactScaling checks both widths for values such as ±1 at (24, 23), ±12345678 at (26, 18), 0.125, 0.5 at the maximum scale, half-even rounding at (2, 1) and an overflow at (23, 23). TestCasts/TestFloatingToDecimal gets array cast cases for the same inputs. Both fail without the fix and pass with it; the decimal and ./arrow/compute/... tests pass.

One existing cast expectation changes: float64 1.8446744073709556e+15 is exactly 1844674407370955.5, so the decimal(20, 4) result is now 1844674407370955.5000 instead of 1844674407370955.5712, where the extra digits came from the float multiplication.

Are there any user-facing changes?

Yes. Float64 to decimal conversions keep exact values at large scales, which can change the last digits of some existing results to the correctly rounded ones.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the bug is real (1e23 isn't representable, so 1.0 at decimal(24, 23) can't round-trip through float scaling), and I confirmed the new tests fail without the change and that arrow/decimal/..., compute, scalar and array pass with it.

The blocker is cost: every float64→decimal conversion with a non-negative scale now goes through big.Rat/big.Int. On 1024 values, Decimal128FromFloat goes from 9.1µs to ~404–469µs, Decimal256FromFloat from ~28µs to ~437–456µs, and from 0 to ~13 allocations per value. That hits array casts too, including ordinary cases like decimal(10, 2) that were never affected by the bug.

Could the exact path be limited to the inputs that need it? For scale ≤ 22, 10^scale is an exact float64, so p := v*pscale plus e := math.FMA(v, pscale, -p) gives the exact product as p + e, which is enough to apply round-half-even correctly without allocating. Only scale > 22, or products ≥ 2^53, would need the big-number path. Even there, scaleMultipliers[...].BigInt() allocates on every call and could be precomputed.

Also: Decimal32FromFloat/Decimal64FromFloat and the float32 paths still scale in floating point; could you either cover them or explain why they're unaffected? And since some existing cast results change (e.g. …955.5712 → …955.5000), this deserves a mention in the release notes.


This review was drafted with an AI-assisted tool and may contain mistakes; it was confirmed and submitted by an Apache Arrow maintainer.

@zeroshade

Copy link
Copy Markdown
Member

No CI workflows were triggered for this PR's head commit (ca6b4d49), so I'm closing and reopening it to kick them off. This is separate from the review above.


This comment was drafted with an AI-assisted tool and may contain mistakes; it was confirmed and submitted by an Apache Arrow maintainer.

@zeroshade zeroshade closed this Nov 23, 2026
@zeroshade zeroshade reopened this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants