vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/misc_test.go (about) 1 /* 2 Copyright 2019 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 vtgate 18 19 import ( 20 "fmt" 21 "testing" 22 23 "vitess.io/vitess/go/mysql" 24 "vitess.io/vitess/go/test/endtoend/utils" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestSelectNull(t *testing.T) { 31 conn, closer := start(t) 32 defer closer() 33 34 utils.Exec(t, conn, "begin") 35 utils.Exec(t, conn, "insert into t5_null_vindex(id, idx) values(1, 'a'), (2, 'b'), (3, null)") 36 utils.Exec(t, conn, "commit") 37 38 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex order by id", "[[INT64(1) VARCHAR(\"a\")] [INT64(2) VARCHAR(\"b\")] [INT64(3) NULL]]") 39 utils.AssertIsEmpty(t, conn, "select id, idx from t5_null_vindex where idx = null") 40 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex where idx is null", "[[INT64(3) NULL]]") 41 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex where idx <=> null", "[[INT64(3) NULL]]") 42 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex where idx is not null order by id", "[[INT64(1) VARCHAR(\"a\")] [INT64(2) VARCHAR(\"b\")]]") 43 utils.AssertIsEmpty(t, conn, "select id, idx from t5_null_vindex where id IN (null)") 44 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex where id IN (1,2,null) order by id", "[[INT64(1) VARCHAR(\"a\")] [INT64(2) VARCHAR(\"b\")]]") 45 utils.AssertIsEmpty(t, conn, "select id, idx from t5_null_vindex where id NOT IN (1,null) order by id") 46 utils.AssertMatches(t, conn, "select id, idx from t5_null_vindex where id NOT IN (1,3)", "[[INT64(2) VARCHAR(\"b\")]]") 47 } 48 49 func TestDoStatement(t *testing.T) { 50 conn, closer := start(t) 51 defer closer() 52 53 utils.Exec(t, conn, "do 1") 54 utils.Exec(t, conn, "do 'a', 1+2,database()") 55 } 56 57 func TestShowColumns(t *testing.T) { 58 conn, closer := start(t) 59 defer closer() 60 61 expected80 := `[[VARCHAR("id") BLOB("bigint") VARCHAR("NO") BINARY("PRI") NULL VARCHAR("")] [VARCHAR("idx") BLOB("varchar(50)") VARCHAR("YES") BINARY("") NULL VARCHAR("")]]` 62 expected57 := `[[VARCHAR("id") TEXT("bigint(20)") VARCHAR("NO") VARCHAR("PRI") NULL VARCHAR("")] [VARCHAR("idx") TEXT("varchar(50)") VARCHAR("YES") VARCHAR("") NULL VARCHAR("")]]` 63 utils.AssertMatchesAny(t, conn, "show columns from `t5_null_vindex` in `ks`", expected80, expected57) 64 utils.AssertMatchesAny(t, conn, "SHOW COLUMNS from `t5_null_vindex` in `ks`", expected80, expected57) 65 utils.AssertMatchesAny(t, conn, "SHOW columns FROM `t5_null_vindex` in `ks`", expected80, expected57) 66 utils.AssertMatchesAny(t, conn, "SHOW columns FROM `t5_null_vindex` where Field = 'id'", 67 `[[VARCHAR("id") BLOB("bigint") VARCHAR("NO") BINARY("PRI") NULL VARCHAR("")]]`, 68 `[[VARCHAR("id") TEXT("bigint(20)") VARCHAR("NO") VARCHAR("PRI") NULL VARCHAR("")]]`) 69 } 70 71 func TestShowTables(t *testing.T) { 72 conn, closer := start(t) 73 defer closer() 74 75 query := "show tables;" 76 qr := utils.Exec(t, conn, query) 77 78 assert.Equal(t, "Tables_in_ks", qr.Fields[0].Name) 79 } 80 81 func TestCastConvert(t *testing.T) { 82 conn, closer := start(t) 83 defer closer() 84 85 utils.AssertMatches(t, conn, `SELECT CAST("test" AS CHAR(60))`, `[[VARCHAR("test")]]`) 86 } 87 88 func TestCompositeIN(t *testing.T) { 89 conn, closer := start(t) 90 defer closer() 91 92 utils.Exec(t, conn, "insert into t1(id1, id2) values(1, 2), (4, 5)") 93 94 // Just check for correct results. Plan generation is tested in unit tests. 95 utils.AssertMatches(t, conn, "select id1 from t1 where (id1, id2) in ((1, 2))", "[[INT64(1)]]") 96 } 97 98 func TestSavepointInTx(t *testing.T) { 99 conn, closer := start(t) 100 defer closer() 101 102 utils.Exec(t, conn, "savepoint a") 103 utils.Exec(t, conn, "start transaction") 104 utils.Exec(t, conn, "savepoint b") 105 utils.Exec(t, conn, "rollback to b") 106 utils.Exec(t, conn, "release savepoint b") 107 utils.Exec(t, conn, "savepoint b") 108 utils.Exec(t, conn, "insert into t1(id1, id2) values(1,1)") // -80 109 utils.Exec(t, conn, "savepoint c") 110 utils.Exec(t, conn, "insert into t1(id1, id2) values(4,4)") // 80- 111 utils.Exec(t, conn, "savepoint d") 112 utils.Exec(t, conn, "insert into t1(id1, id2) values(2,2)") // -80 113 utils.Exec(t, conn, "savepoint e") 114 115 // Validate all the data. 116 utils.Exec(t, conn, "use `ks:-80`") 117 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)]]`) 118 utils.Exec(t, conn, "use `ks:80-`") 119 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(4)]]`) 120 utils.Exec(t, conn, "use ks") 121 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)] [INT64(4)]]`) 122 123 _, err := conn.ExecuteFetch("rollback work to savepoint a", 1000, true) 124 require.Error(t, err) 125 126 utils.Exec(t, conn, "release savepoint d") 127 128 _, err = conn.ExecuteFetch("rollback to d", 1000, true) 129 require.Error(t, err) 130 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)] [INT64(4)]]`) 131 132 utils.Exec(t, conn, "rollback to c") 133 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)]]`) 134 135 utils.Exec(t, conn, "insert into t1(id1, id2) values(2,2),(3,3),(4,4)") 136 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`) 137 138 utils.Exec(t, conn, "rollback to b") 139 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 140 141 utils.Exec(t, conn, "commit") 142 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 143 144 utils.Exec(t, conn, "start transaction") 145 146 utils.Exec(t, conn, "insert into t1(id1, id2) values(2,2),(3,3),(4,4)") 147 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(2)] [INT64(3)] [INT64(4)]]`) 148 149 // After previous commit all the savepoints are cleared. 150 _, err = conn.ExecuteFetch("rollback to b", 1000, true) 151 require.Error(t, err) 152 153 utils.Exec(t, conn, "rollback") 154 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 155 } 156 157 func TestSavepointOutsideTx(t *testing.T) { 158 conn, closer := start(t) 159 defer closer() 160 161 utils.Exec(t, conn, "savepoint a") 162 utils.Exec(t, conn, "savepoint b") 163 164 _, err := conn.ExecuteFetch("rollback to b", 1, true) 165 require.Error(t, err) 166 _, err = conn.ExecuteFetch("release savepoint a", 1, true) 167 require.Error(t, err) 168 } 169 170 func TestSavepointAdditionalCase(t *testing.T) { 171 conn, closer := start(t) 172 defer closer() 173 174 utils.Exec(t, conn, "start transaction") 175 utils.Exec(t, conn, "savepoint a") 176 utils.Exec(t, conn, "insert into t1(id1, id2) values(1,1)") // -80 177 utils.Exec(t, conn, "insert into t1(id1, id2) values(2,2),(3,3),(4,4)") // -80 & 80- 178 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`) 179 180 utils.Exec(t, conn, "rollback to a") 181 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 182 183 utils.Exec(t, conn, "commit") 184 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 185 186 utils.Exec(t, conn, "start transaction") 187 utils.Exec(t, conn, "insert into t1(id1, id2) values(1,1)") // -80 188 utils.Exec(t, conn, "savepoint a") 189 utils.Exec(t, conn, "insert into t1(id1, id2) values(2,2),(3,3)") // -80 190 utils.Exec(t, conn, "insert into t1(id1, id2) values(4,4)") // 80- 191 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`) 192 193 utils.Exec(t, conn, "rollback to a") 194 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[[INT64(1)]]`) 195 196 utils.Exec(t, conn, "rollback") 197 utils.AssertMatches(t, conn, "select id1 from t1 order by id1", `[]`) 198 } 199 200 func TestExplainPassthrough(t *testing.T) { 201 conn, closer := start(t) 202 defer closer() 203 204 result := utils.Exec(t, conn, "explain select * from t1") 205 got := fmt.Sprintf("%v", result.Rows) 206 require.Contains(t, got, "SIMPLE") // there is a lot more coming from mysql, 207 // but we are trying to make the test less fragile 208 209 result = utils.Exec(t, conn, "explain ks.t1") 210 require.EqualValues(t, 2, len(result.Rows)) 211 } 212 213 func TestXXHash(t *testing.T) { 214 conn, closer := start(t) 215 defer closer() 216 217 utils.Exec(t, conn, "insert into t7_xxhash(uid, phone, msg) values('u-1', 1, 'message')") 218 utils.AssertMatches(t, conn, "select uid, phone, msg from t7_xxhash where phone = 1", `[[VARCHAR("u-1") INT64(1) VARCHAR("message")]]`) 219 utils.AssertMatches(t, conn, "select phone, keyspace_id from t7_xxhash_idx", `[[INT64(1) VARBINARY("\x1cU^f\xbfyE^")]]`) 220 utils.Exec(t, conn, "update t7_xxhash set phone = 2 where uid = 'u-1'") 221 utils.AssertMatches(t, conn, "select uid, phone, msg from t7_xxhash where phone = 1", `[]`) 222 utils.AssertMatches(t, conn, "select uid, phone, msg from t7_xxhash where phone = 2", `[[VARCHAR("u-1") INT64(2) VARCHAR("message")]]`) 223 utils.AssertMatches(t, conn, "select phone, keyspace_id from t7_xxhash_idx", `[[INT64(2) VARBINARY("\x1cU^f\xbfyE^")]]`) 224 utils.Exec(t, conn, "delete from t7_xxhash where uid = 'u-1'") 225 utils.AssertMatches(t, conn, "select uid, phone, msg from t7_xxhash where uid = 'u-1'", `[]`) 226 utils.AssertMatches(t, conn, "select phone, keyspace_id from t7_xxhash_idx", `[]`) 227 } 228 229 func TestShowTablesWithWhereClause(t *testing.T) { 230 conn, closer := start(t) 231 defer closer() 232 233 utils.AssertMatchesAny(t, conn, "show tables from ks where Tables_in_ks='t6'", `[[VARBINARY("t6")]]`, `[[VARCHAR("t6")]]`) 234 utils.Exec(t, conn, "begin") 235 utils.AssertMatchesAny(t, conn, "show tables from ks where Tables_in_ks='t3'", `[[VARBINARY("t3")]]`, `[[VARCHAR("t3")]]`) 236 } 237 238 func TestOffsetAndLimitWithOLAP(t *testing.T) { 239 conn, closer := start(t) 240 defer closer() 241 242 utils.Exec(t, conn, "insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)") 243 utils.AssertMatches(t, conn, "select id1 from t1 order by id1 limit 3 offset 2", "[[INT64(3)] [INT64(4)] [INT64(5)]]") 244 utils.Exec(t, conn, "set workload='olap'") 245 utils.AssertMatches(t, conn, "select id1 from t1 order by id1 limit 3 offset 2", "[[INT64(3)] [INT64(4)] [INT64(5)]]") 246 } 247 248 func TestSwitchBetweenOlapAndOltp(t *testing.T) { 249 conn, closer := start(t) 250 defer closer() 251 252 utils.AssertMatches(t, conn, "select @@workload", `[[VARCHAR("OLTP")]]`) 253 254 utils.Exec(t, conn, "set workload='olap'") 255 256 utils.AssertMatches(t, conn, "select @@workload", `[[VARCHAR("OLAP")]]`) 257 258 utils.Exec(t, conn, "set workload='oltp'") 259 260 utils.AssertMatches(t, conn, "select @@workload", `[[VARCHAR("OLTP")]]`) 261 } 262 263 func TestFoundRowsOnDualQueries(t *testing.T) { 264 conn, closer := start(t) 265 defer closer() 266 267 utils.Exec(t, conn, "select 42") 268 utils.AssertMatches(t, conn, "select found_rows()", "[[INT64(1)]]") 269 } 270 271 func TestUseStmtInOLAP(t *testing.T) { 272 conn, closer := start(t) 273 defer closer() 274 275 queries := []string{"set workload='olap'", "use `ks:80-`", "use `ks:-80`"} 276 for i, q := range queries { 277 t.Run(fmt.Sprintf("%d-%s", i, q), func(t *testing.T) { 278 utils.Exec(t, conn, q) 279 }) 280 } 281 } 282 283 func TestInsertStmtInOLAP(t *testing.T) { 284 conn, closer := start(t) 285 defer closer() 286 287 utils.Exec(t, conn, `set workload='olap'`) 288 utils.Exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`) 289 utils.AssertMatches(t, conn, `select id1 from t1 order by id1`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)] [INT64(5)]]`) 290 } 291 292 func TestCreateIndex(t *testing.T) { 293 conn, closer := start(t) 294 defer closer() 295 // Test that create index with the correct table name works 296 utils.Exec(t, conn, `create index i1 on t1 (id1)`) 297 // Test routing rules for create index. 298 utils.Exec(t, conn, `create index i2 on ks.t1000 (id1)`) 299 } 300 301 func TestCreateView(t *testing.T) { 302 // The test wont work since we cant change the vschema without reloading the vtgate. 303 t.Skip() 304 conn, closer := start(t) 305 defer closer() 306 // Test that create view works and the output is as expected 307 utils.Exec(t, conn, `create view v1 as select * from t1`) 308 utils.Exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`) 309 // This wont work, since ALTER VSCHEMA ADD TABLE is only supported for unsharded keyspaces 310 utils.Exec(t, conn, "alter vschema add table v1") 311 utils.AssertMatches(t, conn, "select * from v1", `[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)] [INT64(5) INT64(5)]]`) 312 } 313 314 func TestVersions(t *testing.T) { 315 conn, closer := start(t) 316 defer closer() 317 318 qr := utils.Exec(t, conn, `select @@version`) 319 assert.Contains(t, fmt.Sprintf("%v", qr.Rows), "vitess") 320 321 qr = utils.Exec(t, conn, `select @@version_comment`) 322 assert.Contains(t, fmt.Sprintf("%v", qr.Rows), "Git revision") 323 } 324 325 func TestFlush(t *testing.T) { 326 conn, closer := start(t) 327 defer closer() 328 utils.Exec(t, conn, "flush local tables t1, t2") 329 } 330 331 func TestShowVariables(t *testing.T) { 332 conn, closer := start(t) 333 defer closer() 334 res := utils.Exec(t, conn, "show variables like \"%version%\";") 335 found := false 336 for _, row := range res.Rows { 337 if row[0].ToString() == "version" { 338 assert.Contains(t, row[1].ToString(), "vitess") 339 found = true 340 } 341 } 342 require.True(t, found, "Expected a row for version in show query") 343 } 344 345 func TestShowVGtid(t *testing.T) { 346 conn, closer := start(t) 347 defer closer() 348 349 query := "show global vgtid_executed from ks" 350 qr := utils.Exec(t, conn, query) 351 require.Equal(t, 1, len(qr.Rows)) 352 require.Equal(t, 2, len(qr.Rows[0])) 353 354 utils.Exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`) 355 qr2 := utils.Exec(t, conn, query) 356 require.Equal(t, 1, len(qr2.Rows)) 357 require.Equal(t, 2, len(qr2.Rows[0])) 358 359 require.Equal(t, qr.Rows[0][0], qr2.Rows[0][0], "keyspace should be same") 360 require.NotEqual(t, qr.Rows[0][1].ToString(), qr2.Rows[0][1].ToString(), "vgtid should have changed") 361 } 362 363 func TestShowGtid(t *testing.T) { 364 conn, closer := start(t) 365 defer closer() 366 367 query := "show global gtid_executed from ks" 368 qr := utils.Exec(t, conn, query) 369 require.Equal(t, 2, len(qr.Rows)) 370 371 res := make(map[string]string, 2) 372 for _, row := range qr.Rows { 373 require.Equal(t, KeyspaceName, row[0].ToString()) 374 res[row[2].ToString()] = row[1].ToString() 375 } 376 377 utils.Exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`) 378 qr2 := utils.Exec(t, conn, query) 379 require.Equal(t, 2, len(qr2.Rows)) 380 381 for _, row := range qr2.Rows { 382 require.Equal(t, KeyspaceName, row[0].ToString()) 383 gtid, exists := res[row[2].ToString()] 384 require.True(t, exists, "gtid not cached for row: %v", row) 385 require.NotEqual(t, gtid, row[1].ToString()) 386 } 387 } 388 389 func TestDeleteAlias(t *testing.T) { 390 conn, closer := start(t) 391 defer closer() 392 393 utils.Exec(t, conn, "delete t1 from t1 where id1 = 1") 394 utils.Exec(t, conn, "delete t.* from t1 t where t.id1 = 1") 395 } 396 397 func TestFunctionInDefault(t *testing.T) { 398 conn, closer := start(t) 399 defer closer() 400 401 // set the sql mode ALLOW_INVALID_DATES 402 utils.Exec(t, conn, `SET sql_mode = 'ALLOW_INVALID_DATES'`) 403 404 // test that default expression works for columns. 405 utils.Exec(t, conn, `create table function_default (x varchar(25) DEFAULT (TRIM(" check ")))`) 406 utils.Exec(t, conn, "drop table function_default") 407 408 // verify that current_timestamp and it's aliases work as default values 409 utils.Exec(t, conn, `create table function_default ( 410 ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 411 dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 412 ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 413 dt2 DATETIME DEFAULT CURRENT_TIMESTAMP, 414 ts3 TIMESTAMP DEFAULT 0, 415 dt3 DATETIME DEFAULT 0, 416 ts4 TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP, 417 dt4 DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP, 418 ts5 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 419 ts6 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP, 420 dt5 DATETIME ON UPDATE CURRENT_TIMESTAMP, 421 dt6 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP, 422 ts7 TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), 423 ts8 TIMESTAMP DEFAULT NOW(), 424 ts9 TIMESTAMP DEFAULT LOCALTIMESTAMP, 425 ts10 TIMESTAMP DEFAULT LOCALTIME, 426 ts11 TIMESTAMP DEFAULT LOCALTIMESTAMP(), 427 ts12 TIMESTAMP DEFAULT LOCALTIME() 428 )`) 429 utils.Exec(t, conn, "drop table function_default") 430 431 utils.Exec(t, conn, `create table function_default (ts TIMESTAMP DEFAULT UTC_TIMESTAMP)`) 432 utils.Exec(t, conn, "drop table function_default") 433 434 utils.Exec(t, conn, `create table function_default (x varchar(25) DEFAULT "check")`) 435 utils.Exec(t, conn, "drop table function_default") 436 } 437 438 func TestRenameFieldsOnOLAP(t *testing.T) { 439 conn, closer := start(t) 440 defer closer() 441 442 _ = utils.Exec(t, conn, "set workload = olap") 443 444 qr := utils.Exec(t, conn, "show tables") 445 require.Equal(t, 1, len(qr.Fields)) 446 assert.Equal(t, `Tables_in_ks`, fmt.Sprintf("%v", qr.Fields[0].Name)) 447 _ = utils.Exec(t, conn, "use mysql") 448 qr = utils.Exec(t, conn, "select @@workload") 449 assert.Equal(t, `[[VARCHAR("OLAP")]]`, fmt.Sprintf("%v", qr.Rows)) 450 } 451 452 func TestSelectEqualUniqueOuterJoinRightPredicate(t *testing.T) { 453 conn, closer := start(t) 454 defer closer() 455 456 utils.Exec(t, conn, "insert into t1(id1, id2) values (0,10),(1,9),(2,8),(3,7),(4,6),(5,5)") 457 utils.Exec(t, conn, "insert into t2(id3, id4) values (0,20),(1,19),(2,18),(3,17),(4,16),(5,15)") 458 utils.AssertMatches(t, conn, `SELECT id3 FROM t1 LEFT JOIN t2 ON t1.id1 = t2.id3 WHERE t2.id3 = 10`, `[]`) 459 } 460 461 func TestSQLSelectLimit(t *testing.T) { 462 conn, closer := start(t) 463 defer closer() 464 465 utils.Exec(t, conn, "insert into t7_xxhash(uid, msg) values(1, 'a'), (2, 'b'), (3, null), (4, 'a'), (5, 'a'), (6, 'b')") 466 467 for _, workload := range []string{"olap", "oltp"} { 468 utils.Exec(t, conn, fmt.Sprintf("set workload = %s", workload)) 469 utils.Exec(t, conn, "set sql_select_limit = 2") 470 utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash order by uid", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")]]`) 471 utils.AssertMatches(t, conn, "(select uid, msg from t7_xxhash order by uid)", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")]]`) 472 utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash order by uid limit 4", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")] [VARCHAR("3") NULL] [VARCHAR("4") VARCHAR("a")]]`) 473 /* 474 planner does not support query with order by in union query. without order by the results are not deterministic for testing purpose 475 utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash union all select uid, msg from t7_xxhash order by uid", ``) 476 utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash union all select uid, msg from t7_xxhash order by uid limit 3", ``) 477 */ 478 479 // without order by the results are not deterministic for testing purpose. Checking row count only. 480 qr := utils.Exec(t, conn, "select /*vt+ PLANNER=gen4 */ uid, msg from t7_xxhash union all select uid, msg from t7_xxhash") 481 assert.Equal(t, 2, len(qr.Rows)) 482 483 qr = utils.Exec(t, conn, "select /*vt+ PLANNER=gen4 */ uid, msg from t7_xxhash union all select uid, msg from t7_xxhash limit 3") 484 assert.Equal(t, 3, len(qr.Rows)) 485 } 486 } 487 488 func TestSQLSelectLimitWithPlanCache(t *testing.T) { 489 conn, closer := start(t) 490 defer closer() 491 492 utils.Exec(t, conn, "insert into t7_xxhash(uid, msg) values(1, 'a'), (2, 'b'), (3, null)") 493 494 tcases := []struct { 495 limit int 496 out string 497 }{{ 498 limit: -1, 499 out: `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")] [VARCHAR("3") NULL]]`, 500 }, { 501 limit: 1, 502 out: `[[VARCHAR("1") VARCHAR("a")]]`, 503 }, { 504 limit: 2, 505 out: `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")]]`, 506 }, { 507 limit: 3, 508 out: `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")] [VARCHAR("3") NULL]]`, 509 }, { 510 limit: 4, 511 out: `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")] [VARCHAR("3") NULL]]`, 512 }} 513 for _, workload := range []string{"olap", "oltp"} { 514 utils.Exec(t, conn, fmt.Sprintf("set workload = %s", workload)) 515 for _, tcase := range tcases { 516 utils.Exec(t, conn, fmt.Sprintf("set sql_select_limit = %d", tcase.limit)) 517 utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash order by uid", tcase.out) 518 } 519 } 520 } 521 522 func TestSavepointInReservedConn(t *testing.T) { 523 conn, closer := start(t) 524 defer closer() 525 526 utils.Exec(t, conn, "set session sql_mode = ''") 527 utils.Exec(t, conn, "BEGIN") 528 utils.Exec(t, conn, "SAVEPOINT sp_1") 529 utils.Exec(t, conn, "insert into t7_xxhash(uid, msg) values(1, 'a')") 530 utils.Exec(t, conn, "RELEASE SAVEPOINT sp_1") 531 utils.Exec(t, conn, "ROLLBACK") 532 533 utils.Exec(t, conn, "set session sql_mode = ''") 534 utils.Exec(t, conn, "BEGIN") 535 utils.Exec(t, conn, "SAVEPOINT sp_1") 536 utils.Exec(t, conn, "RELEASE SAVEPOINT sp_1") 537 utils.Exec(t, conn, "SAVEPOINT sp_2") 538 utils.Exec(t, conn, "insert into t7_xxhash(uid, msg) values(2, 'a')") 539 utils.Exec(t, conn, "RELEASE SAVEPOINT sp_2") 540 utils.Exec(t, conn, "COMMIT") 541 utils.AssertMatches(t, conn, "select uid from t7_xxhash", `[[VARCHAR("2")]]`) 542 } 543 544 func TestUnionWithManyInfSchemaQueries(t *testing.T) { 545 // trying to reproduce the problems in https://github.com/vitessio/vitess/issues/9139 546 conn, closer := start(t) 547 defer closer() 548 549 utils.Exec(t, conn, `SELECT /*vt+ PLANNER=gen4 */ 550 TABLE_SCHEMA, 551 TABLE_NAME 552 FROM 553 INFORMATION_SCHEMA.TABLES 554 WHERE 555 TABLE_SCHEMA = 'ionescu' 556 AND 557 TABLE_NAME = 'company_invite_code' 558 UNION 559 SELECT 560 TABLE_SCHEMA, 561 TABLE_NAME 562 FROM 563 INFORMATION_SCHEMA.TABLES 564 WHERE 565 TABLE_SCHEMA = 'ionescu' 566 AND 567 TABLE_NAME = 'site_role' 568 UNION 569 SELECT 570 TABLE_SCHEMA, 571 TABLE_NAME 572 FROM 573 INFORMATION_SCHEMA.TABLES 574 WHERE 575 TABLE_SCHEMA = 'ionescu' 576 AND 577 TABLE_NAME = 'item' 578 UNION 579 SELECT 580 TABLE_SCHEMA, 581 TABLE_NAME 582 FROM 583 INFORMATION_SCHEMA.TABLES 584 WHERE 585 TABLE_SCHEMA = 'ionescu' 586 AND 587 TABLE_NAME = 'site_item_urgent' 588 UNION 589 SELECT 590 TABLE_SCHEMA, 591 TABLE_NAME 592 FROM 593 INFORMATION_SCHEMA.TABLES 594 WHERE 595 TABLE_SCHEMA = 'ionescu' 596 AND 597 TABLE_NAME = 'site_item_event' 598 UNION 599 SELECT 600 TABLE_SCHEMA, 601 TABLE_NAME 602 FROM 603 INFORMATION_SCHEMA.TABLES 604 WHERE 605 TABLE_SCHEMA = 'ionescu' 606 AND 607 TABLE_NAME = 'site_item' 608 UNION 609 SELECT 610 TABLE_SCHEMA, 611 TABLE_NAME 612 FROM 613 INFORMATION_SCHEMA.TABLES 614 WHERE 615 TABLE_SCHEMA = 'ionescu' 616 AND 617 TABLE_NAME = 'site' 618 UNION 619 SELECT 620 TABLE_SCHEMA, 621 TABLE_NAME 622 FROM 623 INFORMATION_SCHEMA.TABLES 624 WHERE 625 TABLE_SCHEMA = 'ionescu' 626 AND 627 TABLE_NAME = 'company' 628 UNION 629 SELECT 630 TABLE_SCHEMA, 631 TABLE_NAME 632 FROM 633 INFORMATION_SCHEMA.TABLES 634 WHERE 635 TABLE_SCHEMA = 'ionescu' 636 AND 637 TABLE_NAME = 'user_company' 638 UNION 639 SELECT 640 TABLE_SCHEMA, 641 TABLE_NAME 642 FROM 643 INFORMATION_SCHEMA.TABLES 644 WHERE 645 TABLE_SCHEMA = 'ionescu' 646 AND 647 TABLE_NAME = 'user'`) 648 } 649 650 func TestTransactionsInStreamingMode(t *testing.T) { 651 conn, closer := start(t) 652 defer closer() 653 654 utils.Exec(t, conn, "set workload = olap") 655 utils.Exec(t, conn, "begin") 656 utils.Exec(t, conn, "insert into t1(id1, id2) values (1,2)") 657 utils.AssertMatches(t, conn, "select id1, id2 from t1", `[[INT64(1) INT64(2)]]`) 658 utils.Exec(t, conn, "commit") 659 utils.AssertMatches(t, conn, "select id1, id2 from t1", `[[INT64(1) INT64(2)]]`) 660 661 utils.Exec(t, conn, "begin") 662 utils.Exec(t, conn, "insert into t1(id1, id2) values (2,3)") 663 utils.AssertMatches(t, conn, "select id1, id2 from t1 where id1 = 2", `[[INT64(2) INT64(3)]]`) 664 utils.Exec(t, conn, "rollback") 665 utils.AssertMatches(t, conn, "select id1, id2 from t1 where id1 = 2", `[]`) 666 } 667 668 func TestCharsetIntro(t *testing.T) { 669 conn, closer := start(t) 670 defer closer() 671 672 utils.Exec(t, conn, "insert into t4 (id1,id2) values (666, _binary'abc')") 673 utils.Exec(t, conn, "update t4 set id2 = _latin1'xyz' where id1 = 666") 674 utils.Exec(t, conn, "delete from t4 where id2 = _utf8'xyz'") 675 qr := utils.Exec(t, conn, "select id1 from t4 where id2 = _utf8mb4'xyz'") 676 require.EqualValues(t, 0, qr.RowsAffected) 677 } 678 679 func TestFilterAfterLeftJoin(t *testing.T) { 680 conn, closer := start(t) 681 defer closer() 682 683 utils.Exec(t, conn, "insert into t1 (id1,id2) values (1, 10)") 684 utils.Exec(t, conn, "insert into t1 (id1,id2) values (2, 3)") 685 utils.Exec(t, conn, "insert into t1 (id1,id2) values (3, 2)") 686 687 query := "select /*vt+ PLANNER=gen4 */ A.id1, A.id2 from t1 as A left join t1 as B on A.id1 = B.id2 WHERE B.id1 IS NULL" 688 utils.AssertMatches(t, conn, query, `[[INT64(1) INT64(10)]]`) 689 } 690 691 func TestDescribeVindex(t *testing.T) { 692 conn, closer := start(t) 693 defer closer() 694 695 _, err := conn.ExecuteFetch("describe hash", 1000, false) 696 require.Error(t, err) 697 mysqlErr := err.(*mysql.SQLError) 698 assert.Equal(t, 1146, mysqlErr.Num) 699 assert.Equal(t, "42S02", mysqlErr.State) 700 assert.Contains(t, mysqlErr.Message, "NotFound desc") 701 } 702 703 func TestEmptyQuery(t *testing.T) { 704 conn, closer := start(t) 705 defer closer() 706 707 utils.AssertContainsError(t, conn, "", "Query was empty") 708 utils.AssertContainsError(t, conn, ";", "Query was empty") 709 utils.AssertIsEmpty(t, conn, "-- this is a comment") 710 } 711 712 // TestJoinWithMergedRouteWithPredicate checks the issue found in https://github.com/vitessio/vitess/issues/10713 713 func TestJoinWithMergedRouteWithPredicate(t *testing.T) { 714 conn, closer := start(t) 715 defer closer() 716 717 utils.Exec(t, conn, "insert into t1 (id1,id2) values (1, 13)") 718 utils.Exec(t, conn, "insert into t2 (id3,id4) values (5, 10), (15, 20)") 719 utils.Exec(t, conn, "insert into t3 (id5,id6,id7) values (13, 5, 8)") 720 721 utils.AssertMatches(t, conn, "select t3.id7, t2.id3, t3.id6 from t1 join t3 on t1.id2 = t3.id5 join t2 on t3.id6 = t2.id3 where t1.id2 = 13", `[[INT64(8) INT64(5) INT64(5)]]`) 722 } 723 724 func TestRowCountExceed(t *testing.T) { 725 conn, closer := start(t) 726 defer closer() 727 728 for i := 0; i < 250; i++ { 729 utils.Exec(t, conn, fmt.Sprintf("insert into t1 (id1, id2) values (%d, %d)", i, i+1)) 730 } 731 732 utils.AssertContainsError(t, conn, "select id1 from t1 where id1 < 1000", `Row count exceeded 100`) 733 }