Skip to content

Commit

Permalink
Merge pull request #6301 from planetscale/charset_introducer
Browse files Browse the repository at this point in the history
Allow charset introducers for vindex columns
  • Loading branch information
deepthi authored Jun 11, 2020
2 parents fc614f5 + 532dea3 commit f672f4f
Show file tree
Hide file tree
Showing 10 changed files with 2,541 additions and 2,355 deletions.
36 changes: 34 additions & 2 deletions go/test/endtoend/vtgate/setstatement/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,54 @@ var (
val2 int,
val3 float,
primary key(id)
)Engine=InnoDB;`
)Engine=InnoDB;
CREATE TABLE test_vdx (
val1 varchar(16) NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (val1)
) ENGINE=Innodb;
`

vSchema = `
{
"sharded":true,
"vindexes": {
"hash_index": {
"type": "hash"
}
},
"lookup1": {
"type": "consistent_lookup",
"params": {
"table": "test_vdx",
"from": "val1",
"to": "keyspace_id",
"ignore_nulls": "true"
},
"owner": "test"
},
"unicode_vdx":{
"type": "unicode_loose_md5"
}
},
"tables": {
"test":{
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
},
{
"column": "val1",
"name": "lookup1"
}
]
},
"test_vdx":{
"column_vindexes": [
{
"column": "val1",
"name": "unicode_vdx"
}
]
}
Expand Down
17 changes: 17 additions & 0 deletions go/test/endtoend/vtgate/setstatement/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ import (
"vitess.io/vitess/go/test/endtoend/cluster"
)

func TestCharsetIntro(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

_, err = exec(t, conn, "delete from test")
require.NoError(t, err)
_, err = exec(t, conn, "insert into test (id,val1) values (666, _binary'abc')")
require.NoError(t, err)
}

func TestSetSysVar(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
Expand Down
2 changes: 2 additions & 0 deletions go/test/endtoend/vtgate/setstatement/udv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func TestSetUDV(t *testing.T) {
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()
_, err = exec(t, conn, "delete from test")
require.NoError(t, err)

for i, q := range queries {
t.Run(fmt.Sprintf("%d-%s", i, q.query), func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ func NewPlanValue(node Expr) (sqltypes.PlanValue, error) {
return pv, nil
case *NullVal:
return sqltypes.PlanValue{}, nil
case *UnaryExpr:
switch node.Operator {
case UBinaryStr, Utf8mb4Str, Utf8Str, Latin1Str: // for some charset introducers, we can just ignore them
return NewPlanValue(node.Expr)
}
}
return sqltypes.PlanValue{}, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "expression is too complex '%v'", String(node))
}
80 changes: 65 additions & 15 deletions go/vt/sqlparser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package sqlparser

import (
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/require"
"vitess.io/vitess/go/test/utils"

"github.com/stretchr/testify/assert"
"vitess.io/vitess/go/sqltypes"
)
Expand Down Expand Up @@ -413,25 +414,74 @@ func TestNewPlanValue(t *testing.T) {
Val: []byte("2.1"),
},
out: sqltypes.PlanValue{Value: sqltypes.NewFloat64(2.1)},
}, {
in: &UnaryExpr{
Operator: Latin1Str,
Expr: &SQLVal{
Type: StrVal,
Val: []byte("strval"),
},
},
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")},
}, {
in: &UnaryExpr{
Operator: UBinaryStr,
Expr: &SQLVal{
Type: StrVal,
Val: []byte("strval"),
},
},
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")},
}, {
in: &UnaryExpr{
Operator: Utf8mb4Str,
Expr: &SQLVal{
Type: StrVal,
Val: []byte("strval"),
},
},
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")},
}, {
in: &UnaryExpr{
Operator: Utf8Str,
Expr: &SQLVal{
Type: StrVal,
Val: []byte("strval"),
},
},
out: sqltypes.PlanValue{Value: sqltypes.NewVarBinary("strval")},
}, {
in: &UnaryExpr{
Operator: MinusStr,
Expr: &SQLVal{
Type: FloatVal,
Val: []byte("2.1"),
},
},
err: "expression is too complex",
}}
for _, tc := range tcases {
got, err := NewPlanValue(tc.in)
if tc.err != "" {
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("NewPlanValue(%s) error: %v, want '%s'", String(tc.in), err, tc.err)
t.Run(String(tc.in), func(t *testing.T) {
got, err := NewPlanValue(tc.in)
if tc.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.err)
return
}
continue
}
if err != nil {
t.Error(err)
continue
}
if !reflect.DeepEqual(tc.out, got) {
t.Errorf("NewPlanValue(%s): %v, want %v", String(tc.in), got, tc.out)
}

require.NoError(t, err)
mustMatch(t, tc.out, got, "wut!")
})
}
}

var mustMatch = utils.MustMatchFn(
[]interface{}{ // types with unexported fields
sqltypes.Value{},
},
[]string{".Conn"}, // ignored fields
)

func newStrVal(in string) *SQLVal {
return NewStrVal([]byte(in))
}
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ const (
BinaryStr = "binary "
UBinaryStr = "_binary "
Utf8mb4Str = "_utf8mb4 "
Utf8Str = "_utf8 "
Latin1Str = "_latin1 "

// this string is "character set" and this comment is required
CharacterSetStr = " character set"
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,8 @@ var (
input: "select binary 'a' = 'A' from t",
}, {
input: "select 1 from t where foo = _binary 'bar'",
}, {
input: "select 1 from t where foo = _utf8 'bar' and bar = _latin1 'sjösjuk'",
}, {
input: "select 1 from t where foo = _binary'bar'",
output: "select 1 from t where foo = _binary 'bar'",
Expand Down
Loading

0 comments on commit f672f4f

Please sign in to comment.