hcrosse opened a new issue, #1507:
URL: https://github.com/apache/arrow-go/issues/1507
### Describe the bug, including details regarding any error messages,
version, and platform.
`decimal128.Num.ToBigFloat` and `decimal128.FromString` panic with `index
out of range` when the scale's absolute value exceeds 38. The corresponding
`decimal256` functions panic beyond 76. Both panic even for zero.
The panic reaches array `ValueStr` and JSON output, scalar `String` and
string casts, builders, `array.FromJSON`, `scalar.ParseScalar` and the CSV
reader.
I reproduced this on main at `32947b25`, on macOS arm64 with Go 1.26.10.
**Reproduction**
Save this as `main.go` in a new subdirectory of an arrow-go checkout at
`32947b259414a77ae6347d3d29bd69ec00b9d560`, then run `go run . format128`, `go
run . format256`, `go run . scalar128`, `go run . parse128` and `go run .
parse256` from that directory:
```go
package main
import (
"fmt"
"os"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/decimal128"
"github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/scalar"
)
func main() {
switch os.Args[1] {
case "format128":
fmt.Println(decimal128.FromI64(12345).ToBigFloat(39).Text('g',
38))
case "format256":
fmt.Println(decimal256.FromI64(12345).ToBigFloat(77).Text('g',
76))
case "scalar128":
fmt.Println(scalar.NewDecimal128Scalar(decimal128.FromI64(12345),
&arrow.Decimal128Type{Precision: 38, Scale: 100000}).String())
case "parse128":
n, err := decimal128.FromString("1e-39", 38, 39)
fmt.Println(n.BigInt(), err)
case "parse256":
n, err := decimal256.FromString("1e-77", 76, 77)
fmt.Println(n.BigInt(), err)
}
}
```
Actual output starts with this for `format128` and `parse128`:
```text
panic: runtime error: index out of range [39] with length 39
```
For `format256` and `parse256`:
```text
panic: runtime error: index out of range [77] with length 77
```
For `scalar128`:
```text
panic: runtime error: index out of range [100000] with length 39
```
Expected output for `format128`:
```text
1.2345e-35
```
Expected output for `format256`:
```text
1.2345e-73
```
Expected output for `scalar128`:
```text
1.2345e-99996
```
Expected output for either parse command:
```text
1 <nil>
```
**Cause**
`ToBigFloat` and `FromString` index fixed tables of powers of ten without
handling scales outside them.
https://github.com/apache/arrow-go/blob/32947b259414a77ae6347d3d29bd69ec00b9d560/arrow/decimal128/decimal128.go#L339-L347
https://github.com/apache/arrow-go/blob/32947b259414a77ae6347d3d29bd69ec00b9d560/arrow/decimal256/decimal256.go#L158-L168
I have a patch with tests and can 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]