Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): add support of float32 type #9525

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add some more tests
  • Loading branch information
olavloite committed Mar 8, 2024
commit 7651ec3366206aaa82d7060e21b2625294faa162
4 changes: 1 addition & 3 deletions spanner/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func keyPartValue(part interface{}) (pb *proto3.Value, err error) {
pb, _, err = encodeValue(int64(v))
case uint32:
pb, _, err = encodeValue(int64(v))
case float32:
pb, _, err = encodeValue(float64(v))
case int64, float64, NullInt64, NullFloat64, bool, NullBool, []byte, string, NullString, time.Time, civil.Date, NullTime, NullDate, big.Rat, NullNumeric:
case int64, float64, float32, NullInt64, NullFloat64, NullFloat32, bool, NullBool, []byte, string, NullString, time.Time, civil.Date, NullTime, NullDate, big.Rat, NullNumeric:
pb, _, err = encodeValue(v)
case Encoder:
part, err = v.EncodeSpanner()
Expand Down
10 changes: 10 additions & 0 deletions spanner/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ func TestKey(t *testing.T) {
wantProto: listValueProto(nullProto()),
wantStr: "(<null>)",
},
{
k: Key{NullFloat32{3.14, true}},
wantProto: listValueProto(floatProto(float64(float32(3.14)))),
wantStr: "(3.14)",
},
{
k: Key{NullFloat32{2.0, false}},
wantProto: listValueProto(nullProto()),
wantStr: "(<null>)",
},
{
k: Key{NullBool{true, true}},
wantProto: listValueProto(boolProto(true)),
Expand Down
20 changes: 18 additions & 2 deletions spanner/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ func TestConvertParams(t *testing.T) {
{[]NullFloat64(nil), nullProto(), listType(floatType())},
{[]NullFloat64{}, listProto(), listType(floatType())},
{[]NullFloat64{{2.72, true}, {}}, listProto(floatProto(2.72), nullProto()), listType(floatType())},
// float32
{float32(0.0), float32Proto(0.0), float32Type()},
{float32(math.Inf(1)), float32Proto(float32(math.Inf(1))), float32Type()},
{float32(math.Inf(-1)), float32Proto(float32(math.Inf(-1))), float32Type()},
{float32(math.NaN()), float32Proto(float32(math.NaN())), float32Type()},
{NullFloat32{3.14, true}, float32Proto(3.14), float32Type()},
{NullFloat32{-99.99, false}, nullProto(), float32Type()},
{[]float32(nil), nullProto(), listType(float32Type())},
{[]float32{}, listProto(), listType(float32Type())},
{[]float32{3.14, float32(math.Inf(1))}, listProto(float32Proto(3.14), float32Proto(float32(math.Inf(1)))), listType(float32Type())},
{[]NullFloat32(nil), nullProto(), listType(float32Type())},
{[]NullFloat32{}, listProto(), listType(float32Type())},
{[]NullFloat32{{3.14, true}, {}}, listProto(float32Proto(3.14), nullProto()), listType(float32Type())},
// string
{"", stringProto(""), stringType()},
{"foo", stringProto("foo"), stringType()},
Expand Down Expand Up @@ -178,11 +191,14 @@ func TestConvertParams(t *testing.T) {
if test.wantType.Code == floatType().Code && proto.MarshalTextString(gotParamField) == proto.MarshalTextString(test.wantField) {
continue
}
t.Errorf("%#v: got %v, want %v\n", test.val, gotParamField, test.wantField)
if test.wantType.Code == float32Type().Code && proto.MarshalTextString(gotParamField) == proto.MarshalTextString(test.wantField) {
continue
}
t.Errorf("%#v:\n got: %v\nwant: %v\n", test.val, gotParamField, test.wantField)
}
gotParamType := gotParamTypes["var"]
if !proto.Equal(gotParamType, test.wantType) {
t.Errorf("%#v: got %v, want %v\n", test.val, gotParamType, test.wantField)
t.Errorf("%#v:\n got: %v\nwant: %v\n", test.val, gotParamType, test.wantField)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4138,6 +4138,10 @@ func convertCustomTypeValue(sourceType decodableSpannerType, v interface{}) (int
destination = reflect.Indirect(reflect.New(reflect.TypeOf(float64(0.0))))
case spannerTypeNullFloat64:
destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullFloat64{})))
case spannerTypeNonNullFloat32:
destination = reflect.Indirect(reflect.New(reflect.TypeOf(float32(0.0))))
case spannerTypeNullFloat32:
destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullFloat32{})))
case spannerTypeNonNullTime:
destination = reflect.Indirect(reflect.New(reflect.TypeOf(time.Time{})))
case spannerTypeNullTime:
Expand Down Expand Up @@ -4201,6 +4205,16 @@ func convertCustomTypeValue(sourceType decodableSpannerType, v interface{}) (int
return []NullFloat64(nil), nil
}
destination = reflect.MakeSlice(reflect.TypeOf([]NullFloat64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
case spannerTypeArrayOfNonNullFloat32:
if reflect.ValueOf(v).IsNil() {
return []float32(nil), nil
}
destination = reflect.MakeSlice(reflect.TypeOf([]float32{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
case spannerTypeArrayOfNullFloat32:
if reflect.ValueOf(v).IsNil() {
return []NullFloat32(nil), nil
}
destination = reflect.MakeSlice(reflect.TypeOf([]NullFloat32{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
case spannerTypeArrayOfNonNullTime:
if reflect.ValueOf(v).IsNil() {
return []time.Time(nil), nil
Expand Down