[go: up one dir, main page]

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
8 changes: 8 additions & 0 deletions spanner/protoutils.go
Expand Up @@ -57,6 +57,14 @@ func intType() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_INT64}
}

func float32Proto(n float32) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_NumberValue{NumberValue: float64(n)}}
}

func float32Type() *sppb.Type {
return &sppb.Type{Code: sppb.TypeCode_FLOAT32}
}

func floatProto(n float64) *proto3.Value {
return &proto3.Value{Kind: &proto3.Value_NumberValue{NumberValue: n}}
}
Expand Down
2 changes: 2 additions & 0 deletions spanner/row.go
Expand Up @@ -57,6 +57,8 @@ import (
// *[]int64, *[]NullInt64 - INT64 ARRAY
// *bool(not NULL), *NullBool - BOOL
// *[]bool, *[]NullBool - BOOL ARRAY
// *float32(not NULL), *NullFloat32 - FLOAT32
// *[]float32, *[]NullFloat32 - FLOAT32 ARRAY
// *float64(not NULL), *NullFloat64 - FLOAT64
// *[]float64, *[]NullFloat64 - FLOAT64 ARRAY
// *big.Rat(not NULL), *NullNumeric - NUMERIC
Expand Down
139 changes: 122 additions & 17 deletions spanner/row_test.go
Expand Up @@ -66,6 +66,11 @@ var (
{Name: "NULL_FLOAT64", Type: floatType()},
{Name: "FLOAT64_ARRAY", Type: listType(floatType())},
{Name: "NULL_FLOAT64_ARRAY", Type: listType(floatType())},
// FLOAT32 / FLOAT32 ARRAY
{Name: "FLOAT32", Type: float32Type()},
{Name: "NULL_FLOAT32", Type: float32Type()},
{Name: "FLOAT32_ARRAY", Type: listType(float32Type())},
{Name: "NULL_FLOAT32_ARRAY", Type: listType(float32Type())},
// TIMESTAMP / TIMESTAMP ARRAY
{Name: "TIMESTAMP", Type: timeType()},
{Name: "NULL_TIMESTAMP", Type: timeType()},
Expand All @@ -84,7 +89,8 @@ var (
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", stringType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
),
),
},
Expand All @@ -94,7 +100,8 @@ var (
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", stringType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
),
),
},
Expand Down Expand Up @@ -125,6 +132,11 @@ var (
nullProto(),
listProto(nullProto(), nullProto(), floatProto(1.7)),
nullProto(),
// FLOAT32 / FLOAT32 ARRAY
float32Proto(0.3),
nullProto(),
listProto(nullProto(), nullProto(), float32Proto(0.3)),
nullProto(),
// TIMESTAMP / TIMESTAMP ARRAY
timeProto(tm),
nullProto(),
Expand All @@ -138,7 +150,7 @@ var (
// STRUCT ARRAY
listProto(
nullProto(),
listProto(intProto(3), floatProto(33.3), stringProto("three")),
listProto(intProto(3), floatProto(33.3), float32Proto(0.3), stringProto("three")),
nullProto(),
),
nullProto(),
Expand Down Expand Up @@ -177,6 +189,11 @@ func TestColumnValues(t *testing.T) {
{NullFloat64{}},
{[]NullFloat64{{}, {}, {1.7, true}}},
{[]NullFloat64(nil)},
// FLOAT32 / FLOAT64 ARRAY
{float32(0.3), NullFloat32{0.3, true}},
{NullFloat32{}},
{[]NullFloat32{{}, {}, {float32(0.3), true}}},
{[]NullFloat32(nil)},
// TIMESTAMP / TIMESTAMP ARRAY
{tm, NullTime{tm, true}},
{NullTime{}},
Expand All @@ -192,13 +209,15 @@ func TestColumnValues(t *testing.T) {
[]*struct {
Col1 NullInt64
Col2 NullFloat64
Col3 string
Col3 NullFloat32
Col4 string
}{
nil,

{
NullInt64{3, true},
NullFloat64{33.3, true},
NullFloat32{0.3, true},
"three",
},
nil,
Expand All @@ -210,11 +229,13 @@ func TestColumnValues(t *testing.T) {
fields: []*sppb.StructType_Field{
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", stringType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
},
vals: []*proto3.Value{
intProto(3),
floatProto(33.3),
float32Proto(0.3),
stringProto("three"),
},
},
Expand All @@ -227,7 +248,8 @@ func TestColumnValues(t *testing.T) {
[]*struct {
Col1 NullInt64
Col2 NullFloat64
Col3 string
Col3 NullFloat32
Col4 string
}(nil),
[]NullRow(nil),
},
Expand Down Expand Up @@ -311,32 +333,37 @@ func TestNilDst(t *testing.T) {
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", float32Type()),
),
),
},
},
[]*proto3.Value{listProto(
listProto(intProto(3), floatProto(33.3)),
listProto(intProto(3), floatProto(33.3), float32Proto(0.3)),
)},
},
(*[]*struct {
Col1 int
Col2 float64
Col3 float32
})(nil),
errDecodeColumn(0, errNilDst((*[]*struct {
Col1 int
Col2 float64
Col3 float32
})(nil))),
(*struct {
StructArray []*struct {
Col1 int
Col2 float64
Col3 float32
} `spanner:"STRUCT_ARRAY"`
})(nil),
errNilDst((*struct {
StructArray []*struct {
Col1 int
Col2 float64
Col3 float32
} `spanner:"STRUCT_ARRAY"`
})(nil)),
},
Expand Down Expand Up @@ -399,6 +426,10 @@ func TestNullTypeErr(t *testing.T) {
"NULL_FLOAT64",
proto.Float64(0.0),
},
{
"NULL_FLOAT32",
proto.Float32(0.0),
},
{
"NULL_TIMESTAMP",
&tm,
Expand Down Expand Up @@ -857,6 +888,50 @@ func TestBrokenRow(t *testing.T) {
proto.Float64(0),
errDecodeColumn(0, errUnexpectedFloat64Str("nan")),
},
{
// Field specifies FLOAT32 type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{{Kind: (*proto3.Value_NumberValue)(nil)}},
},
&NullFloat32{1.0, true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_NumberValue)(nil)}, "Number")),
},
{
// Field specifies FLOAT32 type, but value is for BOOL type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{boolProto(true)},
},
&NullFloat32{1.0, true},
errDecodeColumn(0, errSrcVal(boolProto(true), "Number")),
},
{
// Field specifies FLOAT32 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{stringProto("nan")},
},
&NullFloat32{},
errDecodeColumn(0, errUnexpectedFloat32Str("nan")),
},
{
// Field specifies FLOAT32 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{stringProto("nan")},
},
proto.Float32(0),
errDecodeColumn(0, errUnexpectedFloat32Str("nan")),
},
{
// Field specifies BYTES type, value is having a nil Kind.
&Row{
Expand Down Expand Up @@ -1531,6 +1606,11 @@ func TestToStruct(t *testing.T) {
NullFloat64 NullFloat64 `spanner:"NULL_FLOAT64"`
Float64Array []NullFloat64 `spanner:"FLOAT64_ARRAY"`
NullFloat64Array []NullFloat64 `spanner:"NULL_FLOAT64_ARRAY"`
// FLOAT32 / FLOAT32 ARRAY
Float32 float32 `spanner:"FLOAT32"`
NullFloat32 NullFloat32 `spanner:"NULL_FLOAT32"`
Float32Array []NullFloat32 `spanner:"FLOAT32_ARRAY"`
NullFloat32Array []NullFloat32 `spanner:"NULL_FLOAT32_ARRAY"`
// TIMESTAMP / TIMESTAMP ARRAY
Timestamp time.Time `spanner:"TIMESTAMP"`
NullTimestamp NullTime `spanner:"NULL_TIMESTAMP"`
Expand All @@ -1546,12 +1626,14 @@ func TestToStruct(t *testing.T) {
StructArray []*struct {
Col1 int64
Col2 float64
Col3 string
Col3 float32
Col4 string
} `spanner:"STRUCT_ARRAY"`
NullStructArray []*struct {
Col1 int64
Col2 float64
Col3 string
Col3 float32
Col4 string
} `spanner:"NULL_STRUCT_ARRAY"`
}{
{}, // got
Expand Down Expand Up @@ -1581,6 +1663,11 @@ func TestToStruct(t *testing.T) {
NullFloat64{},
[]NullFloat64{{}, {}, {1.7, true}},
[]NullFloat64(nil),
// FLOAT32 / FLOAT32 ARRAY
float32(0.3),
NullFloat32{},
[]NullFloat32{{}, {}, {float32(0.3), true}},
[]NullFloat32(nil),
// TIMESTAMP / TIMESTAMP ARRAY
tm,
NullTime{},
Expand All @@ -1595,17 +1682,19 @@ func TestToStruct(t *testing.T) {
[]*struct {
Col1 int64
Col2 float64
Col3 string
Col3 float32
Col4 string
}{
nil,

{3, 33.3, "three"},
{3, 33.3, float32(0.3), "three"},
nil,
},
[]*struct {
Col1 int64
Col2 float64
Col3 string
Col3 float32
Col4 string
}(nil),
}, // want
}
Expand All @@ -1632,7 +1721,9 @@ func TestToStructWithCustomTypes(t *testing.T) {
type CustomBool bool
type CustomNullBool NullBool
type CustomFloat64 float64
type CustomFloat32 float32
type CustomNullFloat64 NullFloat64
type CustomNullFloat32 NullFloat32
type CustomTime time.Time
type CustomNullTime NullTime
type CustomDate civil.Date
Expand Down Expand Up @@ -1665,6 +1756,11 @@ func TestToStructWithCustomTypes(t *testing.T) {
NullFloat64 CustomNullFloat64 `spanner:"NULL_FLOAT64"`
Float64Array []CustomNullFloat64 `spanner:"FLOAT64_ARRAY"`
NullFloat64Array []CustomNullFloat64 `spanner:"NULL_FLOAT64_ARRAY"`
// FLOAT32 / FLOAT32 ARRAY
Float32 CustomFloat32 `spanner:"FLOAT32"`
NullFloat32 CustomNullFloat32 `spanner:"NULL_FLOAT32"`
Float32Array []CustomNullFloat32 `spanner:"FLOAT32_ARRAY"`
NullFloat32Array []CustomNullFloat32 `spanner:"NULL_FLOAT32_ARRAY"`
// TIMESTAMP / TIMESTAMP ARRAY
Timestamp CustomTime `spanner:"TIMESTAMP"`
NullTimestamp CustomNullTime `spanner:"NULL_TIMESTAMP"`
Expand All @@ -1680,12 +1776,14 @@ func TestToStructWithCustomTypes(t *testing.T) {
StructArray []*struct {
Col1 CustomInt64
Col2 CustomFloat64
Col3 CustomString
Col3 CustomFloat32
Col4 CustomString
} `spanner:"STRUCT_ARRAY"`
NullStructArray []*struct {
Col1 CustomInt64
Col2 CustomFloat64
Col3 CustomString
Col3 CustomFloat32
Col4 CustomString
} `spanner:"NULL_STRUCT_ARRAY"`
}{
{}, // got
Expand Down Expand Up @@ -1715,6 +1813,11 @@ func TestToStructWithCustomTypes(t *testing.T) {
CustomNullFloat64{},
[]CustomNullFloat64{{}, {}, {1.7, true}},
[]CustomNullFloat64(nil),
// FLOAT32 / FLOAT32 ARRAY
0.3,
CustomNullFloat32{},
[]CustomNullFloat32{{}, {}, {0.3, true}},
[]CustomNullFloat32(nil),
// TIMESTAMP / TIMESTAMP ARRAY
CustomTime(tm),
CustomNullTime{},
Expand All @@ -1729,17 +1832,19 @@ func TestToStructWithCustomTypes(t *testing.T) {
[]*struct {
Col1 CustomInt64
Col2 CustomFloat64
Col3 CustomString
Col3 CustomFloat32
Col4 CustomString
}{
nil,

{3, 33.3, "three"},
{3, 33.3, 0.3, "three"},
nil,
},
[]*struct {
Col1 CustomInt64
Col2 CustomFloat64
Col3 CustomString
Col3 CustomFloat32
Col4 CustomString
}(nil),
}, // want
}
Expand Down