This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new e16e546f fix(arrow/decimal256): handle float32 values scaled past the
float32 range in FromFloat32 (#1574)
e16e546f is described below
commit e16e546f506c50557ef896e6777b0cf1dfa9dcdf
Author: 0z5a <[email protected]>
AuthorDate: Mon Nov 16 19:45:23 2026 +0100
fix(arrow/decimal256): handle float32 values scaled past the float32 range
in FromFloat32 (#1574)
### Rationale for this change
`decimal256.FromFloat32` scales the input in float64 and then converts
the scaled value back to float32 before splitting it into 64-bit words.
A decimal256 can hold values far above `math.MaxFloat32` (about 3.4e38).
When the scaled value is larger than that, the float32 conversion gives
+Inf, the words are built from `uint64(+Inf)`, and the function returns
a garbage value with a nil error. With the `assert` build tag it panics
in a `debug.Assert` instead.
This can be reached from a safe cast. Results for float32 inputs, on
main, with this change, and from pyarrow:
| cast | main | this PR | pyarrow |
| --- | --- | --- | --- |
| `123.5` to `decimal256(76, 38)` |
`-62771017353866807638.35789423207666416102355444464034512896` |
`123.50000000000000260327792243959757012992` |
`123.50000000000000000000000000000000000000` |
| `3.4e38` to `decimal256(50, 5)` |
`-62771017353866807638357894232076664161023554444640345.12896` |
`339999995214436424907732413799364296704.00000` |
`339999995214436424907732413799364296704.00000` |
| `1e30` to `decimal256(60, 20)` |
`-62771017353866807638357894232076664161.02355444464034512896` |
`1000000015047466202704776911698.34746997528420417536` |
`1000000015047466219876688855040.00000000000000000000` |
With this change these values are accurate to float64 precision. pyarrow
uses an exact conversion, so its digits can differ past about the 16th
significant digit.
Only decimal256 is affected. Decimal128, decimal32 and decimal64 check
the scaled value against their precision before narrowing it, and their
largest values are below the float32 maximum.
### What changes are included in this PR?
When the scaled value does not fit in a float32, `fromPositiveFloat32`
now splits it as a float64. The word split is moved out of
`fromPositiveFloat64` into a small `splitPositiveFloat64` helper that
both functions use.
Values whose scaled form fits in a float32 still go through the existing
float32 path, so their results, including the existing float32 rounding,
do not change. I compared the old and new code on about 300,000 random
float32 values and scales where the scaled value fits in a float32, and
every result was identical.
### Are these changes tested?
Yes. A new `TestDecimalFromFloat/float32/scaled_past_float32_range`
subtest round-trips float32 values whose scaled form is past the float32
range. It covers positive and negative values up to `math.MaxFloat32`,
plus one control value that stays in range. The subtest fails on main
(wrong values by default, and a panic with `-tags assert,test`) and
passes with this change. `go test -short ./arrow/decimal256/...
./arrow/decimal128/... ./arrow/decimal/... ./arrow/compute/...
./arrow/scalar/...` passes with and without `-tags assert,test`.
### Are there any user-facing changes?
Yes. `decimal256.FromFloat32`, and casts from float32 to decimal256, now
return a value accurate to float64 precision instead of a garbage value
when the scaled input is larger than the float32 range.
This change was drafted with Claude Code (claude-opus-5-5); the test
results above are from the runs described.
---
arrow/decimal256/decimal256.go | 14 +++++++++++++-
arrow/decimal256/decimal256_test.go | 25 +++++++++++++++++++++++++
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/arrow/decimal256/decimal256.go b/arrow/decimal256/decimal256.go
index 0cd80c39..5ffc5617 100644
--- a/arrow/decimal256/decimal256.go
+++ b/arrow/decimal256/decimal256.go
@@ -286,6 +286,12 @@ func fromPositiveFloat32(v float32, prec, scale int32)
(Num, error) {
}
v = float32(val)
+ if math.IsInf(float64(v), 1) {
+ // the scaled value is past the float32 range but can still fit
+ // in a decimal256, so split it as a float64 instead
+ return splitPositiveFloat64(val), nil
+ }
+
var arr [4]float32
arr[3] = float32(math.Floor(math.Ldexp(float64(v), -192)))
v -= float32(math.Ldexp(float64(arr[3]), 192))
@@ -330,6 +336,12 @@ func fromPositiveFloat64(v float64, prec, scale int32)
(Num, error) {
return Num{}, err
}
+ return splitPositiveFloat64(val), nil
+}
+
+// splitPositiveFloat64 converts an already scaled and rounded non-negative
+// float64 into the 64-bit words of a Num.
+func splitPositiveFloat64(val float64) Num {
var arr [4]float64
arr[3] = math.Floor(math.Ldexp(val, -192))
val -= math.Ldexp(arr[3], 192)
@@ -347,7 +359,7 @@ func fromPositiveFloat64(v float64, prec, scale int32)
(Num, error) {
debug.Assert(arr[1] < 1.8446744073709552e+19, "bad conversion float64
to decimal256") // 2**64
debug.Assert(arr[0] >= 0, "bad conversion float64 to decimal256")
debug.Assert(arr[0] < 1.8446744073709552e+19, "bad conversion float64
to decimal256") // 2**64
- return Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]),
uint64(arr[3])}}, nil
+ return Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]),
uint64(arr[3])}}
}
func (n Num) tofloat64Positive(scale int32) float64 {
diff --git a/arrow/decimal256/decimal256_test.go
b/arrow/decimal256/decimal256_test.go
index 8a2e5264..81604f94 100644
--- a/arrow/decimal256/decimal256_test.go
+++ b/arrow/decimal256/decimal256_test.go
@@ -581,6 +581,31 @@ func TestDecimalFromFloat(t *testing.T) {
assert.Equal(t, "1230", n.BigInt().String())
}
})
+
+ t.Run("scaled past float32 range", func(t *testing.T) {
+ // decimal256 can hold values far larger than
math.MaxFloat32,
+ // so scaling a float32 up must not overflow the
conversion
+ tests := []struct {
+ val float32
+ precision, scale int32
+ }{
+ {456.78, 38, 30}, // stays below
math.MaxFloat32 once scaled
+ {123.5, 76, 38},
+ {-123.5, 76, 38},
+ {1, 76, 39},
+ {1e30, 60, 20},
+ {3.4e38, 50, 5},
+ {-3.4e38, 50, 5},
+ {math.MaxFloat32, 40, 1},
+ }
+
+ for _, tt := range tests {
+ n, err := decimal256.FromFloat32(tt.val,
tt.precision, tt.scale)
+ assert.NoError(t, err)
+ assert.Equal(t, tt.val, n.ToFloat32(tt.scale),
"FromFloat32(%v, %d, %d) = %s",
+ tt.val, tt.precision, tt.scale,
n.ToString(tt.scale))
+ }
+ })
})
}