diff --git a/go/test/endtoend/cluster/cluster_util.go b/go/test/endtoend/cluster/cluster_util.go index 3d442bbb576..1af9504389b 100644 --- a/go/test/endtoend/cluster/cluster_util.go +++ b/go/test/endtoend/cluster/cluster_util.go @@ -127,7 +127,7 @@ func VerifyRowsInTablet(t *testing.T, vttablet *Vttablet, ksName string, expecte } // PanicHandler handles the panic in the testcase. -func PanicHandler(t testing.TB) { +func PanicHandler(t *testing.T) { err := recover() if t == nil { return diff --git a/go/test/endtoend/utils/utils.go b/go/test/endtoend/utils/utils.go index 594ca35b633..8396ea9b613 100644 --- a/go/test/endtoend/utils/utils.go +++ b/go/test/endtoend/utils/utils.go @@ -172,7 +172,7 @@ func ExecCompareMySQL(t *testing.T, vtConn, mysqlConn *mysql.Conn, query string) // ExecAllowError executes the given query without failing the test if it produces // an error. The error is returned to the client, along with the result set. -func ExecAllowError(t testing.TB, conn *mysql.Conn, query string) (*sqltypes.Result, error) { +func ExecAllowError(t *testing.T, conn *mysql.Conn, query string) (*sqltypes.Result, error) { t.Helper() return conn.ExecuteFetch(query, 1000, true) } diff --git a/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go b/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go deleted file mode 100644 index 3fd7edd14de..00000000000 --- a/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go +++ /dev/null @@ -1,80 +0,0 @@ -/* -Copyright 2023 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 dml - -import ( - "fmt" - "math/rand" - "strconv" - "strings" - "testing" - - "vitess.io/vitess/go/test/endtoend/utils" -) - -type testQuery struct { - tableName string - cols []string - intTyp []bool -} - -func (tq *testQuery) getInsertQuery(rows int) string { - var allRows []string - for i := 0; i < rows; i++ { - var row []string - for _, isInt := range tq.intTyp { - if isInt { - row = append(row, strconv.Itoa(i)) - continue - } - row = append(row, "'"+getRandomString(50)+"'") - } - allRows = append(allRows, "("+strings.Join(row, ",")+")") - } - return fmt.Sprintf("insert into %s(%s) values %s", tq.tableName, strings.Join(tq.cols, ","), strings.Join(allRows, ",")) -} - -func getRandomString(size int) string { - var str strings.Builder - - for i := 0; i < size; i++ { - str.WriteByte(byte(rand.Intn(27) + 97)) - } - return str.String() -} - -func BenchmarkShardedTblNoLookup(b *testing.B) { - conn, closer := start(b) - defer closer() - - cols := []string{"id", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12"} - intType := make([]bool, len(cols)) - intType[0] = true - tq := &testQuery{ - tableName: "tbl_no_lkp_vdx", - cols: cols, - intTyp: intType, - } - for _, rows := range []int{1, 10, 100, 500, 1000, 5000, 10000} { - insStmt := tq.getInsertQuery(rows) - b.Run(fmt.Sprintf("16-shards-%d-rows", rows), func(b *testing.B) { - for i := 0; i < b.N; i++ { - _ = utils.Exec(b, conn, insStmt) - } - }) - } -} diff --git a/go/test/endtoend/vtgate/queries/benchmark/main_test.go b/go/test/endtoend/vtgate/queries/benchmark/main_test.go deleted file mode 100644 index 6978d0b9428..00000000000 --- a/go/test/endtoend/vtgate/queries/benchmark/main_test.go +++ /dev/null @@ -1,124 +0,0 @@ -/* -Copyright 2023 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 dml - -import ( - "context" - _ "embed" - "flag" - "os" - "testing" - - "github.com/stretchr/testify/require" - - "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/test/endtoend/cluster" - "vitess.io/vitess/go/test/endtoend/utils" -) - -var ( - clusterInstance *cluster.LocalProcessCluster - vtParams mysql.ConnParams - mysqlParams mysql.ConnParams - sKs = "sks" - uKs = "uks" - cell = "test" - - //go:embed sharded_schema.sql - sSchemaSQL string - - //go:embed vschema.json - sVSchema string -) - -var ( - shards4 = []string{ - "-40", "40-80", "80-c0", "c0-", - } - - shards8 = []string{ - "-20", "20-40", "40-60", "60-80", "80-a0", "a0-c0", "c0-e0", "e0-", - } - - shards16 = []string{ - "-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-a0", "a0-b0", "b0-c0", "c0-d0", "d0-e0", "e0-f0", "f0-", - } - - shards32 = []string{ - "-05", "05-10", "10-15", "15-20", "20-25", "25-30", "30-35", "35-40", "40-45", "45-50", "50-55", "55-60", "60-65", "65-70", "70-75", "75-80", - "80-85", "85-90", "90-95", "95-a0", "a0-a5", "a5-b0", "b0-b5", "b5-c0", "c0-c5", "c5-d0", "d0-d5", "d5-e0", "e0-e5", "e5-f0", "f0-f5", "f5-", - } -) - -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 sharded keyspace - sKeyspace := &cluster.Keyspace{ - Name: sKs, - SchemaSQL: sSchemaSQL, - VSchema: sVSchema, - } - - err = clusterInstance.StartKeyspace(*sKeyspace, shards4, 0, false) - if err != nil { - return 1 - } - - // Start vtgate - err = clusterInstance.StartVtgate() - if err != nil { - return 1 - } - - vtParams = clusterInstance.GetVTParams(sKs) - - return m.Run() - }() - os.Exit(exitCode) -} - -func start(b *testing.B) (*mysql.Conn, func()) { - conn, err := mysql.Connect(context.Background(), &vtParams) - require.NoError(b, err) - - deleteAll := func() { - tables := []string{"tbl_no_lkp_vdx"} - for _, table := range tables { - _, _ = utils.ExecAllowError(b, conn, "delete from "+table) - } - } - - deleteAll() - - return conn, func() { - deleteAll() - conn.Close() - cluster.PanicHandler(b) - } -} diff --git a/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql b/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql deleted file mode 100644 index 850b6ffc15a..00000000000 --- a/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql +++ /dev/null @@ -1,16 +0,0 @@ -create table tbl_no_lkp_vdx -( - id bigint, - c1 varchar(50), - c2 varchar(50), - c3 varchar(50), - c4 varchar(50), - c5 varchar(50), - c6 varchar(50), - c7 varchar(50), - c8 varchar(50), - c9 varchar(50), - c10 varchar(50), - c11 varchar(50), - c12 varchar(50) -) Engine = InnoDB; \ No newline at end of file diff --git a/go/test/endtoend/vtgate/queries/benchmark/vschema.json b/go/test/endtoend/vtgate/queries/benchmark/vschema.json deleted file mode 100644 index 4970e8b7437..00000000000 --- a/go/test/endtoend/vtgate/queries/benchmark/vschema.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "sharded": true, - "vindexes": { - "xxhash": { - "type": "xxhash" - } - }, - "tables": { - "tbl_no_lkp_vdx": { - "column_vindexes": [ - { - "column": "id", - "name": "xxhash" - } - ] - } - } -} \ No newline at end of file diff --git a/go/vt/vtgate/autocommit_test.go b/go/vt/vtgate/autocommit_test.go index fa63695bfbd..0d55bbf2875 100644 --- a/go/vt/vtgate/autocommit_test.go +++ b/go/vt/vtgate/autocommit_test.go @@ -274,6 +274,7 @@ func TestAutocommitInsertLookup(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname"), + "__seq0": sqltypes.Int64BindVariable(1), }, }}) testCommitCount(t, "sbc1", sbc1, 1) @@ -291,6 +292,7 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) { Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_0, 2)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), + "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) testCommitCount(t, "sbc1", sbc1, 0) @@ -298,6 +300,7 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) { assertQueries(t, sbc2, []*querypb.BoundQuery{{ Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)", BindVariables: map[string]*querypb.BindVariable{ + "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) @@ -318,6 +321,7 @@ func TestAutocommitInsertMultishardAutoCommit(t *testing.T) { assertQueries(t, sbc2, []*querypb.BoundQuery{{ Sql: "insert /*vt+ MULTI_SHARD_AUTOCOMMIT=1 */ into user_extra(user_id, v) values (:_user_id_1, 4)", BindVariables: map[string]*querypb.BindVariable{ + "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) @@ -335,6 +339,7 @@ func TestAutocommitInsertMultishard(t *testing.T) { Sql: "insert into user_extra(user_id, v) values (:_user_id_0, 2)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), + "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) testCommitCount(t, "sbc1", sbc1, 1) @@ -342,6 +347,7 @@ func TestAutocommitInsertMultishard(t *testing.T) { assertQueries(t, sbc2, []*querypb.BoundQuery{{ Sql: "insert into user_extra(user_id, v) values (:_user_id_1, 4)", BindVariables: map[string]*querypb.BindVariable{ + "_user_id_0": sqltypes.Int64BindVariable(1), "_user_id_1": sqltypes.Int64BindVariable(3), }, }}) diff --git a/go/vt/vtgate/engine/cached_size.go b/go/vt/vtgate/engine/cached_size.go index 10d862ea3df..6aef4e05e2b 100644 --- a/go/vt/vtgate/engine/cached_size.go +++ b/go/vt/vtgate/engine/cached_size.go @@ -417,18 +417,11 @@ func (cached *Insert) CachedSize(alloc bool) int64 { size += cached.Generate.CachedSize(true) // field Prefix string size += hack.RuntimeAllocSize(int64(len(cached.Prefix))) - // field Mid vitess.io/vitess/go/vt/sqlparser.Values + // field Mid []string { - size += hack.RuntimeAllocSize(int64(cap(cached.Mid)) * int64(24)) + size += hack.RuntimeAllocSize(int64(cap(cached.Mid)) * int64(16)) for _, elem := range cached.Mid { - { - size += hack.RuntimeAllocSize(int64(cap(elem)) * int64(16)) - for _, elem := range elem { - if cc, ok := elem.(cachedObject); ok { - size += cc.CachedSize(true) - } - } - } + size += hack.RuntimeAllocSize(int64(len(elem))) } } // field Suffix string diff --git a/go/vt/vtgate/engine/insert.go b/go/vt/vtgate/engine/insert.go index 212bcd6620c..394ccb8ecce 100644 --- a/go/vt/vtgate/engine/insert.go +++ b/go/vt/vtgate/engine/insert.go @@ -25,7 +25,6 @@ import ( "sync" "time" - "vitess.io/vitess/go/slice" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/key" querypb "vitess.io/vitess/go/vt/proto/query" @@ -76,7 +75,7 @@ type ( // Prefix, Mid and Suffix are for sharded insert plans. Prefix string - Mid sqlparser.Values + Mid []string Suffix string // Option to override the standard behavior and allow a multi-shard insert @@ -133,7 +132,7 @@ func NewInsert( vindexValues [][][]evalengine.Expr, table *vindexes.Table, prefix string, - mid sqlparser.Values, + mid []string, suffix string, ) *Insert { ins := &Insert{ @@ -759,23 +758,17 @@ func (ins *Insert) getInsertShardedRoute( queries := make([]*querypb.BoundQuery, len(rss)) for i := range rss { - shardBindVars := map[string]*querypb.BindVariable{} var mids []string for _, indexValue := range indexesPerRss[i] { index, _ := strconv.ParseInt(string(indexValue.Value), 0, 64) if keyspaceIDs[index] != nil { - mids = append(mids, sqlparser.String(ins.Mid[index])) - for _, expr := range ins.Mid[index] { - if arg, ok := expr.(*sqlparser.Argument); ok { - shardBindVars[arg.Name] = bindVars[arg.Name] - } - } + mids = append(mids, ins.Mid[index]) } } rewritten := ins.Prefix + strings.Join(mids, ",") + ins.Suffix queries[i] = &querypb.BoundQuery{ Sql: rewritten, - BindVariables: shardBindVars, + BindVariables: bindVars, } } @@ -992,10 +985,7 @@ func (ins *Insert) description() PrimitiveDescription { other["VindexOffsetFromSelect"] = valuesOffsets } if len(ins.Mid) > 0 { - mids := slice.Map(ins.Mid, func(from sqlparser.ValTuple) string { - return sqlparser.String(from) - }) - shardQuery := fmt.Sprintf("%s%s%s", ins.Prefix, strings.Join(mids, ", "), ins.Suffix) + shardQuery := fmt.Sprintf("%s%s%s", ins.Prefix, strings.Join(ins.Mid, ", "), ins.Suffix) if shardQuery != ins.Query { other["ShardedQuery"] = shardQuery } diff --git a/go/vt/vtgate/engine/insert_test.go b/go/vt/vtgate/engine/insert_test.go index 014654f37d6..b651efe2b03 100644 --- a/go/vt/vtgate/engine/insert_test.go +++ b/go/vt/vtgate/engine/insert_test.go @@ -21,15 +21,17 @@ import ( "errors" "testing" + topodatapb "vitess.io/vitess/go/vt/proto/topodata" + + "vitess.io/vitess/go/vt/vtgate/evalengine" + "github.com/stretchr/testify/require" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/vt/vtgate/vindexes" + querypb "vitess.io/vitess/go/vt/proto/query" - topodatapb "vitess.io/vitess/go/vt/proto/topodata" vschemapb "vitess.io/vitess/go/vt/proto/vschema" - "vitess.io/vitess/go/vt/sqlparser" - "vitess.io/vitess/go/vt/vtgate/evalengine" - "vitess.io/vitess/go/vt/vtgate/vindexes" ) func TestInsertUnsharded(t *testing.T) { @@ -210,9 +212,7 @@ func TestInsertShardedSimple(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}}, - }, + []string{" mid1"}, " suffix", ) vc := newDMLTestVCursor("-20", "20-") @@ -227,7 +227,7 @@ func TestInsertShardedSimple(t *testing.T) { `ResolveDestinations sharded [value:"0"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6)`, // Row 2 will go to -20, rows 1 & 3 will go to 20- `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */) suffix {_id_0: type:INT64 value:"1"} ` + + `sharded.20-: prefix mid1 suffix {_id_0: type:INT64 value:"1"} ` + `true true`, }) @@ -247,11 +247,7 @@ func TestInsertShardedSimple(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) vc = newDMLTestVCursor("-20", "20-") @@ -266,8 +262,8 @@ func TestInsertShardedSimple(t *testing.T) { `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, // Row 2 will go to -20, rows 1 & 3 will go to 20- `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */),(:_id_2 /* INT64 */) suffix {_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + - `sharded.-20: prefix(:_id_1 /* INT64 */) suffix {_id_1: type:INT64 value:"2"} ` + + `sharded.20-: prefix mid1, mid3 suffix {_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix {_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + `true false`, }) @@ -288,11 +284,7 @@ func TestInsertShardedSimple(t *testing.T) { ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) ins.MultiShardAutocommit = true @@ -309,8 +301,8 @@ func TestInsertShardedSimple(t *testing.T) { `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, // Row 2 will go to -20, rows 1 & 3 will go to 20- `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */),(:_id_2 /* INT64 */) suffix {_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + - `sharded.-20: prefix(:_id_1 /* INT64 */) suffix {_id_1: type:INT64 value:"2"} ` + + `sharded.20-: prefix mid1, mid3 suffix {_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix {_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + `true true`, }) } @@ -357,9 +349,7 @@ func TestInsertShardedFail(t *testing.T) { ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -409,11 +399,7 @@ func TestInsertShardedGenerate(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "__seq0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "__seq1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "__seq2", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -426,7 +412,7 @@ func TestInsertShardedGenerate(t *testing.T) { Values: evalengine.NewTupleExpr( evalengine.NewLiteralInt(1), evalengine.NullExpr, - evalengine.NewLiteralInt(3), + evalengine.NewLiteralInt(2), ), } @@ -454,10 +440,12 @@ func TestInsertShardedGenerate(t *testing.T) { `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, // Row 2 will go to -20, rows 1 & 3 will go to 20- `ExecuteMultiShard ` + - `sharded.20-: prefix(:__seq0 /* INT64 */),(:__seq2 /* INT64 */) suffix ` + - `{__seq0: type:INT64 value:"1" __seq2: type:INT64 value:"3"} ` + - `sharded.-20: prefix(:__seq1 /* INT64 */) suffix ` + - `{__seq1: type:INT64 value:"2"} ` + + `sharded.20-: prefix mid1, mid3 suffix ` + + `{__seq0: type:INT64 value:"1" __seq1: type:INT64 value:"2" __seq2: type:INT64 value:"2" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix ` + + `{__seq0: type:INT64 value:"1" __seq1: type:INT64 value:"2" __seq2: type:INT64 value:"2" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + `true false`, }) @@ -547,11 +535,7 @@ func TestInsertShardedOwned(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_2", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -573,15 +557,16 @@ func TestInsertShardedOwned(t *testing.T) { // Based on shardForKsid, values returned will be 20-, -20, 20-. `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */, :_c1_0 /* INT64 */, :_c2_0 /* INT64 */, :_c3_0 /* INT64 */)` + - `,(:_id_2 /* INT64 */, :_c1_2 /* INT64 */, :_c2_2 /* INT64 */, :_c3_2 /* INT64 */) suffix ` + - `{_c1_0: type:INT64 value:"4" _c1_2: type:INT64 value:"6" ` + - `_c2_0: type:INT64 value:"7" _c2_2: type:INT64 value:"9" ` + - `_c3_0: type:INT64 value:"10" _c3_2: type:INT64 value:"12" ` + - `_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + - `sharded.-20: prefix(:_id_1 /* INT64 */, :_c1_1 /* INT64 */, :_c2_1 /* INT64 */, :_c3_1 /* INT64 */) suffix ` + - `{_c1_1: type:INT64 value:"5" _c2_1: type:INT64 value:"8" _c3_1: type:INT64 value:"11" ` + - `_id_1: type:INT64 value:"2"} ` + + `sharded.20-: prefix mid1, mid3 suffix ` + + `{_c1_0: type:INT64 value:"4" _c1_1: type:INT64 value:"5" _c1_2: type:INT64 value:"6" ` + + `_c2_0: type:INT64 value:"7" _c2_1: type:INT64 value:"8" _c2_2: type:INT64 value:"9" ` + + `_c3_0: type:INT64 value:"10" _c3_1: type:INT64 value:"11" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix ` + + `{_c1_0: type:INT64 value:"4" _c1_1: type:INT64 value:"5" _c1_2: type:INT64 value:"6" ` + + `_c2_0: type:INT64 value:"7" _c2_1: type:INT64 value:"8" _c2_2: type:INT64 value:"9" ` + + `_c3_0: type:INT64 value:"10" _c3_1: type:INT64 value:"11" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + `true false`, }) } @@ -641,9 +626,7 @@ func TestInsertShardedOwnedWithNull(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Null}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -656,7 +639,7 @@ func TestInsertShardedOwnedWithNull(t *testing.T) { } vc.ExpectLog(t, []string{ `ResolveDestinations sharded [value:"0"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6)`, - `ExecuteMultiShard sharded.20-: prefix(:_id_0 /* INT64 */, :_c3_0 /* NULL_TYPE */) suffix ` + + `ExecuteMultiShard sharded.20-: prefix mid1 suffix ` + `{_c3_0: _id_0: type:INT64 value:"1"} true true`, }) } @@ -726,10 +709,7 @@ func TestInsertShardedGeo(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_region_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_region_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2"}, " suffix", ) @@ -745,10 +725,12 @@ func TestInsertShardedGeo(t *testing.T) { `id_0: type:INT64 value:"1" id_1: type:INT64 value:"1" ` + `keyspace_id_0: type:VARBINARY value:"\x01\x16k@\xb4J\xbaK\xd6" keyspace_id_1: type:VARBINARY value:"\xff\x16k@\xb4J\xbaK\xd6" true`, `ResolveDestinations sharded [value:"0" value:"1"] Destinations:DestinationKeyspaceID(01166b40b44aba4bd6),DestinationKeyspaceID(ff166b40b44aba4bd6)`, - `ExecuteMultiShard sharded.20-: prefix(:_region_0 /* INT64 */, :_id_0 /* INT64 */) suffix ` + - `{_id_0: type:INT64 value:"1" _region_0: type:INT64 value:"1"} ` + - `sharded.-20: prefix(:_region_1 /* INT64 */, :_id_1 /* INT64 */) suffix ` + - `{_id_1: type:INT64 value:"1" _region_1: type:INT64 value:"255"} ` + + `ExecuteMultiShard sharded.20-: prefix mid1 suffix ` + + `{_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"1" ` + + `_region_0: type:INT64 value:"1" _region_1: type:INT64 value:"255"} ` + + `sharded.-20: prefix mid2 suffix ` + + `{_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"1" ` + + `_region_0: type:INT64 value:"1" _region_1: type:INT64 value:"255"} ` + `true false`, }) } @@ -848,12 +830,7 @@ func TestInsertShardedIgnoreOwned(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_2", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_3", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_3", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_3", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_3", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3", " mid4"}, " suffix", ) @@ -917,10 +894,16 @@ func TestInsertShardedIgnoreOwned(t *testing.T) { `ResolveDestinations sharded [value:"0" value:"3"] Destinations:DestinationKeyspaceID(00),DestinationKeyspaceID(00)`, // Bind vars for rows 2 & 3 may be missing because they were not sent. `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */, :_c1_0 /* INT64 */, :_c2_0 /* INT64 */, :_c3_0 /* INT64 */) suffix ` + - `{_c1_0: type:INT64 value:"5" _c2_0: type:INT64 value:"9" _c3_0: type:INT64 value:"13" _id_0: type:INT64 value:"1"} ` + - `sharded.-20: prefix(:_id_3 /* INT64 */, :_c1_3 /* INT64 */, :_c2_3 /* INT64 */, :_c3_3 /* INT64 */) suffix ` + - `{_c1_3: type:INT64 value:"8" _c2_3: type:INT64 value:"12" _c3_3: type:INT64 value:"16" _id_3: type:INT64 value:"4"} ` + + `sharded.20-: prefix mid1 suffix ` + + `{_c1_0: type:INT64 value:"5" _c1_3: type:INT64 value:"8" ` + + `_c2_0: type:INT64 value:"9" _c2_3: type:INT64 value:"12" ` + + `_c3_0: type:INT64 value:"13" _c3_3: type:INT64 value:"16" ` + + `_id_0: type:INT64 value:"1" _id_3: type:INT64 value:"4"} ` + + `sharded.-20: prefix mid4 suffix ` + + `{_c1_0: type:INT64 value:"5" _c1_3: type:INT64 value:"8" ` + + `_c2_0: type:INT64 value:"9" _c2_3: type:INT64 value:"12" ` + + `_c3_0: type:INT64 value:"13" _c3_3: type:INT64 value:"16" ` + + `_id_0: type:INT64 value:"1" _id_3: type:INT64 value:"4"} ` + `true false`, }) } @@ -981,9 +964,7 @@ func TestInsertShardedIgnoreOwnedWithNull(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3", " mid4"}, " suffix", ) @@ -1009,7 +990,7 @@ func TestInsertShardedIgnoreOwnedWithNull(t *testing.T) { vc.ExpectLog(t, []string{ `Execute select from from lkp1 where from = :from and toc = :toc from: toc: type:VARBINARY value:"\x16k@\xb4J\xbaK\xd6" false`, `ResolveDestinations sharded [value:"0"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6)`, - `ExecuteMultiShard sharded.-20: prefix(:_id_0 /* INT64 */, :_c3_0 /* INT64 */) suffix ` + + `ExecuteMultiShard sharded.-20: prefix mid1 suffix ` + `{_c3_0: _id_0: type:INT64 value:"1"} true true`, }) } @@ -1097,11 +1078,7 @@ func TestInsertShardedUnownedVerify(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_1", Type: sqltypes.Int64}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c2_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_2", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -1140,15 +1117,16 @@ func TestInsertShardedUnownedVerify(t *testing.T) { // Based on shardForKsid, values returned will be 20-, -20, 20-. `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */, :_c1_0 /* INT64 */, :_c2_0 /* INT64 */, :_c3_0 /* INT64 */),` + - `(:_id_2 /* INT64 */, :_c1_2 /* INT64 */, :_c2_2 /* INT64 */, :_c3_2 /* INT64 */) suffix ` + - `{_c1_0: type:INT64 value:"4" _c1_2: type:INT64 value:"6" ` + - `_c2_0: type:INT64 value:"7" _c2_2: type:INT64 value:"9" ` + - `_c3_0: type:INT64 value:"10" _c3_2: type:INT64 value:"12" ` + - `_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + - `sharded.-20: prefix(:_id_1 /* INT64 */, :_c1_1 /* INT64 */, :_c2_1 /* INT64 */, :_c3_1 /* INT64 */) suffix ` + - `{_c1_1: type:INT64 value:"5" _c2_1: type:INT64 value:"8" ` + - `_c3_1: type:INT64 value:"11" _id_1: type:INT64 value:"2"} ` + + `sharded.20-: prefix mid1, mid3 suffix ` + + `{_c1_0: type:INT64 value:"4" _c1_1: type:INT64 value:"5" _c1_2: type:INT64 value:"6" ` + + `_c2_0: type:INT64 value:"7" _c2_1: type:INT64 value:"8" _c2_2: type:INT64 value:"9" ` + + `_c3_0: type:INT64 value:"10" _c3_1: type:INT64 value:"11" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix ` + + `{_c1_0: type:INT64 value:"4" _c1_1: type:INT64 value:"5" _c1_2: type:INT64 value:"6" ` + + `_c2_0: type:INT64 value:"7" _c2_1: type:INT64 value:"8" _c2_2: type:INT64 value:"9" ` + + `_c3_0: type:INT64 value:"10" _c3_1: type:INT64 value:"11" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + `true false`, }) } @@ -1211,11 +1189,7 @@ func TestInsertShardedIgnoreUnownedVerify(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "v1", Type: sqltypes.VarChar}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "v2", Type: sqltypes.VarChar}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "v3", Type: sqltypes.VarChar}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -1236,9 +1210,7 @@ func TestInsertShardedIgnoreUnownedVerify(t *testing.T) { {}, nonemptyResult, } - _, err := ins.TryExecute(context.Background(), vc, map[string]*querypb.BindVariable{ - "v1": sqltypes.StringBindVariable("a"), "v2": sqltypes.StringBindVariable("b"), "v3": sqltypes.StringBindVariable("c"), - }, false) + _, err := ins.TryExecute(context.Background(), vc, map[string]*querypb.BindVariable{}, false) if err != nil { t.Fatal(err) } @@ -1251,10 +1223,12 @@ func TestInsertShardedIgnoreUnownedVerify(t *testing.T) { // Based on shardForKsid, values returned will be 20-, -20. `ResolveDestinations sharded [value:"0" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(4eb190c9a2fa169c)`, `ExecuteMultiShard ` + - `sharded.20-: prefix(:_id_0 /* INT64 */, :_c3_0 /* INT64 */, :v1 /* VARCHAR */) suffix ` + - `{_c3_0: type:INT64 value:"10" _id_0: type:INT64 value:"1" v1: type:VARCHAR value:"a"} ` + - `sharded.-20: prefix(:_id_2 /* INT64 */, :_c3_2 /* INT64 */, :v3 /* VARCHAR */) suffix ` + - `{_c3_2: type:INT64 value:"12" _id_2: type:INT64 value:"3" v3: type:VARCHAR value:"c"} ` + + `sharded.20-: prefix mid1 suffix ` + + `{_c3_0: type:INT64 value:"10" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid3 suffix ` + + `{_c3_0: type:INT64 value:"10" _c3_2: type:INT64 value:"12" ` + + `_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + `true false`, }) } @@ -1313,9 +1287,7 @@ func TestInsertShardedIgnoreUnownedVerifyFail(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Int64}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -1408,11 +1380,7 @@ func TestInsertShardedUnownedReverseMap(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_0", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c2_0", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Null}}, - {&sqlparser.Argument{Name: "_id_1", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_1", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c2_1", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c3_1", Type: sqltypes.Null}}, - {&sqlparser.Argument{Name: "_id_2", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c1_2", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c2_2", Type: sqltypes.Null}, &sqlparser.Argument{Name: "_c3_2", Type: sqltypes.Null}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) @@ -1437,16 +1405,18 @@ func TestInsertShardedUnownedReverseMap(t *testing.T) { } vc.ExpectLog(t, []string{ `ResolveDestinations sharded [value:"0" value:"1" value:"2"] Destinations:DestinationKeyspaceID(166b40b44aba4bd6),DestinationKeyspaceID(06e7ea22ce92708f),DestinationKeyspaceID(4eb190c9a2fa169c)`, - `ExecuteMultiShard sharded.20-: ` + - `prefix(:_id_0 /* INT64 */, :_c1_0 /* NULL_TYPE */, :_c2_0 /* NULL_TYPE */, :_c3_0 /* NULL_TYPE */),` + - `(:_id_2 /* INT64 */, :_c1_2 /* NULL_TYPE */, :_c2_2 /* NULL_TYPE */, :_c3_2 /* NULL_TYPE */) suffix ` + - `{_c1_0: type:UINT64 value:"1" _c1_2: type:UINT64 value:"3" ` + - `_c2_0: _c2_2: ` + - `_c3_0: type:UINT64 value:"1" _c3_2: type:UINT64 value:"3" ` + - `_id_0: type:INT64 value:"1" _id_2: type:INT64 value:"3"} ` + - `sharded.-20: ` + - `prefix(:_id_1 /* INT64 */, :_c1_1 /* NULL_TYPE */, :_c2_1 /* NULL_TYPE */, :_c3_1 /* NULL_TYPE */) suffix ` + - `{_c1_1: type:UINT64 value:"2" _c2_1: _c3_1: type:UINT64 value:"2" _id_1: type:INT64 value:"2"} true false`, + `ExecuteMultiShard ` + + `sharded.20-: prefix mid1, mid3 suffix ` + + `{_c1_0: type:UINT64 value:"1" _c1_1: type:UINT64 value:"2" _c1_2: type:UINT64 value:"3" ` + + `_c2_0: _c2_1: _c2_2: ` + + `_c3_0: type:UINT64 value:"1" _c3_1: type:UINT64 value:"2" _c3_2: type:UINT64 value:"3" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `sharded.-20: prefix mid2 suffix ` + + `{_c1_0: type:UINT64 value:"1" _c1_1: type:UINT64 value:"2" _c1_2: type:UINT64 value:"3" ` + + `_c2_0: _c2_1: _c2_2: ` + + `_c3_0: type:UINT64 value:"1" _c3_1: type:UINT64 value:"2" _c3_2: type:UINT64 value:"3" ` + + `_id_0: type:INT64 value:"1" _id_1: type:INT64 value:"2" _id_2: type:INT64 value:"3"} ` + + `true false`, }) } @@ -1504,9 +1474,7 @@ func TestInsertShardedUnownedReverseMapSuccess(t *testing.T) { }}, ks.Tables["t1"], "prefix", - sqlparser.Values{ - {&sqlparser.Argument{Name: "_id_0", Type: sqltypes.Int64}, &sqlparser.Argument{Name: "_c3_0", Type: sqltypes.Null}}, - }, + []string{" mid1", " mid2", " mid3"}, " suffix", ) diff --git a/go/vt/vtgate/executor_dml_test.go b/go/vt/vtgate/executor_dml_test.go index c53d24eb6fb..59fbe314346 100644 --- a/go/vt/vtgate/executor_dml_test.go +++ b/go/vt/vtgate/executor_dml_test.go @@ -1273,6 +1273,7 @@ func TestInsertSharded(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname"), + "__seq0": sqltypes.Int64BindVariable(1), }, }} assertQueries(t, sbc1, wantQueries) @@ -1298,6 +1299,7 @@ func TestInsertSharded(t *testing.T) { Sql: "insert into `user`(id, v, `name`) values (:_Id_0, 2, :_name_0)", BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(3), + "__seq0": sqltypes.Int64BindVariable(3), "_name_0": sqltypes.StringBindVariable("myname2"), }, }} @@ -1343,7 +1345,10 @@ func TestInsertSharded(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.BytesBindVariable([]byte("myname")), + "__seq0": sqltypes.Int64BindVariable(1), + "vtg1": sqltypes.Int64BindVariable(1), "vtg2": sqltypes.Int64BindVariable(2), + "vtg3": sqltypes.StringBindVariable("myname"), }, }} assertQueries(t, sbc1, wantQueries) @@ -1443,6 +1448,7 @@ func TestInsertShardedAutocommitLookup(t *testing.T) { "_Id_0": sqltypes.Int64BindVariable(1), "_music_0": sqltypes.StringBindVariable("star"), "_name_0": sqltypes.StringBindVariable("myname"), + "__seq0": sqltypes.Int64BindVariable(1), }, }} assertQueries(t, sbc1, wantQueries) @@ -1504,18 +1510,27 @@ func TestInsertShardedIgnore(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_pv_0": sqltypes.Int64BindVariable(1), "_pv_4": sqltypes.Int64BindVariable(5), + "_pv_5": sqltypes.Int64BindVariable(6), "_owned_0": sqltypes.Int64BindVariable(1), "_owned_4": sqltypes.Int64BindVariable(5), + "_owned_5": sqltypes.Int64BindVariable(6), "_verify_0": sqltypes.Int64BindVariable(1), "_verify_4": sqltypes.Int64BindVariable(1), + "_verify_5": sqltypes.Int64BindVariable(3), }, }} assertQueries(t, sbc1, wantQueries) wantQueries = []*querypb.BoundQuery{{ Sql: "insert ignore into insert_ignore_test(pv, owned, verify) values (:_pv_5, :_owned_5, :_verify_5)", BindVariables: map[string]*querypb.BindVariable{ + "_pv_0": sqltypes.Int64BindVariable(1), + "_pv_4": sqltypes.Int64BindVariable(5), "_pv_5": sqltypes.Int64BindVariable(6), + "_owned_0": sqltypes.Int64BindVariable(1), + "_owned_4": sqltypes.Int64BindVariable(5), "_owned_5": sqltypes.Int64BindVariable(6), + "_verify_0": sqltypes.Int64BindVariable(1), + "_verify_4": sqltypes.Int64BindVariable(1), "_verify_5": sqltypes.Int64BindVariable(3), }, }} @@ -1685,6 +1700,7 @@ func TestInsertComments(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname"), + "__seq0": sqltypes.Int64BindVariable(1), }, }} assertQueries(t, sbc1, wantQueries) @@ -1718,6 +1734,7 @@ func TestInsertGeneratorSharded(t *testing.T) { Sql: "insert into `user`(v, `name`, id) values (2, :_name_0, :_Id_0)", BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), + "__seq0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname"), }, }} @@ -1839,6 +1856,7 @@ func TestInsertLookupOwned(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(2), "_id_0": sqltypes.Int64BindVariable(3), + "__seq0": sqltypes.Int64BindVariable(3), }, }} assertQueries(t, sbc, wantQueries) @@ -1872,6 +1890,7 @@ func TestInsertLookupOwnedGenerator(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(2), "_id_0": sqltypes.Int64BindVariable(4), + "__seq0": sqltypes.Int64BindVariable(4), }, }} assertQueries(t, sbc, wantQueries) @@ -2001,6 +2020,7 @@ func TestInsertPartialFail2(t *testing.T) { Sql: "insert into `user`(id, v, `name`) values (:_Id_0, 2, :_name_0)", BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), + "__seq0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname"), }, }, { @@ -2023,14 +2043,22 @@ func TestMultiInsertSharded(t *testing.T) { BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname1"), + "__seq0": sqltypes.Int64BindVariable(1), + "_Id_1": sqltypes.Int64BindVariable(3), + "_name_1": sqltypes.StringBindVariable("myname3"), + "__seq1": sqltypes.Int64BindVariable(3), }, }} wantQueries2 := []*querypb.BoundQuery{{ Sql: "insert into `user`(id, v, `name`) values (:_Id_1, 3, :_name_1)", BindVariables: map[string]*querypb.BindVariable{ + "_Id_0": sqltypes.Int64BindVariable(1), + "_name_0": sqltypes.StringBindVariable("myname1"), + "__seq0": sqltypes.Int64BindVariable(1), "_Id_1": sqltypes.Int64BindVariable(3), "_name_1": sqltypes.StringBindVariable("myname3"), + "__seq1": sqltypes.Int64BindVariable(3), }, }} assertQueries(t, sbc1, wantQueries1) @@ -2056,8 +2084,10 @@ func TestMultiInsertSharded(t *testing.T) { Sql: "insert into `user`(id, v, `name`) values (:_Id_0, 1, :_name_0),(:_Id_1, 2, :_name_1)", BindVariables: map[string]*querypb.BindVariable{ "_Id_0": sqltypes.Int64BindVariable(1), + "__seq0": sqltypes.Int64BindVariable(1), "_name_0": sqltypes.StringBindVariable("myname1"), "_Id_1": sqltypes.Int64BindVariable(2), + "__seq1": sqltypes.Int64BindVariable(2), "_name_1": sqltypes.StringBindVariable("myname2"), }, }} @@ -2087,18 +2117,12 @@ func TestMultiInsertSharded(t *testing.T) { "_id_0": sqltypes.Int64BindVariable(2), "_name_0": sqltypes.StringBindVariable("myname"), "_lastname_0": sqltypes.StringBindVariable("mylastname"), - }, - }} - assertQueries(t, sbc1, wantQueries) - wantQueries = []*querypb.BoundQuery{{ - Sql: "insert into user2(id, `name`, lastname) values (:_id_1, :_name_1, :_lastname_1)", - BindVariables: map[string]*querypb.BindVariable{ "_id_1": sqltypes.Int64BindVariable(3), "_name_1": sqltypes.StringBindVariable("myname2"), "_lastname_1": sqltypes.StringBindVariable("mylastname2"), }, }} - assertQueries(t, sbc2, wantQueries) + assertQueries(t, sbc1, wantQueries) wantQueries = []*querypb.BoundQuery{{ Sql: "insert into name_lastname_keyspace_id_map(`name`, lastname, keyspace_id) values (:name_0, :lastname_0, :keyspace_id_0), (:name_1, :lastname_1, :keyspace_id_1)", BindVariables: map[string]*querypb.BindVariable{ @@ -2131,9 +2155,12 @@ func TestMultiInsertGenerator(t *testing.T) { wantQueries := []*querypb.BoundQuery{{ Sql: "insert into music(user_id, `name`, id) values (:_user_id_0, 'myname1', :_id_0),(:_user_id_1, 'myname2', :_id_1)", BindVariables: map[string]*querypb.BindVariable{ + "u": sqltypes.Int64BindVariable(2), "_id_0": sqltypes.Int64BindVariable(1), + "__seq0": sqltypes.Int64BindVariable(1), "_user_id_0": sqltypes.Int64BindVariable(2), "_id_1": sqltypes.Int64BindVariable(2), + "__seq1": sqltypes.Int64BindVariable(2), "_user_id_1": sqltypes.Int64BindVariable(2), }, }} @@ -2176,11 +2203,15 @@ func TestMultiInsertGeneratorSparse(t *testing.T) { wantQueries := []*querypb.BoundQuery{{ Sql: "insert into music(id, user_id, `name`) values (:_id_0, :_user_id_0, 'myname1'),(:_id_1, :_user_id_1, 'myname2'),(:_id_2, :_user_id_2, 'myname3')", BindVariables: map[string]*querypb.BindVariable{ + "u": sqltypes.Int64BindVariable(2), "_id_0": sqltypes.Int64BindVariable(1), + "__seq0": sqltypes.Int64BindVariable(1), "_user_id_0": sqltypes.Int64BindVariable(2), "_id_1": sqltypes.Int64BindVariable(2), + "__seq1": sqltypes.Int64BindVariable(2), "_user_id_1": sqltypes.Int64BindVariable(2), "_id_2": sqltypes.Int64BindVariable(2), + "__seq2": sqltypes.Int64BindVariable(2), "_user_id_2": sqltypes.Int64BindVariable(2), }, }} @@ -2704,8 +2735,12 @@ func TestPartialVindexInsertQueryFailure(t *testing.T) { }, { Sql: "insert into t1_lkp_idx(unq_col, keyspace_id) values (:_unq_col_0, :keyspace_id_0)", BindVariables: map[string]*querypb.BindVariable{ + "unq_col_0": sqltypes.Int64BindVariable(1), "keyspace_id_0": sqltypes.BytesBindVariable([]byte("\x16k@\xb4J\xbaK\xd6")), + "unq_col_1": sqltypes.Int64BindVariable(3), + "keyspace_id_1": sqltypes.BytesBindVariable([]byte("\x06\xe7\xea\"Βp\x8f")), "_unq_col_0": sqltypes.Int64BindVariable(1), + "_unq_col_1": sqltypes.Int64BindVariable(3), }, }, { Sql: "rollback to x", @@ -2722,10 +2757,6 @@ func TestPartialVindexInsertQueryFailure(t *testing.T) { // only parameter in expected query changes wantQ[1].Sql = "insert into t1_lkp_idx(unq_col, keyspace_id) values (:_unq_col_1, :keyspace_id_1)" - wantQ[1].BindVariables = map[string]*querypb.BindVariable{ - "keyspace_id_1": sqltypes.BytesBindVariable([]byte("\x06\xe7\xea\"Βp\x8f")), - "_unq_col_1": sqltypes.Int64BindVariable(3), - } assertQueriesWithSavepoint(t, sbc2, wantQ) testQueryLog(t, executor, logChan, "TestExecute", "BEGIN", "begin", 0) @@ -2749,8 +2780,12 @@ func TestPartialVindexInsertQueryFailureAutoCommit(t *testing.T) { wantQ := []*querypb.BoundQuery{{ Sql: "insert into t1_lkp_idx(unq_col, keyspace_id) values (:_unq_col_0, :keyspace_id_0)", BindVariables: map[string]*querypb.BindVariable{ + "unq_col_0": sqltypes.Int64BindVariable(1), "keyspace_id_0": sqltypes.BytesBindVariable([]byte("\x16k@\xb4J\xbaK\xd6")), + "unq_col_1": sqltypes.Int64BindVariable(3), + "keyspace_id_1": sqltypes.BytesBindVariable([]byte("\x06\xe7\xea\"Βp\x8f")), "_unq_col_0": sqltypes.Int64BindVariable(1), + "_unq_col_1": sqltypes.Int64BindVariable(3), }, }} @@ -2764,10 +2799,6 @@ func TestPartialVindexInsertQueryFailureAutoCommit(t *testing.T) { // only parameter in expected query changes wantQ[0].Sql = "insert into t1_lkp_idx(unq_col, keyspace_id) values (:_unq_col_1, :keyspace_id_1)" - wantQ[0].BindVariables = map[string]*querypb.BindVariable{ - "keyspace_id_1": sqltypes.BytesBindVariable([]byte("\x06\xe7\xea\"Βp\x8f")), - "_unq_col_1": sqltypes.Int64BindVariable(3), - } assertQueriesWithSavepoint(t, sbc2, wantQ) testQueryLog(t, executor, logChan, "VindexCreate", "INSERT", "insert into t1_lkp_idx(unq_col, keyspace_id) values (:unq_col_0, :keyspace_id_0), (:unq_col_1, :keyspace_id_1)", 2) @@ -2796,6 +2827,7 @@ func TestMultiInternalSavepoint(t *testing.T) { Sql: "insert into user_extra(user_id) values (:_user_id_0)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), + "_user_id_1": sqltypes.Int64BindVariable(4), }, }} assertQueriesWithSavepoint(t, sbc1, wantQ) @@ -2814,6 +2846,7 @@ func TestMultiInternalSavepoint(t *testing.T) { Sql: "insert into user_extra(user_id) values (:_user_id_0)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(3), + "_user_id_1": sqltypes.Int64BindVariable(6), }, }} assertQueriesWithSavepoint(t, sbc2, wantQ) diff --git a/go/vt/vtgate/executor_vschema_ddl_test.go b/go/vt/vtgate/executor_vschema_ddl_test.go index 1c2813a33c4..4218dcf93e0 100644 --- a/go/vt/vtgate/executor_vschema_ddl_test.go +++ b/go/vt/vtgate/executor_vschema_ddl_test.go @@ -17,9 +17,8 @@ limitations under the License. package vtgate import ( - "context" "reflect" - "slices" + "sort" "testing" "time" @@ -79,19 +78,13 @@ func waitForVschemaTables(t *testing.T, ks string, tables []string, executor *Ex // Wait up to 100ms until the vindex manager gets notified of the update for i := 0; i < 10; i++ { vschema := executor.vm.GetCurrentSrvVschema() - var gotTables []string + gotTables := []string{} for t := range vschema.Keyspaces[ks].Tables { gotTables = append(gotTables, t) } - - foundAll := true - for _, expTbl := range tables { - if !slices.Contains(gotTables, expTbl) { - foundAll = false - break - } - } - if foundAll { + sort.Strings(tables) + sort.Strings(gotTables) + if reflect.DeepEqual(tables, gotTables) { return vschema } time.Sleep(10 * time.Millisecond) @@ -406,32 +399,30 @@ func TestExecutorDropSequenceDDL(t *testing.T) { t.Fatalf("test_seq should not exist in original vschema") } + var vschemaTables []string + for t := range vschema.Keyspaces[ks].Tables { + vschemaTables = append(vschemaTables, t) + } + session := NewSafeSession(&vtgatepb.Session{TargetString: ks}) // add test sequence stmt := "alter vschema add sequence test_seq" _, err := executor.Execute(ctx, nil, "TestExecute", session, stmt, nil) require.NoError(t, err) - _ = waitForVschemaTables(t, ks, []string{"test_seq"}, executor) + _ = waitForVschemaTables(t, ks, append(vschemaTables, []string{"test_seq"}...), executor) vschema = executor.vm.GetCurrentSrvVschema() table := vschema.Keyspaces[ks].Tables["test_seq"] wantType := "sequence" - require.Equal(t, wantType, table.Type) - - // note the last vschema updated time. - ts := executor.VSchema().GetCreated() + if table.Type != wantType { + t.Errorf("want table type sequence got %v", table) + } // drop existing test sequence stmt = "alter vschema drop sequence test_seq" _, err = executor.Execute(ctx, nil, "TestExecute", session, stmt, nil) require.NoError(t, err) - ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - if !waitForNewerVSchema(ctxWithTimeout, executor, ts) { - t.Fatalf("vschema did not drop the sequene 'test_seq'") - } - // Should fail dropping a non-existing test sequence session = NewSafeSession(&vtgatepb.Session{TargetString: ks}) @@ -458,33 +449,21 @@ func TestExecutorDropAutoIncDDL(t *testing.T) { _, err := executor.Execute(ctx, nil, "TestExecute", session, stmt, nil) require.NoError(t, err) - _ = waitForVschemaTables(t, ks, []string{"test_table"}, executor) - ts := executor.VSchema().GetCreated() - stmt = "alter vschema on test_table add auto_increment id using `db-name`.`test_seq`" _, err = executor.Execute(ctx, nil, "TestExecute", session, stmt, nil) require.NoError(t, err) - ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - if !waitForNewerVSchema(ctxWithTimeout, executor, ts) { - t.Fatalf("vschema did not update with auto_increment for 'test_table'") - } - ts = executor.VSchema().GetCreated() wantAutoInc := &vschemapb.AutoIncrement{Column: "id", Sequence: "`db-name`.test_seq"} gotAutoInc := executor.vm.GetCurrentSrvVschema().Keyspaces[ks].Tables["test_table"].AutoIncrement - utils.MustMatch(t, wantAutoInc, gotAutoInc) + if !reflect.DeepEqual(wantAutoInc, gotAutoInc) { + t.Errorf("want autoinc %v, got autoinc %v", wantAutoInc, gotAutoInc) + } stmt = "alter vschema on test_table drop auto_increment" _, err = executor.Execute(ctx, nil, "TestExecute", session, stmt, nil) require.NoError(t, err) - ctxWithTimeout, cancel2 := context.WithTimeout(ctx, 5*time.Second) - defer cancel2() - if !waitForNewerVSchema(ctxWithTimeout, executor, ts) { - t.Fatalf("vschema did not drop the auto_increment for 'test_table'") - } if executor.vm.GetCurrentSrvVschema().Keyspaces[ks].Tables["test_table"].AutoIncrement != nil { t.Errorf("auto increment should be nil after drop") } diff --git a/go/vt/vtgate/planbuilder/operator_transformers.go b/go/vt/vtgate/planbuilder/operator_transformers.go index 8104a20bc6c..735971838b0 100644 --- a/go/vt/vtgate/planbuilder/operator_transformers.go +++ b/go/vt/vtgate/planbuilder/operator_transformers.go @@ -517,8 +517,8 @@ func autoIncGenerate(gen *operators.Generate) *engine.Generate { } } -func generateInsertShardedQuery(ins *sqlparser.Insert) (prefix string, mids sqlparser.Values, suffix string) { - mids, isValues := ins.Rows.(sqlparser.Values) +func generateInsertShardedQuery(ins *sqlparser.Insert) (prefix string, mid []string, suffix string) { + valueTuples, isValues := ins.Rows.(sqlparser.Values) prefixFormat := "insert %v%sinto %v%v " if isValues { // the mid values are filled differently @@ -535,6 +535,20 @@ func generateInsertShardedQuery(ins *sqlparser.Insert) (prefix string, mids sqlp suffixBuf := sqlparser.NewTrackedBuffer(dmlFormatter) suffixBuf.Myprintf("%v", ins.OnDup) suffix = suffixBuf.String() + + if !isValues { + // this is a insert query using select to insert the rows. + return + } + + midBuf := sqlparser.NewTrackedBuffer(dmlFormatter) + mid = make([]string, len(valueTuples)) + for rowNum, val := range valueTuples { + midBuf.Myprintf("%v", val) + mid[rowNum] = midBuf.String() + midBuf.Reset() + } + return } diff --git a/go/vt/vtgate/vtgate_test.go b/go/vt/vtgate/vtgate_test.go index c113ed16308..6f21158d7bb 100644 --- a/go/vt/vtgate/vtgate_test.go +++ b/go/vt/vtgate/vtgate_test.go @@ -626,14 +626,14 @@ func TestMultiInternalSavepointVtGate(t *testing.T) { Sql: "insert into sp_tbl(user_id) values (:_user_id_0)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(1), + "_user_id_1": sqltypes.Int64BindVariable(3), + "vtg1": sqltypes.Int64BindVariable(1), + "vtg2": sqltypes.Int64BindVariable(3), }, }} assertQueriesWithSavepoint(t, sbc1, wantQ) wantQ[1].Sql = "insert into sp_tbl(user_id) values (:_user_id_1)" - wantQ[1].BindVariables = map[string]*querypb.BindVariable{ - "_user_id_1": sqltypes.Int64BindVariable(3), - } assertQueriesWithSavepoint(t, sbc2, wantQ) assert.Len(t, sbc3.Queries, 0) // internal savepoint should be removed. @@ -650,7 +650,10 @@ func TestMultiInternalSavepointVtGate(t *testing.T) { }, { Sql: "insert into sp_tbl(user_id) values (:_user_id_1)", BindVariables: map[string]*querypb.BindVariable{ + "_user_id_0": sqltypes.Int64BindVariable(2), "_user_id_1": sqltypes.Int64BindVariable(4), + "vtg1": sqltypes.Int64BindVariable(2), + "vtg2": sqltypes.Int64BindVariable(4), }, }} assertQueriesWithSavepoint(t, sbc3, wantQ) @@ -666,6 +669,7 @@ func TestMultiInternalSavepointVtGate(t *testing.T) { Sql: "insert into sp_tbl(user_id) values (:_user_id_0)", BindVariables: map[string]*querypb.BindVariable{ "_user_id_0": sqltypes.Int64BindVariable(5), + "vtg1": sqltypes.Int64BindVariable(5), }, }} assertQueriesWithSavepoint(t, sbc2, wantQ)