Skip to content

Commit

Permalink
fix(bigquery): parse negative NUMERIC from arrow (googleapis#11052)
Browse files Browse the repository at this point in the history
The `big.Int.SetBytes` method interprets input as an unsigned number, so we lose sign information when parsing using. So Arrow decimal parsing code is simplified by just relying on `big.Rat.SetString` method for parsing. 

Towards internal b/376149769
  • Loading branch information
alvarowolfx authored Oct 29, 2024
1 parent 2a667c6 commit 83352c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 4 additions & 8 deletions bigquery/arrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,14 @@ func convertArrowValue(col arrow.Array, i int, ft arrow.DataType, fs *FieldSchem
case *arrow.Decimal128Type:
dft := ft.(*arrow.Decimal128Type)
v := col.(*array.Decimal128).Value(i)
rat := big.NewRat(1, 1)
rat.Num().SetBytes(v.BigInt().Bytes())
d := rat.Denom()
d.Exp(big.NewInt(10), big.NewInt(int64(dft.Scale)), nil)
rat := new(big.Rat)
rat.SetString(v.ToString(dft.Scale))
return Value(rat), nil
case *arrow.Decimal256Type:
dft := ft.(*arrow.Decimal256Type)
v := col.(*array.Decimal256).Value(i)
rat := big.NewRat(1, 1)
rat.Num().SetBytes(v.BigInt().Bytes())
d := rat.Denom()
d.Exp(big.NewInt(10), big.NewInt(int64(dft.Scale)), nil)
rat := new(big.Rat)
rat.SetString(v.ToString(dft.Scale))
return Value(rat), nil
case *arrow.ListType:
arr := col.(*array.List)
Expand Down
8 changes: 8 additions & 0 deletions bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,7 @@ func initQueryParameterTestCases() {
dtm := civil.DateTime{Date: d, Time: tm}
ts := time.Date(2016, 3, 20, 15, 04, 05, 0, time.UTC)
rat := big.NewRat(13, 10)
nrat := big.NewRat(-13, 10)
bigRat := big.NewRat(12345, 10e10)
rangeTimestamp1 := &RangeValue{
Start: time.Date(2016, 3, 20, 15, 04, 05, 0, time.UTC),
Expand Down Expand Up @@ -2208,6 +2209,13 @@ func initQueryParameterTestCases() {
[]Value{rat},
rat,
},
{
"NegativeBigRatParam",
"SELECT @val",
[]QueryParameter{{Name: "val", Value: nrat}},
[]Value{nrat},
nrat,
},
{
"BoolParam",
"SELECT @val",
Expand Down

0 comments on commit 83352c4

Please sign in to comment.