From 793079af6e347288b442ff8c145629f41d404622 Mon Sep 17 00:00:00 2001 From: James Cor Date: Fri, 29 Mar 2024 10:46:52 -0700 Subject: [PATCH 1/3] add case for time --- go/sqltypes/bind_variables.go | 6 + go/sqltypes/bind_variables_test.go | 318 ++++++++++++++++------------- 2 files changed, 187 insertions(+), 137 deletions(-) diff --git a/go/sqltypes/bind_variables.go b/go/sqltypes/bind_variables.go index 05e201f8d2b..e23447877aa 100644 --- a/go/sqltypes/bind_variables.go +++ b/go/sqltypes/bind_variables.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "strconv" + "time" "google.golang.org/protobuf/proto" @@ -120,6 +121,11 @@ func BuildBindVariable(v interface{}) (*querypb.BindVariable, error) { return Uint64BindVariable(v), nil case float64: return Float64BindVariable(v), nil + case time.Time: + return &querypb.BindVariable{ + Type: querypb.Type_TIMESTAMP, + Value: []byte(v.String()), + }, nil case nil: return NullBindVariable, nil case Value: diff --git a/go/sqltypes/bind_variables_test.go b/go/sqltypes/bind_variables_test.go index b1042f2d59f..982253ef41e 100644 --- a/go/sqltypes/bind_variables_test.go +++ b/go/sqltypes/bind_variables_test.go @@ -21,6 +21,7 @@ import ( "reflect" "strings" "testing" + "time" "google.golang.org/protobuf/proto" @@ -84,163 +85,206 @@ func TestBuildBindVariable(t *testing.T) { in interface{} out *querypb.BindVariable err string - }{{ - in: "aa", - out: &querypb.BindVariable{ - Type: querypb.Type_VARCHAR, - Value: []byte("aa"), - }, - }, { - in: []byte("aa"), - out: &querypb.BindVariable{ - Type: querypb.Type_VARBINARY, - Value: []byte("aa"), - }, - }, { - in: true, - out: &querypb.BindVariable{ - Type: querypb.Type_INT8, - Value: []byte("1"), - }, - }, { - in: false, - out: &querypb.BindVariable{ - Type: querypb.Type_INT8, - Value: []byte("0"), - }, - }, { - in: int(1), - out: &querypb.BindVariable{ - Type: querypb.Type_INT64, - Value: []byte("1"), + }{ + { + in: "aa", + out: &querypb.BindVariable{ + Type: querypb.Type_VARCHAR, + Value: []byte("aa"), + }, }, - }, { - in: int64(1), - out: &querypb.BindVariable{ - Type: querypb.Type_INT64, - Value: []byte("1"), + { + in: []byte("aa"), + out: &querypb.BindVariable{ + Type: querypb.Type_VARBINARY, + Value: []byte("aa"), + }, }, - }, { - in: uint64(1), - out: &querypb.BindVariable{ - Type: querypb.Type_UINT64, - Value: []byte("1"), + { + in: true, + out: &querypb.BindVariable{ + Type: querypb.Type_INT8, + Value: []byte("1"), + }, }, - }, { - in: float64(1), - out: &querypb.BindVariable{ - Type: querypb.Type_FLOAT64, - Value: []byte("1"), + { + in: false, + out: &querypb.BindVariable{ + Type: querypb.Type_INT8, + Value: []byte("0"), + }, }, - }, { - in: nil, - out: NullBindVariable, - }, { - in: MakeTrusted(Int64, []byte("1")), - out: &querypb.BindVariable{ - Type: querypb.Type_INT64, - Value: []byte("1"), + { + in: int(1), + out: &querypb.BindVariable{ + Type: querypb.Type_INT64, + Value: []byte("1"), + }, }, - }, { - in: &querypb.BindVariable{ - Type: querypb.Type_INT64, - Value: []byte("1"), + { + in: int64(1), + out: &querypb.BindVariable{ + Type: querypb.Type_INT64, + Value: []byte("1"), + }, }, - out: &querypb.BindVariable{ - Type: querypb.Type_INT64, - Value: []byte("1"), + { + in: uint64(1), + out: &querypb.BindVariable{ + Type: querypb.Type_UINT64, + Value: []byte("1"), + }, }, - }, { - in: []interface{}{"aa", int64(1)}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ - Type: querypb.Type_VARCHAR, - Value: []byte("aa"), - }, { - Type: querypb.Type_INT64, + { + in: float64(1), + out: &querypb.BindVariable{ + Type: querypb.Type_FLOAT64, Value: []byte("1"), - }}, + }, }, - }, { - in: []string{"aa", "bb"}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ - Type: querypb.Type_VARCHAR, - Value: []byte("aa"), - }, { - Type: querypb.Type_VARCHAR, - Value: []byte("bb"), - }}, + { + in: time.Time{}, + out: &querypb.BindVariable{ + Type: querypb.Type_TIMESTAMP, + Value: []byte("0001-01-01 00:00:00 +0000 UTC"), + }, }, - }, { - in: [][]byte{[]byte("aa"), []byte("bb")}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ - Type: querypb.Type_VARBINARY, - Value: []byte("aa"), - }, { - Type: querypb.Type_VARBINARY, - Value: []byte("bb"), - }}, + { + in: nil, + out: NullBindVariable, }, - }, { - in: []int{1, 2}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ + { + in: MakeTrusted(Int64, []byte("1")), + out: &querypb.BindVariable{ Type: querypb.Type_INT64, Value: []byte("1"), - }, { - Type: querypb.Type_INT64, - Value: []byte("2"), - }}, + }, }, - }, { - in: []int64{1, 2}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ + { + in: &querypb.BindVariable{ Type: querypb.Type_INT64, Value: []byte("1"), - }, { + }, + out: &querypb.BindVariable{ Type: querypb.Type_INT64, - Value: []byte("2"), - }}, - }, - }, { - in: []uint64{1, 2}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ - Type: querypb.Type_UINT64, Value: []byte("1"), - }, { - Type: querypb.Type_UINT64, - Value: []byte("2"), - }}, + }, }, - }, { - in: []float64{1, 2}, - out: &querypb.BindVariable{ - Type: querypb.Type_TUPLE, - Values: []*querypb.Value{{ - Type: querypb.Type_FLOAT64, - Value: []byte("1"), - }, { - Type: querypb.Type_FLOAT64, - Value: []byte("2"), - }}, + { + in: []interface{}{"aa", int64(1)}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_VARCHAR, + Value: []byte("aa"), + }, + { + Type: querypb.Type_INT64, + Value: []byte("1"), + }, + }, + }, + }, { + in: []string{"aa", "bb"}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_VARCHAR, + Value: []byte("aa"), + }, { + Type: querypb.Type_VARCHAR, + Value: []byte("bb"), + }, + }, + }, + }, { + in: [][]byte{[]byte("aa"), []byte("bb")}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{{ + Type: querypb.Type_VARBINARY, + Value: []byte("aa"), + }, { + Type: querypb.Type_VARBINARY, + Value: []byte("bb"), + }, + }, + }, }, - }, { - in: byte(1), - err: "type uint8 not supported as bind var: 1", - }, { - in: []interface{}{1, byte(1)}, - err: "type uint8 not supported as bind var: 1", - }} + { + in: []int{1, 2}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_INT64, + Value: []byte("1"), + }, + { + Type: querypb.Type_INT64, + Value: []byte("2"), + }, + }, + }, + }, + { + in: []int64{1, 2}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_INT64, + Value: []byte("1"), + }, { + Type: querypb.Type_INT64, + Value: []byte("2"), + }, + }, + }, + }, + { + in: []uint64{1, 2}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_UINT64, + Value: []byte("1"), + }, + { + Type: querypb.Type_UINT64, + Value: []byte("2"), + }, + }, + }, + }, + { + in: []float64{1, 2}, + out: &querypb.BindVariable{ + Type: querypb.Type_TUPLE, + Values: []*querypb.Value{ + { + Type: querypb.Type_FLOAT64, + Value: []byte("1"), + }, + { + Type: querypb.Type_FLOAT64, + Value: []byte("2"), + }, + }, + }, + }, + { + in: byte(1), + err: "type uint8 not supported as bind var: 1", + }, + { + in: []interface{}{1, byte(1)}, + err: "type uint8 not supported as bind var: 1", + }, + } for _, tcase := range tcases { bv, err := BuildBindVariable(tcase.in) if err != nil { From d112c44ba23e6d9d5a345a4da339cfb683d9e55c Mon Sep 17 00:00:00 2001 From: James Cor Date: Fri, 29 Mar 2024 12:17:47 -0700 Subject: [PATCH 2/3] add TODO --- go/sqltypes/bind_variables.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/sqltypes/bind_variables.go b/go/sqltypes/bind_variables.go index e23447877aa..4ef7f728280 100644 --- a/go/sqltypes/bind_variables.go +++ b/go/sqltypes/bind_variables.go @@ -126,6 +126,7 @@ func BuildBindVariable(v interface{}) (*querypb.BindVariable, error) { Type: querypb.Type_TIMESTAMP, Value: []byte(v.String()), }, nil + // TODO: somehow support types.Timespan case nil: return NullBindVariable, nil case Value: From 17daf8f078205a64b015b752f69e4285656ab26b Mon Sep 17 00:00:00 2001 From: James Cor Date: Fri, 29 Mar 2024 14:12:46 -0700 Subject: [PATCH 3/3] also support decimal --- go.mod | 1 + go.sum | 2 ++ go/sqltypes/bind_variables.go | 6 ++++++ go/sqltypes/bind_variables_test.go | 8 ++++++++ 4 files changed, 17 insertions(+) diff --git a/go.mod b/go.mod index f2021b44435..5873aac88d8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/dolthub/vitess go 1.19 require ( + github.com/shopspring/decimal v1.3.1 github.com/stretchr/testify v1.4.0 golang.org/x/tools v0.1.9 google.golang.org/grpc v1.24.0 diff --git a/go.sum b/go.sum index 03c40223ccf..fe6aa265999 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= +github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/go/sqltypes/bind_variables.go b/go/sqltypes/bind_variables.go index 4ef7f728280..1a396fb2919 100644 --- a/go/sqltypes/bind_variables.go +++ b/go/sqltypes/bind_variables.go @@ -23,6 +23,7 @@ import ( "strconv" "time" + "github.com/shopspring/decimal" "google.golang.org/protobuf/proto" querypb "github.com/dolthub/vitess/go/vt/proto/query" @@ -121,6 +122,11 @@ func BuildBindVariable(v interface{}) (*querypb.BindVariable, error) { return Uint64BindVariable(v), nil case float64: return Float64BindVariable(v), nil + case decimal.Decimal: + return &querypb.BindVariable{ + Type: querypb.Type_DECIMAL, + Value: []byte(v.String()), + }, nil case time.Time: return &querypb.BindVariable{ Type: querypb.Type_TIMESTAMP, diff --git a/go/sqltypes/bind_variables_test.go b/go/sqltypes/bind_variables_test.go index 982253ef41e..92ead598c7f 100644 --- a/go/sqltypes/bind_variables_test.go +++ b/go/sqltypes/bind_variables_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/shopspring/decimal" "google.golang.org/protobuf/proto" querypb "github.com/dolthub/vitess/go/vt/proto/query" @@ -142,6 +143,13 @@ func TestBuildBindVariable(t *testing.T) { Value: []byte("1"), }, }, + { + in: decimal.NewFromInt(1), + out: &querypb.BindVariable{ + Type: querypb.Type_DECIMAL, + Value: []byte("1"), + }, + }, { in: time.Time{}, out: &querypb.BindVariable{