mansur-akhmadi opened a new issue, #1690:
URL: https://github.com/apache/arrow-go/issues/1690
### Describe the bug, including details regarding any error messages,
version, and platform.
On arm64, a safe cast (`compute.SafeCastOptions`) from a float to an integer
type succeeds and returns the integer maximum when the input is exactly 2^n,
the first power of two the target type can't hold. On amd64 the same casts fail
with `was truncated converting to ...`, as expected.
```go
in, _, _ := array.FromJSON(memory.DefaultAllocator,
arrow.PrimitiveTypes.Float64,
strings.NewReader(`[9223372036854775808]`))
out, err := compute.CastArray(context.Background(), in,
compute.SafeCastOptions(arrow.PrimitiveTypes.Int64))
fmt.Println(out, err)
```
main at b97dc35, Go 1.27.0, macOS:
| input → target | `GOARCH=arm64` | `GOARCH=amd64` |
|---|---|---|
| float64 2^63 → int64 | `[9223372036854775807] <nil>` | `float value
9223372036854775808.000000 was truncated converting to int64` |
| float64 2^64 → uint64 | `[18446744073709551615] <nil>` | error |
| float32 2^31 → int32 | `[2147483647] <nil>` | error |
| float32 2^32 → uint32 | `[4294967295] <nil>` | error |
| float32 2^63 → int64 | `[9223372036854775807] <nil>` | error |
| float64 128 → int8 | error | error |
The truncation check in `checkFloatTrunc`
(`arrow/compute/internal/kernels/numeric_cast.go`) converts the result back to
a float and compares it with the input. Go leaves out-of-range float-to-integer
conversions implementation-defined. arm64 saturates, so 2^63 becomes
`MaxInt64`, and `float64(MaxInt64)` rounds back to 2^63, so the check passes.
amd64 produces `MinInt64` there, which doesn't round-trip. The narrow types are
fine because their maximum is exact in the float type.
In practice, 2^63 is what you get when `math.MaxInt64` (or a JSON number
like `9223372036854775807`) passes through a float64. A safe cast back to int64
then returns a value the input didn't have, and the result depends on the
machine.
For reference, pyarrow 26.0.1 on arm64 shows the same saturation for these
inputs (e.g. `pc.cast(pa.array([2.0**63]), pa.int64())` →
`[9223372036854775807]`), so Arrow C++ seems to have the same round-trip check.
I've only looked at the Go side.
Expected: the cast fails on every platform, as it already does on amd64.
I have a fix with tests and will open a PR.
### Component(s)
Other
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]