vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/vexplain/vexplain_test.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package vexplain 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 25 "vitess.io/vitess/go/test/endtoend/cluster" 26 27 "vitess.io/vitess/go/mysql" 28 "vitess.io/vitess/go/test/endtoend/utils" 29 ) 30 31 func start(t *testing.T) (*mysql.Conn, func()) { 32 ctx := context.Background() 33 vtConn, err := mysql.Connect(ctx, &vtParams) 34 require.NoError(t, err) 35 36 deleteAll := func() { 37 _, _ = utils.ExecAllowError(t, vtConn, "set workload = oltp") 38 39 tables := []string{"user", "lookup", "lookup_unique"} 40 for _, table := range tables { 41 _, _ = utils.ExecAllowError(t, vtConn, "delete from "+table) 42 } 43 } 44 45 deleteAll() 46 47 return vtConn, func() { 48 deleteAll() 49 vtConn.Close() 50 cluster.PanicHandler(t) 51 } 52 } 53 54 func TestVtGateVExplain(t *testing.T) { 55 conn, closer := start(t) 56 defer closer() 57 58 utils.AssertContainsError(t, conn, 59 `vexplain queries insert into user (id,lookup,lookup_unique) values (4,'apa','foo'),(5,'apa','bar'),(6,'monkey','nobar')`, 60 "vexplain queries/all will actually run queries") 61 62 expected := `[[INT32(0) VARCHAR("ks") VARCHAR("-40") VARCHAR("begin")]` + 63 ` [INT32(0) VARCHAR("ks") VARCHAR("-40") VARCHAR("insert into lookup(lookup, id, keyspace_id) values ('apa', 1, '\x16k@\xb4J\xbaK\xd6') on duplicate key update lookup = values(lookup), id = values(id), keyspace_id = values(keyspace_id)")]` + 64 ` [INT32(1) VARCHAR("ks") VARCHAR("40-80") VARCHAR("begin")]` + 65 ` [INT32(1) VARCHAR("ks") VARCHAR("40-80") VARCHAR("insert into lookup(lookup, id, keyspace_id) values ('monkey', 3, 'N\xb1\x90ɢ\xfa\x16\x9c') on duplicate key update lookup = values(lookup), id = values(id), keyspace_id = values(keyspace_id)")]` + 66 ` [INT32(2) VARCHAR("ks") VARCHAR("-40") VARCHAR("commit")]` + 67 ` [INT32(3) VARCHAR("ks") VARCHAR("40-80") VARCHAR("commit")]` + 68 ` [INT32(4) VARCHAR("ks") VARCHAR("40-80") VARCHAR("begin")]` + 69 ` [INT32(4) VARCHAR("ks") VARCHAR("40-80") VARCHAR("insert into lookup_unique(lookup_unique, keyspace_id) values ('monkey', 'N\xb1\x90ɢ\xfa\x16\x9c')")]` + 70 ` [INT32(5) VARCHAR("ks") VARCHAR("-40") VARCHAR("begin")]` + 71 ` [INT32(5) VARCHAR("ks") VARCHAR("-40") VARCHAR("insert into lookup_unique(lookup_unique, keyspace_id) values ('apa', '\x16k@\xb4J\xbaK\xd6')")]` + 72 ` [INT32(6) VARCHAR("ks") VARCHAR("40-80") VARCHAR("commit")]` + 73 ` [INT32(7) VARCHAR("ks") VARCHAR("-40") VARCHAR("commit")]` + 74 ` [INT32(8) VARCHAR("ks") VARCHAR("40-80") VARCHAR("begin")]` + 75 ` [INT32(8) VARCHAR("ks") VARCHAR("40-80") VARCHAR("insert into ` + "`user`" + `(id, lookup, lookup_unique) values (3, 'monkey', 'monkey')")]` + 76 ` [INT32(9) VARCHAR("ks") VARCHAR("-40") VARCHAR("begin")]` + 77 ` [INT32(9) VARCHAR("ks") VARCHAR("-40") VARCHAR("insert into ` + "`user`" + `(id, lookup, lookup_unique) values (1, 'apa', 'apa')")]]` 78 utils.AssertMatchesNoOrder(t, conn, `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries insert into user (id,lookup,lookup_unique) values (1,'apa','apa'),(3,'monkey','monkey')`, expected) 79 80 // Assert that the output of vexplain all doesn't have begin queries because they aren't explainable 81 utils.AssertMatchesNotContains(t, conn, `vexplain /*vt+ EXECUTE_DML_QUERIES */ all insert into user (id,lookup,lookup_unique) values (2,'apa','bandar')`, `begin`) 82 83 expected = `[[INT32(0) VARCHAR("ks") VARCHAR("-40") VARCHAR("select lookup, keyspace_id from lookup where lookup in ('apa')")]` + 84 ` [INT32(1) VARCHAR("ks") VARCHAR("-40") VARCHAR("select id from ` + "`user`" + ` where lookup = 'apa'")]]` 85 for _, mode := range []string{"oltp", "olap"} { 86 t.Run(mode, func(t *testing.T) { 87 utils.Exec(t, conn, "set workload = "+mode) 88 utils.AssertMatches(t, conn, `vexplain queries select id from user where lookup = "apa"`, expected) 89 }) 90 } 91 92 // transaction explicitly started to no commit in the end. 93 utils.Exec(t, conn, "begin") 94 expected = `[[INT32(0) VARCHAR("ks") VARCHAR("-40") VARCHAR("begin")]` + 95 ` [INT32(0) VARCHAR("ks") VARCHAR("-40") VARCHAR("insert into lookup(lookup, id, keyspace_id) values ('apa', 4, '\xd2\xfd\x88g\xd5\\r-\xfe'), ('apa', 5, 'p\xbb\x02<\x81\f\xa8z') on duplicate key update lookup = values(lookup), id = values(id), keyspace_id = values(keyspace_id)")]` + 96 ` [INT32(1) VARCHAR("ks") VARCHAR("40-80") VARCHAR("begin")]` + 97 ` [INT32(1) VARCHAR("ks") VARCHAR("40-80") VARCHAR("insert into lookup(lookup, id, keyspace_id) values ('monkey', 6, '\xf0\x98H\\n\xc4ľq') on duplicate key update lookup = values(lookup), id = values(id), keyspace_id = values(keyspace_id)")]` + 98 ` [INT32(2) VARCHAR("ks") VARCHAR("-40") VARCHAR("commit")]` + 99 ` [INT32(3) VARCHAR("ks") VARCHAR("40-80") VARCHAR("commit")]` + 100 ` [INT32(4) VARCHAR("ks") VARCHAR("-40") VARCHAR("begin")]` + 101 ` [INT32(4) VARCHAR("ks") VARCHAR("-40") VARCHAR("insert into lookup_unique(lookup_unique, keyspace_id) values ('foo', '\xd2\xfd\x88g\xd5\\r-\xfe')")]` + 102 ` [INT32(5) VARCHAR("ks") VARCHAR("80-c0") VARCHAR("begin")]` + 103 ` [INT32(5) VARCHAR("ks") VARCHAR("80-c0") VARCHAR("insert into lookup_unique(lookup_unique, keyspace_id) values ('bar', 'p\xbb\x02<\x81\f\xa8z')")]` + 104 ` [INT32(6) VARCHAR("ks") VARCHAR("c0-") VARCHAR("begin")]` + 105 ` [INT32(6) VARCHAR("ks") VARCHAR("c0-") VARCHAR("insert into lookup_unique(lookup_unique, keyspace_id) values ('nobar', '\xf0\x98H\\n\xc4ľq')")]` + 106 ` [INT32(7) VARCHAR("ks") VARCHAR("-40") VARCHAR("commit")]` + 107 ` [INT32(8) VARCHAR("ks") VARCHAR("80-c0") VARCHAR("commit")]` + 108 ` [INT32(9) VARCHAR("ks") VARCHAR("c0-") VARCHAR("commit")]` + 109 ` [INT32(10) VARCHAR("ks") VARCHAR("40-80") VARCHAR("begin")]` + 110 ` [INT32(10) VARCHAR("ks") VARCHAR("40-80") VARCHAR("insert into ` + "`user`" + `(id, lookup, lookup_unique) values (5, 'apa', 'bar')")]` + 111 ` [INT32(11) VARCHAR("ks") VARCHAR("c0-") VARCHAR("begin")]` + 112 ` [INT32(11) VARCHAR("ks") VARCHAR("c0-") VARCHAR("insert into ` + "`user`" + `(id, lookup, lookup_unique) values (4, 'apa', 'foo'), (6, 'monkey', 'nobar')")]]` 113 114 utils.AssertMatchesNoOrder(t, conn, 115 `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries insert into user (id,lookup,lookup_unique) values (4,'apa','foo'),(5,'apa','bar'),(6,'monkey','nobar')`, 116 expected) 117 118 utils.Exec(t, conn, "rollback") 119 } 120 121 func TestVExplainPlan(t *testing.T) { 122 conn, closer := start(t) 123 defer closer() 124 125 // the test infra is adding \ to the test output 126 utils.AssertMatchesContains(t, conn, `vexplain plan select id from user where lookup = "apa"`, `\"OperatorType\": \"VindexLookup\"`) 127 utils.AssertMatchesContains(t, conn, `vexplain plan insert into user (id,lookup,lookup_unique) values (4,'apa','foo'),(5,'apa','bar'),(6,'monkey','nobar')`, "Insert") 128 } 129 130 func TestVExplainAll(t *testing.T) { 131 conn, closer := start(t) 132 defer closer() 133 134 utils.AssertMatchesContains(t, conn, `vexplain /*vt+ EXECUTE_DML_QUERIES */ all insert into user (id,lookup,lookup_unique) values (4,'apa','foo'),(5,'apa','bar'),(6,'monkey','nobar')`, "Insert", "mysql_explain_json") 135 utils.AssertMatchesContains(t, conn, `vexplain all select id from user where lookup = "apa"`, "mysql_explain_json", "ByDestination") 136 }