Repository navigation
fix(arrow): preserve precision when scaling float64 to decimals - #1663
vera-rubin wants to merge 1 commit into
Conversation
zeroshade
left a comment
There was a problem hiding this comment.
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.
|
No CI workflows were triggered for this PR's head commit ( This comment was drafted with an AI-assisted tool and may contain mistakes; it was confirmed and submitted by an Apache Arrow maintainer. |
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.0converted to decimal(24, 23) gives0.99999999999999991611392, also through the default safe array cast:What changes are included in this PR?
For scales from 0 to the type's maximum scale,
fromPositiveFloat64(decimal128) andfromPositiveFloat64_256(decimal256) now take the exact binary value of the float64 as abig.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.
TestDecimalFromFloat64ExactScalingchecks 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/TestFloatingToDecimalgets 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+15is exactly1844674407370955.5, so the decimal(20, 4) result is now1844674407370955.5000instead of1844674407370955.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.