Skip to content

Commit

Permalink
Handle strings as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 14, 2024
1 parent 1c1d30a commit 044861f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 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 @@ -136,6 +137,12 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto
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(float64(floatValue)))
default:
return nil, fmt.Errorf("expected float32/float64/int32/int64 recieved %T with value %v", value, value)
}
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 044861f

Please sign in to comment.