Skip to content

Commit

Permalink
feat: support float32 data type (#208)
Browse files Browse the repository at this point in the history
Add support for FLOAT32 data type.
  • Loading branch information
olavloite authored Mar 14, 2024
1 parent 6c94089 commit 7db1b8f
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 78 deletions.
6 changes: 6 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,12 @@ func checkIsValidType(v driver.Value) bool {
case []spanner.NullBool:
case *bool:
case []*bool:
case float32:
case []float32:
case spanner.NullFloat32:
case []spanner.NullFloat32:
case *float32:
case []*float32:
case float64:
case []float64:
case spanner.NullFloat64:
Expand Down
65 changes: 54 additions & 11 deletions driver_with_mockserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ func TestQueryWithAllTypes(t *testing.T) {
AND ColString=@string
AND ColBytes=@bytes
AND ColInt=@int64
AND ColFloat=@float64
AND ColFloat32=@float32
AND ColFloat64=@float64
AND ColNumeric=@numeric
AND ColDate=@date
AND ColTimestamp=@timestamp
Expand All @@ -451,7 +452,8 @@ func TestQueryWithAllTypes(t *testing.T) {
AND ColStringArray=@stringArray
AND ColBytesArray=@bytesArray
AND ColIntArray=@int64Array
AND ColFloatArray=@float64Array
AND ColFloat32Array=@float32Array
AND ColFloat64Array=@float64Array
AND ColNumericArray=@numericArray
AND ColDateArray=@dateArray
AND ColTimestampArray=@timestampArray
Expand All @@ -478,6 +480,7 @@ func TestQueryWithAllTypes(t *testing.T) {
"test",
[]byte("testbytes"),
uint(5),
float32(3.14),
3.14,
numeric("6.626"),
civil.Date{Year: 2021, Month: 7, Day: 21},
Expand All @@ -487,6 +490,7 @@ func TestQueryWithAllTypes(t *testing.T) {
[]spanner.NullString{{Valid: true, StringVal: "test1"}, {}, {Valid: true, StringVal: "test2"}},
[][]byte{[]byte("testbytes1"), nil, []byte("testbytes2")},
[]spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}},
[]spanner.NullFloat32{{Valid: true, Float32: 3.14}, {}, {Valid: true, Float32: -99.99}},
[]spanner.NullFloat64{{Valid: true, Float64: 6.626}, {}, {Valid: true, Float64: 10.01}},
[]spanner.NullNumeric{nullNumeric(true, "3.14"), {}, nullNumeric(true, "10.01")},
[]spanner.NullDate{{Valid: true, Date: civil.Date{Year: 2000, Month: 2, Day: 29}}, {}, {Valid: true, Date: civil.Date{Year: 2021, Month: 7, Day: 27}}},
Expand All @@ -507,6 +511,7 @@ func TestQueryWithAllTypes(t *testing.T) {
var s string
var bt []byte
var i int64
var f32 float32
var f float64
var r big.Rat
var d civil.Date
Expand All @@ -516,12 +521,13 @@ func TestQueryWithAllTypes(t *testing.T) {
var sArray []spanner.NullString
var btArray [][]byte
var iArray []spanner.NullInt64
var f32Array []spanner.NullFloat32
var fArray []spanner.NullFloat64
var rArray []spanner.NullNumeric
var dArray []spanner.NullDate
var tsArray []spanner.NullTime
var jArray []spanner.NullJSON
err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts, &j, &bArray, &sArray, &btArray, &iArray, &fArray, &rArray, &dArray, &tsArray, &jArray)
err = rows.Scan(&b, &s, &bt, &i, &f32, &f, &r, &d, &ts, &j, &bArray, &sArray, &btArray, &iArray, &f32Array, &fArray, &rArray, &dArray, &tsArray, &jArray)
if err != nil {
t.Fatal(err)
}
Expand All @@ -537,6 +543,9 @@ func TestQueryWithAllTypes(t *testing.T) {
if g, w := i, int64(5); g != w {
t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", g, w)
}
if g, w := f32, float32(3.14); g != w {
t.Errorf("row value mismatch for float32\nGot: %v\nWant: %v", g, w)
}
if g, w := f, 3.14; g != w {
t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", g, w)
}
Expand Down Expand Up @@ -566,8 +575,11 @@ func TestQueryWithAllTypes(t *testing.T) {
if g, w := iArray, []spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}}; !cmp.Equal(g, w) {
t.Errorf("row value mismatch for int array\nGot: %v\nWant: %v", g, w)
}
if g, w := f32Array, []spanner.NullFloat32{{Valid: true, Float32: 3.14}, {}, {Valid: true, Float32: -99.99}}; !cmp.Equal(g, w) {
t.Errorf("row value mismatch for float32 array\nGot: %v\nWant: %v", g, w)
}
if g, w := fArray, []spanner.NullFloat64{{Valid: true, Float64: 6.626}, {}, {Valid: true, Float64: 10.01}}; !cmp.Equal(g, w) {
t.Errorf("row value mismatch for float array\nGot: %v\nWant: %v", g, w)
t.Errorf("row value mismatch for float64 array\nGot: %v\nWant: %v", g, w)
}
if g, w := rArray, []spanner.NullNumeric{nullNumeric(true, "3.14"), {}, nullNumeric(true, "10.01")}; !cmp.Equal(g, w, cmp.AllowUnexported(big.Rat{}, big.Int{})) {
t.Errorf("row value mismatch for numeric array\nGot: %v\nWant: %v", g, w)
Expand Down Expand Up @@ -597,10 +609,10 @@ func TestQueryWithAllTypes(t *testing.T) {
t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w)
}
req := sqlRequests[0].(*sppb.ExecuteSqlRequest)
if g, w := len(req.ParamTypes), 18; g != w {
if g, w := len(req.ParamTypes), 20; g != w {
t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w)
}
if g, w := len(req.Params.Fields), 18; g != w {
if g, w := len(req.Params.Fields), 20; g != w {
t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w)
}
wantParams := []struct {
Expand Down Expand Up @@ -629,6 +641,11 @@ func TestQueryWithAllTypes(t *testing.T) {
code: sppb.TypeCode_INT64,
value: "5",
},
{
name: "float32",
code: sppb.TypeCode_FLOAT32,
value: float64(float32(3.14)),
},
{
name: "float64",
code: sppb.TypeCode_FLOAT64,
Expand Down Expand Up @@ -694,6 +711,16 @@ func TestQueryWithAllTypes(t *testing.T) {
{Kind: &structpb.Value_StringValue{StringValue: "2"}},
}},
},
{
name: "float32Array",
code: sppb.TypeCode_FLOAT32,
array: true,
value: &structpb.ListValue{Values: []*structpb.Value{
{Kind: &structpb.Value_NumberValue{NumberValue: float64(float32(3.14))}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NumberValue{NumberValue: float64(float32(-99.99))}},
}},
},
{
name: "float64Array",
code: sppb.TypeCode_FLOAT64,
Expand Down Expand Up @@ -770,6 +797,8 @@ func TestQueryWithAllTypes(t *testing.T) {
switch wantParam.code {
case sppb.TypeCode_BOOL:
g = val.GetBoolValue()
case sppb.TypeCode_FLOAT32:
g = val.GetNumberValue()
case sppb.TypeCode_FLOAT64:
g = val.GetNumberValue()
default:
Expand Down Expand Up @@ -802,7 +831,8 @@ func TestQueryWithNullParameters(t *testing.T) {
AND ColString=@string
AND ColBytes=@bytes
AND ColInt=@int64
AND ColFloat=@float64
AND ColFloat32=@float32
AND ColFloat64=@float64
AND ColNumeric=@numeric
AND ColDate=@date
AND ColTimestamp=@timestamp
Expand All @@ -811,7 +841,8 @@ func TestQueryWithNullParameters(t *testing.T) {
AND ColStringArray=@stringArray
AND ColBytesArray=@bytesArray
AND ColIntArray=@int64Array
AND ColFloatArray=@float64Array
AND ColFloat32Array=@float32Array
AND ColFloat64Array=@float64Array
AND ColNumericArray=@numericArray
AND ColDateArray=@dateArray
AND ColTimestampArray=@timestampArray
Expand Down Expand Up @@ -840,6 +871,7 @@ func TestQueryWithNullParameters(t *testing.T) {
nil, // string
nil, // bytes
nil, // int64
nil, // float32
nil, // float64
nil, // numeric
nil, // date
Expand All @@ -849,19 +881,21 @@ func TestQueryWithNullParameters(t *testing.T) {
nil, // string array
nil, // bytes array
nil, // int64 array
nil, // float32 array
nil, // float64 array
nil, // numeric array
nil, // date array
nil, // timestamp array
nil, // json array
}},
{
typed: 8,
typed: 9,
values: []interface{}{
spanner.NullBool{},
spanner.NullString{},
nil, // bytes
spanner.NullInt64{},
spanner.NullFloat32{},
spanner.NullFloat64{},
spanner.NullNumeric{},
spanner.NullDate{},
Expand All @@ -871,6 +905,7 @@ func TestQueryWithNullParameters(t *testing.T) {
nil, // string array
nil, // bytes array
nil, // int64 array
nil, // float32 array
nil, // float64 array
nil, // numeric array
nil, // date array
Expand All @@ -889,6 +924,7 @@ func TestQueryWithNullParameters(t *testing.T) {
var s sql.NullString
var bt []byte
var i sql.NullInt64
var f32 spanner.NullFloat32 // There's no equivalent sql type.
var f sql.NullFloat64
var r spanner.NullNumeric // There's no equivalent sql type.
var d spanner.NullDate // There's no equivalent sql type.
Expand All @@ -898,12 +934,13 @@ func TestQueryWithNullParameters(t *testing.T) {
var sArray []spanner.NullString
var btArray [][]byte
var iArray []spanner.NullInt64
var f32Array []spanner.NullFloat32
var fArray []spanner.NullFloat64
var rArray []spanner.NullNumeric
var dArray []spanner.NullDate
var tsArray []spanner.NullTime
var jArray []spanner.NullJSON
err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts, &j, &bArray, &sArray, &btArray, &iArray, &fArray, &rArray, &dArray, &tsArray, &jArray)
err = rows.Scan(&b, &s, &bt, &i, &f32, &f, &r, &d, &ts, &j, &bArray, &sArray, &btArray, &iArray, &f32Array, &fArray, &rArray, &dArray, &tsArray, &jArray)
if err != nil {
t.Fatal(err)
}
Expand All @@ -919,6 +956,9 @@ func TestQueryWithNullParameters(t *testing.T) {
if i.Valid {
t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", i, spanner.NullInt64{})
}
if f32.Valid {
t.Errorf("row value mismatch for float32\nGot: %v\nWant: %v", f, spanner.NullFloat32{})
}
if f.Valid {
t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", f, spanner.NullFloat64{})
}
Expand Down Expand Up @@ -946,6 +986,9 @@ func TestQueryWithNullParameters(t *testing.T) {
if iArray != nil {
t.Errorf("row value mismatch for int64 array\nGot: %v\nWant: %v", iArray, nil)
}
if f32Array != nil {
t.Errorf("row value mismatch for float32 array\nGot: %v\nWant: %v", f32Array, nil)
}
if fArray != nil {
t.Errorf("row value mismatch for float64 array\nGot: %v\nWant: %v", fArray, nil)
}
Expand Down Expand Up @@ -975,7 +1018,7 @@ func TestQueryWithNullParameters(t *testing.T) {
if g, w := len(req.ParamTypes), p.typed; g != w {
t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w)
}
if g, w := len(req.Params.Fields), 18; g != w {
if g, w := len(req.Params.Fields), 20; g != w {
t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w)
}
for _, param := range req.Params.Fields {
Expand Down
56 changes: 28 additions & 28 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,71 @@ go 1.19
replace github.com/googleapis/go-sql-spanner => ../

require (
cloud.google.com/go v0.112.0
cloud.google.com/go/spanner v1.56.0
cloud.google.com/go v0.112.1
cloud.google.com/go/spanner v1.59.0
github.com/docker/docker v24.0.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/googleapis/go-sql-spanner v1.0.1
)

require (
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/longrunning v0.5.5 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe // indirect
github.com/cncf/xds/go v0.0.0-20231109132714-523115ebc101 // indirect
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/go-control-plane v0.11.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/envoyproxy/go-control-plane v0.12.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.161.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
Loading

0 comments on commit 7db1b8f

Please sign in to comment.