Skip to content

Commit

Permalink
[bigquery] Support additional types for float columns (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 14, 2024
1 parent 668c5d6 commit 355beb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bigquery

import (
"fmt"
"strconv"
"time"

"cloud.google.com/go/bigquery/storage/apiv1/storagepb"
Expand Down Expand Up @@ -132,8 +133,18 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto
message.Set(field, protoreflect.ValueOfFloat64(float64(value)))
case float64:
message.Set(field, protoreflect.ValueOfFloat64(value))
case int32:
message.Set(field, protoreflect.ValueOfFloat64(float64(value)))
case int64:
message.Set(field, protoreflect.ValueOfFloat64(float64(value)))
case string:
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse string to float64: %w", err)
}
message.Set(field, protoreflect.ValueOfFloat64(floatValue))
default:
return nil, fmt.Errorf("expected float32/float64 recieved %T with value %v", value, value)
return nil, fmt.Errorf("expected float32/float64/int32/int64/string recieved %T with value %v", value, value)
}
case typing.EDecimal.Kind:
if decimalValue, ok := value.(*decimal.Decimal); ok {
Expand Down
9 changes: 9 additions & 0 deletions clients/bigquery/storagewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func TestRowToMessage(t *testing.T) {
columns.NewColumn("c_int64", typing.Integer),
columns.NewColumn("c_float32", typing.Float),
columns.NewColumn("c_float64", typing.Float),
columns.NewColumn("c_float_int32", typing.Float),
columns.NewColumn("c_float_int64", typing.Float),
columns.NewColumn("c_float_string", typing.Float),
columns.NewColumn("c_numeric", typing.EDecimal),
columns.NewColumn("c_string", typing.String),
columns.NewColumn("c_string_decimal", typing.String),
Expand All @@ -123,6 +126,9 @@ func TestRowToMessage(t *testing.T) {
"c_int64": int64(1234),
"c_float32": float32(1234.567),
"c_float64": float64(1234.567),
"c_float_int32": int32(1234),
"c_float_int64": int64(1234),
"c_float_string": "4444.55555",
"c_numeric": decimal.NewDecimal(nil, 5, big.NewFloat(3.1415926)),
"c_string": "foo bar",
"c_string_decimal": decimal.NewDecimal(nil, 5, big.NewFloat(1.618033)),
Expand All @@ -149,6 +155,9 @@ func TestRowToMessage(t *testing.T) {
"cBool": true,
"cFloat32": 1234.5670166015625,
"cFloat64": 1234.567,
"cFloatInt32": 1234.0,
"cFloatInt64": 1234.0,
"cFloatString": 4444.55555,
"cInt": "1234",
"cInt32": "1234",
"cInt64": "1234",
Expand Down

0 comments on commit 355beb5

Please sign in to comment.