Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 2f83836

Browse files
author
Juanjo Alvarez
committed
Added tests for VarChar
Added note about VarChar in MysqlTypeToType Signed-off-by: Juanjo Alvarez <juanjo@sourced.tech>
1 parent 25d7888 commit 2f83836

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

sql/expression/function/nullif_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ func TestNullIf(t *testing.T) {
2828
)
2929
require.Equal(t, sql.Text, f.Type())
3030

31+
var3 := sql.VarChar(3)
32+
f = NewNullIf(
33+
expression.NewGetField(0, var3, "ex1", true),
34+
expression.NewGetField(1, var3, "ex2", true),
35+
)
36+
require.Equal(t, var3, f.Type())
37+
3138
for _, tc := range testCases {
3239
v, err := f.Eval(sql.NewEmptyContext(), sql.NewRow(tc.ex1, tc.ex2))
3340
require.NoError(t, err)

sql/expression/literal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type Literal struct {
1414

1515
// NewLiteral creates a new Literal expression.
1616
func NewLiteral(value interface{}, fieldType sql.Type) *Literal {
17+
// TODO(juanjux): we should probably check here if the type is sql.VarChar and the
18+
// Capacity of the Type and the length of the value, but this can't return an error
1719
return &Literal{
1820
value: value,
1921
fieldType: fieldType,

sql/expression/tuple_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package expression
33
import (
44
"testing"
55

6-
"github.com/stretchr/testify/require"
76
"github.com/src-d/go-mysql-server/sql"
7+
"github.com/stretchr/testify/require"
88
)
99

1010
func TestTuple(t *testing.T) {

sql/type.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
// ErrValueNotNil is thrown when a value that was expected to be nil, is not
3434
ErrValueNotNil = errors.NewKind("value not nil: %#v")
3535

36-
// ErrNotTuple is retuned when the value is not a tuple.
36+
// ErrNotTuple is returned when the value is not a tuple.
3737
ErrNotTuple = errors.NewKind("value of type %T is not a tuple")
3838

3939
// ErrInvalidColumnNumber is returned when a tuple has an invalid number of
@@ -200,8 +200,6 @@ var (
200200
Date dateT
201201
// Text is a string type.
202202
Text textT
203-
// VarChar is a string type with a length.
204-
VarChar varcharT
205203
// Boolean is a boolean type.
206204
Boolean booleanT
207205
// JSON is a type that holds any valid JSON object.
@@ -220,6 +218,11 @@ func Array(underlying Type) Type {
220218
return arrayT{underlying}
221219
}
222220

221+
// VarChar returns a new VarChar type of the given length.
222+
func VarChar(length int) Type {
223+
return varCharT{length: length}
224+
}
225+
223226
// MysqlTypeToType gets the column type using the mysql type
224227
func MysqlTypeToType(sql query.Type) (Type, error) {
225228
switch sql {
@@ -249,10 +252,12 @@ func MysqlTypeToType(sql query.Type) (Type, error) {
249252
return Timestamp, nil
250253
case sqltypes.Date:
251254
return Date, nil
252-
case sqltypes.VarChar:
253-
return VarChar, nil
254255
case sqltypes.Text:
255256
return Text, nil
257+
case sqltypes.VarChar:
258+
// Since we can't get the size of the sqltypes.VarChar to instantiate a
259+
// specific VarChar(length) type we return a Text here
260+
return Text, nil
256261
case sqltypes.Bit:
257262
return Boolean, nil
258263
case sqltypes.TypeJSON:
@@ -557,28 +562,35 @@ func (t dateT) Compare(a, b interface{}) (int, error) {
557562
return 0, nil
558563
}
559564

560-
type varcharT struct{
565+
type varCharT struct {
561566
length int
562567
}
563568

564-
func (t varcharT) String() string { return fmt.Sprintf("VARCHAR(%d)", t.length) }
569+
func (t varCharT) Capacity() int { return t.length }
570+
571+
func (t varCharT) String() string { return fmt.Sprintf("VARCHAR(%d)", t.length) }
565572

566573
// Type implements Type interface
567-
func (t varcharT) Type() query.Type {
574+
func (t varCharT) Type() query.Type {
568575
return sqltypes.VarChar
569576
}
570577

571578
// SQL implements Type interface
572-
func (t varcharT) SQL(v interface{}) sqltypes.Value {
573-
if _, ok := v.(nullT); ok {
574-
return sqltypes.NULL
579+
func (t varCharT) SQL(v interface{}) (sqltypes.Value, error) {
580+
if v == nil {
581+
return sqltypes.MakeTrusted(sqltypes.VarChar, nil), nil
575582
}
576583

577-
return sqltypes.MakeTrusted(sqltypes.VarChar, []byte(MustConvert(t, v).(string)))
584+
v, err := t.Convert(v)
585+
if err != nil {
586+
return sqltypes.Value{}, err
587+
}
588+
589+
return sqltypes.MakeTrusted(sqltypes.VarChar, []byte(v.(string))), nil
578590
}
579591

580592
// Convert implements Type interface
581-
func (t varcharT) Convert(v interface{}) (interface{}, error) {
593+
func (t varCharT) Convert(v interface{}) (interface{}, error) {
582594
val, err := cast.ToStringE(v)
583595
if err != nil {
584596
return nil, ErrConvertToSQL.New(t)
@@ -591,9 +603,10 @@ func (t varcharT) Convert(v interface{}) (interface{}, error) {
591603
}
592604

593605
// Compare implements Type interface.
594-
func (t varcharT) Compare(a interface{}, b interface{}) (int, error) {
606+
func (t varCharT) Compare(a interface{}, b interface{}) (int, error) {
595607
return strings.Compare(a.(string), b.(string)), nil
596608
}
609+
597610
type textT struct{}
598611

599612
func (t textT) String() string { return "TEXT" }
@@ -972,11 +985,12 @@ func IsDecimal(t Type) bool {
972985

973986
// IsText checks if t is a text type.
974987
func IsText(t Type) bool {
975-
return t == Text || t == Blob || t == JSON || t == VarChar
988+
return t == Text || t == Blob || t == JSON || IsVarChar(t)
976989
}
977990

978991
func IsVarChar(t Type) bool {
979-
return t == VarChar
992+
_, ok := t.(varCharT)
993+
return ok
980994
}
981995

982996
// IsTuple checks if t is a tuple type.

sql/type_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestText(t *testing.T) {
2424
lt(t, Text, "a", "b")
2525
eq(t, Text, "a", "a")
2626
gt(t, Text, "b", "a")
27+
28+
var3, err := VarChar(3).Convert("abc")
29+
require.NoError(t, err)
30+
convert(t, Text, var3, "abc")
2731
}
2832

2933
func TestInt32(t *testing.T) {
@@ -237,6 +241,38 @@ func TestTuple(t *testing.T) {
237241
gt(t, typ, []interface{}{1, 2, 4}, []interface{}{1, 2, 3})
238242
}
239243

244+
func TestVarChar(t *testing.T) {
245+
typ := VarChar(3)
246+
require.True(t, IsVarChar(typ))
247+
require.True(t, IsText(typ))
248+
convert(t, typ, "foo", "foo")
249+
fooByte := []byte{'f', 'o', 'o'}
250+
convert(t, typ, fooByte, "foo")
251+
252+
typ = VarChar(1)
253+
convertErr(t, typ, "foo")
254+
convertErr(t, typ, fooByte)
255+
convertErr(t, typ, 123)
256+
257+
typ = VarChar(10)
258+
convert(t, typ, 123, "123")
259+
convertErr(t, typ, 1234567890123)
260+
261+
convert(t, typ, "", "")
262+
convert(t, typ, 1, "1")
263+
264+
lt(t, typ, "a", "b")
265+
eq(t, typ, "a", "a")
266+
gt(t, typ, "b", "a")
267+
268+
text, err := Text.Convert("abc")
269+
require.NoError(t, err)
270+
271+
convert(t, typ, text, "abc")
272+
typ1 := VarChar(1)
273+
convertErr(t, typ1, text)
274+
}
275+
240276
func TestArray(t *testing.T) {
241277
require := require.New(t)
242278

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy