Skip to content

Commit

Permalink
Merge pull request #6324 from planetscale/release-6.0-bindvars
Browse files Browse the repository at this point in the history
Backport #6270 to 6.0 release branch
  • Loading branch information
deepthi authored Jun 17, 2020
2 parents e5877ac + dfb7102 commit a4d5c67
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 321 deletions.
225 changes: 225 additions & 0 deletions go/test/endtoend/vtgate/vindex_bindvars/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vtgate

import (
"context"
"flag"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/assert"
"vitess.io/vitess/go/sqltypes"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
KeyspaceName = "ks"
Cell = "test"
SchemaSQL = `CREATE TABLE t1 (
id BIGINT NOT NULL,
field BIGINT NOT NULL,
field2 BIGINT,
PRIMARY KEY (id)
) ENGINE=Innodb;
CREATE TABLE lookup1 (
field BIGINT NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (field)
) ENGINE=Innodb;
CREATE TABLE lookup2 (
field2 BIGINT NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (field2)
) ENGINE=Innodb;
`

VSchema = `
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
},
"lookup1": {
"type": "consistent_lookup",
"params": {
"table": "lookup1",
"from": "field",
"to": "keyspace_id",
"ignore_nulls": "true"
},
"owner": "t1"
},
"lookup2": {
"type": "consistent_lookup",
"params": {
"table": "lookup2",
"from": "field2",
"to": "keyspace_id",
"ignore_nulls": "true"
},
"owner": "t1"
}
},
"tables": {
"t1": {
"column_vindexes": [
{
"column": "id",
"name": "hash"
},
{
"column": "field",
"name": "lookup1"
},
{
"column": "field2",
"name": "lookup2"
}
]
},
"lookup1": {
"column_vindexes": [
{
"column": "field",
"name": "hash"
}
]
},
"lookup2": {
"column_vindexes": [
{
"column": "field2",
"name": "hash"
}
]
}
}
}`
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(Cell, "localhost")
defer clusterInstance.Teardown()

// Start topo server
err := clusterInstance.StartTopo()
if err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: KeyspaceName,
SchemaSQL: SchemaSQL,
VSchema: VSchema,
}
err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, true)
if err != nil {
return 1
}

// Start vtgate
err = clusterInstance.StartVtgate()
if err != nil {
return 1
}
vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
return m.Run()
}()
os.Exit(exitCode)
}

func TestVindexBindVarOverlap(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.Nil(t, err)
defer conn.Close()

exec(t, conn, "INSERT INTO t1 (id, field, field2) VALUES "+
"(0,1,2), "+
"(1,2,3), "+
"(2,3,4), "+
"(3,4,5), "+
"(4,5,6), "+
"(5,6,7), "+
"(6,7,8), "+
"(7,8,9), "+
"(8,9,10), "+
"(9,10,11), "+
"(10,11,12), "+
"(11,12,13), "+
"(12,13,14), "+
"(13,14,15), "+
"(14,15,16), "+
"(15,16,17), "+
"(16,17,18), "+
"(17,18,19), "+
"(18,19,20), "+
"(19,20,21), "+
"(20,21,22)")
result := exec(t, conn, "select id, field, field2 from t1 order by id")

expected :=
"[[INT64(0) INT64(1) INT64(2)] " +
"[INT64(1) INT64(2) INT64(3)] " +
"[INT64(2) INT64(3) INT64(4)] " +
"[INT64(3) INT64(4) INT64(5)] " +
"[INT64(4) INT64(5) INT64(6)] " +
"[INT64(5) INT64(6) INT64(7)] " +
"[INT64(6) INT64(7) INT64(8)] " +
"[INT64(7) INT64(8) INT64(9)] " +
"[INT64(8) INT64(9) INT64(10)] " +
"[INT64(9) INT64(10) INT64(11)] " +
"[INT64(10) INT64(11) INT64(12)] " +
"[INT64(11) INT64(12) INT64(13)] " +
"[INT64(12) INT64(13) INT64(14)] " +
"[INT64(13) INT64(14) INT64(15)] " +
"[INT64(14) INT64(15) INT64(16)] " +
"[INT64(15) INT64(16) INT64(17)] " +
"[INT64(16) INT64(17) INT64(18)] " +
"[INT64(17) INT64(18) INT64(19)] " +
"[INT64(18) INT64(19) INT64(20)] " +
"[INT64(19) INT64(20) INT64(21)] " +
"[INT64(20) INT64(21) INT64(22)]]"
assert.Equal(t, expected, fmt.Sprintf("%v", result.Rows))
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.NoError(t, err)
return qr
}
42 changes: 21 additions & 21 deletions go/vt/vtgate/autocommit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ func TestAutocommitInsertSharded(t *testing.T) {
require.NoError(t, err)

testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{
Sql: "insert into user_extra(user_id, v) values (:_user_id0, 2)",
Sql: "insert into user_extra(user_id, v) values (:_user_id_0, 2)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id_0": sqltypes.Int64BindVariable(1),
},
})
testAsTransactionCount(t, "sbc1", sbc1, 1)
Expand Down Expand Up @@ -279,11 +279,11 @@ func TestAutocommitInsertLookup(t *testing.T) {
testCommitCount(t, "sbclookup", sbclookup, 1)

testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{
Sql: "insert into user(id, v, name) values (:_Id0, 2, :_name0)",
Sql: "insert into user(id, v, name) values (:_Id_0, 2, :_name_0)",
BindVariables: map[string]*querypb.BindVariable{
"_Id0": sqltypes.Int64BindVariable(1),
"_name0": sqltypes.BytesBindVariable([]byte("myname")),
"__seq0": sqltypes.Int64BindVariable(1),
"_Id_0": sqltypes.Int64BindVariable(1),
"_name_0": sqltypes.BytesBindVariable([]byte("myname")),
"__seq0": sqltypes.Int64BindVariable(1),
},
}})
testAsTransactionCount(t, "sbc1", sbc1, 0)
Expand All @@ -298,20 +298,20 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) {
require.NoError(t, err)

testBatchQuery(t, "sbc1", sbc1, &querypb.BoundQuery{
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id0, 2)",
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_0, 2)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id1": sqltypes.Int64BindVariable(3),
"_user_id_0": sqltypes.Int64BindVariable(1),
"_user_id_1": sqltypes.Int64BindVariable(3),
},
})
testAsTransactionCount(t, "sbc1", sbc1, 1)
testCommitCount(t, "sbc1", sbc1, 0)

testBatchQuery(t, "sbc2", sbc2, &querypb.BoundQuery{
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id1, 4)",
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id1": sqltypes.Int64BindVariable(3),
"_user_id_0": sqltypes.Int64BindVariable(1),
"_user_id_1": sqltypes.Int64BindVariable(3),
},
})
testAsTransactionCount(t, "sbc2", sbc2, 1)
Expand All @@ -330,10 +330,10 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) {
testCommitCount(t, "sbc1", sbc1, 0)

testBatchQuery(t, "sbc2", sbc2, &querypb.BoundQuery{
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id1, 4)",
Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id1": sqltypes.Int64BindVariable(3),
"_user_id_0": sqltypes.Int64BindVariable(1),
"_user_id_1": sqltypes.Int64BindVariable(3),
},
})
testAsTransactionCount(t, "sbc2", sbc2, 1)
Expand All @@ -348,20 +348,20 @@ func TestAutocommitInsertMultishard(t *testing.T) {
require.NoError(t, err)

testQueries(t, "sbc1", sbc1, []*querypb.BoundQuery{{
Sql: "insert into user_extra(user_id, v) values (:_user_id0, 2)",
Sql: "insert into user_extra(user_id, v) values (:_user_id_0, 2)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id1": sqltypes.Int64BindVariable(3),
"_user_id_0": sqltypes.Int64BindVariable(1),
"_user_id_1": sqltypes.Int64BindVariable(3),
},
}})
testAsTransactionCount(t, "sbc1", sbc1, 0)
testCommitCount(t, "sbc1", sbc1, 1)

testQueries(t, "sbc2", sbc2, []*querypb.BoundQuery{{
Sql: "insert into user_extra(user_id, v) values (:_user_id1, 4)",
Sql: "insert into user_extra(user_id, v) values (:_user_id_1, 4)",
BindVariables: map[string]*querypb.BindVariable{
"_user_id0": sqltypes.Int64BindVariable(1),
"_user_id1": sqltypes.Int64BindVariable(3),
"_user_id_0": sqltypes.Int64BindVariable(1),
"_user_id_1": sqltypes.Int64BindVariable(3),
},
}})
testAsTransactionCount(t, "sbc2", sbc2, 0)
Expand Down
16 changes: 10 additions & 6 deletions go/vt/vtgate/engine/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (
"strings"
"time"

"vitess.io/vitess/go/vt/sqlparser"

"vitess.io/vitess/go/vt/vtgate/evalengine"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/vindexes"
Expand Down Expand Up @@ -397,7 +398,8 @@ func (ins *Insert) getInsertShardedRoute(vcursor VCursor, bindVars map[string]*q
}
for colIdx, vindexKey := range rowColumnKeys {
col := colVindex.Columns[colIdx]
bindVars[insertVarName(col, rowNum)] = sqltypes.ValueBindVariable(vindexKey)
name := InsertVarName(col, rowNum)
bindVars[name] = sqltypes.ValueBindVariable(vindexKey)
}
}
}
Expand Down Expand Up @@ -514,9 +516,9 @@ func (ins *Insert) processOwned(vcursor VCursor, vindexColumnsKeys [][]sqltypes.

// processUnowned either reverse maps or validates the values for an unowned column.
func (ins *Insert) processUnowned(vcursor VCursor, vindexColumnsKeys [][]sqltypes.Value, colVindex *vindexes.ColumnVindex, ksids [][]byte) error {
var reverseIndexes []int
reverseIndexes := []int{}
var reverseKsids [][]byte
var verifyIndexes []int
verifyIndexes := []int{}
var verifyKeys [][]sqltypes.Value
var verifyKsids [][]byte

Expand Down Expand Up @@ -580,8 +582,10 @@ func (ins *Insert) processUnowned(vcursor VCursor, vindexColumnsKeys [][]sqltype
return nil
}

func insertVarName(col sqlparser.ColIdent, rowNum int) string {
return "_" + col.CompliantName() + strconv.Itoa(rowNum)
//InsertVarName returns a name for the bind var for this column. This method is used by the planner and engine,
//to make sure they both produce the same names
func InsertVarName(col sqlparser.ColIdent, rowNum int) string {
return fmt.Sprintf("_%s_%d", col.CompliantName(), rowNum)
}

func (ins *Insert) description() PrimitiveDescription {
Expand Down
Loading

0 comments on commit a4d5c67

Please sign in to comment.