Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scalar/float.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalar

import (
"encoding/json"
"math"
"strconv"

Expand Down Expand Up @@ -119,6 +120,8 @@ func (s *Float) Set(val any) error {
return err
}
s.Value = value
case json.Number:
return s.Set(string(value))
case string:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions scalar/int.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalar

import (
"encoding/json"
"math"
"strconv"

Expand Down Expand Up @@ -124,6 +125,8 @@ func (s *Int) Set(val any) error {
return s.Set(int64(value))
case float64:
return s.Set(int64(value))
case json.Number:
return s.Set(string(value))
case string:
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions scalar/json_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scalar

import (
"bytes"
"encoding/json"
)

// unmarshalJSONWithNumbers decodes JSON with UseNumber so int64/uint64 values that
// exceed float64 precision survive as json.Number instead of being rounded.
func unmarshalJSONWithNumbers(data []byte, v any) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
return dec.Decode(v)
}
27 changes: 27 additions & 0 deletions scalar/json_number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scalar

import (
"testing"

"github.com/apache/arrow-go/v18/arrow"
"github.com/stretchr/testify/require"
)

func TestNestedIntPrecision(t *testing.T) {
for _, tc := range []struct {
name string
dt arrow.DataType
str string
}{
{name: "struct_int64", dt: arrow.StructOf(arrow.Field{Name: "v", Type: arrow.PrimitiveTypes.Int64, Nullable: true}), str: `{"v":-8717895732742165505}`},
{name: "struct_uint64", dt: arrow.StructOf(arrow.Field{Name: "v", Type: arrow.PrimitiveTypes.Uint64, Nullable: true}), str: `{"v":18428615660272232523}`},
{name: "list_int64", dt: arrow.ListOf(arrow.PrimitiveTypes.Int64), str: `[-8717895732742165505]`},
{name: "list_uint64", dt: arrow.ListOf(arrow.PrimitiveTypes.Uint64), str: `[18428615660272232523]`},
} {
t.Run(tc.name, func(t *testing.T) {
s := NewScalar(tc.dt)
require.NoError(t, s.Set(tc.str))
require.Equal(t, tc.str, s.String())
})
}
}
5 changes: 2 additions & 3 deletions scalar/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scalar

import (
"encoding/json"
"reflect"
"strings"

Expand Down Expand Up @@ -93,7 +92,7 @@ func (s *List) Set(val any) error {
switch value := val.(type) {
case string:
var x []any
if err := json.Unmarshal([]byte(value), &x); err != nil {
if err := unmarshalJSONWithNumbers([]byte(value), &x); err != nil {
return err
}
length := len(x)
Expand All @@ -110,7 +109,7 @@ func (s *List) Set(val any) error {

case []byte:
var x []any
if err := json.Unmarshal(value, &x); err != nil {
if err := unmarshalJSONWithNumbers(value, &x); err != nil {
return err
}
length := len(x)
Expand Down
4 changes: 2 additions & 2 deletions scalar/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *Struct) Set(val any) error {
switch value := val.(type) {
case string:
var x map[string]any
if err := json.Unmarshal([]byte(value), &x); err != nil {
if err := unmarshalJSONWithNumbers([]byte(value), &x); err != nil {
return err
}
for name := range x {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *Struct) Set(val any) error {

case []byte:
var x map[string]any
if err := json.Unmarshal(value, &x); err != nil {
if err := unmarshalJSONWithNumbers(value, &x); err != nil {
return err
}
s.Value = x
Expand Down
3 changes: 3 additions & 0 deletions scalar/uint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalar

import (
"encoding/json"
"math"
"strconv"

Expand Down Expand Up @@ -128,6 +129,8 @@ func (s *Uint) Set(val any) error {
return &ValidationError{Type: s.DataType(), Msg: "float64 is greater than MaxUint64", Value: value}
}
return s.Set(uint64(value))
case json.Number:
return s.Set(string(value))
case string:
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
Expand Down