github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/stochastik/session_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package stochastik_test 15 16 import ( 17 "context" 18 "flag" 19 "fmt" 20 "os" 21 "path" 22 "strings" 23 "sync" 24 "sync/atomic" 25 "time" 26 27 "github.com/whtcorpsinc/BerolinaSQL" 28 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 29 "github.com/whtcorpsinc/BerolinaSQL/auth" 30 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 31 "github.com/whtcorpsinc/BerolinaSQL/terror" 32 . "github.com/whtcorpsinc/check" 33 "github.com/whtcorpsinc/errors" 34 "github.com/whtcorpsinc/failpoint" 35 "github.com/whtcorpsinc/fidelpb/go-binlog" 36 "github.com/whtcorpsinc/milevadb/causet/blocks" 37 causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded" 38 "github.com/whtcorpsinc/milevadb/causetstore/einsteindb" 39 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 40 "github.com/whtcorpsinc/milevadb/causetstore/mockstore/cluster" 41 "github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb" 42 "github.com/whtcorpsinc/milevadb/config" 43 "github.com/whtcorpsinc/milevadb/ekv" 44 "github.com/whtcorpsinc/milevadb/interlock" 45 "github.com/whtcorpsinc/milevadb/petri" 46 "github.com/whtcorpsinc/milevadb/privilege/privileges" 47 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 48 "github.com/whtcorpsinc/milevadb/soliton/sqlexec" 49 "github.com/whtcorpsinc/milevadb/soliton/testkit" 50 "github.com/whtcorpsinc/milevadb/soliton/testleak" 51 "github.com/whtcorpsinc/milevadb/spacetime/autoid" 52 "github.com/whtcorpsinc/milevadb/stochastik" 53 "github.com/whtcorpsinc/milevadb/stochastikctx" 54 "github.com/whtcorpsinc/milevadb/stochastikctx/binloginfo" 55 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 56 "github.com/whtcorpsinc/milevadb/types" 57 "go.etcd.io/etcd/clientv3" 58 "google.golang.org/grpc" 59 ) 60 61 var ( 62 withEinsteinDB = flag.Bool("with-einsteindb", false, "run tests with EinsteinDB cluster started. (not use the mock server)") 63 FIDelAddrs = flag.String("fidel-addrs", "127.0.0.1:2379", "fidel addrs") 64 FIDelAddrChan chan string 65 initFIDelAddrsOnce sync.Once 66 ) 67 68 var _ = Suite(&testStochastikSuite{}) 69 var _ = Suite(&testStochastikSuite2{}) 70 var _ = Suite(&testStochastikSuite3{}) 71 var _ = Suite(&testSchemaSuite{}) 72 var _ = Suite(&testIsolationSuite{}) 73 var _ = SerialSuites(&testSchemaSerialSuite{}) 74 var _ = SerialSuites(&testStochastikSerialSuite{}) 75 var _ = SerialSuites(&testBackupRestoreSuite{}) 76 77 type testStochastikSuiteBase struct { 78 cluster cluster.Cluster 79 causetstore ekv.CausetStorage 80 dom *petri.Petri 81 FIDelAddr string 82 } 83 84 type testStochastikSuite struct { 85 testStochastikSuiteBase 86 } 87 88 type testStochastikSuite2 struct { 89 testStochastikSuiteBase 90 } 91 92 type testStochastikSuite3 struct { 93 testStochastikSuiteBase 94 } 95 96 type testStochastikSerialSuite struct { 97 testStochastikSuiteBase 98 } 99 100 type testBackupRestoreSuite struct { 101 testStochastikSuiteBase 102 } 103 104 func clearStorage(causetstore ekv.CausetStorage) error { 105 txn, err := causetstore.Begin() 106 if err != nil { 107 return errors.Trace(err) 108 } 109 iter, err := txn.Iter(nil, nil) 110 if err != nil { 111 return errors.Trace(err) 112 } 113 for iter.Valid() { 114 txn.Delete(iter.Key()) 115 if err := iter.Next(); err != nil { 116 return errors.Trace(err) 117 } 118 } 119 return txn.Commit(context.Background()) 120 } 121 122 func clearETCD(ebd einsteindb.EtcdBackend) error { 123 endpoints, err := ebd.EtcdAddrs() 124 if err != nil { 125 return err 126 } 127 cli, err := clientv3.New(clientv3.Config{ 128 Endpoints: endpoints, 129 AutoSyncInterval: 30 * time.Second, 130 DialTimeout: 5 * time.Second, 131 DialOptions: []grpc.DialOption{ 132 grpc.WithBackoffMaxDelay(time.Second * 3), 133 }, 134 TLS: ebd.TLSConfig(), 135 }) 136 if err != nil { 137 return errors.Trace(err) 138 } 139 defer cli.Close() 140 141 resp, err := cli.Get(context.Background(), "/milevadb", clientv3.WithPrefix()) 142 if err != nil { 143 return errors.Trace(err) 144 } 145 for _, ekv := range resp.Ekvs { 146 if ekv.Lease != 0 { 147 if _, err := cli.Revoke(context.Background(), clientv3.LeaseID(ekv.Lease)); err != nil { 148 return errors.Trace(err) 149 } 150 } 151 } 152 _, err = cli.Delete(context.Background(), "/milevadb", clientv3.WithPrefix()) 153 if err != nil { 154 return errors.Trace(err) 155 } 156 return nil 157 } 158 159 func initFIDelAddrs() { 160 initFIDelAddrsOnce.Do(func() { 161 addrs := strings.Split(*FIDelAddrs, ",") 162 FIDelAddrChan = make(chan string, len(addrs)) 163 for _, addr := range addrs { 164 addr = strings.TrimSpace(addr) 165 if addr != "" { 166 FIDelAddrChan <- addr 167 } 168 } 169 }) 170 } 171 172 func (s *testStochastikSuiteBase) SetUpSuite(c *C) { 173 testleak.BeforeTest() 174 175 if *withEinsteinDB { 176 initFIDelAddrs() 177 s.FIDelAddr = <-FIDelAddrChan 178 var d einsteindb.Driver 179 config.UFIDelateGlobal(func(conf *config.Config) { 180 conf.TxnLocalLatches.Enabled = false 181 }) 182 causetstore, err := d.Open(fmt.Sprintf("einsteindb://%s", s.FIDelAddr)) 183 c.Assert(err, IsNil) 184 err = clearStorage(causetstore) 185 c.Assert(err, IsNil) 186 err = clearETCD(causetstore.(einsteindb.EtcdBackend)) 187 c.Assert(err, IsNil) 188 stochastik.ResetStoreForWithEinsteinDBTest(causetstore) 189 s.causetstore = causetstore 190 } else { 191 causetstore, err := mockstore.NewMockStore( 192 mockstore.WithClusterInspector(func(c cluster.Cluster) { 193 mockstore.BootstrapWithSingleStore(c) 194 s.cluster = c 195 }), 196 ) 197 c.Assert(err, IsNil) 198 s.causetstore = causetstore 199 stochastik.DisableStats4Test() 200 } 201 202 var err error 203 s.dom, err = stochastik.BootstrapStochastik(s.causetstore) 204 c.Assert(err, IsNil) 205 s.dom.GetGlobalVarsCache().Disable() 206 } 207 208 func (s *testStochastikSuiteBase) TearDownSuite(c *C) { 209 s.dom.Close() 210 s.causetstore.Close() 211 testleak.AfterTest(c)() 212 if *withEinsteinDB { 213 FIDelAddrChan <- s.FIDelAddr 214 } 215 } 216 217 func (s *testStochastikSuiteBase) TearDownTest(c *C) { 218 tk := testkit.NewTestKitWithInit(c, s.causetstore) 219 r := tk.MustQuery("show full blocks") 220 for _, tb := range r.Rows() { 221 blockName := tb[0] 222 blockType := tb[1] 223 if blockType == "VIEW" { 224 tk.MustInterDirc(fmt.Sprintf("drop view %v", blockName)) 225 } else if blockType == "BASE TABLE" { 226 tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName)) 227 } else { 228 panic(fmt.Sprintf("Unexpected causet '%s' with type '%s'.", blockName, blockType)) 229 } 230 } 231 } 232 233 type mockBinlogPump struct { 234 } 235 236 var _ binlog.PumpClient = &mockBinlogPump{} 237 238 func (p *mockBinlogPump) WriteBinlog(ctx context.Context, in *binlog.WriteBinlogReq, opts ...grpc.CallOption) (*binlog.WriteBinlogResp, error) { 239 return &binlog.WriteBinlogResp{}, nil 240 } 241 242 type mockPumpPullBinlogsClient struct { 243 grpc.ClientStream 244 } 245 246 func (m mockPumpPullBinlogsClient) Recv() (*binlog.PullBinlogResp, error) { 247 return nil, nil 248 } 249 250 func (p *mockBinlogPump) PullBinlogs(ctx context.Context, in *binlog.PullBinlogReq, opts ...grpc.CallOption) (binlog.Pump_PullBinlogsClient, error) { 251 return mockPumpPullBinlogsClient{mockeinsteindb.MockGRPCClientStream()}, nil 252 } 253 254 func (s *testStochastikSuite) TestForCoverage(c *C) { 255 // Just for test coverage. 256 tk := testkit.NewTestKitWithInit(c, s.causetstore) 257 tk.MustInterDirc("drop causet if exists t") 258 tk.MustInterDirc("create causet t (id int auto_increment, v int, index (id))") 259 tk.MustInterDirc("insert t values ()") 260 tk.MustInterDirc("insert t values ()") 261 tk.MustInterDirc("insert t values ()") 262 263 // Normal request will not cover txn.Seek. 264 tk.MustInterDirc("admin check causet t") 265 266 // Cover dirty causet operations in StateTxn. 267 tk.Se.GetStochastikVars().BinlogClient = binloginfo.MockPumpsClient(&mockBinlogPump{}) 268 tk.MustInterDirc("begin") 269 tk.MustInterDirc("truncate causet t") 270 tk.MustInterDirc("insert t values ()") 271 tk.MustInterDirc("delete from t where id = 2") 272 tk.MustInterDirc("uFIDelate t set v = 5 where id = 2") 273 tk.MustInterDirc("insert t values ()") 274 tk.MustInterDirc("rollback") 275 276 c.Check(tk.Se.SetDefCauslation(allegrosql.DefaultDefCauslationID), IsNil) 277 278 tk.MustInterDirc("show processlist") 279 _, err := tk.Se.FieldList("t") 280 c.Check(err, IsNil) 281 } 282 283 func (s *testStochastikSuite2) TestErrorRollback(c *C) { 284 tk := testkit.NewTestKitWithInit(c, s.causetstore) 285 tk.MustInterDirc("drop causet if exists t_rollback") 286 tk.MustInterDirc("create causet t_rollback (c1 int, c2 int, primary key(c1))") 287 tk.MustInterDirc("insert into t_rollback values (0, 0)") 288 289 var wg sync.WaitGroup 290 cnt := 4 291 wg.Add(cnt) 292 num := 20 293 294 for i := 0; i < cnt; i++ { 295 go func() { 296 defer wg.Done() 297 localTk := testkit.NewTestKitWithInit(c, s.causetstore) 298 localTk.MustInterDirc("set @@stochastik.milevadb_retry_limit = 100") 299 for j := 0; j < num; j++ { 300 localTk.InterDirc("insert into t_rollback values (1, 1)") 301 localTk.MustInterDirc("uFIDelate t_rollback set c2 = c2 + 1 where c1 = 0") 302 } 303 }() 304 } 305 306 wg.Wait() 307 tk.MustQuery("select c2 from t_rollback where c1 = 0").Check(testkit.Rows(fmt.Sprint(cnt * num))) 308 } 309 310 func (s *testStochastikSuite) TestQueryString(c *C) { 311 tk := testkit.NewTestKitWithInit(c, s.causetstore) 312 313 tk.MustInterDirc("create causet mutil1 (a int);create causet multi2 (a int)") 314 queryStr := tk.Se.Value(stochastikctx.QueryString) 315 c.Assert(queryStr, Equals, "create causet multi2 (a int)") 316 317 // Test execution of DBS through the "InterDircutePreparedStmt" interface. 318 _, err := tk.Se.InterDircute(context.Background(), "use test;") 319 c.Assert(err, IsNil) 320 _, err = tk.Se.InterDircute(context.Background(), "CREATE TABLE t (id bigint PRIMARY KEY, age int)") 321 c.Assert(err, IsNil) 322 _, err = tk.Se.InterDircute(context.Background(), "show create causet t") 323 c.Assert(err, IsNil) 324 id, _, _, err := tk.Se.PrepareStmt("CREATE TABLE t2(id bigint PRIMARY KEY, age int)") 325 c.Assert(err, IsNil) 326 params := []types.Causet{} 327 _, err = tk.Se.InterDircutePreparedStmt(context.Background(), id, params) 328 c.Assert(err, IsNil) 329 qs := tk.Se.Value(stochastikctx.QueryString) 330 c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") 331 332 // Test execution of DBS through the "InterDircute" interface. 333 _, err = tk.Se.InterDircute(context.Background(), "use test;") 334 c.Assert(err, IsNil) 335 _, err = tk.Se.InterDircute(context.Background(), "drop causet t2") 336 c.Assert(err, IsNil) 337 _, err = tk.Se.InterDircute(context.Background(), "prepare stmt from 'CREATE TABLE t2(id bigint PRIMARY KEY, age int)'") 338 c.Assert(err, IsNil) 339 _, err = tk.Se.InterDircute(context.Background(), "execute stmt") 340 c.Assert(err, IsNil) 341 qs = tk.Se.Value(stochastikctx.QueryString) 342 c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") 343 } 344 345 func (s *testStochastikSuite) TestAffectedRows(c *C) { 346 tk := testkit.NewTestKitWithInit(c, s.causetstore) 347 348 tk.MustInterDirc("drop causet if exists t") 349 tk.MustInterDirc("create causet t(id TEXT)") 350 tk.MustInterDirc(`INSERT INTO t VALUES ("a");`) 351 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 352 tk.MustInterDirc(`INSERT INTO t VALUES ("b");`) 353 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 354 tk.MustInterDirc(`UFIDelATE t set id = 'c' where id = 'a';`) 355 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 356 tk.MustInterDirc(`UFIDelATE t set id = 'a' where id = 'a';`) 357 c.Assert(int(tk.Se.AffectedRows()), Equals, 0) 358 tk.MustQuery(`SELECT * from t`).Check(testkit.Rows("c", "b")) 359 c.Assert(int(tk.Se.AffectedRows()), Equals, 0) 360 361 tk.MustInterDirc("drop causet if exists t") 362 tk.MustInterDirc("create causet t (id int, data int)") 363 tk.MustInterDirc(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) 364 tk.MustInterDirc(`UFIDelATE t set id = 1 where data = 0;`) 365 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 366 367 tk.MustInterDirc("drop causet if exists t") 368 tk.MustInterDirc("create causet t (id int, c1 timestamp);") 369 tk.MustInterDirc(`insert t values(1, 0);`) 370 tk.MustInterDirc(`UFIDelATE t set id = 1 where id = 1;`) 371 c.Assert(int(tk.Se.AffectedRows()), Equals, 0) 372 373 // With ON DUPLICATE KEY UFIDelATE, the affected-rows value per event is 1 if the event is inserted as a new event, 374 // 2 if an existing event is uFIDelated, and 0 if an existing event is set to its current values. 375 tk.MustInterDirc("drop causet if exists t") 376 tk.MustInterDirc("create causet t (c1 int PRIMARY KEY, c2 int);") 377 tk.MustInterDirc(`insert t values(1, 1);`) 378 tk.MustInterDirc(`insert into t values (1, 1) on duplicate key uFIDelate c2=2;`) 379 c.Assert(int(tk.Se.AffectedRows()), Equals, 2) 380 tk.MustInterDirc(`insert into t values (1, 1) on duplicate key uFIDelate c2=2;`) 381 c.Assert(int(tk.Se.AffectedRows()), Equals, 0) 382 tk.MustInterDirc("drop causet if exists test") 383 createALLEGROSQL := `CREATE TABLE test ( 384 id VARCHAR(36) PRIMARY KEY NOT NULL, 385 factor INTEGER NOT NULL DEFAULT 2);` 386 tk.MustInterDirc(createALLEGROSQL) 387 insertALLEGROSQL := `INSERT INTO test(id) VALUES('id') ON DUPLICATE KEY UFIDelATE factor=factor+3;` 388 tk.MustInterDirc(insertALLEGROSQL) 389 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 390 tk.MustInterDirc(insertALLEGROSQL) 391 c.Assert(int(tk.Se.AffectedRows()), Equals, 2) 392 tk.MustInterDirc(insertALLEGROSQL) 393 c.Assert(int(tk.Se.AffectedRows()), Equals, 2) 394 395 tk.Se.SetClientCapability(allegrosql.ClientFoundRows) 396 tk.MustInterDirc("drop causet if exists t") 397 tk.MustInterDirc("create causet t (id int, data int)") 398 tk.MustInterDirc(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) 399 tk.MustInterDirc(`UFIDelATE t set id = 1 where data = 0;`) 400 c.Assert(int(tk.Se.AffectedRows()), Equals, 2) 401 } 402 403 func (s *testStochastikSuite3) TestLastMessage(c *C) { 404 tk := testkit.NewTestKitWithInit(c, s.causetstore) 405 406 tk.MustInterDirc("drop causet if exists t") 407 tk.MustInterDirc("create causet t(id TEXT)") 408 409 // Insert 410 tk.MustInterDirc(`INSERT INTO t VALUES ("a");`) 411 tk.CheckLastMessage("") 412 tk.MustInterDirc(`INSERT INTO t VALUES ("b"), ("c");`) 413 tk.CheckLastMessage("Records: 2 Duplicates: 0 Warnings: 0") 414 415 // UFIDelate 416 tk.MustInterDirc(`UFIDelATE t set id = 'c' where id = 'a';`) 417 c.Assert(int(tk.Se.AffectedRows()), Equals, 1) 418 tk.CheckLastMessage("Rows matched: 1 Changed: 1 Warnings: 0") 419 tk.MustInterDirc(`UFIDelATE t set id = 'a' where id = 'a';`) 420 c.Assert(int(tk.Se.AffectedRows()), Equals, 0) 421 tk.CheckLastMessage("Rows matched: 0 Changed: 0 Warnings: 0") 422 423 // Replace 424 tk.MustInterDirc(`drop causet if exists t, t1; 425 create causet t (c1 int PRIMARY KEY, c2 int); 426 create causet t1 (a1 int, a2 int);`) 427 tk.MustInterDirc(`INSERT INTO t VALUES (1,1)`) 428 tk.MustInterDirc(`REPLACE INTO t VALUES (2,2)`) 429 tk.CheckLastMessage("") 430 tk.MustInterDirc(`INSERT INTO t1 VALUES (1,10), (3,30);`) 431 tk.CheckLastMessage("Records: 2 Duplicates: 0 Warnings: 0") 432 tk.MustInterDirc(`REPLACE INTO t SELECT * from t1`) 433 tk.CheckLastMessage("Records: 2 Duplicates: 1 Warnings: 0") 434 435 // Check insert with CLIENT_FOUND_ROWS is set 436 tk.Se.SetClientCapability(allegrosql.ClientFoundRows) 437 tk.MustInterDirc(`drop causet if exists t, t1; 438 create causet t (c1 int PRIMARY KEY, c2 int); 439 create causet t1 (a1 int, a2 int);`) 440 tk.MustInterDirc(`INSERT INTO t1 VALUES (1, 10), (2, 2), (3, 30);`) 441 tk.MustInterDirc(`INSERT INTO t1 VALUES (1, 10), (2, 20), (3, 30);`) 442 tk.MustInterDirc(`INSERT INTO t SELECT * FROM t1 ON DUPLICATE KEY UFIDelATE c2=a2;`) 443 tk.CheckLastMessage("Records: 6 Duplicates: 3 Warnings: 0") 444 } 445 446 // TestRowLock . See http://dev.allegrosql.com/doc/refman/5.7/en/commit.html. 447 func (s *testStochastikSuite) TestRowLock(c *C) { 448 tk := testkit.NewTestKitWithInit(c, s.causetstore) 449 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 450 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 451 452 tk.MustInterDirc("drop causet if exists t") 453 txn, err := tk.Se.Txn(true) 454 c.Assert(ekv.ErrInvalidTxn.Equal(err), IsTrue) 455 c.Assert(txn.Valid(), IsFalse) 456 tk.MustInterDirc("create causet t (c1 int, c2 int, c3 int)") 457 tk.MustInterDirc("insert t values (11, 2, 3)") 458 tk.MustInterDirc("insert t values (12, 2, 3)") 459 tk.MustInterDirc("insert t values (13, 2, 3)") 460 461 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 462 tk1.MustInterDirc("begin") 463 tk1.MustInterDirc("uFIDelate t set c2=21 where c1=11") 464 465 tk2.MustInterDirc("begin") 466 tk2.MustInterDirc("uFIDelate t set c2=211 where c1=11") 467 tk2.MustInterDirc("commit") 468 469 // tk1 will retry and the final value is 21 470 tk1.MustInterDirc("commit") 471 472 // Check the result is correct 473 tk.MustQuery("select c2 from t where c1=11").Check(testkit.Rows("21")) 474 475 tk1.MustInterDirc("begin") 476 tk1.MustInterDirc("uFIDelate t set c2=21 where c1=11") 477 478 tk2.MustInterDirc("begin") 479 tk2.MustInterDirc("uFIDelate t set c2=22 where c1=12") 480 tk2.MustInterDirc("commit") 481 482 tk1.MustInterDirc("commit") 483 } 484 485 // TestAutocommit . See https://dev.allegrosql.com/doc/internals/en/status-flags.html 486 func (s *testStochastikSuite) TestAutocommit(c *C) { 487 tk := testkit.NewTestKitWithInit(c, s.causetstore) 488 489 tk.MustInterDirc("drop causet if exists t;") 490 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 491 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 492 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 493 tk.MustInterDirc("insert t values ()") 494 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 495 tk.MustInterDirc("begin") 496 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 497 tk.MustInterDirc("insert t values ()") 498 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 499 tk.MustInterDirc("drop causet if exists t") 500 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 501 502 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 503 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 504 tk.MustInterDirc("set autocommit=0") 505 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Equals, 0) 506 tk.MustInterDirc("insert t values ()") 507 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Equals, 0) 508 tk.MustInterDirc("commit") 509 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Equals, 0) 510 tk.MustInterDirc("drop causet if exists t") 511 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Equals, 0) 512 tk.MustInterDirc("set autocommit='On'") 513 c.Assert(int(tk.Se.Status()&allegrosql.ServerStatusAutocommit), Greater, 0) 514 515 // When autocommit is 0, transaction start ts should be the first *valid* 516 // memex, rather than *any* memex. 517 tk.MustInterDirc("create causet t (id int)") 518 tk.MustInterDirc("set @@autocommit = 0") 519 tk.MustInterDirc("rollback") 520 tk.MustInterDirc("set @@autocommit = 0") 521 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 522 tk1.MustInterDirc("insert into t select 1") 523 tk.MustQuery("select * from t").Check(testkit.Rows("1")) 524 525 // TODO: MyALLEGROSQL compatibility for setting global variable. 526 // tk.MustInterDirc("begin") 527 // tk.MustInterDirc("insert into t values (42)") 528 // tk.MustInterDirc("set @@global.autocommit = 1") 529 // tk.MustInterDirc("rollback") 530 // tk.MustQuery("select count(*) from t where id = 42").Check(testkit.Rows("0")) 531 // Even the transaction is rollbacked, the set memex succeed. 532 // tk.MustQuery("select @@global.autocommit").Rows("1") 533 } 534 535 // TestTxnLazyInitialize tests that when autocommit = 0, not all memex starts 536 // a new transaction. 537 func (s *testStochastikSuite) TestTxnLazyInitialize(c *C) { 538 testTxnLazyInitialize(s, c, false) 539 testTxnLazyInitialize(s, c, true) 540 } 541 542 func testTxnLazyInitialize(s *testStochastikSuite, c *C, isPessimistic bool) { 543 tk := testkit.NewTestKitWithInit(c, s.causetstore) 544 tk.MustInterDirc("drop causet if exists t") 545 tk.MustInterDirc("create causet t (id int)") 546 if isPessimistic { 547 tk.MustInterDirc("set milevadb_txn_mode = 'pessimistic'") 548 } 549 550 tk.MustInterDirc("set @@autocommit = 0") 551 _, err := tk.Se.Txn(true) 552 c.Assert(ekv.ErrInvalidTxn.Equal(err), IsTrue) 553 txn, err := tk.Se.Txn(false) 554 c.Assert(err, IsNil) 555 c.Assert(txn.Valid(), IsFalse) 556 tk.MustQuery("select @@milevadb_current_ts").Check(testkit.Rows("0")) 557 tk.MustQuery("select @@milevadb_current_ts").Check(testkit.Rows("0")) 558 559 // Those memex should not start a new transaction automacally. 560 tk.MustQuery("select 1") 561 tk.MustQuery("select @@milevadb_current_ts").Check(testkit.Rows("0")) 562 563 tk.MustInterDirc("set @@milevadb_general_log = 0") 564 tk.MustQuery("select @@milevadb_current_ts").Check(testkit.Rows("0")) 565 566 tk.MustQuery("explain select * from t") 567 tk.MustQuery("select @@milevadb_current_ts").Check(testkit.Rows("0")) 568 569 // Begin memex should start a new transaction. 570 tk.MustInterDirc("begin") 571 txn, err = tk.Se.Txn(false) 572 c.Assert(err, IsNil) 573 c.Assert(txn.Valid(), IsTrue) 574 tk.MustInterDirc("rollback") 575 576 tk.MustInterDirc("select * from t") 577 txn, err = tk.Se.Txn(false) 578 c.Assert(err, IsNil) 579 c.Assert(txn.Valid(), IsTrue) 580 tk.MustInterDirc("rollback") 581 582 tk.MustInterDirc("insert into t values (1)") 583 txn, err = tk.Se.Txn(false) 584 c.Assert(err, IsNil) 585 c.Assert(txn.Valid(), IsTrue) 586 tk.MustInterDirc("rollback") 587 } 588 589 func (s *testStochastikSuite) TestGlobalVarAccessor(c *C) { 590 varName := "max_allowed_packet" 591 varValue := "67108864" // This is the default value for max_allowed_packet 592 varValue1 := "4194305" 593 varValue2 := "4194306" 594 595 tk := testkit.NewTestKitWithInit(c, s.causetstore) 596 se := tk.Se.(variable.GlobalVarAccessor) 597 // Get globalSysVar twice and get the same value 598 v, err := se.GetGlobalSysVar(varName) 599 c.Assert(err, IsNil) 600 c.Assert(v, Equals, varValue) 601 v, err = se.GetGlobalSysVar(varName) 602 c.Assert(err, IsNil) 603 c.Assert(v, Equals, varValue) 604 // Set global var to another value 605 err = se.SetGlobalSysVar(varName, varValue1) 606 c.Assert(err, IsNil) 607 v, err = se.GetGlobalSysVar(varName) 608 c.Assert(err, IsNil) 609 c.Assert(v, Equals, varValue1) 610 c.Assert(tk.Se.CommitTxn(context.TODO()), IsNil) 611 612 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 613 se1 := tk1.Se.(variable.GlobalVarAccessor) 614 v, err = se1.GetGlobalSysVar(varName) 615 c.Assert(err, IsNil) 616 c.Assert(v, Equals, varValue1) 617 err = se1.SetGlobalSysVar(varName, varValue2) 618 c.Assert(err, IsNil) 619 v, err = se1.GetGlobalSysVar(varName) 620 c.Assert(err, IsNil) 621 c.Assert(v, Equals, varValue2) 622 c.Assert(tk1.Se.CommitTxn(context.TODO()), IsNil) 623 624 // Make sure the change is visible to any client that accesses that global variable. 625 v, err = se.GetGlobalSysVar(varName) 626 c.Assert(err, IsNil) 627 c.Assert(v, Equals, varValue2) 628 629 // For issue 10955, make sure the new stochastik load `max_execution_time` into stochastikVars. 630 s.dom.GetGlobalVarsCache().Disable() 631 tk1.MustInterDirc("set @@global.max_execution_time = 100") 632 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 633 c.Assert(tk2.Se.GetStochastikVars().MaxInterDircutionTime, Equals, uint64(100)) 634 tk1.MustInterDirc("set @@global.max_execution_time = 0") 635 636 result := tk.MustQuery("show global variables where variable_name='sql_select_limit';") 637 result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) 638 result = tk.MustQuery("show stochastik variables where variable_name='sql_select_limit';") 639 result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) 640 tk.MustInterDirc("set stochastik sql_select_limit=100000000000;") 641 result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") 642 result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) 643 result = tk.MustQuery("show stochastik variables where variable_name='sql_select_limit';") 644 result.Check(testkit.Rows("sql_select_limit 100000000000")) 645 tk.MustInterDirc("set @@global.sql_select_limit = 1") 646 result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") 647 result.Check(testkit.Rows("sql_select_limit 1")) 648 tk.MustInterDirc("set @@global.sql_select_limit = default") 649 result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") 650 result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) 651 652 result = tk.MustQuery("select @@global.autocommit;") 653 result.Check(testkit.Rows("1")) 654 result = tk.MustQuery("select @@autocommit;") 655 result.Check(testkit.Rows("1")) 656 tk.MustInterDirc("set @@global.autocommit = 0;") 657 result = tk.MustQuery("select @@global.autocommit;") 658 result.Check(testkit.Rows("0")) 659 result = tk.MustQuery("select @@autocommit;") 660 result.Check(testkit.Rows("1")) 661 tk.MustInterDirc("set @@global.autocommit=1") 662 663 _, err = tk.InterDirc("set global time_zone = 'timezone'") 664 c.Assert(err, NotNil) 665 c.Assert(terror.ErrorEqual(err, variable.ErrUnknownTimeZone), IsTrue) 666 } 667 668 func (s *testStochastikSuite) TestGetSysVariables(c *C) { 669 tk := testkit.NewTestKitWithInit(c, s.causetstore) 670 671 // Test ScopeStochastik 672 tk.MustInterDirc("select @@warning_count") 673 tk.MustInterDirc("select @@stochastik.warning_count") 674 tk.MustInterDirc("select @@local.warning_count") 675 _, err := tk.InterDirc("select @@global.warning_count") 676 c.Assert(terror.ErrorEqual(err, variable.ErrIncorrectScope), IsTrue, Commentf("err %v", err)) 677 678 // Test ScopeGlobal 679 tk.MustInterDirc("select @@max_connections") 680 tk.MustInterDirc("select @@global.max_connections") 681 _, err = tk.InterDirc("select @@stochastik.max_connections") 682 c.Assert(terror.ErrorEqual(err, variable.ErrIncorrectScope), IsTrue, Commentf("err %v", err)) 683 _, err = tk.InterDirc("select @@local.max_connections") 684 c.Assert(terror.ErrorEqual(err, variable.ErrIncorrectScope), IsTrue, Commentf("err %v", err)) 685 686 // Test ScopeNone 687 tk.MustInterDirc("select @@performance_schema_max_mutex_classes") 688 tk.MustInterDirc("select @@global.performance_schema_max_mutex_classes") 689 // For issue 19524, test 690 tk.MustInterDirc("select @@stochastik.performance_schema_max_mutex_classes") 691 tk.MustInterDirc("select @@local.performance_schema_max_mutex_classes") 692 } 693 694 func (s *testStochastikSuite) TestRetryResetStmtCtx(c *C) { 695 tk := testkit.NewTestKitWithInit(c, s.causetstore) 696 tk.MustInterDirc("create causet retrytxn (a int unique, b int)") 697 tk.MustInterDirc("insert retrytxn values (1, 1)") 698 tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 699 tk.MustInterDirc("begin") 700 tk.MustInterDirc("uFIDelate retrytxn set b = b + 1 where a = 1") 701 702 // Make retryable error. 703 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 704 tk1.MustInterDirc("uFIDelate retrytxn set b = b + 1 where a = 1") 705 706 err := tk.Se.CommitTxn(context.TODO()) 707 c.Assert(err, IsNil) 708 c.Assert(tk.Se.AffectedRows(), Equals, uint64(1)) 709 } 710 711 func (s *testStochastikSuite) TestRetryCleanTxn(c *C) { 712 tk := testkit.NewTestKitWithInit(c, s.causetstore) 713 tk.MustInterDirc("create causet retrytxn (a int unique, b int)") 714 tk.MustInterDirc("insert retrytxn values (1, 1)") 715 tk.MustInterDirc("begin") 716 tk.MustInterDirc("uFIDelate retrytxn set b = b + 1 where a = 1") 717 718 // Make retryable error. 719 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 720 tk1.MustInterDirc("uFIDelate retrytxn set b = b + 1 where a = 1") 721 722 // Hijack retry history, add a memex that returns error. 723 history := stochastik.GetHistory(tk.Se) 724 stmtNode, err := BerolinaSQL.New().ParseOneStmt("insert retrytxn values (2, 'a')", "", "") 725 c.Assert(err, IsNil) 726 compiler := interlock.Compiler{Ctx: tk.Se} 727 stmt, _ := compiler.Compile(context.TODO(), stmtNode) 728 interlock.ResetContextOfStmt(tk.Se, stmtNode) 729 history.Add(stmt, tk.Se.GetStochastikVars().StmtCtx) 730 _, err = tk.InterDirc("commit") 731 c.Assert(err, NotNil) 732 txn, err := tk.Se.Txn(false) 733 c.Assert(err, IsNil) 734 c.Assert(txn.Valid(), IsFalse) 735 c.Assert(tk.Se.GetStochastikVars().InTxn(), IsFalse) 736 } 737 738 func (s *testStochastikSuite) TestReadOnlyNotInHistory(c *C) { 739 tk := testkit.NewTestKitWithInit(c, s.causetstore) 740 tk.MustInterDirc("create causet history (a int)") 741 tk.MustInterDirc("insert history values (1), (2), (3)") 742 tk.MustInterDirc("set @@autocommit = 0") 743 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 744 tk.MustQuery("select * from history") 745 history := stochastik.GetHistory(tk.Se) 746 c.Assert(history.Count(), Equals, 0) 747 748 tk.MustInterDirc("insert history values (4)") 749 tk.MustInterDirc("insert history values (5)") 750 c.Assert(history.Count(), Equals, 2) 751 tk.MustInterDirc("commit") 752 tk.MustQuery("select * from history") 753 history = stochastik.GetHistory(tk.Se) 754 c.Assert(history.Count(), Equals, 0) 755 } 756 757 func (s *testStochastikSuite) TestRetryUnion(c *C) { 758 tk := testkit.NewTestKitWithInit(c, s.causetstore) 759 tk.MustInterDirc("create causet history (a int)") 760 tk.MustInterDirc("insert history values (1), (2), (3)") 761 tk.MustInterDirc("set @@autocommit = 0") 762 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 763 // UNION should't be in retry history. 764 tk.MustQuery("(select * from history) union (select * from history)") 765 history := stochastik.GetHistory(tk.Se) 766 c.Assert(history.Count(), Equals, 0) 767 tk.MustQuery("(select * from history for uFIDelate) union (select * from history)") 768 tk.MustInterDirc("uFIDelate history set a = a + 1") 769 history = stochastik.GetHistory(tk.Se) 770 c.Assert(history.Count(), Equals, 2) 771 772 // Make retryable error. 773 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 774 tk1.MustInterDirc("uFIDelate history set a = a + 1") 775 776 _, err := tk.InterDirc("commit") 777 c.Assert(err, ErrorMatches, ".*can not retry select for uFIDelate memex") 778 } 779 780 func (s *testStochastikSuite) TestRetryShow(c *C) { 781 tk := testkit.NewTestKitWithInit(c, s.causetstore) 782 tk.MustInterDirc("set @@autocommit = 0") 783 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 784 // UNION should't be in retry history. 785 tk.MustQuery("show variables") 786 tk.MustQuery("show databases") 787 history := stochastik.GetHistory(tk.Se) 788 c.Assert(history.Count(), Equals, 0) 789 } 790 791 func (s *testStochastikSuite) TestNoRetryForCurrentTxn(c *C) { 792 tk := testkit.NewTestKitWithInit(c, s.causetstore) 793 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 794 tk.MustInterDirc("create causet history (a int)") 795 tk.MustInterDirc("insert history values (1)") 796 797 // Firstly, disable retry. 798 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 1") 799 tk.MustInterDirc("begin") 800 tk.MustInterDirc("uFIDelate history set a = 2") 801 // Enable retry now. 802 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 803 804 tk1.MustInterDirc("uFIDelate history set a = 3") 805 c.Assert(tk.InterDircToErr("commit"), NotNil) 806 } 807 808 func (s *testStochastikSuite) TestRetryForCurrentTxn(c *C) { 809 tk := testkit.NewTestKitWithInit(c, s.causetstore) 810 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 811 tk.MustInterDirc("create causet history (a int)") 812 tk.MustInterDirc("insert history values (1)") 813 814 // Firstly, enable retry. 815 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 816 tk.MustInterDirc("begin") 817 tk.MustInterDirc("uFIDelate history set a = 2") 818 // Disable retry now. 819 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 1") 820 821 tk1.MustInterDirc("uFIDelate history set a = 3") 822 tk.MustInterDirc("commit") 823 tk.MustQuery("select * from history").Check(testkit.Rows("2")) 824 } 825 826 // TestTruncateAlloc tests that the auto_increment ID does not reuse the old causet's allocator. 827 func (s *testStochastikSuite) TestTruncateAlloc(c *C) { 828 tk := testkit.NewTestKitWithInit(c, s.causetstore) 829 tk.MustInterDirc("create causet truncate_id (a int primary key auto_increment)") 830 tk.MustInterDirc("insert truncate_id values (), (), (), (), (), (), (), (), (), ()") 831 tk.MustInterDirc("truncate causet truncate_id") 832 tk.MustInterDirc("insert truncate_id values (), (), (), (), (), (), (), (), (), ()") 833 tk.MustQuery("select a from truncate_id where a > 11").Check(testkit.Rows()) 834 } 835 836 func (s *testStochastikSuite) TestString(c *C) { 837 tk := testkit.NewTestKitWithInit(c, s.causetstore) 838 tk.MustInterDirc("select 1") 839 // here to check the panic bug in String() when txn is nil after committed. 840 c.Log(tk.Se.String()) 841 } 842 843 func (s *testStochastikSuite) TestDatabase(c *C) { 844 tk := testkit.NewTestKitWithInit(c, s.causetstore) 845 846 // Test database. 847 tk.MustInterDirc("create database xxx") 848 tk.MustInterDirc("drop database xxx") 849 850 tk.MustInterDirc("drop database if exists xxx") 851 tk.MustInterDirc("create database xxx") 852 tk.MustInterDirc("create database if not exists xxx") 853 tk.MustInterDirc("drop database if exists xxx") 854 855 // Test schemaReplicant. 856 tk.MustInterDirc("create schemaReplicant xxx") 857 tk.MustInterDirc("drop schemaReplicant xxx") 858 859 tk.MustInterDirc("drop schemaReplicant if exists xxx") 860 tk.MustInterDirc("create schemaReplicant xxx") 861 tk.MustInterDirc("create schemaReplicant if not exists xxx") 862 tk.MustInterDirc("drop schemaReplicant if exists xxx") 863 } 864 865 func (s *testStochastikSuite) TestInterDircRestrictedALLEGROSQL(c *C) { 866 tk := testkit.NewTestKitWithInit(c, s.causetstore) 867 r, _, err := tk.Se.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate).InterDircRestrictedALLEGROSQL("select 1;") 868 c.Assert(err, IsNil) 869 c.Assert(len(r), Equals, 1) 870 } 871 872 // TestInTrans . See https://dev.allegrosql.com/doc/internals/en/status-flags.html 873 func (s *testStochastikSuite) TestInTrans(c *C) { 874 tk := testkit.NewTestKitWithInit(c, s.causetstore) 875 tk.MustInterDirc("drop causet if exists t;") 876 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 877 tk.MustInterDirc("insert t values ()") 878 tk.MustInterDirc("begin") 879 txn, err := tk.Se.Txn(true) 880 c.Assert(err, IsNil) 881 c.Assert(txn.Valid(), IsTrue) 882 tk.MustInterDirc("insert t values ()") 883 c.Assert(txn.Valid(), IsTrue) 884 tk.MustInterDirc("drop causet if exists t;") 885 c.Assert(txn.Valid(), IsFalse) 886 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 887 c.Assert(txn.Valid(), IsFalse) 888 tk.MustInterDirc("insert t values ()") 889 c.Assert(txn.Valid(), IsFalse) 890 tk.MustInterDirc("commit") 891 tk.MustInterDirc("insert t values ()") 892 893 tk.MustInterDirc("set autocommit=0") 894 tk.MustInterDirc("begin") 895 c.Assert(txn.Valid(), IsTrue) 896 tk.MustInterDirc("insert t values ()") 897 c.Assert(txn.Valid(), IsTrue) 898 tk.MustInterDirc("commit") 899 c.Assert(txn.Valid(), IsFalse) 900 tk.MustInterDirc("insert t values ()") 901 c.Assert(txn.Valid(), IsTrue) 902 tk.MustInterDirc("commit") 903 c.Assert(txn.Valid(), IsFalse) 904 905 tk.MustInterDirc("set autocommit=1") 906 tk.MustInterDirc("drop causet if exists t") 907 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 908 tk.MustInterDirc("begin") 909 c.Assert(txn.Valid(), IsTrue) 910 tk.MustInterDirc("insert t values ()") 911 c.Assert(txn.Valid(), IsTrue) 912 tk.MustInterDirc("rollback") 913 c.Assert(txn.Valid(), IsFalse) 914 } 915 916 func (s *testStochastikSuite) TestRetryPreparedStmt(c *C) { 917 tk := testkit.NewTestKitWithInit(c, s.causetstore) 918 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 919 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 920 921 tk.MustInterDirc("drop causet if exists t") 922 txn, err := tk.Se.Txn(true) 923 c.Assert(ekv.ErrInvalidTxn.Equal(err), IsTrue) 924 c.Assert(txn.Valid(), IsFalse) 925 tk.MustInterDirc("create causet t (c1 int, c2 int, c3 int)") 926 tk.MustInterDirc("insert t values (11, 2, 3)") 927 928 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 929 tk1.MustInterDirc("begin") 930 tk1.MustInterDirc("uFIDelate t set c2=? where c1=11;", 21) 931 932 tk2.MustInterDirc("begin") 933 tk2.MustInterDirc("uFIDelate t set c2=? where c1=11", 22) 934 tk2.MustInterDirc("commit") 935 936 tk1.MustInterDirc("commit") 937 938 tk.MustQuery("select c2 from t where c1=11").Check(testkit.Rows("21")) 939 } 940 941 func (s *testStochastikSuite) TestStochastik(c *C) { 942 tk := testkit.NewTestKitWithInit(c, s.causetstore) 943 tk.MustInterDirc("ROLLBACK;") 944 tk.Se.Close() 945 } 946 947 func (s *testStochastikSuite) TestStochastikAuth(c *C) { 948 tk := testkit.NewTestKitWithInit(c, s.causetstore) 949 c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "Any not exist username with zero password!", Hostname: "anyhost"}, []byte(""), []byte("")), IsFalse) 950 } 951 952 func (s *testStochastikSerialSuite) TestSkipWithGrant(c *C) { 953 tk := testkit.NewTestKitWithInit(c, s.causetstore) 954 save2 := privileges.SkipWithGrant 955 956 privileges.SkipWithGrant = false 957 c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "user_not_exist"}, []byte("yyy"), []byte("zzz")), IsFalse) 958 959 privileges.SkipWithGrant = true 960 c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "xxx", Hostname: `%`}, []byte("yyy"), []byte("zzz")), IsTrue) 961 c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: `%`}, []byte(""), []byte("")), IsTrue) 962 tk.MustInterDirc("create causet t (id int)") 963 tk.MustInterDirc("create role r_1") 964 tk.MustInterDirc("grant r_1 to root") 965 tk.MustInterDirc("set role all") 966 tk.MustInterDirc("show grants for root") 967 privileges.SkipWithGrant = save2 968 } 969 970 func (s *testStochastikSuite) TestLastInsertID(c *C) { 971 tk := testkit.NewTestKitWithInit(c, s.causetstore) 972 // insert 973 tk.MustInterDirc("create causet t (c1 int not null auto_increment, c2 int, PRIMARY KEY (c1))") 974 tk.MustInterDirc("insert into t set c2 = 11") 975 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("1")) 976 977 tk.MustInterDirc("insert into t (c2) values (22), (33), (44)") 978 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) 979 980 tk.MustInterDirc("insert into t (c1, c2) values (10, 55)") 981 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) 982 983 // replace 984 tk.MustInterDirc("replace t (c2) values(66)") 985 tk.MustQuery("select * from t").Check(testkit.Rows("1 11", "2 22", "3 33", "4 44", "10 55", "11 66")) 986 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("11")) 987 988 // uFIDelate 989 tk.MustInterDirc("uFIDelate t set c1=last_insert_id(c1 + 100)") 990 tk.MustQuery("select * from t").Check(testkit.Rows("101 11", "102 22", "103 33", "104 44", "110 55", "111 66")) 991 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("111")) 992 tk.MustInterDirc("insert into t (c2) values (77)") 993 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) 994 995 // drop 996 tk.MustInterDirc("drop causet t") 997 tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) 998 999 tk.MustInterDirc("create causet t (c2 int, c3 int, c1 int not null auto_increment, PRIMARY KEY (c1))") 1000 tk.MustInterDirc("insert into t set c2 = 30") 1001 1002 // insert values 1003 lastInsertID := tk.Se.LastInsertID() 1004 tk.MustInterDirc("prepare stmt1 from 'insert into t (c2) values (?)'") 1005 tk.MustInterDirc("set @v1=10") 1006 tk.MustInterDirc("set @v2=20") 1007 tk.MustInterDirc("execute stmt1 using @v1") 1008 tk.MustInterDirc("execute stmt1 using @v2") 1009 tk.MustInterDirc("deallocate prepare stmt1") 1010 currLastInsertID := tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1011 tk.MustQuery("select c1 from t where c2 = 20").Check(testkit.Rows(fmt.Sprint(currLastInsertID))) 1012 c.Assert(lastInsertID+2, Equals, currLastInsertID) 1013 } 1014 1015 func (s *testStochastikSuite) TestPrepareZero(c *C) { 1016 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1017 tk.MustInterDirc("drop causet if exists t") 1018 tk.MustInterDirc("create causet t(v timestamp)") 1019 tk.MustInterDirc("prepare s1 from 'insert into t (v) values (?)'") 1020 tk.MustInterDirc("set @v1='0'") 1021 _, rs := tk.InterDirc("execute s1 using @v1") 1022 c.Assert(rs, NotNil) 1023 tk.MustInterDirc("set @v2='" + types.ZeroDatetimeStr + "'") 1024 tk.MustInterDirc("execute s1 using @v2") 1025 tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00")) 1026 } 1027 1028 func (s *testStochastikSuite) TestPrimaryKeyAutoIncrement(c *C) { 1029 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1030 tk.MustInterDirc("drop causet if exists t") 1031 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(255) UNIQUE NOT NULL, status int)") 1032 tk.MustInterDirc("insert t (name) values (?)", "abc") 1033 id := tk.Se.LastInsertID() 1034 c.Check(id != 0, IsTrue) 1035 1036 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 1037 tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc <nil>", id))) 1038 1039 tk.MustInterDirc("uFIDelate t set name = 'abc', status = 1 where id = ?", id) 1040 tk1.MustQuery("select * from t").Check(testkit.Rows(fmt.Sprintf("%d abc 1", id))) 1041 1042 // Check for pass bool param to milevadb prepared memex 1043 tk.MustInterDirc("drop causet if exists t") 1044 tk.MustInterDirc("create causet t (id tinyint)") 1045 tk.MustInterDirc("insert t values (?)", true) 1046 tk.MustQuery("select * from t").Check(testkit.Rows("1")) 1047 } 1048 1049 func (s *testStochastikSuite) TestAutoIncrementID(c *C) { 1050 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1051 tk.MustInterDirc("drop causet if exists t") 1052 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 1053 tk.MustInterDirc("insert t values ()") 1054 tk.MustInterDirc("insert t values ()") 1055 tk.MustInterDirc("insert t values ()") 1056 tk.MustInterDirc("drop causet if exists t;") 1057 tk.MustInterDirc("create causet t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") 1058 tk.MustInterDirc("insert t values ()") 1059 lastID := tk.Se.LastInsertID() 1060 c.Assert(lastID, Less, uint64(4)) 1061 tk.MustInterDirc("insert t () values ()") 1062 c.Assert(tk.Se.LastInsertID(), Greater, lastID) 1063 lastID = tk.Se.LastInsertID() 1064 tk.MustInterDirc("insert t values (100)") 1065 c.Assert(tk.Se.LastInsertID(), Equals, uint64(100)) 1066 1067 // If the auto_increment column value is given, it uses the value of the latest event. 1068 tk.MustInterDirc("insert t values (120), (112)") 1069 c.Assert(tk.Se.LastInsertID(), Equals, uint64(112)) 1070 1071 // The last_insert_id function only use last auto-generated id. 1072 tk.MustQuery("select last_insert_id()").Check(testkit.Rows(fmt.Sprint(lastID))) 1073 1074 tk.MustInterDirc("drop causet if exists t") 1075 tk.MustInterDirc("create causet t (i tinyint unsigned not null auto_increment, primary key (i));") 1076 tk.MustInterDirc("insert into t set i = 254;") 1077 tk.MustInterDirc("insert t values ()") 1078 1079 // The last insert ID doesn't care about primary key, it is set even if its a normal index column. 1080 tk.MustInterDirc("create causet autoid (id int auto_increment, index (id))") 1081 tk.MustInterDirc("insert autoid values ()") 1082 c.Assert(tk.Se.LastInsertID(), Greater, uint64(0)) 1083 tk.MustInterDirc("insert autoid values (100)") 1084 c.Assert(tk.Se.LastInsertID(), Equals, uint64(100)) 1085 1086 tk.MustQuery("select last_insert_id(20)").Check(testkit.Rows(fmt.Sprint(20))) 1087 tk.MustQuery("select last_insert_id()").Check(testkit.Rows(fmt.Sprint(20))) 1088 1089 // Corner cases for unsigned bigint auto_increment DeferredCausets. 1090 tk.MustInterDirc("drop causet if exists autoid") 1091 tk.MustInterDirc("create causet autoid(`auto_inc_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,UNIQUE KEY `auto_inc_id` (`auto_inc_id`))") 1092 tk.MustInterDirc("insert into autoid values(9223372036854775808);") 1093 tk.MustInterDirc("insert into autoid values();") 1094 tk.MustInterDirc("insert into autoid values();") 1095 tk.MustQuery("select * from autoid").Check(testkit.Rows("9223372036854775808", "9223372036854775810", "9223372036854775812")) 1096 // In MilevaDB : _milevadb_rowid will also consume the autoID when the auto_increment column is not the primary key. 1097 // Using the MaxUint64 and MaxInt64 as the autoID upper limit like MyALLEGROSQL will cause _milevadb_rowid allocation fail here. 1098 _, err := tk.InterDirc("insert into autoid values(18446744073709551614)") 1099 c.Assert(terror.ErrorEqual(err, autoid.ErrAutoincReadFailed), IsTrue) 1100 _, err = tk.InterDirc("insert into autoid values()") 1101 c.Assert(terror.ErrorEqual(err, autoid.ErrAutoincReadFailed), IsTrue) 1102 // FixMe: MyALLEGROSQL works fine with the this allegrosql. 1103 _, err = tk.InterDirc("insert into autoid values(18446744073709551615)") 1104 c.Assert(terror.ErrorEqual(err, autoid.ErrAutoincReadFailed), IsTrue) 1105 1106 tk.MustInterDirc("drop causet if exists autoid") 1107 tk.MustInterDirc("create causet autoid(`auto_inc_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,UNIQUE KEY `auto_inc_id` (`auto_inc_id`))") 1108 tk.MustInterDirc("insert into autoid values()") 1109 tk.MustQuery("select * from autoid").Check(testkit.Rows("1")) 1110 tk.MustInterDirc("insert into autoid values(5000)") 1111 tk.MustQuery("select * from autoid").Check(testkit.Rows("1", "5000")) 1112 _, err = tk.InterDirc("uFIDelate autoid set auto_inc_id = 8000") 1113 c.Assert(terror.ErrorEqual(err, ekv.ErrKeyExists), IsTrue) 1114 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("1", "5000")) 1115 tk.MustInterDirc("uFIDelate autoid set auto_inc_id = 9000 where auto_inc_id=1") 1116 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("9000", "5000")) 1117 tk.MustInterDirc("insert into autoid values()") 1118 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("9000", "5000", "9001")) 1119 1120 // Corner cases for signed bigint auto_increment DeferredCausets. 1121 tk.MustInterDirc("drop causet if exists autoid") 1122 tk.MustInterDirc("create causet autoid(`auto_inc_id` bigint(20) NOT NULL AUTO_INCREMENT,UNIQUE KEY `auto_inc_id` (`auto_inc_id`))") 1123 // In MilevaDB : _milevadb_rowid will also consume the autoID when the auto_increment column is not the primary key. 1124 // Using the MaxUint64 and MaxInt64 as autoID upper limit like MyALLEGROSQL will cause insert fail if the values is 1125 // 9223372036854775806. Because _milevadb_rowid will be allocated 9223372036854775807 at same time. 1126 tk.MustInterDirc("insert into autoid values(9223372036854775805);") 1127 tk.MustQuery("select auto_inc_id, _milevadb_rowid from autoid use index()").Check(testkit.Rows("9223372036854775805 9223372036854775806")) 1128 _, err = tk.InterDirc("insert into autoid values();") 1129 c.Assert(terror.ErrorEqual(err, autoid.ErrAutoincReadFailed), IsTrue) 1130 tk.MustQuery("select auto_inc_id, _milevadb_rowid from autoid use index()").Check(testkit.Rows("9223372036854775805 9223372036854775806")) 1131 tk.MustQuery("select auto_inc_id, _milevadb_rowid from autoid use index(auto_inc_id)").Check(testkit.Rows("9223372036854775805 9223372036854775806")) 1132 1133 tk.MustInterDirc("drop causet if exists autoid") 1134 tk.MustInterDirc("create causet autoid(`auto_inc_id` bigint(20) NOT NULL AUTO_INCREMENT,UNIQUE KEY `auto_inc_id` (`auto_inc_id`))") 1135 tk.MustInterDirc("insert into autoid values()") 1136 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("1")) 1137 tk.MustInterDirc("insert into autoid values(5000)") 1138 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("1", "5000")) 1139 _, err = tk.InterDirc("uFIDelate autoid set auto_inc_id = 8000") 1140 c.Assert(terror.ErrorEqual(err, ekv.ErrKeyExists), IsTrue) 1141 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("1", "5000")) 1142 tk.MustInterDirc("uFIDelate autoid set auto_inc_id = 9000 where auto_inc_id=1") 1143 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("9000", "5000")) 1144 tk.MustInterDirc("insert into autoid values()") 1145 tk.MustQuery("select * from autoid use index()").Check(testkit.Rows("9000", "5000", "9001")) 1146 } 1147 1148 func (s *testStochastikSuite) TestAutoIncrementWithRetry(c *C) { 1149 // test for https://github.com/whtcorpsinc/milevadb/issues/827 1150 1151 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1152 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 1153 1154 tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1155 tk.MustInterDirc("create causet t (c2 int, c1 int not null auto_increment, PRIMARY KEY (c1))") 1156 tk.MustInterDirc("insert into t (c2) values (1), (2), (3), (4), (5)") 1157 1158 // insert values 1159 lastInsertID := tk.Se.LastInsertID() 1160 tk.MustInterDirc("begin") 1161 tk.MustInterDirc("insert into t (c2) values (11), (12), (13)") 1162 tk.MustQuery("select c1 from t where c2 = 11").Check(testkit.Rows("6")) 1163 tk.MustInterDirc("uFIDelate t set c2 = 33 where c2 = 1") 1164 1165 tk1.MustInterDirc("uFIDelate t set c2 = 22 where c2 = 1") 1166 1167 tk.MustInterDirc("commit") 1168 1169 tk.MustQuery("select c1 from t where c2 = 11").Check(testkit.Rows("6")) 1170 currLastInsertID := tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1171 c.Assert(lastInsertID+5, Equals, currLastInsertID) 1172 1173 // insert set 1174 lastInsertID = currLastInsertID 1175 tk.MustInterDirc("begin") 1176 tk.MustInterDirc("insert into t set c2 = 31") 1177 tk.MustQuery("select c1 from t where c2 = 31").Check(testkit.Rows("9")) 1178 tk.MustInterDirc("uFIDelate t set c2 = 44 where c2 = 2") 1179 1180 tk1.MustInterDirc("uFIDelate t set c2 = 55 where c2 = 2") 1181 1182 tk.MustInterDirc("commit") 1183 1184 tk.MustQuery("select c1 from t where c2 = 31").Check(testkit.Rows("9")) 1185 currLastInsertID = tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1186 c.Assert(lastInsertID+3, Equals, currLastInsertID) 1187 1188 // replace 1189 lastInsertID = currLastInsertID 1190 tk.MustInterDirc("begin") 1191 tk.MustInterDirc("insert into t (c2) values (21), (22), (23)") 1192 tk.MustQuery("select c1 from t where c2 = 21").Check(testkit.Rows("10")) 1193 tk.MustInterDirc("uFIDelate t set c2 = 66 where c2 = 3") 1194 1195 tk1.MustInterDirc("uFIDelate t set c2 = 77 where c2 = 3") 1196 1197 tk.MustInterDirc("commit") 1198 1199 tk.MustQuery("select c1 from t where c2 = 21").Check(testkit.Rows("10")) 1200 currLastInsertID = tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1201 c.Assert(lastInsertID+1, Equals, currLastInsertID) 1202 1203 // uFIDelate 1204 lastInsertID = currLastInsertID 1205 tk.MustInterDirc("begin") 1206 tk.MustInterDirc("insert into t set c2 = 41") 1207 tk.MustInterDirc("uFIDelate t set c1 = 0 where c2 = 41") 1208 tk.MustQuery("select c1 from t where c2 = 41").Check(testkit.Rows("0")) 1209 tk.MustInterDirc("uFIDelate t set c2 = 88 where c2 = 4") 1210 1211 tk1.MustInterDirc("uFIDelate t set c2 = 99 where c2 = 4") 1212 1213 tk.MustInterDirc("commit") 1214 1215 tk.MustQuery("select c1 from t where c2 = 41").Check(testkit.Rows("0")) 1216 currLastInsertID = tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1217 c.Assert(lastInsertID+3, Equals, currLastInsertID) 1218 1219 // prepare 1220 lastInsertID = currLastInsertID 1221 tk.MustInterDirc("begin") 1222 tk.MustInterDirc("prepare stmt from 'insert into t (c2) values (?)'") 1223 tk.MustInterDirc("set @v1=100") 1224 tk.MustInterDirc("set @v2=200") 1225 tk.MustInterDirc("set @v3=300") 1226 tk.MustInterDirc("execute stmt using @v1") 1227 tk.MustInterDirc("execute stmt using @v2") 1228 tk.MustInterDirc("execute stmt using @v3") 1229 tk.MustInterDirc("deallocate prepare stmt") 1230 tk.MustQuery("select c1 from t where c2 = 12").Check(testkit.Rows("7")) 1231 tk.MustInterDirc("uFIDelate t set c2 = 111 where c2 = 5") 1232 1233 tk1.MustInterDirc("uFIDelate t set c2 = 222 where c2 = 5") 1234 1235 tk.MustInterDirc("commit") 1236 1237 tk.MustQuery("select c1 from t where c2 = 12").Check(testkit.Rows("7")) 1238 currLastInsertID = tk.Se.GetStochastikVars().StmtCtx.PrevLastInsertID 1239 c.Assert(lastInsertID+3, Equals, currLastInsertID) 1240 } 1241 1242 func (s *testStochastikSuite) TestBinaryReadOnly(c *C) { 1243 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1244 tk.MustInterDirc("create causet t (i int key)") 1245 id, _, _, err := tk.Se.PrepareStmt("select i from t where i = ?") 1246 c.Assert(err, IsNil) 1247 id2, _, _, err := tk.Se.PrepareStmt("insert into t values (?)") 1248 c.Assert(err, IsNil) 1249 tk.MustInterDirc("set autocommit = 0") 1250 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 1251 _, err = tk.Se.InterDircutePreparedStmt(context.Background(), id, []types.Causet{types.NewCauset(1)}) 1252 c.Assert(err, IsNil) 1253 c.Assert(stochastik.GetHistory(tk.Se).Count(), Equals, 0) 1254 tk.MustInterDirc("insert into t values (1)") 1255 c.Assert(stochastik.GetHistory(tk.Se).Count(), Equals, 1) 1256 _, err = tk.Se.InterDircutePreparedStmt(context.Background(), id2, []types.Causet{types.NewCauset(2)}) 1257 c.Assert(err, IsNil) 1258 c.Assert(stochastik.GetHistory(tk.Se).Count(), Equals, 2) 1259 tk.MustInterDirc("commit") 1260 } 1261 1262 func (s *testStochastikSuite) TestPrepare(c *C) { 1263 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1264 tk.MustInterDirc("create causet t(id TEXT)") 1265 tk.MustInterDirc(`INSERT INTO t VALUES ("id");`) 1266 id, ps, _, err := tk.Se.PrepareStmt("select id+? from t") 1267 ctx := context.Background() 1268 c.Assert(err, IsNil) 1269 c.Assert(id, Equals, uint32(1)) 1270 c.Assert(ps, Equals, 1) 1271 tk.MustInterDirc(`set @a=1`) 1272 _, err = tk.Se.InterDircutePreparedStmt(ctx, id, []types.Causet{types.NewCauset("1")}) 1273 c.Assert(err, IsNil) 1274 err = tk.Se.DropPreparedStmt(id) 1275 c.Assert(err, IsNil) 1276 1277 tk.MustInterDirc("prepare stmt from 'select 1+?'") 1278 tk.MustInterDirc("set @v1=100") 1279 tk.MustQuery("execute stmt using @v1").Check(testkit.Rows("101")) 1280 1281 tk.MustInterDirc("set @v2=200") 1282 tk.MustQuery("execute stmt using @v2").Check(testkit.Rows("201")) 1283 1284 tk.MustInterDirc("set @v3=300") 1285 tk.MustQuery("execute stmt using @v3").Check(testkit.Rows("301")) 1286 tk.MustInterDirc("deallocate prepare stmt") 1287 1288 // InterDircute prepared memexs for more than one time. 1289 tk.MustInterDirc("create causet multiexec (a int, b int)") 1290 tk.MustInterDirc("insert multiexec values (1, 1), (2, 2)") 1291 id, _, _, err = tk.Se.PrepareStmt("select a from multiexec where b = ? order by b") 1292 c.Assert(err, IsNil) 1293 rs, err := tk.Se.InterDircutePreparedStmt(ctx, id, []types.Causet{types.NewCauset(1)}) 1294 c.Assert(err, IsNil) 1295 rs.Close() 1296 rs, err = tk.Se.InterDircutePreparedStmt(ctx, id, []types.Causet{types.NewCauset(2)}) 1297 rs.Close() 1298 c.Assert(err, IsNil) 1299 } 1300 1301 func (s *testStochastikSuite2) TestSpecifyIndexPrefixLength(c *C) { 1302 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1303 1304 _, err := tk.InterDirc("create causet t (c1 char, index(c1(3)));") 1305 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1306 c.Assert(err, NotNil) 1307 1308 _, err = tk.InterDirc("create causet t (c1 int, index(c1(3)));") 1309 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1310 c.Assert(err, NotNil) 1311 1312 _, err = tk.InterDirc("create causet t (c1 bit(10), index(c1(3)));") 1313 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1314 c.Assert(err, NotNil) 1315 1316 tk.MustInterDirc("create causet t (c1 char, c2 int, c3 bit(10));") 1317 1318 _, err = tk.InterDirc("create index idx_c1 on t (c1(3));") 1319 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1320 c.Assert(err, NotNil) 1321 1322 _, err = tk.InterDirc("create index idx_c1 on t (c2(3));") 1323 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1324 c.Assert(err, NotNil) 1325 1326 _, err = tk.InterDirc("create index idx_c1 on t (c3(3));") 1327 // ERROR 1089 (HY000): Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys 1328 c.Assert(err, NotNil) 1329 1330 tk.MustInterDirc("drop causet if exists t;") 1331 1332 _, err = tk.InterDirc("create causet t (c1 int, c2 blob, c3 varchar(64), index(c2));") 1333 // ERROR 1170 (42000): BLOB/TEXT column 'c2' used in key specification without a key length 1334 c.Assert(err, NotNil) 1335 1336 tk.MustInterDirc("create causet t (c1 int, c2 blob, c3 varchar(64));") 1337 _, err = tk.InterDirc("create index idx_c1 on t (c2);") 1338 // ERROR 1170 (42000): BLOB/TEXT column 'c2' used in key specification without a key length 1339 c.Assert(err, NotNil) 1340 1341 _, err = tk.InterDirc("create index idx_c1 on t (c2(555555));") 1342 // ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes 1343 c.Assert(err, NotNil) 1344 1345 _, err = tk.InterDirc("create index idx_c1 on t (c1(5))") 1346 // ERROR 1089 (HY000): Incorrect prefix key; 1347 // the used key part isn't a string, the used length is longer than the key part, 1348 // or the storage engine doesn't support unique prefix keys 1349 c.Assert(err, NotNil) 1350 1351 tk.MustInterDirc("create index idx_c1 on t (c1);") 1352 tk.MustInterDirc("create index idx_c2 on t (c2(3));") 1353 tk.MustInterDirc("create unique index idx_c3 on t (c3(5));") 1354 1355 tk.MustInterDirc("insert into t values (3, 'abc', 'def');") 1356 tk.MustQuery("select c2 from t where c2 = 'abc';").Check(testkit.Rows("abc")) 1357 1358 tk.MustInterDirc("insert into t values (4, 'abcd', 'xxx');") 1359 tk.MustInterDirc("insert into t values (4, 'abcf', 'yyy');") 1360 tk.MustQuery("select c2 from t where c2 = 'abcf';").Check(testkit.Rows("abcf")) 1361 tk.MustQuery("select c2 from t where c2 = 'abcd';").Check(testkit.Rows("abcd")) 1362 1363 tk.MustInterDirc("insert into t values (4, 'ignore', 'abcdeXXX');") 1364 _, err = tk.InterDirc("insert into t values (5, 'ignore', 'abcdeYYY');") 1365 // ERROR 1062 (23000): Duplicate entry 'abcde' for key 'idx_c3' 1366 c.Assert(err, NotNil) 1367 tk.MustQuery("select c3 from t where c3 = 'abcde';").Check(testkit.Rows()) 1368 1369 tk.MustInterDirc("delete from t where c3 = 'abcdeXXX';") 1370 tk.MustInterDirc("delete from t where c2 = 'abc';") 1371 1372 tk.MustQuery("select c2 from t where c2 > 'abcd';").Check(testkit.Rows("abcf")) 1373 tk.MustQuery("select c2 from t where c2 < 'abcf';").Check(testkit.Rows("abcd")) 1374 tk.MustQuery("select c2 from t where c2 >= 'abcd';").Check(testkit.Rows("abcd", "abcf")) 1375 tk.MustQuery("select c2 from t where c2 <= 'abcf';").Check(testkit.Rows("abcd", "abcf")) 1376 tk.MustQuery("select c2 from t where c2 != 'abc';").Check(testkit.Rows("abcd", "abcf")) 1377 tk.MustQuery("select c2 from t where c2 != 'abcd';").Check(testkit.Rows("abcf")) 1378 1379 tk.MustInterDirc("drop causet if exists t1;") 1380 tk.MustInterDirc("create causet t1 (a int, b char(255), key(a, b(20)));") 1381 tk.MustInterDirc("insert into t1 values (0, '1');") 1382 tk.MustInterDirc("uFIDelate t1 set b = b + 1 where a = 0;") 1383 tk.MustQuery("select b from t1 where a = 0;").Check(testkit.Rows("2")) 1384 1385 // test union index. 1386 tk.MustInterDirc("drop causet if exists t;") 1387 tk.MustInterDirc("create causet t (a text, b text, c int, index (a(3), b(3), c));") 1388 tk.MustInterDirc("insert into t values ('abc', 'abcd', 1);") 1389 tk.MustInterDirc("insert into t values ('abcx', 'abcf', 2);") 1390 tk.MustInterDirc("insert into t values ('abcy', 'abcf', 3);") 1391 tk.MustInterDirc("insert into t values ('bbc', 'abcd', 4);") 1392 tk.MustInterDirc("insert into t values ('bbcz', 'abcd', 5);") 1393 tk.MustInterDirc("insert into t values ('cbck', 'abd', 6);") 1394 tk.MustQuery("select c from t where a = 'abc' and b <= 'abc';").Check(testkit.Rows()) 1395 tk.MustQuery("select c from t where a = 'abc' and b <= 'abd';").Check(testkit.Rows("1")) 1396 tk.MustQuery("select c from t where a < 'cbc' and b > 'abcd';").Check(testkit.Rows("2", "3")) 1397 tk.MustQuery("select c from t where a <= 'abd' and b > 'abc';").Check(testkit.Rows("1", "2", "3")) 1398 tk.MustQuery("select c from t where a < 'bbcc' and b = 'abcd';").Check(testkit.Rows("1", "4")) 1399 tk.MustQuery("select c from t where a > 'bbcf';").Check(testkit.Rows("5", "6")) 1400 } 1401 1402 func (s *testStochastikSuite) TestResultField(c *C) { 1403 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1404 tk.MustInterDirc("create causet t (id int);") 1405 1406 tk.MustInterDirc(`INSERT INTO t VALUES (1);`) 1407 tk.MustInterDirc(`INSERT INTO t VALUES (2);`) 1408 r, err := tk.InterDirc(`SELECT count(*) from t;`) 1409 c.Assert(err, IsNil) 1410 fields := r.Fields() 1411 c.Assert(err, IsNil) 1412 c.Assert(len(fields), Equals, 1) 1413 field := fields[0].DeferredCauset 1414 c.Assert(field.Tp, Equals, allegrosql.TypeLonglong) 1415 c.Assert(field.Flen, Equals, 21) 1416 } 1417 1418 func (s *testStochastikSuite) TestResultType(c *C) { 1419 // Testcase for https://github.com/whtcorpsinc/milevadb/issues/325 1420 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1421 rs, err := tk.InterDirc(`select cast(null as char(30))`) 1422 c.Assert(err, IsNil) 1423 req := rs.NewChunk() 1424 err = rs.Next(context.Background(), req) 1425 c.Assert(err, IsNil) 1426 c.Assert(req.GetRow(0).IsNull(0), IsTrue) 1427 c.Assert(rs.Fields()[0].DeferredCauset.FieldType.Tp, Equals, allegrosql.TypeVarString) 1428 } 1429 1430 func (s *testStochastikSuite) TestFieldText(c *C) { 1431 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1432 tk.MustInterDirc("create causet t (a int)") 1433 tests := []struct { 1434 allegrosql string 1435 field string 1436 }{ 1437 {"select distinct(a) from t", "a"}, 1438 {"select (1)", "1"}, 1439 {"select (1+1)", "(1+1)"}, 1440 {"select a from t", "a"}, 1441 {"select ((a+1)) from t", "((a+1))"}, 1442 {"select 1 /*!32301 +1 */;", "1 +1 "}, 1443 {"select /*!32301 1 +1 */;", "1 +1 "}, 1444 {"/*!32301 select 1 +1 */;", "1 +1 "}, 1445 {"select 1 + /*!32301 1 +1 */;", "1 + 1 +1 "}, 1446 {"select 1 /*!32301 + 1, 1 */;", "1 + 1"}, 1447 {"select /*!32301 1, 1 +1 */;", "1"}, 1448 {"select /*!32301 1 + 1, */ +1;", "1 + 1"}, 1449 } 1450 for _, tt := range tests { 1451 result, err := tk.InterDirc(tt.allegrosql) 1452 c.Assert(err, IsNil) 1453 c.Assert(result.Fields()[0].DeferredCausetAsName.O, Equals, tt.field) 1454 } 1455 } 1456 1457 func (s *testStochastikSuite3) TestIndexMaxLength(c *C) { 1458 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1459 tk.MustInterDirc("create database test_index_max_length") 1460 tk.MustInterDirc("use test_index_max_length") 1461 1462 // create simple index at causet creation 1463 tk.MustGetErrCode("create causet t (c1 varchar(3073), index(c1)) charset = ascii;", allegrosql.ErrTooLongKey) 1464 1465 // create simple index after causet creation 1466 tk.MustInterDirc("create causet t (c1 varchar(3073)) charset = ascii;") 1467 tk.MustGetErrCode("create index idx_c1 on t(c1) ", allegrosql.ErrTooLongKey) 1468 tk.MustInterDirc("drop causet t;") 1469 1470 // create compound index at causet creation 1471 tk.MustGetErrCode("create causet t (c1 varchar(3072), c2 varchar(1), index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1472 tk.MustGetErrCode("create causet t (c1 varchar(3072), c2 char(1), index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1473 tk.MustGetErrCode("create causet t (c1 varchar(3072), c2 char, index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1474 tk.MustGetErrCode("create causet t (c1 varchar(3072), c2 date, index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1475 tk.MustGetErrCode("create causet t (c1 varchar(3069), c2 timestamp(1), index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1476 1477 tk.MustInterDirc("create causet t (c1 varchar(3068), c2 bit(26), index(c1, c2)) charset = ascii;") // 26 bit = 4 bytes 1478 tk.MustInterDirc("drop causet t;") 1479 tk.MustInterDirc("create causet t (c1 varchar(3068), c2 bit(32), index(c1, c2)) charset = ascii;") // 32 bit = 4 bytes 1480 tk.MustInterDirc("drop causet t;") 1481 tk.MustGetErrCode("create causet t (c1 varchar(3068), c2 bit(33), index(c1, c2)) charset = ascii;", allegrosql.ErrTooLongKey) 1482 1483 // create compound index after causet creation 1484 tk.MustInterDirc("create causet t (c1 varchar(3072), c2 varchar(1)) charset = ascii;") 1485 tk.MustGetErrCode("create index idx_c1_c2 on t(c1, c2);", allegrosql.ErrTooLongKey) 1486 tk.MustInterDirc("drop causet t;") 1487 1488 tk.MustInterDirc("create causet t (c1 varchar(3072), c2 char(1)) charset = ascii;") 1489 tk.MustGetErrCode("create index idx_c1_c2 on t(c1, c2);", allegrosql.ErrTooLongKey) 1490 tk.MustInterDirc("drop causet t;") 1491 1492 tk.MustInterDirc("create causet t (c1 varchar(3072), c2 char) charset = ascii;") 1493 tk.MustGetErrCode("create index idx_c1_c2 on t(c1, c2);", allegrosql.ErrTooLongKey) 1494 tk.MustInterDirc("drop causet t;") 1495 1496 tk.MustInterDirc("create causet t (c1 varchar(3072), c2 date) charset = ascii;") 1497 tk.MustGetErrCode("create index idx_c1_c2 on t(c1, c2);", allegrosql.ErrTooLongKey) 1498 tk.MustInterDirc("drop causet t;") 1499 1500 tk.MustInterDirc("create causet t (c1 varchar(3069), c2 timestamp(1)) charset = ascii;") 1501 tk.MustGetErrCode("create index idx_c1_c2 on t(c1, c2);", allegrosql.ErrTooLongKey) 1502 tk.MustInterDirc("drop causet t;") 1503 1504 // Test charsets other than `ascii`. 1505 assertCharsetLimit := func(charset string, bytesPerChar int) { 1506 base := 3072 / bytesPerChar 1507 tk.MustGetErrCode(fmt.Sprintf("create causet t (a varchar(%d) primary key) charset=%s", base+1, charset), allegrosql.ErrTooLongKey) 1508 tk.MustInterDirc(fmt.Sprintf("create causet t (a varchar(%d) primary key) charset=%s", base, charset)) 1509 tk.MustInterDirc("drop causet if exists t") 1510 } 1511 assertCharsetLimit("binary", 1) 1512 assertCharsetLimit("latin1", 1) 1513 assertCharsetLimit("utf8", 3) 1514 assertCharsetLimit("utf8mb4", 4) 1515 1516 // Test types bit length limit. 1517 assertTypeLimit := func(tp string, limitBitLength int) { 1518 base := 3072 - limitBitLength 1519 tk.MustGetErrCode(fmt.Sprintf("create causet t (a blob(10000), b %s, index idx(a(%d), b))", tp, base+1), allegrosql.ErrTooLongKey) 1520 tk.MustInterDirc(fmt.Sprintf("create causet t (a blob(10000), b %s, index idx(a(%d), b))", tp, base)) 1521 tk.MustInterDirc("drop causet if exists t") 1522 } 1523 1524 assertTypeLimit("tinyint", 1) 1525 assertTypeLimit("smallint", 2) 1526 assertTypeLimit("mediumint", 3) 1527 assertTypeLimit("int", 4) 1528 assertTypeLimit("integer", 4) 1529 assertTypeLimit("bigint", 8) 1530 assertTypeLimit("float", 4) 1531 assertTypeLimit("float(24)", 4) 1532 assertTypeLimit("float(25)", 8) 1533 assertTypeLimit("decimal(9)", 4) 1534 assertTypeLimit("decimal(10)", 5) 1535 assertTypeLimit("decimal(17)", 8) 1536 assertTypeLimit("year", 1) 1537 assertTypeLimit("date", 3) 1538 assertTypeLimit("time", 3) 1539 assertTypeLimit("datetime", 8) 1540 assertTypeLimit("timestamp", 4) 1541 } 1542 1543 func (s *testStochastikSuite2) TestIndexDeferredCausetLength(c *C) { 1544 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1545 tk.MustInterDirc("create causet t (c1 int, c2 blob);") 1546 tk.MustInterDirc("create index idx_c1 on t(c1);") 1547 tk.MustInterDirc("create index idx_c2 on t(c2(6));") 1548 1549 is := s.dom.SchemaReplicant() 1550 tab, err2 := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 1551 c.Assert(err2, Equals, nil) 1552 1553 idxC1DefCauss := blocks.FindIndexByDefCausName(tab, "c1").Meta().DeferredCausets 1554 c.Assert(idxC1DefCauss[0].Length, Equals, types.UnspecifiedLength) 1555 1556 idxC2DefCauss := blocks.FindIndexByDefCausName(tab, "c2").Meta().DeferredCausets 1557 c.Assert(idxC2DefCauss[0].Length, Equals, 6) 1558 } 1559 1560 func (s *testStochastikSuite2) TestIgnoreForeignKey(c *C) { 1561 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1562 sqlText := `CREATE TABLE address ( 1563 id bigint(20) NOT NULL AUTO_INCREMENT, 1564 user_id bigint(20) NOT NULL, 1565 PRIMARY KEY (id), 1566 CONSTRAINT FK_7rod8a71yep5vxasb0ms3osbg FOREIGN KEY (user_id) REFERENCES waimaiqa.user (id), 1567 INDEX FK_7rod8a71yep5vxasb0ms3osbg (user_id) comment '' 1568 ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT=COMPACT COMMENT='' CHECKSUM=0 DELAY_KEY_WRITE=0;` 1569 tk.MustInterDirc(sqlText) 1570 } 1571 1572 // TestISDeferredCausets tests information_schema.columns. 1573 func (s *testStochastikSuite3) TestISDeferredCausets(c *C) { 1574 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1575 tk.MustInterDirc("select ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS;") 1576 tk.MustQuery("SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.CHARACTER_SETS WHERE CHARACTER_SET_NAME = 'utf8mb4'").Check(testkit.Rows("utf8mb4")) 1577 } 1578 1579 func (s *testStochastikSuite2) TestRetry(c *C) { 1580 // For https://github.com/whtcorpsinc/milevadb/issues/571 1581 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1582 1583 tk.MustInterDirc("begin") 1584 tk.MustInterDirc("drop causet if exists t") 1585 tk.MustInterDirc("create causet t (c int)") 1586 tk.MustInterDirc("insert t values (1), (2), (3)") 1587 tk.MustInterDirc("commit") 1588 1589 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 1590 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 1591 tk3 := testkit.NewTestKitWithInit(c, s.causetstore) 1592 tk3.MustInterDirc("SET SESSION autocommit=0;") 1593 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1594 tk2.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1595 tk3.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1596 1597 var wg sync.WaitGroup 1598 wg.Add(3) 1599 f1 := func() { 1600 defer wg.Done() 1601 for i := 0; i < 30; i++ { 1602 tk1.MustInterDirc("uFIDelate t set c = 1;") 1603 } 1604 } 1605 f2 := func() { 1606 defer wg.Done() 1607 for i := 0; i < 30; i++ { 1608 tk2.MustInterDirc("uFIDelate t set c = ?;", 1) 1609 } 1610 } 1611 f3 := func() { 1612 defer wg.Done() 1613 for i := 0; i < 30; i++ { 1614 tk3.MustInterDirc("begin") 1615 tk3.MustInterDirc("uFIDelate t set c = 1;") 1616 tk3.MustInterDirc("commit") 1617 } 1618 } 1619 go f1() 1620 go f2() 1621 go f3() 1622 wg.Wait() 1623 } 1624 1625 func (s *testStochastikSuite3) TestMultiStmts(c *C) { 1626 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1627 tk.MustInterDirc("drop causet if exists t1; create causet t1(id int ); insert into t1 values (1);") 1628 tk.MustQuery("select * from t1;").Check(testkit.Rows("1")) 1629 } 1630 1631 func (s *testStochastikSuite2) TestLastInterDircuteDBSFlag(c *C) { 1632 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1633 tk.MustInterDirc("drop causet if exists t1") 1634 tk.MustInterDirc("create causet t1(id int)") 1635 c.Assert(tk.Se.Value(stochastikctx.LastInterDircuteDBS), NotNil) 1636 tk.MustInterDirc("insert into t1 values (1)") 1637 c.Assert(tk.Se.Value(stochastikctx.LastInterDircuteDBS), IsNil) 1638 } 1639 1640 func (s *testStochastikSuite3) TestDecimal(c *C) { 1641 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1642 1643 tk.MustInterDirc("drop causet if exists t;") 1644 tk.MustInterDirc("create causet t (a decimal unique);") 1645 tk.MustInterDirc("insert t values ('100');") 1646 _, err := tk.InterDirc("insert t values ('1e2');") 1647 c.Check(err, NotNil) 1648 } 1649 1650 func (s *testStochastikSuite2) TestBerolinaSQL(c *C) { 1651 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1652 1653 // test for https://github.com/whtcorpsinc/milevadb/pull/177 1654 tk.MustInterDirc("CREATE TABLE `t1` ( `a` char(3) NOT NULL default '', `b` char(3) NOT NULL default '', `c` char(3) NOT NULL default '', PRIMARY KEY (`a`,`b`,`c`)) ENGINE=InnoDB;") 1655 tk.MustInterDirc("CREATE TABLE `t2` ( `a` char(3) NOT NULL default '', `b` char(3) NOT NULL default '', `c` char(3) NOT NULL default '', PRIMARY KEY (`a`,`b`,`c`)) ENGINE=InnoDB;") 1656 tk.MustInterDirc(`INSERT INTO t1 VALUES (1,1,1);`) 1657 tk.MustInterDirc(`INSERT INTO t2 VALUES (1,1,1);`) 1658 tk.MustInterDirc(`PREPARE my_stmt FROM "SELECT t1.b, count(*) FROM t1 group by t1.b having count(*) > ALL (SELECT COUNT(*) FROM t2 WHERE t2.a=1 GROUP By t2.b)";`) 1659 tk.MustInterDirc(`EXECUTE my_stmt;`) 1660 tk.MustInterDirc(`EXECUTE my_stmt;`) 1661 tk.MustInterDirc(`deallocate prepare my_stmt;`) 1662 tk.MustInterDirc(`drop causet t1,t2;`) 1663 } 1664 1665 func (s *testStochastikSuite3) TestOnDuplicate(c *C) { 1666 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1667 1668 // test for https://github.com/whtcorpsinc/milevadb/pull/454 1669 tk.MustInterDirc("drop causet if exists t") 1670 tk.MustInterDirc("drop causet if exists t1") 1671 tk.MustInterDirc("create causet t1 (c1 int, c2 int, c3 int);") 1672 tk.MustInterDirc("insert into t1 set c1=1, c2=2, c3=1;") 1673 tk.MustInterDirc("create causet t (c1 int, c2 int, c3 int, primary key (c1));") 1674 tk.MustInterDirc("insert into t set c1=1, c2=4;") 1675 tk.MustInterDirc("insert into t select * from t1 limit 1 on duplicate key uFIDelate c3=3333;") 1676 } 1677 1678 func (s *testStochastikSuite2) TestReplace(c *C) { 1679 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1680 1681 // test for https://github.com/whtcorpsinc/milevadb/pull/456 1682 tk.MustInterDirc("drop causet if exists t") 1683 tk.MustInterDirc("drop causet if exists t1") 1684 tk.MustInterDirc("create causet t1 (c1 int, c2 int, c3 int);") 1685 tk.MustInterDirc("replace into t1 set c1=1, c2=2, c3=1;") 1686 tk.MustInterDirc("create causet t (c1 int, c2 int, c3 int, primary key (c1));") 1687 tk.MustInterDirc("replace into t set c1=1, c2=4;") 1688 tk.MustInterDirc("replace into t select * from t1 limit 1;") 1689 } 1690 1691 func (s *testStochastikSuite3) TestDelete(c *C) { 1692 // test for https://github.com/whtcorpsinc/milevadb/pull/1135 1693 1694 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1695 tk1 := testkit.NewTestKit(c, s.causetstore) 1696 tk1.MustInterDirc("create database test1") 1697 tk1.MustInterDirc("use test1") 1698 tk1.MustInterDirc("create causet t (F1 VARCHAR(30));") 1699 tk1.MustInterDirc("insert into t (F1) values ('1'), ('4');") 1700 1701 tk.MustInterDirc("create causet t (F1 VARCHAR(30));") 1702 tk.MustInterDirc("insert into t (F1) values ('1'), ('2');") 1703 tk.MustInterDirc("delete m1 from t m2,t m1 where m1.F1>1;") 1704 tk.MustQuery("select * from t;").Check(testkit.Rows("1")) 1705 1706 tk.MustInterDirc("drop causet if exists t") 1707 tk.MustInterDirc("create causet t (F1 VARCHAR(30));") 1708 tk.MustInterDirc("insert into t (F1) values ('1'), ('2');") 1709 tk.MustInterDirc("delete m1 from t m1,t m2 where true and m1.F1<2;") 1710 tk.MustQuery("select * from t;").Check(testkit.Rows("2")) 1711 1712 tk.MustInterDirc("drop causet if exists t") 1713 tk.MustInterDirc("create causet t (F1 VARCHAR(30));") 1714 tk.MustInterDirc("insert into t (F1) values ('1'), ('2');") 1715 tk.MustInterDirc("delete m1 from t m1,t m2 where false;") 1716 tk.MustQuery("select * from t;").Check(testkit.Rows("1", "2")) 1717 1718 tk.MustInterDirc("drop causet if exists t") 1719 tk.MustInterDirc("create causet t (F1 VARCHAR(30));") 1720 tk.MustInterDirc("insert into t (F1) values ('1'), ('2');") 1721 tk.MustInterDirc("delete m1, m2 from t m1,t m2 where m1.F1>m2.F1;") 1722 tk.MustQuery("select * from t;").Check(testkit.Rows()) 1723 1724 tk.MustInterDirc("drop causet if exists t") 1725 tk.MustInterDirc("create causet t (F1 VARCHAR(30));") 1726 tk.MustInterDirc("insert into t (F1) values ('1'), ('2');") 1727 tk.MustInterDirc("delete test1.t from test1.t inner join test.t where test1.t.F1 > test.t.F1") 1728 tk1.MustQuery("select * from t;").Check(testkit.Rows("1")) 1729 } 1730 1731 func (s *testStochastikSuite2) TestResetCtx(c *C) { 1732 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1733 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 1734 1735 tk.MustInterDirc("create causet t (i int auto_increment not null key);") 1736 tk.MustInterDirc("insert into t values (1);") 1737 tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1738 tk.MustInterDirc("begin;") 1739 tk.MustInterDirc("insert into t values (10);") 1740 tk.MustInterDirc("uFIDelate t set i = i + row_count();") 1741 tk.MustQuery("select * from t;").Check(testkit.Rows("2", "11")) 1742 1743 tk1.MustInterDirc("uFIDelate t set i = 0 where i = 1;") 1744 tk1.MustQuery("select * from t;").Check(testkit.Rows("0")) 1745 1746 tk.MustInterDirc("commit;") 1747 tk.MustQuery("select * from t;").Check(testkit.Rows("1", "11")) 1748 1749 tk.MustInterDirc("delete from t where i = 11;") 1750 tk.MustInterDirc("begin;") 1751 tk.MustInterDirc("insert into t values ();") 1752 tk.MustInterDirc("uFIDelate t set i = i + last_insert_id() + 1;") 1753 tk.MustQuery("select * from t;").Check(testkit.Rows("14", "25")) 1754 1755 tk1.MustInterDirc("uFIDelate t set i = 0 where i = 1;") 1756 tk1.MustQuery("select * from t;").Check(testkit.Rows("0")) 1757 1758 tk.MustInterDirc("commit;") 1759 tk.MustQuery("select * from t;").Check(testkit.Rows("13", "25")) 1760 } 1761 1762 func (s *testStochastikSuite3) TestUnique(c *C) { 1763 // test for https://github.com/whtcorpsinc/milevadb/pull/461 1764 1765 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1766 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 1767 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 1768 1769 tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1770 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 1771 tk.MustInterDirc(`CREATE TABLE test ( id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, val int UNIQUE, PRIMARY KEY (id)); `) 1772 tk.MustInterDirc("begin;") 1773 tk.MustInterDirc("insert into test(id, val) values(1, 1);") 1774 tk1.MustInterDirc("begin;") 1775 tk1.MustInterDirc("insert into test(id, val) values(2, 2);") 1776 tk2.MustInterDirc("begin;") 1777 tk2.MustInterDirc("insert into test(id, val) values(1, 2);") 1778 tk2.MustInterDirc("commit;") 1779 _, err := tk.InterDirc("commit") 1780 c.Assert(err, NotNil) 1781 // Check error type and error message 1782 c.Assert(terror.ErrorEqual(err, ekv.ErrKeyExists), IsTrue, Commentf("err %v", err)) 1783 c.Assert(err.Error(), Equals, "previous memex: insert into test(id, val) values(1, 1);: [ekv:1062]Duplicate entry '1' for key 'PRIMARY'") 1784 1785 _, err = tk1.InterDirc("commit") 1786 c.Assert(err, NotNil) 1787 c.Assert(terror.ErrorEqual(err, ekv.ErrKeyExists), IsTrue, Commentf("err %v", err)) 1788 c.Assert(err.Error(), Equals, "previous memex: insert into test(id, val) values(2, 2);: [ekv:1062]Duplicate entry '2' for key 'val'") 1789 1790 // Test for https://github.com/whtcorpsinc/milevadb/issues/463 1791 tk.MustInterDirc("drop causet test;") 1792 tk.MustInterDirc(`CREATE TABLE test ( 1793 id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, 1794 val int UNIQUE, 1795 PRIMARY KEY (id) 1796 );`) 1797 tk.MustInterDirc("insert into test(id, val) values(1, 1);") 1798 _, err = tk.InterDirc("insert into test(id, val) values(2, 1);") 1799 c.Assert(err, NotNil) 1800 tk.MustInterDirc("insert into test(id, val) values(2, 2);") 1801 1802 tk.MustInterDirc("begin;") 1803 tk.MustInterDirc("insert into test(id, val) values(3, 3);") 1804 _, err = tk.InterDirc("insert into test(id, val) values(4, 3);") 1805 c.Assert(err, NotNil) 1806 tk.MustInterDirc("insert into test(id, val) values(4, 4);") 1807 tk.MustInterDirc("commit;") 1808 1809 tk1.MustInterDirc("begin;") 1810 tk1.MustInterDirc("insert into test(id, val) values(5, 6);") 1811 tk.MustInterDirc("begin;") 1812 tk.MustInterDirc("insert into test(id, val) values(20, 6);") 1813 tk.MustInterDirc("commit;") 1814 tk1.InterDirc("commit") 1815 tk1.MustInterDirc("insert into test(id, val) values(5, 5);") 1816 1817 tk.MustInterDirc("drop causet test;") 1818 tk.MustInterDirc(`CREATE TABLE test ( 1819 id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, 1820 val1 int UNIQUE, 1821 val2 int UNIQUE, 1822 PRIMARY KEY (id) 1823 );`) 1824 tk.MustInterDirc("insert into test(id, val1, val2) values(1, 1, 1);") 1825 tk.MustInterDirc("insert into test(id, val1, val2) values(2, 2, 2);") 1826 tk.InterDirc("uFIDelate test set val1 = 3, val2 = 2 where id = 1;") 1827 tk.MustInterDirc("insert into test(id, val1, val2) values(3, 3, 3);") 1828 } 1829 1830 func (s *testStochastikSuite2) TestSet(c *C) { 1831 // Test for https://github.com/whtcorpsinc/milevadb/issues/1114 1832 1833 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1834 tk.MustInterDirc("set @tmp = 0") 1835 tk.MustInterDirc("set @tmp := @tmp + 1") 1836 tk.MustQuery("select @tmp").Check(testkit.Rows("1")) 1837 tk.MustQuery("select @tmp1 = 1, @tmp2 := 2").Check(testkit.Rows("<nil> 2")) 1838 tk.MustQuery("select @tmp1 := 11, @tmp2").Check(testkit.Rows("11 2")) 1839 1840 tk.MustInterDirc("drop causet if exists t") 1841 tk.MustInterDirc("create causet t (c int);") 1842 tk.MustInterDirc("insert into t values (1),(2);") 1843 tk.MustInterDirc("uFIDelate t set c = 3 WHERE c = @var:= 1") 1844 tk.MustQuery("select * from t").Check(testkit.Rows("3", "2")) 1845 tk.MustQuery("select @tmp := count(*) from t").Check(testkit.Rows("2")) 1846 tk.MustQuery("select @tmp := c-2 from t where c=3").Check(testkit.Rows("1")) 1847 } 1848 1849 func (s *testStochastikSuite3) TestMyALLEGROSQLTypes(c *C) { 1850 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1851 tk.MustQuery(`select 0x01 + 1, x'4D7953514C' = "MyALLEGROSQL"`).Check(testkit.Rows("2 1")) 1852 tk.MustQuery(`select 0b01 + 1, 0b01000001 = "A"`).Check(testkit.Rows("2 1")) 1853 } 1854 1855 func (s *testStochastikSuite2) TestIssue986(c *C) { 1856 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1857 sqlText := `CREATE TABLE address ( 1858 id bigint(20) NOT NULL AUTO_INCREMENT, 1859 PRIMARY KEY (id));` 1860 tk.MustInterDirc(sqlText) 1861 tk.MustInterDirc(`insert into address values ('10')`) 1862 } 1863 1864 func (s *testStochastikSuite3) TestCast(c *C) { 1865 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1866 tk.MustQuery("select cast(0.5 as unsigned)") 1867 tk.MustQuery("select cast(-0.5 as signed)") 1868 tk.MustQuery("select hex(cast(0x10 as binary(2)))").Check(testkit.Rows("1000")) 1869 } 1870 1871 func (s *testStochastikSuite2) TestTableInfoMeta(c *C) { 1872 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1873 1874 checkResult := func(affectedRows uint64, insertID uint64) { 1875 gotRows := tk.Se.AffectedRows() 1876 c.Assert(gotRows, Equals, affectedRows) 1877 1878 gotID := tk.Se.LastInsertID() 1879 c.Assert(gotID, Equals, insertID) 1880 } 1881 1882 // create causet 1883 tk.MustInterDirc("CREATE TABLE tbl_test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 1884 1885 // insert data 1886 tk.MustInterDirc(`INSERT INTO tbl_test VALUES (1, "hello");`) 1887 checkResult(1, 0) 1888 1889 tk.MustInterDirc(`INSERT INTO tbl_test VALUES (2, "hello");`) 1890 checkResult(1, 0) 1891 1892 tk.MustInterDirc(`UFIDelATE tbl_test SET name = "abc" where id = 2;`) 1893 checkResult(1, 0) 1894 1895 tk.MustInterDirc(`DELETE from tbl_test where id = 2;`) 1896 checkResult(1, 0) 1897 1898 // select data 1899 tk.MustQuery("select * from tbl_test").Check(testkit.Rows("1 hello")) 1900 } 1901 1902 func (s *testStochastikSuite3) TestCaseInsensitive(c *C) { 1903 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1904 1905 tk.MustInterDirc("create causet T (a text, B int)") 1906 tk.MustInterDirc("insert t (A, b) values ('aaa', 1)") 1907 rs, _ := tk.InterDirc("select * from t") 1908 fields := rs.Fields() 1909 c.Assert(fields[0].DeferredCausetAsName.O, Equals, "a") 1910 c.Assert(fields[1].DeferredCausetAsName.O, Equals, "B") 1911 rs, _ = tk.InterDirc("select A, b from t") 1912 fields = rs.Fields() 1913 c.Assert(fields[0].DeferredCausetAsName.O, Equals, "A") 1914 c.Assert(fields[1].DeferredCausetAsName.O, Equals, "b") 1915 rs, _ = tk.InterDirc("select a as A from t where A > 0") 1916 fields = rs.Fields() 1917 c.Assert(fields[0].DeferredCausetAsName.O, Equals, "A") 1918 tk.MustInterDirc("uFIDelate T set b = B + 1") 1919 tk.MustInterDirc("uFIDelate T set B = b + 1") 1920 tk.MustQuery("select b from T").Check(testkit.Rows("3")) 1921 } 1922 1923 // TestDeletePanic is for delete panic 1924 func (s *testStochastikSuite2) TestDeletePanic(c *C) { 1925 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1926 tk.MustInterDirc("create causet t (c int)") 1927 tk.MustInterDirc("insert into t values (1), (2), (3)") 1928 tk.MustInterDirc("delete from `t` where `c` = ?", 1) 1929 tk.MustInterDirc("delete from `t` where `c` = ?", 2) 1930 } 1931 1932 func (s *testStochastikSuite2) TestInformationSchemaCreateTime(c *C) { 1933 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1934 tk.MustInterDirc("create causet t (c int)") 1935 ret := tk.MustQuery("select create_time from information_schema.blocks where block_name='t';") 1936 // Make sure t1 is greater than t. 1937 time.Sleep(time.Second) 1938 tk.MustInterDirc("alter causet t modify c int default 11") 1939 ret1 := tk.MustQuery("select create_time from information_schema.blocks where block_name='t';") 1940 t, err := types.ParseDatetime(nil, ret.Rows()[0][0].(string)) 1941 c.Assert(err, IsNil) 1942 t1, err := types.ParseDatetime(nil, ret1.Rows()[0][0].(string)) 1943 c.Assert(err, IsNil) 1944 r := t1.Compare(t) 1945 c.Assert(r, Equals, 1) 1946 } 1947 1948 type testSchemaSuiteBase struct { 1949 cluster cluster.Cluster 1950 causetstore ekv.CausetStorage 1951 dom *petri.Petri 1952 } 1953 1954 type testSchemaSuite struct { 1955 testSchemaSuiteBase 1956 } 1957 1958 type testSchemaSerialSuite struct { 1959 testSchemaSuiteBase 1960 } 1961 1962 func (s *testSchemaSuiteBase) TearDownTest(c *C) { 1963 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1964 r := tk.MustQuery("show blocks") 1965 for _, tb := range r.Rows() { 1966 blockName := tb[0] 1967 tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName)) 1968 } 1969 } 1970 1971 func (s *testSchemaSuiteBase) SetUpSuite(c *C) { 1972 testleak.BeforeTest() 1973 causetstore, err := mockstore.NewMockStore( 1974 mockstore.WithClusterInspector(func(c cluster.Cluster) { 1975 mockstore.BootstrapWithSingleStore(c) 1976 s.cluster = c 1977 }), 1978 ) 1979 c.Assert(err, IsNil) 1980 s.causetstore = causetstore 1981 stochastik.DisableStats4Test() 1982 dom, err := stochastik.BootstrapStochastik(s.causetstore) 1983 c.Assert(err, IsNil) 1984 s.dom = dom 1985 } 1986 1987 func (s *testSchemaSuiteBase) TearDownSuite(c *C) { 1988 s.dom.Close() 1989 s.causetstore.Close() 1990 testleak.AfterTest(c)() 1991 } 1992 1993 func (s *testSchemaSerialSuite) TestLoadSchemaFailed(c *C) { 1994 atomic.StoreInt32(&petri.SchemaOutOfDateRetryTimes, int32(3)) 1995 atomic.StoreInt64(&petri.SchemaOutOfDateRetryInterval, int64(20*time.Millisecond)) 1996 defer func() { 1997 atomic.StoreInt32(&petri.SchemaOutOfDateRetryTimes, 10) 1998 atomic.StoreInt64(&petri.SchemaOutOfDateRetryInterval, int64(500*time.Millisecond)) 1999 }() 2000 2001 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2002 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2003 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 2004 2005 tk.MustInterDirc("create causet t (a int);") 2006 tk.MustInterDirc("create causet t1 (a int);") 2007 tk.MustInterDirc("create causet t2 (a int);") 2008 2009 tk1.MustInterDirc("begin") 2010 tk2.MustInterDirc("begin") 2011 2012 // Make sure loading information schemaReplicant is failed and server is invalid. 2013 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/petri/ErrorMockReloadFailed", `return(true)`), IsNil) 2014 err := petri.GetPetri(tk.Se).Reload() 2015 c.Assert(err, NotNil) 2016 2017 lease := petri.GetPetri(tk.Se).DBS().GetLease() 2018 time.Sleep(lease * 2) 2019 2020 // Make sure executing insert memex is failed when server is invalid. 2021 _, err = tk.InterDirc("insert t values (100);") 2022 c.Check(err, NotNil) 2023 2024 tk1.MustInterDirc("insert t1 values (100);") 2025 tk2.MustInterDirc("insert t2 values (100);") 2026 2027 _, err = tk1.InterDirc("commit") 2028 c.Check(err, NotNil) 2029 2030 ver, err := s.causetstore.CurrentVersion() 2031 c.Assert(err, IsNil) 2032 c.Assert(ver, NotNil) 2033 2034 failpoint.Disable("github.com/whtcorpsinc/milevadb/petri/ErrorMockReloadFailed") 2035 time.Sleep(lease * 2) 2036 2037 tk.MustInterDirc("drop causet if exists t;") 2038 tk.MustInterDirc("create causet t (a int);") 2039 tk.MustInterDirc("insert t values (100);") 2040 // Make sure insert to causet t2 transaction executes. 2041 tk2.MustInterDirc("commit") 2042 } 2043 2044 func (s *testSchemaSerialSuite) TestSchemaCheckerALLEGROSQL(c *C) { 2045 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2046 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2047 2048 // create causet 2049 tk.MustInterDirc(`create causet t (id int, c int);`) 2050 tk.MustInterDirc(`create causet t1 (id int, c int);`) 2051 // insert data 2052 tk.MustInterDirc(`insert into t values(1, 1);`) 2053 2054 // The schemaReplicant version is out of date in the first transaction, but the ALLEGROALLEGROSQL can be retried. 2055 tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 2056 tk.MustInterDirc(`begin;`) 2057 tk1.MustInterDirc(`alter causet t add index idx(c);`) 2058 tk.MustInterDirc(`insert into t values(2, 2);`) 2059 tk.MustInterDirc(`commit;`) 2060 2061 // The schemaReplicant version is out of date in the first transaction, and the ALLEGROALLEGROSQL can't be retried. 2062 atomic.StoreUint32(&stochastik.SchemaChangedWithoutRetry, 1) 2063 defer func() { 2064 atomic.StoreUint32(&stochastik.SchemaChangedWithoutRetry, 0) 2065 }() 2066 tk.MustInterDirc(`begin;`) 2067 tk1.MustInterDirc(`alter causet t modify column c bigint;`) 2068 tk.MustInterDirc(`insert into t values(3, 3);`) 2069 _, err := tk.InterDirc(`commit;`) 2070 c.Assert(terror.ErrorEqual(err, petri.ErrSchemaReplicantChanged), IsTrue, Commentf("err %v", err)) 2071 2072 // But the transaction related causet IDs aren't in the uFIDelated causet IDs. 2073 tk.MustInterDirc(`begin;`) 2074 tk1.MustInterDirc(`alter causet t add index idx2(c);`) 2075 tk.MustInterDirc(`insert into t1 values(4, 4);`) 2076 tk.MustInterDirc(`commit;`) 2077 2078 // Test for "select for uFIDelate". 2079 tk.MustInterDirc(`begin;`) 2080 tk1.MustInterDirc(`alter causet t add index idx3(c);`) 2081 tk.MustQuery(`select * from t for uFIDelate`) 2082 _, err = tk.InterDirc(`commit;`) 2083 c.Assert(err, NotNil) 2084 } 2085 2086 func (s *testSchemaSuite) TestPrepareStmtCommitWhenSchemaChanged(c *C) { 2087 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2088 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2089 2090 tk.MustInterDirc("create causet t (a int, b int)") 2091 tk1.MustInterDirc("prepare stmt from 'insert into t values (?, ?)'") 2092 tk1.MustInterDirc("set @a = 1") 2093 2094 // Commit find unrelated schemaReplicant change. 2095 tk1.MustInterDirc("begin") 2096 tk.MustInterDirc("create causet t1 (id int)") 2097 tk1.MustInterDirc("execute stmt using @a, @a") 2098 tk1.MustInterDirc("commit") 2099 2100 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 2101 tk1.MustInterDirc("begin") 2102 tk.MustInterDirc("alter causet t drop column b") 2103 tk1.MustInterDirc("execute stmt using @a, @a") 2104 _, err := tk1.InterDirc("commit") 2105 c.Assert(terror.ErrorEqual(err, causetembedded.ErrWrongValueCountOnRow), IsTrue, Commentf("err %v", err)) 2106 } 2107 2108 func (s *testSchemaSuite) TestCommitWhenSchemaChanged(c *C) { 2109 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2110 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2111 tk.MustInterDirc("create causet t (a int, b int)") 2112 2113 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 2114 tk1.MustInterDirc("begin") 2115 tk1.MustInterDirc("insert into t values (1, 1)") 2116 2117 tk.MustInterDirc("alter causet t drop column b") 2118 2119 // When tk1 commit, it will find schemaReplicant already changed. 2120 tk1.MustInterDirc("insert into t values (4, 4)") 2121 _, err := tk1.InterDirc("commit") 2122 c.Assert(terror.ErrorEqual(err, causetembedded.ErrWrongValueCountOnRow), IsTrue, Commentf("err %v", err)) 2123 } 2124 2125 func (s *testSchemaSuite) TestRetrySchemaChangeForEmptyChange(c *C) { 2126 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2127 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2128 tk.MustInterDirc("create causet t (i int)") 2129 tk.MustInterDirc("create causet t1 (i int)") 2130 tk.MustInterDirc("begin") 2131 tk1.MustInterDirc("alter causet t add j int") 2132 tk.MustInterDirc("select * from t for uFIDelate") 2133 tk.MustInterDirc("uFIDelate t set i = -i") 2134 tk.MustInterDirc("delete from t") 2135 tk.MustInterDirc("insert into t1 values (1)") 2136 tk.MustInterDirc("commit") 2137 2138 tk.MustInterDirc("begin pessimistic") 2139 tk1.MustInterDirc("alter causet t add k int") 2140 tk.MustInterDirc("select * from t for uFIDelate") 2141 tk.MustInterDirc("uFIDelate t set i = -i") 2142 tk.MustInterDirc("delete from t") 2143 tk.MustInterDirc("insert into t1 values (1)") 2144 tk.MustInterDirc("commit") 2145 } 2146 2147 func (s *testSchemaSuite) TestRetrySchemaChange(c *C) { 2148 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2149 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2150 tk.MustInterDirc("create causet t (a int primary key, b int)") 2151 tk.MustInterDirc("insert into t values (1, 1)") 2152 2153 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 2154 tk1.MustInterDirc("begin") 2155 tk1.MustInterDirc("uFIDelate t set b = 5 where a = 1") 2156 2157 tk.MustInterDirc("alter causet t add index b_i (b)") 2158 2159 run := false 2160 hook := func() { 2161 if !run { 2162 tk.MustInterDirc("uFIDelate t set b = 3 where a = 1") 2163 run = true 2164 } 2165 } 2166 2167 // In order to cover a bug that memex history is not uFIDelated during retry. 2168 // See https://github.com/whtcorpsinc/milevadb/pull/5202 2169 // Step1: when tk1 commit, it find schemaReplicant changed and retry(). 2170 // Step2: during retry, hook() is called, tk uFIDelate primary key. 2171 // Step3: tk1 continue commit in retry() meet a retryable error(write conflict), retry again. 2172 // Step4: tk1 retry() success, if it use the stale memex, data and index will inconsistent. 2173 fpName := "github.com/whtcorpsinc/milevadb/stochastik/preCommitHook" 2174 c.Assert(failpoint.Enable(fpName, "return"), IsNil) 2175 defer func() { c.Assert(failpoint.Disable(fpName), IsNil) }() 2176 2177 ctx := context.WithValue(context.Background(), "__preCommitHook", hook) 2178 err := tk1.Se.CommitTxn(ctx) 2179 c.Assert(err, IsNil) 2180 tk.MustQuery("select * from t where t.b = 5").Check(testkit.Rows("1 5")) 2181 } 2182 2183 func (s *testSchemaSuite) TestRetryMissingUnionScan(c *C) { 2184 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2185 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2186 tk.MustInterDirc("create causet t (a int primary key, b int unique, c int)") 2187 tk.MustInterDirc("insert into t values (1, 1, 1)") 2188 2189 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0") 2190 tk1.MustInterDirc("begin") 2191 tk1.MustInterDirc("uFIDelate t set b = 1, c = 2 where b = 2") 2192 tk1.MustInterDirc("uFIDelate t set b = 1, c = 2 where a = 1") 2193 2194 // Create a conflict to reproduces the bug that the second uFIDelate memex in retry 2195 // has a dirty causet but doesn't use UnionScan. 2196 tk.MustInterDirc("uFIDelate t set b = 2 where a = 1") 2197 2198 tk1.MustInterDirc("commit") 2199 } 2200 2201 func (s *testSchemaSuite) TestTableReaderChunk(c *C) { 2202 // Since normally a single region mock einsteindb only returns one partial result we need to manually split the 2203 // causet to test multiple chunks. 2204 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2205 tk.MustInterDirc("create causet chk (a int)") 2206 for i := 0; i < 100; i++ { 2207 tk.MustInterDirc(fmt.Sprintf("insert chk values (%d)", i)) 2208 } 2209 tbl, err := petri.GetPetri(tk.Se).SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("chk")) 2210 c.Assert(err, IsNil) 2211 s.cluster.SplitTable(tbl.Meta().ID, 10) 2212 2213 tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1) 2214 tk.MustInterDirc("set milevadb_init_chunk_size = 2") 2215 defer func() { 2216 tk.MustInterDirc(fmt.Sprintf("set milevadb_init_chunk_size = %d", variable.DefInitChunkSize)) 2217 }() 2218 rs, err := tk.InterDirc("select * from chk") 2219 c.Assert(err, IsNil) 2220 req := rs.NewChunk() 2221 var count int 2222 var numChunks int 2223 for { 2224 err = rs.Next(context.TODO(), req) 2225 c.Assert(err, IsNil) 2226 numRows := req.NumRows() 2227 if numRows == 0 { 2228 break 2229 } 2230 for i := 0; i < numRows; i++ { 2231 c.Assert(req.GetRow(i).GetInt64(0), Equals, int64(count)) 2232 count++ 2233 } 2234 numChunks++ 2235 } 2236 c.Assert(count, Equals, 100) 2237 // FIXME: revert this result to new group value after allegrosql can handle initChunkSize. 2238 c.Assert(numChunks, Equals, 1) 2239 rs.Close() 2240 } 2241 2242 func (s *testSchemaSuite) TestInsertInterDircChunk(c *C) { 2243 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2244 tk.MustInterDirc("create causet test1(a int)") 2245 for i := 0; i < 100; i++ { 2246 tk.MustInterDirc(fmt.Sprintf("insert test1 values (%d)", i)) 2247 } 2248 tk.MustInterDirc("create causet test2(a int)") 2249 2250 tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1) 2251 tk.MustInterDirc("insert into test2(a) select a from test1;") 2252 2253 rs, err := tk.InterDirc("select * from test2") 2254 c.Assert(err, IsNil) 2255 var idx int 2256 for { 2257 req := rs.NewChunk() 2258 err = rs.Next(context.TODO(), req) 2259 c.Assert(err, IsNil) 2260 if req.NumRows() == 0 { 2261 break 2262 } 2263 2264 for rowIdx := 0; rowIdx < req.NumRows(); rowIdx++ { 2265 event := req.GetRow(rowIdx) 2266 c.Assert(event.GetInt64(0), Equals, int64(idx)) 2267 idx++ 2268 } 2269 } 2270 2271 c.Assert(idx, Equals, 100) 2272 rs.Close() 2273 } 2274 2275 func (s *testSchemaSuite) TestUFIDelateInterDircChunk(c *C) { 2276 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2277 tk.MustInterDirc("create causet chk(a int)") 2278 for i := 0; i < 100; i++ { 2279 tk.MustInterDirc(fmt.Sprintf("insert chk values (%d)", i)) 2280 } 2281 2282 tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1) 2283 for i := 0; i < 100; i++ { 2284 tk.MustInterDirc(fmt.Sprintf("uFIDelate chk set a = a + 100 where a = %d", i)) 2285 } 2286 2287 rs, err := tk.InterDirc("select * from chk") 2288 c.Assert(err, IsNil) 2289 var idx int 2290 for { 2291 req := rs.NewChunk() 2292 err = rs.Next(context.TODO(), req) 2293 c.Assert(err, IsNil) 2294 if req.NumRows() == 0 { 2295 break 2296 } 2297 2298 for rowIdx := 0; rowIdx < req.NumRows(); rowIdx++ { 2299 event := req.GetRow(rowIdx) 2300 c.Assert(event.GetInt64(0), Equals, int64(idx+100)) 2301 idx++ 2302 } 2303 } 2304 2305 c.Assert(idx, Equals, 100) 2306 rs.Close() 2307 } 2308 2309 func (s *testSchemaSuite) TestDeleteInterDircChunk(c *C) { 2310 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2311 tk.MustInterDirc("create causet chk(a int)") 2312 2313 for i := 0; i < 100; i++ { 2314 tk.MustInterDirc(fmt.Sprintf("insert chk values (%d)", i)) 2315 } 2316 2317 tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1) 2318 2319 for i := 0; i < 99; i++ { 2320 tk.MustInterDirc(fmt.Sprintf("delete from chk where a = %d", i)) 2321 } 2322 2323 rs, err := tk.InterDirc("select * from chk") 2324 c.Assert(err, IsNil) 2325 2326 req := rs.NewChunk() 2327 err = rs.Next(context.TODO(), req) 2328 c.Assert(err, IsNil) 2329 c.Assert(req.NumRows(), Equals, 1) 2330 2331 event := req.GetRow(0) 2332 c.Assert(event.GetInt64(0), Equals, int64(99)) 2333 rs.Close() 2334 } 2335 2336 func (s *testSchemaSuite) TestDeleteMultiTableInterDircChunk(c *C) { 2337 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2338 tk.MustInterDirc("create causet chk1(a int)") 2339 tk.MustInterDirc("create causet chk2(a int)") 2340 2341 for i := 0; i < 100; i++ { 2342 tk.MustInterDirc(fmt.Sprintf("insert chk1 values (%d)", i)) 2343 } 2344 2345 for i := 0; i < 50; i++ { 2346 tk.MustInterDirc(fmt.Sprintf("insert chk2 values (%d)", i)) 2347 } 2348 2349 tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1) 2350 2351 tk.MustInterDirc("delete chk1, chk2 from chk1 inner join chk2 where chk1.a = chk2.a") 2352 2353 rs, err := tk.InterDirc("select * from chk1") 2354 c.Assert(err, IsNil) 2355 2356 var idx int 2357 for { 2358 req := rs.NewChunk() 2359 err = rs.Next(context.TODO(), req) 2360 c.Assert(err, IsNil) 2361 2362 if req.NumRows() == 0 { 2363 break 2364 } 2365 2366 for i := 0; i < req.NumRows(); i++ { 2367 event := req.GetRow(i) 2368 c.Assert(event.GetInt64(0), Equals, int64(idx+50)) 2369 idx++ 2370 } 2371 } 2372 c.Assert(idx, Equals, 50) 2373 rs.Close() 2374 2375 rs, err = tk.InterDirc("select * from chk2") 2376 c.Assert(err, IsNil) 2377 2378 req := rs.NewChunk() 2379 err = rs.Next(context.TODO(), req) 2380 c.Assert(err, IsNil) 2381 c.Assert(req.NumRows(), Equals, 0) 2382 rs.Close() 2383 } 2384 2385 func (s *testSchemaSuite) TestIndexLookUpReaderChunk(c *C) { 2386 // Since normally a single region mock einsteindb only returns one partial result we need to manually split the 2387 // causet to test multiple chunks. 2388 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2389 tk.MustInterDirc("drop causet if exists chk") 2390 tk.MustInterDirc("create causet chk (k int unique, c int)") 2391 for i := 0; i < 100; i++ { 2392 tk.MustInterDirc(fmt.Sprintf("insert chk values (%d, %d)", i, i)) 2393 } 2394 tbl, err := petri.GetPetri(tk.Se).SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("chk")) 2395 c.Assert(err, IsNil) 2396 s.cluster.SplitIndex(tbl.Meta().ID, tbl.Indices()[0].Meta().ID, 10) 2397 2398 tk.Se.GetStochastikVars().IndexLookupSize = 10 2399 rs, err := tk.InterDirc("select * from chk order by k") 2400 c.Assert(err, IsNil) 2401 req := rs.NewChunk() 2402 var count int 2403 for { 2404 err = rs.Next(context.TODO(), req) 2405 c.Assert(err, IsNil) 2406 numRows := req.NumRows() 2407 if numRows == 0 { 2408 break 2409 } 2410 for i := 0; i < numRows; i++ { 2411 c.Assert(req.GetRow(i).GetInt64(0), Equals, int64(count)) 2412 c.Assert(req.GetRow(i).GetInt64(1), Equals, int64(count)) 2413 count++ 2414 } 2415 } 2416 c.Assert(count, Equals, 100) 2417 rs.Close() 2418 2419 rs, err = tk.InterDirc("select k from chk where c < 90 order by k") 2420 c.Assert(err, IsNil) 2421 req = rs.NewChunk() 2422 count = 0 2423 for { 2424 err = rs.Next(context.TODO(), req) 2425 c.Assert(err, IsNil) 2426 numRows := req.NumRows() 2427 if numRows == 0 { 2428 break 2429 } 2430 for i := 0; i < numRows; i++ { 2431 c.Assert(req.GetRow(i).GetInt64(0), Equals, int64(count)) 2432 count++ 2433 } 2434 } 2435 c.Assert(count, Equals, 90) 2436 rs.Close() 2437 } 2438 2439 func (s *testStochastikSuite2) TestStatementErrorInTransaction(c *C) { 2440 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2441 tk.MustInterDirc("create causet memex_side_effect (c int primary key)") 2442 tk.MustInterDirc("begin") 2443 tk.MustInterDirc("insert into memex_side_effect values (1)") 2444 _, err := tk.InterDirc("insert into memex_side_effect value (2),(3),(4),(1)") 2445 c.Assert(err, NotNil) 2446 tk.MustQuery(`select * from memex_side_effect`).Check(testkit.Rows("1")) 2447 tk.MustInterDirc("commit") 2448 tk.MustQuery(`select * from memex_side_effect`).Check(testkit.Rows("1")) 2449 2450 tk.MustInterDirc("drop causet if exists test;") 2451 tk.MustInterDirc(`create causet test ( 2452 a int(11) DEFAULT NULL, 2453 b int(11) DEFAULT NULL 2454 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;`) 2455 tk.MustInterDirc("insert into test values (1, 2), (1, 2), (1, 1), (1, 1);") 2456 2457 tk.MustInterDirc("start transaction;") 2458 // In the transaction, memex error should not rollback the transaction. 2459 _, err = tk.InterDirc("uFIDelate tset set b=11 where a=1 and b=2;") 2460 c.Assert(err, NotNil) 2461 // Test for a bug that last line rollback and exit transaction, this line autocommit. 2462 tk.MustInterDirc("uFIDelate test set b = 11 where a = 1 and b = 2;") 2463 tk.MustInterDirc("rollback") 2464 tk.MustQuery("select * from test where a = 1 and b = 11").Check(testkit.Rows()) 2465 } 2466 2467 func (s *testStochastikSerialSuite) TestStatementCountLimit(c *C) { 2468 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2469 tk.MustInterDirc("create causet stmt_count_limit (id int)") 2470 defer config.RestoreFunc()() 2471 config.UFIDelateGlobal(func(conf *config.Config) { 2472 conf.Performance.StmtCountLimit = 3 2473 }) 2474 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 2475 tk.MustInterDirc("begin") 2476 tk.MustInterDirc("insert into stmt_count_limit values (1)") 2477 tk.MustInterDirc("insert into stmt_count_limit values (2)") 2478 _, err := tk.InterDirc("insert into stmt_count_limit values (3)") 2479 c.Assert(err, NotNil) 2480 2481 // begin is counted into history but this one is not. 2482 tk.MustInterDirc("SET SESSION autocommit = false") 2483 tk.MustInterDirc("insert into stmt_count_limit values (1)") 2484 tk.MustInterDirc("insert into stmt_count_limit values (2)") 2485 tk.MustInterDirc("insert into stmt_count_limit values (3)") 2486 _, err = tk.InterDirc("insert into stmt_count_limit values (4)") 2487 c.Assert(err, NotNil) 2488 } 2489 2490 func (s *testStochastikSerialSuite) TestBatchCommit(c *C) { 2491 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2492 tk.MustInterDirc("set milevadb_batch_commit = 1") 2493 tk.MustInterDirc("set milevadb_disable_txn_auto_retry = 0") 2494 tk.MustInterDirc("create causet t (id int)") 2495 defer config.RestoreFunc()() 2496 config.UFIDelateGlobal(func(conf *config.Config) { 2497 conf.Performance.StmtCountLimit = 3 2498 }) 2499 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2500 tk.MustInterDirc("SET SESSION autocommit = 1") 2501 tk.MustInterDirc("begin") 2502 tk.MustInterDirc("insert into t values (1)") 2503 tk1.MustQuery("select * from t").Check(testkit.Rows()) 2504 tk.MustInterDirc("insert into t values (2)") 2505 tk1.MustQuery("select * from t").Check(testkit.Rows()) 2506 tk.MustInterDirc("rollback") 2507 tk1.MustQuery("select * from t").Check(testkit.Rows()) 2508 2509 // The above rollback will not make the stochastik in transaction. 2510 tk.MustInterDirc("insert into t values (1)") 2511 tk1.MustQuery("select * from t").Check(testkit.Rows("1")) 2512 tk.MustInterDirc("delete from t") 2513 2514 tk.MustInterDirc("begin") 2515 tk.MustInterDirc("insert into t values (5)") 2516 tk1.MustQuery("select * from t").Check(testkit.Rows()) 2517 tk.MustInterDirc("insert into t values (6)") 2518 tk1.MustQuery("select * from t").Check(testkit.Rows()) 2519 tk.MustInterDirc("insert into t values (7)") 2520 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7")) 2521 2522 // The stochastik is still in transaction. 2523 tk.MustInterDirc("insert into t values (8)") 2524 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7")) 2525 tk.MustInterDirc("insert into t values (9)") 2526 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7")) 2527 tk.MustInterDirc("insert into t values (10)") 2528 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7")) 2529 tk.MustInterDirc("commit") 2530 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7", "8", "9", "10")) 2531 2532 // The above commit will not make the stochastik in transaction. 2533 tk.MustInterDirc("insert into t values (11)") 2534 tk1.MustQuery("select * from t").Check(testkit.Rows("5", "6", "7", "8", "9", "10", "11")) 2535 2536 tk.MustInterDirc("delete from t") 2537 tk.MustInterDirc("SET SESSION autocommit = 0") 2538 tk.MustInterDirc("insert into t values (1)") 2539 tk.MustInterDirc("insert into t values (2)") 2540 tk.MustInterDirc("insert into t values (3)") 2541 tk.MustInterDirc("rollback") 2542 tk1.MustInterDirc("insert into t values (4)") 2543 tk1.MustInterDirc("insert into t values (5)") 2544 tk.MustQuery("select * from t").Check(testkit.Rows("4", "5")) 2545 } 2546 2547 func (s *testStochastikSuite3) TestCastTimeToDate(c *C) { 2548 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2549 tk.MustInterDirc("set time_zone = '-8:00'") 2550 date := time.Now().In(time.FixedZone("", -8*int(time.Hour/time.Second))) 2551 tk.MustQuery("select cast(time('12:23:34') as date)").Check(testkit.Rows(date.Format("2006-01-02"))) 2552 2553 tk.MustInterDirc("set time_zone = '+08:00'") 2554 date = time.Now().In(time.FixedZone("", 8*int(time.Hour/time.Second))) 2555 tk.MustQuery("select cast(time('12:23:34') as date)").Check(testkit.Rows(date.Format("2006-01-02"))) 2556 } 2557 2558 func (s *testStochastikSuite) TestSetGlobalTZ(c *C) { 2559 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2560 tk.MustInterDirc("set time_zone = '+08:00'") 2561 tk.MustQuery("show variables like 'time_zone'").Check(testkit.Rows("time_zone +08:00")) 2562 2563 tk.MustInterDirc("set global time_zone = '+00:00'") 2564 2565 tk.MustQuery("show variables like 'time_zone'").Check(testkit.Rows("time_zone +08:00")) 2566 2567 // Disable global variable cache, so load global stochastik variable take effect immediate. 2568 s.dom.GetGlobalVarsCache().Disable() 2569 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2570 tk1.MustQuery("show variables like 'time_zone'").Check(testkit.Rows("time_zone +00:00")) 2571 } 2572 2573 func (s *testStochastikSuite2) TestRollbackOnCompileError(c *C) { 2574 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2575 tk.MustInterDirc("create causet t (a int)") 2576 tk.MustInterDirc("insert t values (1)") 2577 2578 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 2579 tk2.MustQuery("select * from t").Check(testkit.Rows("1")) 2580 2581 tk.MustInterDirc("rename causet t to t2") 2582 2583 var meetErr bool 2584 for i := 0; i < 100; i++ { 2585 _, err := tk2.InterDirc("insert t values (1)") 2586 if err != nil { 2587 meetErr = true 2588 break 2589 } 2590 } 2591 c.Assert(meetErr, IsTrue) 2592 tk.MustInterDirc("rename causet t2 to t") 2593 var recoverErr bool 2594 for i := 0; i < 100; i++ { 2595 _, err := tk2.InterDirc("insert t values (1)") 2596 if err == nil { 2597 recoverErr = true 2598 break 2599 } 2600 } 2601 c.Assert(recoverErr, IsTrue) 2602 } 2603 2604 func (s *testStochastikSuite3) TestSetTransactionIsolationOneShot(c *C) { 2605 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2606 tk.MustInterDirc("create causet t (k int, v int)") 2607 tk.MustInterDirc("insert t values (1, 42)") 2608 tk.MustInterDirc("set tx_isolation = 'read-committed'") 2609 tk.MustQuery("select @@tx_isolation").Check(testkit.Rows("READ-COMMITTED")) 2610 tk.MustInterDirc("set tx_isolation = 'repeablock-read'") 2611 tk.MustInterDirc("set transaction isolation level read committed") 2612 tk.MustQuery("select @@tx_isolation_one_shot").Check(testkit.Rows("READ-COMMITTED")) 2613 tk.MustQuery("select @@tx_isolation").Check(testkit.Rows("REPEATABLE-READ")) 2614 2615 // Check isolation level is set to read committed. 2616 ctx := context.WithValue(context.Background(), "CheckSelectRequestHook", func(req *ekv.Request) { 2617 c.Assert(req.IsolationLevel, Equals, ekv.SI) 2618 }) 2619 tk.Se.InterDircute(ctx, "select * from t where k = 1") 2620 2621 // Check it just take effect for one time. 2622 ctx = context.WithValue(context.Background(), "CheckSelectRequestHook", func(req *ekv.Request) { 2623 c.Assert(req.IsolationLevel, Equals, ekv.SI) 2624 }) 2625 tk.Se.InterDircute(ctx, "select * from t where k = 1") 2626 2627 // Can't change isolation level when it's inside a transaction. 2628 tk.MustInterDirc("begin") 2629 _, err := tk.Se.InterDircute(ctx, "set transaction isolation level read committed") 2630 c.Assert(err, NotNil) 2631 } 2632 2633 func (s *testStochastikSuite2) TestDBUserNameLength(c *C) { 2634 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2635 tk.MustInterDirc("create causet if not exists t (a int)") 2636 // Test user name length can be longer than 16. 2637 tk.MustInterDirc(`CREATE USER 'abcddfjakldfjaldddds'@'%' identified by ''`) 2638 tk.MustInterDirc(`grant all privileges on test.* to 'abcddfjakldfjaldddds'@'%'`) 2639 tk.MustInterDirc(`grant all privileges on test.t to 'abcddfjakldfjaldddds'@'%'`) 2640 } 2641 2642 func (s *testStochastikSerialSuite) TestKVVars(c *C) { 2643 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2644 tk.MustInterDirc("set @@milevadb_backoff_lock_fast = 1") 2645 tk.MustInterDirc("set @@milevadb_backoff_weight = 100") 2646 tk.MustInterDirc("create causet if not exists ekvvars (a int key)") 2647 tk.MustInterDirc("insert into ekvvars values (1)") 2648 tk.MustInterDirc("begin") 2649 txn, err := tk.Se.Txn(false) 2650 c.Assert(err, IsNil) 2651 vars := txn.GetVars() 2652 c.Assert(vars.BackoffLockFast, Equals, 1) 2653 c.Assert(vars.BackOffWeight, Equals, 100) 2654 tk.MustInterDirc("rollback") 2655 tk.MustInterDirc("set @@milevadb_backoff_weight = 50") 2656 tk.MustInterDirc("set @@autocommit = 0") 2657 tk.MustInterDirc("select * from ekvvars") 2658 c.Assert(tk.Se.GetStochastikVars().InTxn(), IsTrue) 2659 txn, err = tk.Se.Txn(false) 2660 c.Assert(err, IsNil) 2661 vars = txn.GetVars() 2662 c.Assert(vars.BackOffWeight, Equals, 50) 2663 2664 tk.MustInterDirc("set @@autocommit = 1") 2665 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/probeSetVars", `return(true)`), IsNil) 2666 tk.MustInterDirc("select * from ekvvars where a = 1") 2667 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/probeSetVars"), IsNil) 2668 c.Assert(einsteindb.SetSuccess, IsTrue) 2669 einsteindb.SetSuccess = false 2670 } 2671 2672 func (s *testStochastikSuite2) TestCommitRetryCount(c *C) { 2673 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2674 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 2675 tk1.MustInterDirc("create causet no_retry (id int)") 2676 tk1.MustInterDirc("insert into no_retry values (1)") 2677 tk1.MustInterDirc("set @@milevadb_retry_limit = 0") 2678 2679 tk1.MustInterDirc("begin") 2680 tk1.MustInterDirc("uFIDelate no_retry set id = 2") 2681 2682 tk2.MustInterDirc("begin") 2683 tk2.MustInterDirc("uFIDelate no_retry set id = 3") 2684 tk2.MustInterDirc("commit") 2685 2686 // No auto retry because retry limit is set to 0. 2687 _, err := tk1.Se.InterDircute(context.Background(), "commit") 2688 c.Assert(err, NotNil) 2689 } 2690 2691 func (s *testStochastikSuite3) TestEnablePartition(c *C) { 2692 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2693 tk.MustInterDirc("set milevadb_enable_block_partition=off") 2694 tk.MustQuery("show variables like 'milevadb_enable_block_partition'").Check(testkit.Rows("milevadb_enable_block_partition off")) 2695 2696 tk.MustInterDirc("set global milevadb_enable_block_partition = on") 2697 2698 tk.MustQuery("show variables like 'milevadb_enable_block_partition'").Check(testkit.Rows("milevadb_enable_block_partition off")) 2699 tk.MustQuery("show global variables like 'milevadb_enable_block_partition'").Check(testkit.Rows("milevadb_enable_block_partition on")) 2700 2701 // Disable global variable cache, so load global stochastik variable take effect immediate. 2702 s.dom.GetGlobalVarsCache().Disable() 2703 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2704 tk1.MustQuery("show variables like 'milevadb_enable_block_partition'").Check(testkit.Rows("milevadb_enable_block_partition on")) 2705 } 2706 2707 func (s *testStochastikSerialSuite) TestTxnRetryErrMsg(c *C) { 2708 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2709 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 2710 tk1.MustInterDirc("create causet no_retry (id int)") 2711 tk1.MustInterDirc("insert into no_retry values (1)") 2712 tk1.MustInterDirc("begin") 2713 tk2.MustInterDirc("uFIDelate no_retry set id = id + 1") 2714 tk1.MustInterDirc("uFIDelate no_retry set id = id + 1") 2715 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/ErrMockRetryableOnly", `return(true)`), IsNil) 2716 _, err := tk1.Se.InterDircute(context.Background(), "commit") 2717 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/ErrMockRetryableOnly"), IsNil) 2718 c.Assert(err, NotNil) 2719 c.Assert(ekv.ErrTxnRetryable.Equal(err), IsTrue, Commentf("error: %s", err)) 2720 c.Assert(strings.Contains(err.Error(), "mock retryable error"), IsTrue, Commentf("error: %s", err)) 2721 c.Assert(strings.Contains(err.Error(), ekv.TxnRetryableMark), IsTrue, Commentf("error: %s", err)) 2722 } 2723 2724 func (s *testSchemaSuite) TestDisableTxnAutoRetry(c *C) { 2725 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2726 tk2 := testkit.NewTestKitWithInit(c, s.causetstore) 2727 tk1.MustInterDirc("create causet no_retry (id int)") 2728 tk1.MustInterDirc("insert into no_retry values (1)") 2729 tk1.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 1") 2730 2731 tk1.MustInterDirc("begin") 2732 tk1.MustInterDirc("uFIDelate no_retry set id = 2") 2733 2734 tk2.MustInterDirc("begin") 2735 tk2.MustInterDirc("uFIDelate no_retry set id = 3") 2736 tk2.MustInterDirc("commit") 2737 2738 // No auto retry because milevadb_disable_txn_auto_retry is set to 1. 2739 _, err := tk1.Se.InterDircute(context.Background(), "commit") 2740 c.Assert(err, NotNil) 2741 2742 // stochastik 1 starts a transaction early. 2743 // execute a select memex to clear retry history. 2744 tk1.MustInterDirc("select 1") 2745 tk1.Se.PrepareTxnCtx(context.Background()) 2746 // stochastik 2 uFIDelate the value. 2747 tk2.MustInterDirc("uFIDelate no_retry set id = 4") 2748 // AutoCommit uFIDelate will retry, so it would not fail. 2749 tk1.MustInterDirc("uFIDelate no_retry set id = 5") 2750 2751 // RestrictedALLEGROSQL should retry. 2752 tk1.Se.GetStochastikVars().InRestrictedALLEGROSQL = true 2753 tk1.MustInterDirc("begin") 2754 2755 tk2.MustInterDirc("uFIDelate no_retry set id = 6") 2756 2757 tk1.MustInterDirc("uFIDelate no_retry set id = 7") 2758 tk1.MustInterDirc("commit") 2759 2760 // test for disable transaction local latch 2761 tk1.Se.GetStochastikVars().InRestrictedALLEGROSQL = false 2762 defer config.RestoreFunc()() 2763 config.UFIDelateGlobal(func(conf *config.Config) { 2764 conf.TxnLocalLatches.Enabled = false 2765 }) 2766 tk1.MustInterDirc("begin") 2767 tk1.MustInterDirc("uFIDelate no_retry set id = 9") 2768 2769 tk2.MustInterDirc("uFIDelate no_retry set id = 8") 2770 2771 _, err = tk1.Se.InterDircute(context.Background(), "commit") 2772 c.Assert(err, NotNil) 2773 c.Assert(ekv.ErrWriteConflict.Equal(err), IsTrue, Commentf("error: %s", err)) 2774 c.Assert(strings.Contains(err.Error(), ekv.TxnRetryableMark), IsTrue, Commentf("error: %s", err)) 2775 tk1.MustInterDirc("rollback") 2776 2777 config.UFIDelateGlobal(func(conf *config.Config) { 2778 conf.TxnLocalLatches.Enabled = true 2779 }) 2780 tk1.MustInterDirc("begin") 2781 tk2.MustInterDirc("alter causet no_retry add index idx(id)") 2782 tk2.MustQuery("select * from no_retry").Check(testkit.Rows("8")) 2783 tk1.MustInterDirc("uFIDelate no_retry set id = 10") 2784 _, err = tk1.Se.InterDircute(context.Background(), "commit") 2785 c.Assert(err, NotNil) 2786 2787 // set autocommit to begin and commit 2788 tk1.MustInterDirc("set autocommit = 0") 2789 tk1.MustQuery("select * from no_retry").Check(testkit.Rows("8")) 2790 tk2.MustInterDirc("uFIDelate no_retry set id = 11") 2791 tk1.MustInterDirc("uFIDelate no_retry set id = 12") 2792 _, err = tk1.Se.InterDircute(context.Background(), "set autocommit = 1") 2793 c.Assert(err, NotNil) 2794 c.Assert(ekv.ErrWriteConflict.Equal(err), IsTrue, Commentf("error: %s", err)) 2795 c.Assert(strings.Contains(err.Error(), ekv.TxnRetryableMark), IsTrue, Commentf("error: %s", err)) 2796 tk1.MustInterDirc("rollback") 2797 tk2.MustQuery("select * from no_retry").Check(testkit.Rows("11")) 2798 2799 tk1.MustInterDirc("set autocommit = 0") 2800 tk1.MustQuery("select * from no_retry").Check(testkit.Rows("11")) 2801 tk2.MustInterDirc("uFIDelate no_retry set id = 13") 2802 tk1.MustInterDirc("uFIDelate no_retry set id = 14") 2803 _, err = tk1.Se.InterDircute(context.Background(), "commit") 2804 c.Assert(err, NotNil) 2805 c.Assert(ekv.ErrWriteConflict.Equal(err), IsTrue, Commentf("error: %s", err)) 2806 c.Assert(strings.Contains(err.Error(), ekv.TxnRetryableMark), IsTrue, Commentf("error: %s", err)) 2807 tk1.MustInterDirc("rollback") 2808 tk2.MustQuery("select * from no_retry").Check(testkit.Rows("13")) 2809 } 2810 2811 // TestSetGroupConcatMaxLen is for issue #7034 2812 func (s *testStochastikSuite2) TestSetGroupConcatMaxLen(c *C) { 2813 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2814 2815 // Normal case 2816 tk.MustInterDirc("set global group_concat_max_len = 100") 2817 tk.MustInterDirc("set @@stochastik.group_concat_max_len = 50") 2818 result := tk.MustQuery("show global variables where variable_name='group_concat_max_len';") 2819 result.Check(testkit.Rows("group_concat_max_len 100")) 2820 2821 result = tk.MustQuery("show stochastik variables where variable_name='group_concat_max_len';") 2822 result.Check(testkit.Rows("group_concat_max_len 50")) 2823 2824 result = tk.MustQuery("select @@group_concat_max_len;") 2825 result.Check(testkit.Rows("50")) 2826 2827 result = tk.MustQuery("select @@global.group_concat_max_len;") 2828 result.Check(testkit.Rows("100")) 2829 2830 result = tk.MustQuery("select @@stochastik.group_concat_max_len;") 2831 result.Check(testkit.Rows("50")) 2832 2833 tk.MustInterDirc("set @@group_concat_max_len = 1024") 2834 2835 result = tk.MustQuery("select @@group_concat_max_len;") 2836 result.Check(testkit.Rows("1024")) 2837 2838 result = tk.MustQuery("select @@global.group_concat_max_len;") 2839 result.Check(testkit.Rows("100")) 2840 2841 result = tk.MustQuery("select @@stochastik.group_concat_max_len;") 2842 result.Check(testkit.Rows("1024")) 2843 2844 // Test value out of range 2845 tk.MustInterDirc("set @@group_concat_max_len=1") 2846 tk.MustQuery("show warnings").Check(solitonutil.RowsWithSep("|", "Warning|1292|Truncated incorrect group_concat_max_len value: '1'")) 2847 result = tk.MustQuery("select @@group_concat_max_len;") 2848 result.Check(testkit.Rows("4")) 2849 2850 _, err := tk.InterDirc("set @@group_concat_max_len = 18446744073709551616") 2851 c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) 2852 2853 // Test illegal type 2854 _, err = tk.InterDirc("set @@group_concat_max_len='hello'") 2855 c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) 2856 } 2857 2858 func (s *testStochastikSuite2) TestUFIDelatePrivilege(c *C) { 2859 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2860 tk.MustInterDirc("drop causet if exists t1, t2;") 2861 tk.MustInterDirc("create causet t1 (id int);") 2862 tk.MustInterDirc("create causet t2 (id int);") 2863 tk.MustInterDirc("insert into t1 values (1);") 2864 tk.MustInterDirc("insert into t2 values (2);") 2865 tk.MustInterDirc("create user xxx;") 2866 tk.MustInterDirc("grant all on test.t1 to xxx;") 2867 tk.MustInterDirc("grant select on test.t2 to xxx;") 2868 2869 tk1 := testkit.NewTestKitWithInit(c, s.causetstore) 2870 c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "xxx", Hostname: "localhost"}, 2871 []byte(""), 2872 []byte("")), IsTrue) 2873 2874 _, err := tk1.InterDirc("uFIDelate t2 set id = 666 where id = 1;") 2875 c.Assert(err, NotNil) 2876 c.Assert(strings.Contains(err.Error(), "privilege check fail"), IsTrue) 2877 2878 // Cover a bug that t1 and t2 both require uFIDelate privilege. 2879 // In fact, the privlege check for t1 should be uFIDelate, and for t2 should be select. 2880 _, err = tk1.InterDirc("uFIDelate t1,t2 set t1.id = t2.id;") 2881 c.Assert(err, IsNil) 2882 2883 // Fix issue 8911 2884 tk.MustInterDirc("create database weperk") 2885 tk.MustInterDirc("use weperk") 2886 tk.MustInterDirc("create causet tb_wehub_server (id int, active_count int, used_count int)") 2887 tk.MustInterDirc("create user 'weperk'") 2888 tk.MustInterDirc("grant all privileges on weperk.* to 'weperk'@'%'") 2889 c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "weperk", Hostname: "%"}, 2890 []byte(""), []byte("")), IsTrue) 2891 tk1.MustInterDirc("use weperk") 2892 tk1.MustInterDirc("uFIDelate tb_wehub_server a set a.active_count=a.active_count+1,a.used_count=a.used_count+1 where id=1") 2893 2894 tk.MustInterDirc("create database service") 2895 tk.MustInterDirc("create database report") 2896 tk.MustInterDirc(`CREATE TABLE service.t1 ( 2897 id int(11) DEFAULT NULL, 2898 a bigint(20) NOT NULL, 2899 b text DEFAULT NULL, 2900 PRIMARY KEY (a) 2901 )`) 2902 tk.MustInterDirc(`CREATE TABLE report.t2 ( 2903 a bigint(20) DEFAULT NULL, 2904 c bigint(20) NOT NULL 2905 )`) 2906 tk.MustInterDirc("grant all privileges on service.* to weperk") 2907 tk.MustInterDirc("grant all privileges on report.* to weperk") 2908 tk1.Se.GetStochastikVars().CurrentDB = "" 2909 tk1.MustInterDirc(`uFIDelate service.t1 s, 2910 report.t2 t 2911 set s.a = t.a 2912 WHERE 2913 s.a = t.a 2914 and t.c >= 1 and t.c <= 10000 2915 and s.b !='xx';`) 2916 2917 // Fix issue 10028 2918 tk.MustInterDirc("create database ap") 2919 tk.MustInterDirc("create database tp") 2920 tk.MustInterDirc("grant all privileges on ap.* to xxx") 2921 tk.MustInterDirc("grant select on tp.* to xxx") 2922 tk.MustInterDirc("create causet tp.record( id int,name varchar(128),age int)") 2923 tk.MustInterDirc("insert into tp.record (id,name,age) values (1,'john',18),(2,'lary',19),(3,'lily',18)") 2924 tk.MustInterDirc("create causet ap.record( id int,name varchar(128),age int)") 2925 tk.MustInterDirc("insert into ap.record(id) values(1)") 2926 c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "xxx", Hostname: "localhost"}, 2927 []byte(""), 2928 []byte("")), IsTrue) 2929 _, err2 := tk1.InterDirc("uFIDelate ap.record t inner join tp.record tt on t.id=tt.id set t.name=tt.name") 2930 c.Assert(err2, IsNil) 2931 } 2932 2933 func (s *testStochastikSuite2) TestTxnGoString(c *C) { 2934 tk := testkit.NewTestKitWithInit(c, s.causetstore) 2935 tk.MustInterDirc("drop causet if exists gostr;") 2936 tk.MustInterDirc("create causet gostr (id int);") 2937 txn, err := tk.Se.Txn(false) 2938 c.Assert(err, IsNil) 2939 str1 := fmt.Sprintf("%#v", txn) 2940 c.Assert(str1, Equals, "Txn{state=invalid}") 2941 tk.MustInterDirc("begin") 2942 txn, err = tk.Se.Txn(false) 2943 c.Assert(err, IsNil) 2944 c.Assert(fmt.Sprintf("%#v", txn), Equals, fmt.Sprintf("Txn{state=valid, txnStartTS=%d}", txn.StartTS())) 2945 2946 tk.MustInterDirc("insert into gostr values (1)") 2947 c.Assert(fmt.Sprintf("%#v", txn), Equals, fmt.Sprintf("Txn{state=valid, txnStartTS=%d}", txn.StartTS())) 2948 2949 tk.MustInterDirc("rollback") 2950 c.Assert(fmt.Sprintf("%#v", txn), Equals, "Txn{state=invalid}") 2951 } 2952 2953 func (s *testStochastikSuite3) TestMaxInterDiructeTime(c *C) { 2954 var err error 2955 tk := testkit.NewTestKit(c, s.causetstore) 2956 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 2957 c.Assert(err, IsNil) 2958 2959 tk.MustInterDirc("use test") 2960 tk.MustInterDirc("create causet MaxInterDircTime( id int,name varchar(128),age int);") 2961 tk.MustInterDirc("begin") 2962 tk.MustInterDirc("insert into MaxInterDircTime (id,name,age) values (1,'john',18),(2,'lary',19),(3,'lily',18);") 2963 2964 tk.MustQuery("select /*+ MAX_EXECUTION_TIME(1000) MAX_EXECUTION_TIME(500) */ * FROM MaxInterDircTime;") 2965 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 2966 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "MAX_EXECUTION_TIME() is defined more than once, only the last definition takes effect: MAX_EXECUTION_TIME(500)") 2967 c.Assert(tk.Se.GetStochastikVars().StmtCtx.HasMaxInterDircutionTime, Equals, true) 2968 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MaxInterDircutionTime, Equals, uint64(500)) 2969 2970 tk.MustQuery("select @@MAX_EXECUTION_TIME;").Check(testkit.Rows("0")) 2971 tk.MustQuery("select @@global.MAX_EXECUTION_TIME;").Check(testkit.Rows("0")) 2972 tk.MustQuery("select /*+ MAX_EXECUTION_TIME(1000) */ * FROM MaxInterDircTime;") 2973 2974 tk.MustInterDirc("set @@global.MAX_EXECUTION_TIME = 300;") 2975 tk.MustQuery("select * FROM MaxInterDircTime;") 2976 2977 tk.MustInterDirc("set @@MAX_EXECUTION_TIME = 150;") 2978 tk.MustQuery("select * FROM MaxInterDircTime;") 2979 2980 tk.MustQuery("select @@global.MAX_EXECUTION_TIME;").Check(testkit.Rows("300")) 2981 tk.MustQuery("select @@MAX_EXECUTION_TIME;").Check(testkit.Rows("150")) 2982 2983 tk.MustInterDirc("set @@global.MAX_EXECUTION_TIME = 0;") 2984 tk.MustInterDirc("set @@MAX_EXECUTION_TIME = 0;") 2985 tk.MustInterDirc("commit") 2986 tk.MustInterDirc("drop causet if exists MaxInterDircTime;") 2987 } 2988 2989 func (s *testStochastikSuite2) TestGrantViewRelated(c *C) { 2990 tkRoot := testkit.NewTestKitWithInit(c, s.causetstore) 2991 tkUser := testkit.NewTestKitWithInit(c, s.causetstore) 2992 2993 tkRoot.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost", CurrentUser: true, AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890")) 2994 2995 tkRoot.MustInterDirc("create causet if not exists t (a int)") 2996 tkRoot.MustInterDirc("create view v_version29 as select * from t") 2997 tkRoot.MustInterDirc("create user 'u_version29'@'%'") 2998 tkRoot.MustInterDirc("grant select on t to u_version29@'%'") 2999 3000 tkUser.Se.Auth(&auth.UserIdentity{Username: "u_version29", Hostname: "localhost", CurrentUser: true, AuthUsername: "u_version29", AuthHostname: "%"}, nil, []byte("012345678901234567890")) 3001 3002 tkUser.MustQuery("select current_user();").Check(testkit.Rows("u_version29@%")) 3003 err := tkUser.InterDircToErr("select * from test.v_version29;") 3004 c.Assert(err, NotNil) 3005 tkUser.MustQuery("select current_user();").Check(testkit.Rows("u_version29@%")) 3006 err = tkUser.InterDircToErr("create view v_version29_c as select * from t;") 3007 c.Assert(err, NotNil) 3008 3009 tkRoot.MustInterDirc(`grant show view on v_version29 to 'u_version29'@'%'`) 3010 tkRoot.MustQuery("select block_priv from allegrosql.blocks_priv where host='%' and EDB='test' and user='u_version29' and block_name='v_version29'").Check(testkit.Rows("Show View")) 3011 3012 tkUser.MustQuery("select current_user();").Check(testkit.Rows("u_version29@%")) 3013 tkUser.MustQuery("show create view v_version29;") 3014 err = tkUser.InterDircToErr("create view v_version29_c as select * from v_version29;") 3015 c.Assert(err, NotNil) 3016 3017 tkRoot.MustInterDirc("create view v_version29_c as select * from v_version29;") 3018 tkRoot.MustInterDirc(`grant create view on v_version29_c to 'u_version29'@'%'`) // Can't grant privilege on a non-exist causet/view. 3019 tkRoot.MustQuery("select block_priv from allegrosql.blocks_priv where host='%' and EDB='test' and user='u_version29' and block_name='v_version29_c'").Check(testkit.Rows("Create View")) 3020 tkRoot.MustInterDirc("drop view v_version29_c") 3021 3022 tkRoot.MustInterDirc(`grant select on v_version29 to 'u_version29'@'%'`) 3023 tkUser.MustQuery("select current_user();").Check(testkit.Rows("u_version29@%")) 3024 tkUser.MustInterDirc("create view v_version29_c as select * from v_version29;") 3025 } 3026 3027 func (s *testStochastikSuite3) TestLoadClientInteractive(c *C) { 3028 var ( 3029 err error 3030 connectionID uint64 3031 ) 3032 tk := testkit.NewTestKit(c, s.causetstore) 3033 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 3034 c.Assert(err, IsNil) 3035 id := atomic.AddUint64(&connectionID, 1) 3036 tk.Se.SetConnectionID(id) 3037 tk.Se.GetStochastikVars().ClientCapability = tk.Se.GetStochastikVars().ClientCapability | allegrosql.ClientInteractive 3038 tk.MustQuery("select @@wait_timeout").Check(testkit.Rows("28800")) 3039 } 3040 3041 func (s *testStochastikSuite2) TestReplicaRead(c *C) { 3042 var err error 3043 tk := testkit.NewTestKit(c, s.causetstore) 3044 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 3045 c.Assert(err, IsNil) 3046 c.Assert(tk.Se.GetStochastikVars().GetReplicaRead(), Equals, ekv.ReplicaReadLeader) 3047 tk.MustInterDirc("set @@milevadb_replica_read = 'follower';") 3048 c.Assert(tk.Se.GetStochastikVars().GetReplicaRead(), Equals, ekv.ReplicaReadFollower) 3049 tk.MustInterDirc("set @@milevadb_replica_read = 'leader';") 3050 c.Assert(tk.Se.GetStochastikVars().GetReplicaRead(), Equals, ekv.ReplicaReadLeader) 3051 } 3052 3053 func (s *testStochastikSuite3) TestIsolationRead(c *C) { 3054 var err error 3055 tk := testkit.NewTestKit(c, s.causetstore) 3056 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 3057 c.Assert(err, IsNil) 3058 c.Assert(len(tk.Se.GetStochastikVars().GetIsolationReadEngines()), Equals, 3) 3059 tk.MustInterDirc("set @@milevadb_isolation_read_engines = 'tiflash';") 3060 engines := tk.Se.GetStochastikVars().GetIsolationReadEngines() 3061 c.Assert(len(engines), Equals, 1) 3062 _, hasTiFlash := engines[ekv.TiFlash] 3063 _, hasEinsteinDB := engines[ekv.EinsteinDB] 3064 c.Assert(hasTiFlash, Equals, true) 3065 c.Assert(hasEinsteinDB, Equals, false) 3066 } 3067 3068 func (s *testStochastikSuite2) TestStmtHints(c *C) { 3069 var err error 3070 tk := testkit.NewTestKit(c, s.causetstore) 3071 tk.Se, err = stochastik.CreateStochastik4Test(s.causetstore) 3072 c.Assert(err, IsNil) 3073 3074 // Test MEMORY_QUOTA hint 3075 tk.MustInterDirc("select /*+ MEMORY_QUOTA(1 MB) */ 1;") 3076 val := int64(1) * 1024 * 1024 3077 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3078 tk.MustInterDirc("select /*+ MEMORY_QUOTA(1 GB) */ 1;") 3079 val = int64(1) * 1024 * 1024 * 1024 3080 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3081 tk.MustInterDirc("select /*+ MEMORY_QUOTA(1 GB), MEMORY_QUOTA(1 MB) */ 1;") 3082 val = int64(1) * 1024 * 1024 3083 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3084 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3085 tk.MustInterDirc("select /*+ MEMORY_QUOTA(0 GB) */ 1;") 3086 val = int64(0) 3087 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3088 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3089 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "Setting the MEMORY_QUOTA to 0 means no memory limit") 3090 3091 tk.MustInterDirc("use test") 3092 tk.MustInterDirc("create causet t1(a int);") 3093 tk.MustInterDirc("insert /*+ MEMORY_QUOTA(1 MB) */ into t1 (a) values (1);") 3094 val = int64(1) * 1024 * 1024 3095 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3096 3097 tk.MustInterDirc("insert /*+ MEMORY_QUOTA(1 MB) */ into t1 select /*+ MEMORY_QUOTA(3 MB) */ * from t1;") 3098 val = int64(1) * 1024 * 1024 3099 c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.CheckBytesLimit(val), IsTrue) 3100 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3101 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "[soliton:3126]Hint MEMORY_QUOTA(`3145728`) is ignored as conflicting/duplicated.") 3102 3103 // Test NO_INDEX_MERGE hint 3104 tk.Se.GetStochastikVars().SetEnableIndexMerge(true) 3105 tk.MustInterDirc("select /*+ NO_INDEX_MERGE() */ 1;") 3106 c.Assert(tk.Se.GetStochastikVars().StmtCtx.NoIndexMergeHint, IsTrue) 3107 tk.MustInterDirc("select /*+ NO_INDEX_MERGE(), NO_INDEX_MERGE() */ 1;") 3108 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3109 c.Assert(tk.Se.GetStochastikVars().GetEnableIndexMerge(), IsTrue) 3110 3111 // Test USE_TOJA hint 3112 tk.Se.GetStochastikVars().SetAllowInSubqToJoinAnPosetDagg(true) 3113 tk.MustInterDirc("select /*+ USE_TOJA(false) */ 1;") 3114 c.Assert(tk.Se.GetStochastikVars().GetAllowInSubqToJoinAnPosetDagg(), IsFalse) 3115 tk.Se.GetStochastikVars().SetAllowInSubqToJoinAnPosetDagg(false) 3116 tk.MustInterDirc("select /*+ USE_TOJA(true) */ 1;") 3117 c.Assert(tk.Se.GetStochastikVars().GetAllowInSubqToJoinAnPosetDagg(), IsTrue) 3118 tk.MustInterDirc("select /*+ USE_TOJA(false), USE_TOJA(true) */ 1;") 3119 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3120 c.Assert(tk.Se.GetStochastikVars().GetAllowInSubqToJoinAnPosetDagg(), IsTrue) 3121 3122 // Test USE_CASCADES hint 3123 tk.Se.GetStochastikVars().SetEnableCascadesCausetAppend(true) 3124 tk.MustInterDirc("select /*+ USE_CASCADES(false) */ 1;") 3125 c.Assert(tk.Se.GetStochastikVars().GetEnableCascadesCausetAppend(), IsFalse) 3126 tk.Se.GetStochastikVars().SetEnableCascadesCausetAppend(false) 3127 tk.MustInterDirc("select /*+ USE_CASCADES(true) */ 1;") 3128 c.Assert(tk.Se.GetStochastikVars().GetEnableCascadesCausetAppend(), IsTrue) 3129 tk.MustInterDirc("select /*+ USE_CASCADES(false), USE_CASCADES(true) */ 1;") 3130 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3131 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "USE_CASCADES() is defined more than once, only the last definition takes effect: USE_CASCADES(true)") 3132 c.Assert(tk.Se.GetStochastikVars().GetEnableCascadesCausetAppend(), IsTrue) 3133 3134 // Test READ_CONSISTENT_REPLICA hint 3135 tk.Se.GetStochastikVars().SetReplicaRead(ekv.ReplicaReadLeader) 3136 tk.MustInterDirc("select /*+ READ_CONSISTENT_REPLICA() */ 1;") 3137 c.Assert(tk.Se.GetStochastikVars().GetReplicaRead(), Equals, ekv.ReplicaReadFollower) 3138 tk.MustInterDirc("select /*+ READ_CONSISTENT_REPLICA(), READ_CONSISTENT_REPLICA() */ 1;") 3139 c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1) 3140 c.Assert(tk.Se.GetStochastikVars().GetReplicaRead(), Equals, ekv.ReplicaReadFollower) 3141 } 3142 3143 func (s *testStochastikSuite3) TestPessimisticLockOnPartition(c *C) { 3144 // This test checks that 'select ... for uFIDelate' locks the partition instead of the causet. 3145 // Cover a bug that causet ID is used to encode the dagger key mistakenly. 3146 tk := testkit.NewTestKit(c, s.causetstore) 3147 tk.MustInterDirc("use test") 3148 tk.MustInterDirc(`create causet if not exists foruFIDelate_on_partition ( 3149 age int not null primary key, 3150 nickname varchar(20) not null, 3151 gender int not null default 0, 3152 first_name varchar(30) not null default '', 3153 last_name varchar(20) not null default '', 3154 full_name varchar(60) as (concat(first_name, ' ', last_name)), 3155 index idx_nickname (nickname) 3156 ) partition by range (age) ( 3157 partition child values less than (18), 3158 partition young values less than (30), 3159 partition midbse values less than (50), 3160 partition old values less than (123) 3161 );`) 3162 tk.MustInterDirc("insert into foruFIDelate_on_partition (`age`, `nickname`) values (25, 'cosven');") 3163 3164 tk1 := testkit.NewTestKit(c, s.causetstore) 3165 tk1.MustInterDirc("use test") 3166 3167 tk.MustInterDirc("begin pessimistic") 3168 tk.MustQuery("select * from foruFIDelate_on_partition where age=25 for uFIDelate").Check(testkit.Rows("25 cosven 0 ")) 3169 tk1.MustInterDirc("begin pessimistic") 3170 3171 ch := make(chan int32, 5) 3172 go func() { 3173 tk1.MustInterDirc("uFIDelate foruFIDelate_on_partition set first_name='sw' where age=25") 3174 ch <- 0 3175 tk1.MustInterDirc("commit") 3176 ch <- 0 3177 }() 3178 3179 // Leave 50ms for tk1 to run, tk1 should be blocked at the uFIDelate operation. 3180 time.Sleep(50 * time.Millisecond) 3181 ch <- 1 3182 3183 tk.MustInterDirc("commit") 3184 // tk1 should be blocked until tk commit, check the order. 3185 c.Assert(<-ch, Equals, int32(1)) 3186 c.Assert(<-ch, Equals, int32(0)) 3187 <-ch // wait for goroutine to quit. 3188 3189 // Once again... 3190 // This time, test for the uFIDelate-uFIDelate conflict. 3191 tk.MustInterDirc("begin pessimistic") 3192 tk.MustInterDirc("uFIDelate foruFIDelate_on_partition set first_name='sw' where age=25") 3193 tk1.MustInterDirc("begin pessimistic") 3194 3195 go func() { 3196 tk1.MustInterDirc("uFIDelate foruFIDelate_on_partition set first_name = 'xxx' where age=25") 3197 ch <- 0 3198 tk1.MustInterDirc("commit") 3199 ch <- 0 3200 }() 3201 3202 // Leave 50ms for tk1 to run, tk1 should be blocked at the uFIDelate operation. 3203 time.Sleep(50 * time.Millisecond) 3204 ch <- 1 3205 3206 tk.MustInterDirc("commit") 3207 // tk1 should be blocked until tk commit, check the order. 3208 c.Assert(<-ch, Equals, int32(1)) 3209 c.Assert(<-ch, Equals, int32(0)) 3210 <-ch // wait for goroutine to quit. 3211 } 3212 3213 func (s *testSchemaSuite) TestTxnSize(c *C) { 3214 tk := testkit.NewTestKitWithInit(c, s.causetstore) 3215 tk.MustInterDirc("drop causet if exists txn_size") 3216 tk.MustInterDirc("create causet txn_size (k int , v varchar(64))") 3217 tk.MustInterDirc("begin") 3218 tk.MustInterDirc("insert txn_size values (1, 'dfaasdfsdf')") 3219 tk.MustInterDirc("insert txn_size values (2, 'dsdfaasdfsdf')") 3220 tk.MustInterDirc("insert txn_size values (3, 'abcdefghijkl')") 3221 txn, err := tk.Se.Txn(false) 3222 c.Assert(err, IsNil) 3223 c.Assert(txn.Size() > 0, IsTrue) 3224 } 3225 3226 func (s *testStochastikSuite2) TestPerStmtTaskID(c *C) { 3227 tk := testkit.NewTestKitWithInit(c, s.causetstore) 3228 tk.MustInterDirc("create causet task_id (v int)") 3229 3230 tk.MustInterDirc("begin") 3231 tk.MustInterDirc("select * from task_id where v > 10") 3232 taskID1 := tk.Se.GetStochastikVars().StmtCtx.TaskID 3233 tk.MustInterDirc("select * from task_id where v < 5") 3234 taskID2 := tk.Se.GetStochastikVars().StmtCtx.TaskID 3235 tk.MustInterDirc("commit") 3236 3237 c.Assert(taskID1 != taskID2, IsTrue) 3238 } 3239 3240 func (s *testStochastikSerialSuite) TestDoDBSJobQuit(c *C) { 3241 // test https://github.com/whtcorpsinc/milevadb/issues/18714, imitate DM's use environment 3242 // use isolated causetstore, because in below failpoint we will cancel its context 3243 causetstore, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.MockEinsteinDB)) 3244 c.Assert(err, IsNil) 3245 defer causetstore.Close() 3246 dom, err := stochastik.BootstrapStochastik(causetstore) 3247 c.Assert(err, IsNil) 3248 defer dom.Close() 3249 se, err := stochastik.CreateStochastik(causetstore) 3250 c.Assert(err, IsNil) 3251 defer se.Close() 3252 3253 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/storeCloseInLoop", `return`), IsNil) 3254 defer failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/storeCloseInLoop") 3255 3256 // this DBS call will enter deadloop before this fix 3257 err = dom.DBS().CreateSchema(se, perceptron.NewCIStr("testschema"), nil) 3258 c.Assert(err.Error(), Equals, "context canceled") 3259 } 3260 3261 func (s *testBackupRestoreSuite) TestBackupAndRestore(c *C) { 3262 // only run BR ALLEGROALLEGROSQL integration test with einsteindb causetstore. 3263 if *withEinsteinDB { 3264 cfg := config.GetGlobalConfig() 3265 cfg.CausetStore = "einsteindb" 3266 cfg.Path = s.FIDelAddr 3267 config.StoreGlobalConfig(cfg) 3268 tk := testkit.NewTestKitWithInit(c, s.causetstore) 3269 tk.MustInterDirc("create database if not exists br") 3270 tk.MustInterDirc("use br") 3271 tk.MustInterDirc("create causet t1(v int)") 3272 tk.MustInterDirc("insert into t1 values (1)") 3273 tk.MustInterDirc("insert into t1 values (2)") 3274 tk.MustInterDirc("insert into t1 values (3)") 3275 tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3")) 3276 3277 tk.MustInterDirc("create database if not exists br02") 3278 tk.MustInterDirc("use br02") 3279 tk.MustInterDirc("create causet t1(v int)") 3280 3281 tmFIDelir := path.Join(os.TemFIDelir(), "bk1") 3282 os.RemoveAll(tmFIDelir) 3283 // backup database to tmp dir 3284 tk.MustQuery("backup database * to 'local://" + tmFIDelir + "'") 3285 3286 // remove database for recovery 3287 tk.MustInterDirc("drop database br") 3288 tk.MustInterDirc("drop database br02") 3289 3290 // restore database with backup data 3291 tk.MustQuery("restore database * from 'local://" + tmFIDelir + "'") 3292 tk.MustInterDirc("use br") 3293 tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3")) 3294 tk.MustInterDirc("drop database br") 3295 tk.MustInterDirc("drop database br02") 3296 } 3297 }