github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/ddl_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 interlock_test 15 16 import ( 17 "context" 18 "fmt" 19 "math" 20 "strconv" 21 "strings" 22 "time" 23 24 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 25 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 26 "github.com/whtcorpsinc/BerolinaSQL/terror" 27 . "github.com/whtcorpsinc/check" 28 "github.com/whtcorpsinc/failpoint" 29 "github.com/whtcorpsinc/milevadb/causet" 30 causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded" 31 "github.com/whtcorpsinc/milevadb/dbs" 32 dbsutil "github.com/whtcorpsinc/milevadb/dbs/soliton" 33 dbssolitonutil "github.com/whtcorpsinc/milevadb/dbs/solitonutil" 34 "github.com/whtcorpsinc/milevadb/ekv" 35 "github.com/whtcorpsinc/milevadb/petri" 36 "github.com/whtcorpsinc/milevadb/schemareplicant" 37 "github.com/whtcorpsinc/milevadb/soliton/chunk" 38 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 39 "github.com/whtcorpsinc/milevadb/soliton/testkit" 40 "github.com/whtcorpsinc/milevadb/spacetime" 41 "github.com/whtcorpsinc/milevadb/spacetime/autoid" 42 "github.com/whtcorpsinc/milevadb/stochastikctx" 43 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 44 "github.com/whtcorpsinc/milevadb/types" 45 ) 46 47 func (s *testSuite6) TestTruncateBlock(c *C) { 48 tk := testkit.NewTestKit(c, s.causetstore) 49 tk.MustInterDirc("use test") 50 tk.MustInterDirc(`drop causet if exists truncate_test;`) 51 tk.MustInterDirc(`create causet truncate_test (a int)`) 52 tk.MustInterDirc(`insert truncate_test values (1),(2),(3)`) 53 result := tk.MustQuery("select * from truncate_test") 54 result.Check(testkit.Events("1", "2", "3")) 55 tk.MustInterDirc("truncate causet truncate_test") 56 result = tk.MustQuery("select * from truncate_test") 57 result.Check(nil) 58 } 59 60 // TestInTxnInterDircDBSFail tests the following case: 61 // 1. InterDircute the ALLEGROALLEGROSQL of "begin"; 62 // 2. A ALLEGROALLEGROSQL that will fail to execute; 63 // 3. InterDircute DBS. 64 func (s *testSuite6) TestInTxnInterDircDBSFail(c *C) { 65 tk := testkit.NewTestKit(c, s.causetstore) 66 tk.MustInterDirc("use test") 67 tk.MustInterDirc("create causet t (i int key);") 68 tk.MustInterDirc("insert into t values (1);") 69 tk.MustInterDirc("begin;") 70 tk.MustInterDirc("insert into t values (1);") 71 _, err := tk.InterDirc("truncate causet t;") 72 c.Assert(err.Error(), Equals, "[ekv:1062]Duplicate entry '1' for key 'PRIMARY'") 73 result := tk.MustQuery("select count(*) from t") 74 result.Check(testkit.Events("1")) 75 } 76 77 func (s *testSuite6) TestCreateBlock(c *C) { 78 tk := testkit.NewTestKit(c, s.causetstore) 79 tk.MustInterDirc("use test") 80 // Test create an exist database 81 _, err := tk.InterDirc("CREATE database test") 82 c.Assert(err, NotNil) 83 84 // Test create an exist causet 85 tk.MustInterDirc("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 86 87 _, err = tk.InterDirc("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 88 c.Assert(err, NotNil) 89 90 // Test "if not exist" 91 tk.MustInterDirc("CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 92 93 // Testcase for https://github.com/whtcorpsinc/milevadb/issues/312 94 tk.MustInterDirc(`create causet issue312_1 (c float(24));`) 95 tk.MustInterDirc(`create causet issue312_2 (c float(25));`) 96 rs, err := tk.InterDirc(`desc issue312_1`) 97 c.Assert(err, IsNil) 98 ctx := context.Background() 99 req := rs.NewChunk() 100 it := chunk.NewIterator4Chunk(req) 101 for { 102 err1 := rs.Next(ctx, req) 103 c.Assert(err1, IsNil) 104 if req.NumEvents() == 0 { 105 break 106 } 107 for event := it.Begin(); event != it.End(); event = it.Next() { 108 c.Assert(event.GetString(1), Equals, "float") 109 } 110 } 111 rs, err = tk.InterDirc(`desc issue312_2`) 112 c.Assert(err, IsNil) 113 req = rs.NewChunk() 114 it = chunk.NewIterator4Chunk(req) 115 for { 116 err1 := rs.Next(ctx, req) 117 c.Assert(err1, IsNil) 118 if req.NumEvents() == 0 { 119 break 120 } 121 for event := it.Begin(); event != it.End(); event = it.Next() { 122 c.Assert(req.GetEvent(0).GetString(1), Equals, "double") 123 } 124 } 125 126 // test multiple defCauslate specified in defCausumn when create. 127 tk.MustInterDirc("drop causet if exists test_multiple_defCausumn_defCauslate;") 128 tk.MustInterDirc("create causet test_multiple_defCausumn_defCauslate (a char(1) defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 129 t, err := petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("test_multiple_defCausumn_defCauslate")) 130 c.Assert(err, IsNil) 131 c.Assert(t.DefCauss()[0].Charset, Equals, "utf8") 132 c.Assert(t.DefCauss()[0].DefCauslate, Equals, "utf8_general_ci") 133 c.Assert(t.Meta().Charset, Equals, "utf8mb4") 134 c.Assert(t.Meta().DefCauslate, Equals, "utf8mb4_bin") 135 136 tk.MustInterDirc("drop causet if exists test_multiple_defCausumn_defCauslate;") 137 tk.MustInterDirc("create causet test_multiple_defCausumn_defCauslate (a char(1) charset utf8 defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 138 t, err = petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("test_multiple_defCausumn_defCauslate")) 139 c.Assert(err, IsNil) 140 c.Assert(t.DefCauss()[0].Charset, Equals, "utf8") 141 c.Assert(t.DefCauss()[0].DefCauslate, Equals, "utf8_general_ci") 142 c.Assert(t.Meta().Charset, Equals, "utf8mb4") 143 c.Assert(t.Meta().DefCauslate, Equals, "utf8mb4_bin") 144 145 // test Err case for multiple defCauslate specified in defCausumn when create. 146 tk.MustInterDirc("drop causet if exists test_err_multiple_defCauslate;") 147 _, err = tk.InterDirc("create causet test_err_multiple_defCauslate (a char(1) charset utf8mb4 defCauslate utf8_unicode_ci defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 148 c.Assert(err, NotNil) 149 c.Assert(err.Error(), Equals, dbs.ErrDefCauslationCharsetMismatch.GenWithStackByArgs("utf8_unicode_ci", "utf8mb4").Error()) 150 151 tk.MustInterDirc("drop causet if exists test_err_multiple_defCauslate;") 152 _, err = tk.InterDirc("create causet test_err_multiple_defCauslate (a char(1) defCauslate utf8_unicode_ci defCauslate utf8mb4_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 153 c.Assert(err, NotNil) 154 c.Assert(err.Error(), Equals, dbs.ErrDefCauslationCharsetMismatch.GenWithStackByArgs("utf8mb4_general_ci", "utf8").Error()) 155 156 // causet option is auto-increment 157 tk.MustInterDirc("drop causet if exists create_auto_increment_test;") 158 tk.MustInterDirc("create causet create_auto_increment_test (id int not null auto_increment, name varchar(255), primary key(id)) auto_increment = 999;") 159 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('aa')") 160 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('bb')") 161 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('cc')") 162 r := tk.MustQuery("select * from create_auto_increment_test;") 163 r.Check(testkit.Events("999 aa", "1000 bb", "1001 cc")) 164 tk.MustInterDirc("drop causet create_auto_increment_test") 165 tk.MustInterDirc("create causet create_auto_increment_test (id int not null auto_increment, name varchar(255), primary key(id)) auto_increment = 1999;") 166 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('aa')") 167 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('bb')") 168 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('cc')") 169 r = tk.MustQuery("select * from create_auto_increment_test;") 170 r.Check(testkit.Events("1999 aa", "2000 bb", "2001 cc")) 171 tk.MustInterDirc("drop causet create_auto_increment_test") 172 tk.MustInterDirc("create causet create_auto_increment_test (id int not null auto_increment, name varchar(255), key(id)) auto_increment = 1000;") 173 tk.MustInterDirc("insert into create_auto_increment_test (name) values ('aa')") 174 r = tk.MustQuery("select * from create_auto_increment_test;") 175 r.Check(testkit.Events("1000 aa")) 176 177 // Test for `drop causet if exists`. 178 tk.MustInterDirc("drop causet if exists t_if_exists;") 179 tk.MustQuery("show warnings;").Check(testkit.Events("Note 1051 Unknown causet 'test.t_if_exists'")) 180 tk.MustInterDirc("create causet if not exists t1_if_exists(c int)") 181 tk.MustInterDirc("drop causet if exists t1_if_exists,t2_if_exists,t3_if_exists") 182 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|1051|Unknown causet 'test.t2_if_exists'", "Note|1051|Unknown causet 'test.t3_if_exists'")) 183 } 184 185 func (s *testSuite6) TestCreateView(c *C) { 186 tk := testkit.NewTestKit(c, s.causetstore) 187 tk.MustInterDirc("use test") 188 //create an source causet 189 tk.MustInterDirc("CREATE TABLE source_block (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") 190 //test create a exist view 191 tk.MustInterDirc("CREATE VIEW view_t AS select id , name from source_block") 192 defer tk.MustInterDirc("DROP VIEW IF EXISTS view_t") 193 _, err := tk.InterDirc("CREATE VIEW view_t AS select id , name from source_block") 194 c.Assert(err.Error(), Equals, "[schemaReplicant:1050]Block 'test.view_t' already exists") 195 //create view on nonexistent causet 196 _, err = tk.InterDirc("create view v1 (c,d) as select a,b from t1") 197 c.Assert(err.Error(), Equals, "[schemaReplicant:1146]Block 'test.t1' doesn't exist") 198 //simple view 199 tk.MustInterDirc("create causet t1 (a int ,b int)") 200 tk.MustInterDirc("insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)") 201 //view with defCausList and SelectFieldExpr 202 tk.MustInterDirc("create view v1 (c) as select b+1 from t1") 203 //view with SelectFieldExpr 204 tk.MustInterDirc("create view v2 as select b+1 from t1") 205 //view with SelectFieldExpr and AsName 206 tk.MustInterDirc("create view v3 as select b+1 as c from t1") 207 //view with defCausList , SelectField and AsName 208 tk.MustInterDirc("create view v4 (c) as select b+1 as d from t1") 209 //view with select wild card 210 tk.MustInterDirc("create view v5 as select * from t1") 211 tk.MustInterDirc("create view v6 (c,d) as select * from t1") 212 _, err = tk.InterDirc("create view v7 (c,d,e) as select * from t1") 213 c.Assert(err.Error(), Equals, dbs.ErrViewWrongList.Error()) 214 //drop multiple views in a memex 215 tk.MustInterDirc("drop view v1,v2,v3,v4,v5,v6") 216 //view with variable 217 tk.MustInterDirc("create view v1 (c,d) as select a,b+@@global.max_user_connections from t1") 218 _, err = tk.InterDirc("create view v1 (c,d) as select a,b from t1 where a = @@global.max_user_connections") 219 c.Assert(err.Error(), Equals, "[schemaReplicant:1050]Block 'test.v1' already exists") 220 tk.MustInterDirc("drop view v1") 221 //view with different defCaus counts 222 _, err = tk.InterDirc("create view v1 (c,d,e) as select a,b from t1 ") 223 c.Assert(err.Error(), Equals, dbs.ErrViewWrongList.Error()) 224 _, err = tk.InterDirc("create view v1 (c) as select a,b from t1 ") 225 c.Assert(err.Error(), Equals, dbs.ErrViewWrongList.Error()) 226 //view with or_replace flag 227 tk.MustInterDirc("drop view if exists v1") 228 tk.MustInterDirc("create view v1 (c,d) as select a,b from t1") 229 tk.MustInterDirc("create or replace view v1 (c,d) as select a,b from t1 ") 230 tk.MustInterDirc("create causet if not exists t1 (a int ,b int)") 231 _, err = tk.InterDirc("create or replace view t1 as select * from t1") 232 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "t1", "VIEW").Error()) 233 // create view using prepare 234 tk.MustInterDirc(`prepare stmt from "create view v10 (x) as select 1";`) 235 tk.MustInterDirc("execute stmt") 236 237 // create view on union 238 tk.MustInterDirc("drop causet if exists t1, t2") 239 tk.MustInterDirc("drop view if exists v") 240 _, err = tk.InterDirc("create view v as select * from t1 union select * from t2") 241 c.Assert(terror.ErrorEqual(err, schemareplicant.ErrBlockNotExists), IsTrue) 242 tk.MustInterDirc("create causet t1(a int, b int)") 243 tk.MustInterDirc("create causet t2(a int, b int)") 244 tk.MustInterDirc("insert into t1 values(1,2), (1,1), (1,2)") 245 tk.MustInterDirc("insert into t2 values(1,1),(1,3)") 246 tk.MustInterDirc("create definer='root'@'localhost' view v as select * from t1 union select * from t2") 247 tk.MustQuery("select * from v").Sort().Check(testkit.Events("1 1", "1 2", "1 3")) 248 tk.MustInterDirc("alter causet t1 drop defCausumn a") 249 _, err = tk.InterDirc("select * from v") 250 c.Assert(terror.ErrorEqual(err, causetembedded.ErrViewInvalid), IsTrue) 251 tk.MustInterDirc("alter causet t1 add defCausumn a int") 252 tk.MustQuery("select * from v").Sort().Check(testkit.Events("1 1", "1 3", "<nil> 1", "<nil> 2")) 253 tk.MustInterDirc("alter causet t1 drop defCausumn a") 254 tk.MustInterDirc("alter causet t2 drop defCausumn b") 255 _, err = tk.InterDirc("select * from v") 256 c.Assert(terror.ErrorEqual(err, causetembedded.ErrViewInvalid), IsTrue) 257 tk.MustInterDirc("drop view v") 258 259 tk.MustInterDirc("create view v as (select * from t1)") 260 tk.MustInterDirc("drop view v") 261 tk.MustInterDirc("create view v as (select * from t1 union select * from t2)") 262 tk.MustInterDirc("drop view v") 263 264 // Test for `drop view if exists`. 265 tk.MustInterDirc("drop view if exists v_if_exists;") 266 tk.MustQuery("show warnings;").Check(testkit.Events("Note 1051 Unknown causet 'test.v_if_exists'")) 267 tk.MustInterDirc("create view v1_if_exists as (select * from t1)") 268 tk.MustInterDirc("drop view if exists v1_if_exists,v2_if_exists,v3_if_exists") 269 tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Note|1051|Unknown causet 'test.v2_if_exists'", "Note|1051|Unknown causet 'test.v3_if_exists'")) 270 271 // Test for create nested view. 272 tk.MustInterDirc("create causet test_v_nested(a int)") 273 tk.MustInterDirc("create definer='root'@'localhost' view v_nested as select * from test_v_nested") 274 tk.MustInterDirc("create definer='root'@'localhost' view v_nested2 as select * from v_nested") 275 _, err = tk.InterDirc("create or replace definer='root'@'localhost' view v_nested as select * from v_nested2") 276 c.Assert(terror.ErrorEqual(err, causetembedded.ErrNoSuchBlock), IsTrue) 277 tk.MustInterDirc("drop causet test_v_nested") 278 tk.MustInterDirc("drop view v_nested, v_nested2") 279 } 280 281 func (s *testSuite6) TestIssue16250(c *C) { 282 tk := testkit.NewTestKit(c, s.causetstore) 283 tk.MustInterDirc("use test") 284 tk.MustInterDirc("create causet if not exists t(a int)") 285 tk.MustInterDirc("create view view_issue16250 as select * from t") 286 _, err := tk.InterDirc("truncate causet view_issue16250") 287 c.Assert(err.Error(), Equals, "[schemaReplicant:1146]Block 'test.view_issue16250' doesn't exist") 288 } 289 290 func (s testSuite6) TestTruncateSequence(c *C) { 291 tk := testkit.NewTestKit(c, s.causetstore) 292 tk.MustInterDirc("use test") 293 tk.MustInterDirc("create sequence if not exists seq") 294 _, err := tk.InterDirc("truncate causet seq") 295 c.Assert(err.Error(), Equals, "[schemaReplicant:1146]Block 'test.seq' doesn't exist") 296 tk.MustInterDirc("create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle") 297 _, err = tk.InterDirc("truncate causet seq1") 298 c.Assert(err.Error(), Equals, "[schemaReplicant:1146]Block 'test.seq1' doesn't exist") 299 tk.MustInterDirc("drop sequence if exists seq") 300 tk.MustInterDirc("drop sequence if exists seq1") 301 } 302 303 func (s *testSuite6) TestCreateViewWithOverlongDefCausName(c *C) { 304 tk := testkit.NewTestKit(c, s.causetstore) 305 tk.MustInterDirc("use test") 306 tk.MustInterDirc("create causet t(a int)") 307 defer tk.MustInterDirc("drop causet t") 308 tk.MustInterDirc("create view v as select distinct'" + strings.Repeat("a", 65) + "', " + 309 "max('" + strings.Repeat("b", 65) + "'), " + 310 "'cccccccccc', '" + strings.Repeat("d", 65) + "';") 311 resultCreateStmt := "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` ALLEGROALLEGROSQL SECURITY DEFINER VIEW `v` (`name_exp_1`, `name_exp_2`, `cccccccccc`, `name_exp_4`) AS SELECT DISTINCT '" + strings.Repeat("a", 65) + "',MAX('" + strings.Repeat("b", 65) + "'),'cccccccccc','" + strings.Repeat("d", 65) + "'" 312 tk.MustQuery("select * from v") 313 tk.MustQuery("select name_exp_1, name_exp_2, cccccccccc, name_exp_4 from v") 314 tk.MustQuery("show create view v").Check(testkit.Events("v " + resultCreateStmt + " ")) 315 tk.MustInterDirc("drop view v;") 316 tk.MustInterDirc(resultCreateStmt) 317 318 tk.MustInterDirc("drop view v ") 319 tk.MustInterDirc("create definer='root'@'localhost' view v as select 'a', '" + strings.Repeat("b", 65) + "' from t " + 320 "union select '" + strings.Repeat("c", 65) + "', " + 321 "count(distinct '" + strings.Repeat("b", 65) + "', " + 322 "'c');") 323 resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` ALLEGROALLEGROSQL SECURITY DEFINER VIEW `v` (`a`, `name_exp_2`) AS SELECT 'a','" + strings.Repeat("b", 65) + "' FROM `test`.`t` UNION SELECT '" + strings.Repeat("c", 65) + "',COUNT(DISTINCT '" + strings.Repeat("b", 65) + "', 'c')" 324 tk.MustQuery("select * from v") 325 tk.MustQuery("select a, name_exp_2 from v") 326 tk.MustQuery("show create view v").Check(testkit.Events("v " + resultCreateStmt + " ")) 327 tk.MustInterDirc("drop view v;") 328 tk.MustInterDirc(resultCreateStmt) 329 330 tk.MustInterDirc("drop view v ") 331 tk.MustInterDirc("create definer='root'@'localhost' view v as select 'a' as '" + strings.Repeat("b", 65) + "' from t;") 332 tk.MustQuery("select * from v") 333 tk.MustQuery("select name_exp_1 from v") 334 resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` ALLEGROALLEGROSQL SECURITY DEFINER VIEW `v` (`name_exp_1`) AS SELECT 'a' AS `" + strings.Repeat("b", 65) + "` FROM `test`.`t`" 335 tk.MustQuery("show create view v").Check(testkit.Events("v " + resultCreateStmt + " ")) 336 tk.MustInterDirc("drop view v;") 337 tk.MustInterDirc(resultCreateStmt) 338 339 tk.MustInterDirc("drop view v ") 340 err := tk.InterDircToErr("create view v(`" + strings.Repeat("b", 65) + "`) as select a from t;") 341 c.Assert(err.Error(), Equals, "[dbs:1059]Identifier name 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' is too long") 342 } 343 344 func (s *testSuite6) TestCreateDroFIDelatabase(c *C) { 345 tk := testkit.NewTestKit(c, s.causetstore) 346 tk.MustInterDirc("create database if not exists drop_test;") 347 tk.MustInterDirc("drop database if exists drop_test;") 348 tk.MustInterDirc("create database drop_test;") 349 tk.MustInterDirc("use drop_test;") 350 tk.MustInterDirc("drop database drop_test;") 351 _, err := tk.InterDirc("drop causet t;") 352 c.Assert(err.Error(), Equals, causetembedded.ErrNoDB.Error()) 353 err = tk.InterDircToErr("select * from t;") 354 c.Assert(err.Error(), Equals, causetembedded.ErrNoDB.Error()) 355 356 _, err = tk.InterDirc("drop database allegrosql") 357 c.Assert(err, NotNil) 358 359 tk.MustInterDirc("create database charset_test charset ascii;") 360 tk.MustQuery("show create database charset_test;").Check(solitonutil.EventsWithSep("|", 361 "charset_test|CREATE DATABASE `charset_test` /*!40100 DEFAULT CHARACTER SET ascii */", 362 )) 363 tk.MustInterDirc("drop database charset_test;") 364 tk.MustInterDirc("create database charset_test charset binary;") 365 tk.MustQuery("show create database charset_test;").Check(solitonutil.EventsWithSep("|", 366 "charset_test|CREATE DATABASE `charset_test` /*!40100 DEFAULT CHARACTER SET binary */", 367 )) 368 tk.MustInterDirc("drop database charset_test;") 369 tk.MustInterDirc("create database charset_test defCauslate utf8_general_ci;") 370 tk.MustQuery("show create database charset_test;").Check(solitonutil.EventsWithSep("|", 371 "charset_test|CREATE DATABASE `charset_test` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci */", 372 )) 373 tk.MustInterDirc("drop database charset_test;") 374 tk.MustInterDirc("create database charset_test charset utf8 defCauslate utf8_general_ci;") 375 tk.MustQuery("show create database charset_test;").Check(solitonutil.EventsWithSep("|", 376 "charset_test|CREATE DATABASE `charset_test` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci */", 377 )) 378 tk.MustGetErrMsg("create database charset_test charset utf8 defCauslate utf8mb4_unicode_ci;", "[dbs:1253]COLLATION 'utf8mb4_unicode_ci' is not valid for CHARACTER SET 'utf8'") 379 } 380 381 func (s *testSuite6) TestCreateDropBlock(c *C) { 382 tk := testkit.NewTestKit(c, s.causetstore) 383 tk.MustInterDirc("use test") 384 tk.MustInterDirc("create causet if not exists drop_test (a int)") 385 tk.MustInterDirc("drop causet if exists drop_test") 386 tk.MustInterDirc("create causet drop_test (a int)") 387 tk.MustInterDirc("drop causet drop_test") 388 389 _, err := tk.InterDirc("drop causet allegrosql.gc_delete_range") 390 c.Assert(err, NotNil) 391 } 392 393 func (s *testSuite6) TestCreateDropView(c *C) { 394 tk := testkit.NewTestKit(c, s.causetstore) 395 tk.MustInterDirc("use test") 396 tk.MustInterDirc("create or replace view drop_test as select 1,2") 397 398 _, err := tk.InterDirc("drop causet drop_test") 399 c.Assert(err.Error(), Equals, "[schemaReplicant:1051]Unknown causet 'test.drop_test'") 400 401 _, err = tk.InterDirc("drop view if exists drop_test") 402 c.Assert(err, IsNil) 403 404 _, err = tk.InterDirc("drop view allegrosql.gc_delete_range") 405 c.Assert(err.Error(), Equals, "Drop milevadb system causet 'allegrosql.gc_delete_range' is forbidden") 406 407 _, err = tk.InterDirc("drop view drop_test") 408 c.Assert(err.Error(), Equals, "[schemaReplicant:1051]Unknown causet 'test.drop_test'") 409 410 tk.MustInterDirc("create causet t_v(a int)") 411 _, err = tk.InterDirc("drop view t_v") 412 c.Assert(err.Error(), Equals, "[dbs:1347]'test.t_v' is not VIEW") 413 414 tk.MustInterDirc("create causet t_v1(a int, b int);") 415 tk.MustInterDirc("create causet t_v2(a int, b int);") 416 tk.MustInterDirc("create view v as select * from t_v1;") 417 tk.MustInterDirc("create or replace view v as select * from t_v2;") 418 tk.MustQuery("select * from information_schema.views where block_name ='v';").Check( 419 testkit.Events("def test v SELECT `test`.`t_v2`.`a`,`test`.`t_v2`.`b` FROM `test`.`t_v2` CASCADED NO @ DEFINER utf8mb4 utf8mb4_bin")) 420 } 421 422 func (s *testSuite6) TestCreateDropIndex(c *C) { 423 tk := testkit.NewTestKit(c, s.causetstore) 424 tk.MustInterDirc("use test") 425 tk.MustInterDirc("create causet if not exists drop_test (a int)") 426 tk.MustInterDirc("create index idx_a on drop_test (a)") 427 tk.MustInterDirc("drop index idx_a on drop_test") 428 tk.MustInterDirc("drop causet drop_test") 429 } 430 431 func (s *testSuite6) TestAlterBlockAddDeferredCauset(c *C) { 432 tk := testkit.NewTestKit(c, s.causetstore) 433 tk.MustInterDirc("use test") 434 tk.MustInterDirc("create causet if not exists alter_test (c1 int)") 435 tk.MustInterDirc("insert into alter_test values(1)") 436 tk.MustInterDirc("alter causet alter_test add defCausumn c2 timestamp default current_timestamp") 437 time.Sleep(1 * time.Millisecond) 438 now := time.Now().Add(-1 * time.Millisecond).Format(types.TimeFormat) 439 r, err := tk.InterDirc("select c2 from alter_test") 440 c.Assert(err, IsNil) 441 req := r.NewChunk() 442 err = r.Next(context.Background(), req) 443 c.Assert(err, IsNil) 444 event := req.GetEvent(0) 445 c.Assert(event.Len(), Equals, 1) 446 c.Assert(now, GreaterEqual, event.GetTime(0).String()) 447 r.Close() 448 tk.MustInterDirc("alter causet alter_test add defCausumn c3 varchar(50) default 'CURRENT_TIMESTAMP'") 449 tk.MustQuery("select c3 from alter_test").Check(testkit.Events("CURRENT_TIMESTAMP")) 450 tk.MustInterDirc("create or replace view alter_view as select c1,c2 from alter_test") 451 _, err = tk.InterDirc("alter causet alter_view add defCausumn c4 varchar(50)") 452 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error()) 453 tk.MustInterDirc("drop view alter_view") 454 tk.MustInterDirc("create sequence alter_seq") 455 _, err = tk.InterDirc("alter causet alter_seq add defCausumn c int") 456 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error()) 457 tk.MustInterDirc("drop sequence alter_seq") 458 } 459 460 func (s *testSuite6) TestAlterBlockAddDeferredCausets(c *C) { 461 tk := testkit.NewTestKit(c, s.causetstore) 462 tk.MustInterDirc("use test") 463 tk.MustInterDirc("create causet if not exists alter_test (c1 int)") 464 tk.MustInterDirc("insert into alter_test values(1)") 465 tk.MustInterDirc("alter causet alter_test add defCausumn c2 timestamp default current_timestamp, add defCausumn c8 varchar(50) default 'CURRENT_TIMESTAMP'") 466 tk.MustInterDirc("alter causet alter_test add defCausumn (c7 timestamp default current_timestamp, c3 varchar(50) default 'CURRENT_TIMESTAMP')") 467 r, err := tk.InterDirc("select c2 from alter_test") 468 c.Assert(err, IsNil) 469 req := r.NewChunk() 470 err = r.Next(context.Background(), req) 471 c.Assert(err, IsNil) 472 event := req.GetEvent(0) 473 c.Assert(event.Len(), Equals, 1) 474 r.Close() 475 tk.MustQuery("select c3 from alter_test").Check(testkit.Events("CURRENT_TIMESTAMP")) 476 tk.MustInterDirc("create or replace view alter_view as select c1,c2 from alter_test") 477 _, err = tk.InterDirc("alter causet alter_view add defCausumn (c4 varchar(50), c5 varchar(50))") 478 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error()) 479 tk.MustInterDirc("drop view alter_view") 480 tk.MustInterDirc("create sequence alter_seq") 481 _, err = tk.InterDirc("alter causet alter_seq add defCausumn (c1 int, c2 varchar(10))") 482 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error()) 483 tk.MustInterDirc("drop sequence alter_seq") 484 } 485 486 func (s *testSuite6) TestAddNotNullDeferredCausetNoDefault(c *C) { 487 tk := testkit.NewTestKit(c, s.causetstore) 488 tk.MustInterDirc("use test") 489 tk.MustInterDirc("create causet nn (c1 int)") 490 tk.MustInterDirc("insert nn values (1), (2)") 491 tk.MustInterDirc("alter causet nn add defCausumn c2 int not null") 492 493 tbl, err := petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("nn")) 494 c.Assert(err, IsNil) 495 defCaus2 := tbl.Meta().DeferredCausets[1] 496 c.Assert(defCaus2.DefaultValue, IsNil) 497 c.Assert(defCaus2.OriginDefaultValue, Equals, "0") 498 499 tk.MustQuery("select * from nn").Check(testkit.Events("1 0", "2 0")) 500 _, err = tk.InterDirc("insert nn (c1) values (3)") 501 c.Check(err, NotNil) 502 tk.MustInterDirc("set sql_mode=''") 503 tk.MustInterDirc("insert nn (c1) values (3)") 504 tk.MustQuery("select * from nn").Check(testkit.Events("1 0", "2 0", "3 0")) 505 } 506 507 func (s *testSuite6) TestAlterBlockModifyDeferredCauset(c *C) { 508 tk := testkit.NewTestKit(c, s.causetstore) 509 tk.MustInterDirc("use test") 510 tk.MustInterDirc("drop causet if exists mc") 511 tk.MustInterDirc("create causet mc(c1 int, c2 varchar(10), c3 bit)") 512 _, err := tk.InterDirc("alter causet mc modify defCausumn c1 short") 513 c.Assert(err, NotNil) 514 tk.MustInterDirc("alter causet mc modify defCausumn c1 bigint") 515 516 _, err = tk.InterDirc("alter causet mc modify defCausumn c2 blob") 517 c.Assert(err, NotNil) 518 519 _, err = tk.InterDirc("alter causet mc modify defCausumn c2 varchar(8)") 520 c.Assert(err, NotNil) 521 tk.MustInterDirc("alter causet mc modify defCausumn c2 varchar(11)") 522 tk.MustInterDirc("alter causet mc modify defCausumn c2 text(13)") 523 tk.MustInterDirc("alter causet mc modify defCausumn c2 text") 524 tk.MustInterDirc("alter causet mc modify defCausumn c3 bit") 525 result := tk.MustQuery("show create causet mc") 526 createALLEGROSQL := result.Events()[0][1] 527 expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL,\n `c3` bit(1) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" 528 c.Assert(createALLEGROSQL, Equals, expected) 529 tk.MustInterDirc("create or replace view alter_view as select c1,c2 from mc") 530 _, err = tk.InterDirc("alter causet alter_view modify defCausumn c2 text") 531 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error()) 532 tk.MustInterDirc("drop view alter_view") 533 tk.MustInterDirc("create sequence alter_seq") 534 _, err = tk.InterDirc("alter causet alter_seq modify defCausumn c int") 535 c.Assert(err.Error(), Equals, dbs.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error()) 536 tk.MustInterDirc("drop sequence alter_seq") 537 538 // test multiple defCauslate modification in defCausumn. 539 tk.MustInterDirc("drop causet if exists modify_defCausumn_multiple_defCauslate") 540 tk.MustInterDirc("create causet modify_defCausumn_multiple_defCauslate (a char(1) defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 541 _, err = tk.InterDirc("alter causet modify_defCausumn_multiple_defCauslate modify defCausumn a char(1) defCauslate utf8mb4_bin;") 542 c.Assert(err, IsNil) 543 t, err := petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("modify_defCausumn_multiple_defCauslate")) 544 c.Assert(err, IsNil) 545 c.Assert(t.DefCauss()[0].Charset, Equals, "utf8mb4") 546 c.Assert(t.DefCauss()[0].DefCauslate, Equals, "utf8mb4_bin") 547 c.Assert(t.Meta().Charset, Equals, "utf8mb4") 548 c.Assert(t.Meta().DefCauslate, Equals, "utf8mb4_bin") 549 550 tk.MustInterDirc("drop causet if exists modify_defCausumn_multiple_defCauslate;") 551 tk.MustInterDirc("create causet modify_defCausumn_multiple_defCauslate (a char(1) defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 552 _, err = tk.InterDirc("alter causet modify_defCausumn_multiple_defCauslate modify defCausumn a char(1) charset utf8mb4 defCauslate utf8mb4_bin;") 553 c.Assert(err, IsNil) 554 t, err = petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("modify_defCausumn_multiple_defCauslate")) 555 c.Assert(err, IsNil) 556 c.Assert(t.DefCauss()[0].Charset, Equals, "utf8mb4") 557 c.Assert(t.DefCauss()[0].DefCauslate, Equals, "utf8mb4_bin") 558 c.Assert(t.Meta().Charset, Equals, "utf8mb4") 559 c.Assert(t.Meta().DefCauslate, Equals, "utf8mb4_bin") 560 561 // test Err case for multiple defCauslate modification in defCausumn. 562 tk.MustInterDirc("drop causet if exists err_modify_multiple_defCauslate;") 563 tk.MustInterDirc("create causet err_modify_multiple_defCauslate (a char(1) defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 564 _, err = tk.InterDirc("alter causet err_modify_multiple_defCauslate modify defCausumn a char(1) charset utf8mb4 defCauslate utf8_bin;") 565 c.Assert(err, NotNil) 566 c.Assert(err.Error(), Equals, dbs.ErrDefCauslationCharsetMismatch.GenWithStackByArgs("utf8_bin", "utf8mb4").Error()) 567 568 tk.MustInterDirc("drop causet if exists err_modify_multiple_defCauslate;") 569 tk.MustInterDirc("create causet err_modify_multiple_defCauslate (a char(1) defCauslate utf8_bin defCauslate utf8_general_ci) charset utf8mb4 defCauslate utf8mb4_bin") 570 _, err = tk.InterDirc("alter causet err_modify_multiple_defCauslate modify defCausumn a char(1) defCauslate utf8_bin defCauslate utf8mb4_bin;") 571 c.Assert(err, NotNil) 572 c.Assert(err.Error(), Equals, dbs.ErrDefCauslationCharsetMismatch.GenWithStackByArgs("utf8mb4_bin", "utf8").Error()) 573 574 } 575 576 func (s *testSuite6) TestDefaultDBAfterDropCurDB(c *C) { 577 tk := testkit.NewTestKit(c, s.causetstore) 578 579 testALLEGROSQL := `create database if not exists test_db CHARACTER SET latin1 COLLATE latin1_swedish_ci;` 580 tk.MustInterDirc(testALLEGROSQL) 581 582 testALLEGROSQL = `use test_db;` 583 tk.MustInterDirc(testALLEGROSQL) 584 tk.MustQuery(`select database();`).Check(testkit.Events("test_db")) 585 tk.MustQuery(`select @@character_set_database;`).Check(testkit.Events("latin1")) 586 tk.MustQuery(`select @@defCauslation_database;`).Check(testkit.Events("latin1_swedish_ci")) 587 588 testALLEGROSQL = `drop database test_db;` 589 tk.MustInterDirc(testALLEGROSQL) 590 tk.MustQuery(`select database();`).Check(testkit.Events("<nil>")) 591 tk.MustQuery(`select @@character_set_database;`).Check(testkit.Events(allegrosql.DefaultCharset)) 592 tk.MustQuery(`select @@defCauslation_database;`).Check(testkit.Events(allegrosql.DefaultDefCauslationName)) 593 } 594 595 func (s *testSuite6) TestDeferredCausetCharsetAndDefCauslate(c *C) { 596 tk := testkit.NewTestKit(c, s.causetstore) 597 dbName := "defCaus_charset_defCauslate" 598 tk.MustInterDirc("create database " + dbName) 599 tk.MustInterDirc("use " + dbName) 600 tests := []struct { 601 defCausType string 602 charset string 603 defCauslates string 604 exptCharset string 605 exptDefCauslate string 606 errMsg string 607 }{ 608 { 609 defCausType: "varchar(10)", 610 charset: "charset utf8", 611 defCauslates: "defCauslate utf8_bin", 612 exptCharset: "utf8", 613 exptDefCauslate: "utf8_bin", 614 errMsg: "", 615 }, 616 { 617 defCausType: "varchar(10)", 618 charset: "charset utf8mb4", 619 defCauslates: "", 620 exptCharset: "utf8mb4", 621 exptDefCauslate: "utf8mb4_bin", 622 errMsg: "", 623 }, 624 { 625 defCausType: "varchar(10)", 626 charset: "charset utf16", 627 defCauslates: "", 628 exptCharset: "", 629 exptDefCauslate: "", 630 errMsg: "Unknown charset utf16", 631 }, 632 { 633 defCausType: "varchar(10)", 634 charset: "charset latin1", 635 defCauslates: "", 636 exptCharset: "latin1", 637 exptDefCauslate: "latin1_bin", 638 errMsg: "", 639 }, 640 { 641 defCausType: "varchar(10)", 642 charset: "charset binary", 643 defCauslates: "", 644 exptCharset: "binary", 645 exptDefCauslate: "binary", 646 errMsg: "", 647 }, 648 { 649 defCausType: "varchar(10)", 650 charset: "charset ascii", 651 defCauslates: "", 652 exptCharset: "ascii", 653 exptDefCauslate: "ascii_bin", 654 errMsg: "", 655 }, 656 } 657 sctx := tk.Se.(stochastikctx.Context) 658 dm := petri.GetPetri(sctx) 659 for i, tt := range tests { 660 tblName := fmt.Sprintf("t%d", i) 661 allegrosql := fmt.Sprintf("create causet %s (a %s %s %s)", tblName, tt.defCausType, tt.charset, tt.defCauslates) 662 if tt.errMsg == "" { 663 tk.MustInterDirc(allegrosql) 664 is := dm.SchemaReplicant() 665 c.Assert(is, NotNil) 666 667 tb, err := is.BlockByName(perceptron.NewCIStr(dbName), perceptron.NewCIStr(tblName)) 668 c.Assert(err, IsNil) 669 c.Assert(tb.Meta().DeferredCausets[0].Charset, Equals, tt.exptCharset, Commentf(allegrosql)) 670 c.Assert(tb.Meta().DeferredCausets[0].DefCauslate, Equals, tt.exptDefCauslate, Commentf(allegrosql)) 671 } else { 672 _, err := tk.InterDirc(allegrosql) 673 c.Assert(err, NotNil, Commentf(allegrosql)) 674 } 675 } 676 tk.MustInterDirc("drop database " + dbName) 677 } 678 679 func (s *testSuite6) TestTooLargeIdentifierLength(c *C) { 680 tk := testkit.NewTestKit(c, s.causetstore) 681 682 // for database. 683 dbName1, dbName2 := strings.Repeat("a", allegrosql.MaxDatabaseNameLength), strings.Repeat("a", allegrosql.MaxDatabaseNameLength+1) 684 tk.MustInterDirc(fmt.Sprintf("create database %s", dbName1)) 685 tk.MustInterDirc(fmt.Sprintf("drop database %s", dbName1)) 686 _, err := tk.InterDirc(fmt.Sprintf("create database %s", dbName2)) 687 c.Assert(err.Error(), Equals, fmt.Sprintf("[dbs:1059]Identifier name '%s' is too long", dbName2)) 688 689 // for causet. 690 tk.MustInterDirc("use test") 691 blockName1, blockName2 := strings.Repeat("b", allegrosql.MaxBlockNameLength), strings.Repeat("b", allegrosql.MaxBlockNameLength+1) 692 tk.MustInterDirc(fmt.Sprintf("create causet %s(c int)", blockName1)) 693 tk.MustInterDirc(fmt.Sprintf("drop causet %s", blockName1)) 694 _, err = tk.InterDirc(fmt.Sprintf("create causet %s(c int)", blockName2)) 695 c.Assert(err.Error(), Equals, fmt.Sprintf("[dbs:1059]Identifier name '%s' is too long", blockName2)) 696 697 // for defCausumn. 698 tk.MustInterDirc("drop causet if exists t;") 699 defCausumnName1, defCausumnName2 := strings.Repeat("c", allegrosql.MaxDeferredCausetNameLength), strings.Repeat("c", allegrosql.MaxDeferredCausetNameLength+1) 700 tk.MustInterDirc(fmt.Sprintf("create causet t(%s int)", defCausumnName1)) 701 tk.MustInterDirc("drop causet t") 702 _, err = tk.InterDirc(fmt.Sprintf("create causet t(%s int)", defCausumnName2)) 703 c.Assert(err.Error(), Equals, fmt.Sprintf("[dbs:1059]Identifier name '%s' is too long", defCausumnName2)) 704 705 // for index. 706 tk.MustInterDirc("create causet t(c int);") 707 indexName1, indexName2 := strings.Repeat("d", allegrosql.MaxIndexIdentifierLen), strings.Repeat("d", allegrosql.MaxIndexIdentifierLen+1) 708 tk.MustInterDirc(fmt.Sprintf("create index %s on t(c)", indexName1)) 709 tk.MustInterDirc(fmt.Sprintf("drop index %s on t", indexName1)) 710 _, err = tk.InterDirc(fmt.Sprintf("create index %s on t(c)", indexName2)) 711 c.Assert(err.Error(), Equals, fmt.Sprintf("[dbs:1059]Identifier name '%s' is too long", indexName2)) 712 713 // for create causet with index. 714 tk.MustInterDirc("drop causet t;") 715 _, err = tk.InterDirc(fmt.Sprintf("create causet t(c int, index %s(c));", indexName2)) 716 c.Assert(err.Error(), Equals, fmt.Sprintf("[dbs:1059]Identifier name '%s' is too long", indexName2)) 717 } 718 719 func (s *testSuite8) TestShardEventIDBits(c *C) { 720 tk := testkit.NewTestKit(c, s.causetstore) 721 722 tk.MustInterDirc("use test") 723 tk.MustInterDirc("create causet t (a int) shard_row_id_bits = 15") 724 for i := 0; i < 100; i++ { 725 tk.MustInterDirc("insert into t values (?)", i) 726 } 727 728 dom := petri.GetPetri(tk.Se) 729 tbl, err := dom.SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 730 c.Assert(err, IsNil) 731 732 assertCountAndShard := func(t causet.Block, expectCount int) { 733 var hasShardedID bool 734 var count int 735 c.Assert(tk.Se.NewTxn(context.Background()), IsNil) 736 err = t.IterRecords(tk.Se, t.FirstKey(), nil, func(h ekv.Handle, rec []types.Causet, defcaus []*causet.DeferredCauset) (more bool, err error) { 737 c.Assert(h.IntValue(), GreaterEqual, int64(0)) 738 first8bits := h.IntValue() >> 56 739 if first8bits > 0 { 740 hasShardedID = true 741 } 742 count++ 743 return true, nil 744 }) 745 c.Assert(err, IsNil) 746 c.Assert(count, Equals, expectCount) 747 c.Assert(hasShardedID, IsTrue) 748 } 749 750 assertCountAndShard(tbl, 100) 751 752 // After PR 10759, shard_row_id_bits is supported with blocks with auto_increment defCausumn. 753 tk.MustInterDirc("create causet auto (id int not null auto_increment unique) shard_row_id_bits = 4") 754 tk.MustInterDirc("alter causet auto shard_row_id_bits = 5") 755 tk.MustInterDirc("drop causet auto") 756 tk.MustInterDirc("create causet auto (id int not null auto_increment unique) shard_row_id_bits = 0") 757 tk.MustInterDirc("alter causet auto shard_row_id_bits = 5") 758 tk.MustInterDirc("drop causet auto") 759 tk.MustInterDirc("create causet auto (id int not null auto_increment unique)") 760 tk.MustInterDirc("alter causet auto shard_row_id_bits = 5") 761 tk.MustInterDirc("drop causet auto") 762 tk.MustInterDirc("create causet auto (id int not null auto_increment unique) shard_row_id_bits = 4") 763 tk.MustInterDirc("alter causet auto shard_row_id_bits = 0") 764 tk.MustInterDirc("drop causet auto") 765 766 // After PR 10759, shard_row_id_bits is not supported with pk_is_handle blocks. 767 err = tk.InterDircToErr("create causet auto (id int not null auto_increment primary key, b int) shard_row_id_bits = 4") 768 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported shard_row_id_bits for causet with primary key as event id") 769 tk.MustInterDirc("create causet auto (id int not null auto_increment primary key, b int) shard_row_id_bits = 0") 770 err = tk.InterDircToErr("alter causet auto shard_row_id_bits = 5") 771 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported shard_row_id_bits for causet with primary key as event id") 772 tk.MustInterDirc("alter causet auto shard_row_id_bits = 0") 773 774 // Hack an existing causet with shard_row_id_bits and primary key as handle 775 EDB, ok := dom.SchemaReplicant().SchemaByName(perceptron.NewCIStr("test")) 776 c.Assert(ok, IsTrue) 777 tbl, err = dom.SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("auto")) 778 tblInfo := tbl.Meta() 779 tblInfo.ShardEventIDBits = 5 780 tblInfo.MaxShardEventIDBits = 5 781 782 ekv.RunInNewTxn(s.causetstore, false, func(txn ekv.Transaction) error { 783 m := spacetime.NewMeta(txn) 784 _, err = m.GenSchemaVersion() 785 c.Assert(err, IsNil) 786 c.Assert(m.UFIDelateBlock(EDB.ID, tblInfo), IsNil) 787 return nil 788 }) 789 err = dom.Reload() 790 c.Assert(err, IsNil) 791 792 tk.MustInterDirc("insert auto(b) values (1), (3), (5)") 793 tk.MustQuery("select id from auto order by id").Check(testkit.Events("1", "2", "3")) 794 795 tk.MustInterDirc("alter causet auto shard_row_id_bits = 0") 796 tk.MustInterDirc("drop causet auto") 797 798 // Test shard_row_id_bits with auto_increment defCausumn 799 tk.MustInterDirc("create causet auto (a int, b int auto_increment unique) shard_row_id_bits = 15") 800 for i := 0; i < 100; i++ { 801 tk.MustInterDirc("insert into auto(a) values (?)", i) 802 } 803 tbl, err = dom.SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("auto")) 804 assertCountAndShard(tbl, 100) 805 prevB, err := strconv.Atoi(tk.MustQuery("select b from auto where a=0").Events()[0][0].(string)) 806 c.Assert(err, IsNil) 807 for i := 1; i < 100; i++ { 808 b, err := strconv.Atoi(tk.MustQuery(fmt.Sprintf("select b from auto where a=%d", i)).Events()[0][0].(string)) 809 c.Assert(err, IsNil) 810 c.Assert(b, Greater, prevB) 811 prevB = b 812 } 813 814 // Test overflow 815 tk.MustInterDirc("drop causet if exists t1") 816 tk.MustInterDirc("create causet t1 (a int) shard_row_id_bits = 15") 817 defer tk.MustInterDirc("drop causet if exists t1") 818 819 tbl, err = dom.SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t1")) 820 c.Assert(err, IsNil) 821 maxID := 1<<(64-15-1) - 1 822 err = tbl.RebaseAutoID(tk.Se, int64(maxID)-1, false, autoid.EventIDAllocType) 823 c.Assert(err, IsNil) 824 tk.MustInterDirc("insert into t1 values(1)") 825 826 // continue inserting will fail. 827 _, err = tk.InterDirc("insert into t1 values(2)") 828 c.Assert(autoid.ErrAutoincReadFailed.Equal(err), IsTrue, Commentf("err:%v", err)) 829 _, err = tk.InterDirc("insert into t1 values(3)") 830 c.Assert(autoid.ErrAutoincReadFailed.Equal(err), IsTrue, Commentf("err:%v", err)) 831 } 832 833 type testAutoRandomSuite struct { 834 *baseTestSuite 835 } 836 837 func (s *testAutoRandomSuite) SetUpTest(c *C) { 838 solitonutil.ConfigTestUtils.SetupAutoRandomTestConfig() 839 } 840 841 func (s *testAutoRandomSuite) TearDownTest(c *C) { 842 solitonutil.ConfigTestUtils.RestoreAutoRandomTestConfig() 843 } 844 845 func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) { 846 tk := testkit.NewTestKit(c, s.causetstore) 847 848 tk.MustInterDirc("create database if not exists test_auto_random_bits") 849 defer tk.MustInterDirc("drop database if exists test_auto_random_bits") 850 tk.MustInterDirc("use test_auto_random_bits") 851 tk.MustInterDirc("drop causet if exists t") 852 853 extractAllHandles := func() []int64 { 854 allHds, err := dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test_auto_random_bits", "t") 855 c.Assert(err, IsNil) 856 return allHds 857 } 858 859 tk.MustInterDirc("set @@allow_auto_random_explicit_insert = true") 860 861 tk.MustInterDirc("create causet t (a bigint primary key auto_random(15), b int)") 862 for i := 0; i < 100; i++ { 863 tk.MustInterDirc("insert into t(b) values (?)", i) 864 } 865 allHandles := extractAllHandles() 866 tk.MustInterDirc("drop causet t") 867 868 // Test auto random id number. 869 c.Assert(len(allHandles), Equals, 100) 870 // Test the handles are not all zero. 871 allZero := true 872 for _, h := range allHandles { 873 allZero = allZero && (h>>(64-16)) == 0 874 } 875 c.Assert(allZero, IsFalse) 876 // Test non-shard-bits part of auto random id is monotonic increasing and continuous. 877 orderedHandles := solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 15, allegrosql.TypeLonglong) 878 size := int64(len(allHandles)) 879 for i := int64(1); i <= size; i++ { 880 c.Assert(i, Equals, orderedHandles[i-1]) 881 } 882 883 // Test explicit insert. 884 autoRandBitsUpperBound := 2<<47 - 1 885 tk.MustInterDirc("create causet t (a bigint primary key auto_random(15), b int)") 886 for i := -10; i < 10; i++ { 887 tk.MustInterDirc(fmt.Sprintf("insert into t values(%d, %d)", i+autoRandBitsUpperBound, i)) 888 } 889 _, err := tk.InterDirc("insert into t (b) values (0)") 890 c.Assert(err, NotNil) 891 c.Assert(err.Error(), Equals, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) 892 tk.MustInterDirc("drop causet t") 893 894 // Test overflow. 895 tk.MustInterDirc("create causet t (a bigint primary key auto_random(15), b int)") 896 // Here we cannot fill the all values for a `bigint` defCausumn, 897 // so firstly we rebase auto_rand to the position before overflow. 898 tk.MustInterDirc(fmt.Sprintf("insert into t values (%d, %d)", autoRandBitsUpperBound, 1)) 899 _, err = tk.InterDirc("insert into t (b) values (0)") 900 c.Assert(err, NotNil) 901 c.Assert(err.Error(), Equals, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) 902 tk.MustInterDirc("drop causet t") 903 904 tk.MustInterDirc("create causet t (a bigint primary key auto_random(15), b int)") 905 tk.MustInterDirc("insert into t values (1, 2)") 906 tk.MustInterDirc(fmt.Sprintf("uFIDelate t set a = %d where a = 1", autoRandBitsUpperBound)) 907 _, err = tk.InterDirc("insert into t (b) values (0)") 908 c.Assert(err, NotNil) 909 c.Assert(err.Error(), Equals, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) 910 tk.MustInterDirc("drop causet t") 911 912 // Test insert negative integers explicitly won't trigger rebase. 913 tk.MustInterDirc("create causet t (a bigint primary key auto_random(15), b int)") 914 for i := 1; i <= 100; i++ { 915 tk.MustInterDirc("insert into t(b) values (?)", i) 916 tk.MustInterDirc("insert into t(a, b) values (?, ?)", -i, i) 917 } 918 // orderedHandles should be [-100, -99, ..., -2, -1, 1, 2, ..., 99, 100] 919 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(extractAllHandles(), 15, allegrosql.TypeLonglong) 920 size = int64(len(allHandles)) 921 for i := int64(0); i < 100; i++ { 922 c.Assert(orderedHandles[i], Equals, i-100) 923 } 924 for i := int64(100); i < size; i++ { 925 c.Assert(orderedHandles[i], Equals, i-99) 926 } 927 tk.MustInterDirc("drop causet t") 928 929 // Test signed/unsigned types. 930 tk.MustInterDirc("create causet t (a bigint primary key auto_random(10), b int)") 931 for i := 0; i < 100; i++ { 932 tk.MustInterDirc("insert into t (b) values(?)", i) 933 } 934 for _, h := range extractAllHandles() { 935 // Sign bit should be reserved. 936 c.Assert(h > 0, IsTrue) 937 } 938 tk.MustInterDirc("drop causet t") 939 940 tk.MustInterDirc("create causet t (a bigint unsigned primary key auto_random(10), b int)") 941 for i := 0; i < 100; i++ { 942 tk.MustInterDirc("insert into t (b) values(?)", i) 943 } 944 signBitUnused := true 945 for _, h := range extractAllHandles() { 946 signBitUnused = signBitUnused && (h > 0) 947 } 948 // Sign bit should be used for shard. 949 c.Assert(signBitUnused, IsFalse) 950 tk.MustInterDirc("drop causet t;") 951 952 // Test rename causet does not affect incremental part of auto_random ID. 953 tk.MustInterDirc("create database test_auto_random_bits_rename;") 954 tk.MustInterDirc("create causet t (a bigint auto_random primary key);") 955 for i := 0; i < 10; i++ { 956 tk.MustInterDirc("insert into t values ();") 957 } 958 tk.MustInterDirc("alter causet t rename to test_auto_random_bits_rename.t1;") 959 for i := 0; i < 10; i++ { 960 tk.MustInterDirc("insert into test_auto_random_bits_rename.t1 values ();") 961 } 962 tk.MustInterDirc("alter causet test_auto_random_bits_rename.t1 rename to t;") 963 for i := 0; i < 10; i++ { 964 tk.MustInterDirc("insert into t values ();") 965 } 966 uniqueHandles := make(map[int64]struct{}) 967 for _, h := range extractAllHandles() { 968 uniqueHandles[h&((1<<(63-5))-1)] = struct{}{} 969 } 970 c.Assert(len(uniqueHandles), Equals, 30) 971 tk.MustInterDirc("drop database test_auto_random_bits_rename;") 972 tk.MustInterDirc("drop causet t;") 973 } 974 975 func (s *testAutoRandomSuite) TestAutoRandomBlockOption(c *C) { 976 tk := testkit.NewTestKit(c, s.causetstore) 977 tk.MustInterDirc("use test") 978 979 // test causet option is auto-random 980 tk.MustInterDirc("drop causet if exists auto_random_block_option") 981 tk.MustInterDirc("create causet auto_random_block_option (a bigint auto_random(5) key) auto_random_base = 1000") 982 t, err := petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("auto_random_block_option")) 983 c.Assert(err, IsNil) 984 c.Assert(t.Meta().AutoRandID, Equals, int64(1000)) 985 tk.MustInterDirc("insert into auto_random_block_option values (),(),(),(),()") 986 allHandles, err := dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "auto_random_block_option") 987 c.Assert(err, IsNil) 988 c.Assert(len(allHandles), Equals, 5) 989 // Test non-shard-bits part of auto random id is monotonic increasing and continuous. 990 orderedHandles := solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 991 size := int64(len(allHandles)) 992 for i := int64(0); i < size; i++ { 993 c.Assert(i+1000, Equals, orderedHandles[i]) 994 } 995 996 tk.MustInterDirc("drop causet if exists alter_block_auto_random_option") 997 tk.MustInterDirc("create causet alter_block_auto_random_option (a bigint primary key auto_random(4), b int)") 998 t, err = petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("alter_block_auto_random_option")) 999 c.Assert(err, IsNil) 1000 c.Assert(t.Meta().AutoRandID, Equals, int64(0)) 1001 tk.MustInterDirc("insert into alter_block_auto_random_option values(),(),(),(),()") 1002 allHandles, err = dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "alter_block_auto_random_option") 1003 c.Assert(err, IsNil) 1004 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1005 size = int64(len(allHandles)) 1006 for i := int64(0); i < size; i++ { 1007 c.Assert(orderedHandles[i], Equals, i+1) 1008 } 1009 tk.MustInterDirc("delete from alter_block_auto_random_option") 1010 1011 // alter causet to change the auto_random option (it will dismiss the local allocator cache) 1012 // To avoid the new base is in the range of local cache, which will leading the next 1013 // value is not what we rebased, because the local cache is dropped, here we choose 1014 // a quite big value to do this. 1015 tk.MustInterDirc("alter causet alter_block_auto_random_option auto_random_base = 3000000") 1016 t, err = petri.GetPetri(tk.Se).SchemaReplicant().BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("alter_block_auto_random_option")) 1017 c.Assert(err, IsNil) 1018 c.Assert(t.Meta().AutoRandID, Equals, int64(3000000)) 1019 tk.MustInterDirc("insert into alter_block_auto_random_option values(),(),(),(),()") 1020 allHandles, err = dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "alter_block_auto_random_option") 1021 c.Assert(err, IsNil) 1022 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1023 size = int64(len(allHandles)) 1024 for i := int64(0); i < size; i++ { 1025 c.Assert(orderedHandles[i], Equals, i+3000000) 1026 } 1027 tk.MustInterDirc("drop causet alter_block_auto_random_option") 1028 1029 // Alter auto_random_base on non auto_random causet. 1030 tk.MustInterDirc("create causet alter_auto_random_normal (a int)") 1031 _, err = tk.InterDirc("alter causet alter_auto_random_normal auto_random_base = 100") 1032 c.Assert(err, NotNil) 1033 c.Assert(strings.Contains(err.Error(), autoid.AutoRandomRebaseNotApplicable), IsTrue, Commentf(err.Error())) 1034 } 1035 1036 // Test filter different HoTT of allocators. 1037 // In special dbs type, for example: 1038 // 1: CausetActionRenameBlock : it will abandon all the old allocators. 1039 // 2: CausetActionRebaseAutoID : it will drop event-id-type allocator. 1040 // 3: CausetActionModifyBlockAutoIdCache : it will drop event-id-type allocator. 1041 // 3: CausetActionRebaseAutoRandomBase : it will drop auto-rand-type allocator. 1042 func (s *testAutoRandomSuite) TestFilterDifferentSlabPredictors(c *C) { 1043 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1044 tk.MustInterDirc("use test") 1045 tk.MustInterDirc("drop causet if exists t") 1046 tk.MustInterDirc("drop causet if exists t1") 1047 1048 tk.MustInterDirc("create causet t(a bigint auto_random(5) key, b int auto_increment unique)") 1049 tk.MustInterDirc("insert into t values()") 1050 tk.MustQuery("select b from t").Check(testkit.Events("1")) 1051 allHandles, err := dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "t") 1052 c.Assert(err, IsNil) 1053 c.Assert(len(allHandles), Equals, 1) 1054 orderedHandles := solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1055 c.Assert(orderedHandles[0], Equals, int64(1)) 1056 tk.MustInterDirc("delete from t") 1057 1058 // Test rebase auto_increment. 1059 tk.MustInterDirc("alter causet t auto_increment 3000000") 1060 tk.MustInterDirc("insert into t values()") 1061 tk.MustQuery("select b from t").Check(testkit.Events("3000000")) 1062 allHandles, err = dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "t") 1063 c.Assert(err, IsNil) 1064 c.Assert(len(allHandles), Equals, 1) 1065 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1066 c.Assert(orderedHandles[0], Equals, int64(2)) 1067 tk.MustInterDirc("delete from t") 1068 1069 // Test rebase auto_random. 1070 tk.MustInterDirc("alter causet t auto_random_base 3000000") 1071 tk.MustInterDirc("insert into t values()") 1072 tk.MustQuery("select b from t").Check(testkit.Events("3000001")) 1073 allHandles, err = dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "t") 1074 c.Assert(err, IsNil) 1075 c.Assert(len(allHandles), Equals, 1) 1076 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1077 c.Assert(orderedHandles[0], Equals, int64(3000000)) 1078 tk.MustInterDirc("delete from t") 1079 1080 // Test rename causet. 1081 tk.MustInterDirc("rename causet t to t1") 1082 tk.MustInterDirc("insert into t1 values()") 1083 res := tk.MustQuery("select b from t1") 1084 strInt64, err := strconv.ParseInt(res.Events()[0][0].(string), 10, 64) 1085 c.Assert(err, IsNil) 1086 c.Assert(strInt64, Greater, int64(3000002)) 1087 allHandles, err = dbssolitonutil.ExtractAllBlockHandles(tk.Se, "test", "t1") 1088 c.Assert(err, IsNil) 1089 c.Assert(len(allHandles), Equals, 1) 1090 orderedHandles = solitonutil.ConfigTestUtils.MaskSortHandles(allHandles, 5, allegrosql.TypeLonglong) 1091 c.Assert(orderedHandles[0], Greater, int64(3000001)) 1092 } 1093 1094 func (s *testSuite6) TestMaxHandleAddIndex(c *C) { 1095 tk := testkit.NewTestKit(c, s.causetstore) 1096 1097 tk.MustInterDirc("use test") 1098 tk.MustInterDirc("create causet t(a bigint PRIMARY KEY, b int)") 1099 tk.MustInterDirc(fmt.Sprintf("insert into t values(%v, 1)", math.MaxInt64)) 1100 tk.MustInterDirc(fmt.Sprintf("insert into t values(%v, 1)", math.MinInt64)) 1101 tk.MustInterDirc("alter causet t add index idx_b(b)") 1102 tk.MustInterDirc("admin check causet t") 1103 1104 tk.MustInterDirc("create causet t1(a bigint UNSIGNED PRIMARY KEY, b int)") 1105 tk.MustInterDirc(fmt.Sprintf("insert into t1 values(%v, 1)", uint64(math.MaxUint64))) 1106 tk.MustInterDirc(fmt.Sprintf("insert into t1 values(%v, 1)", 0)) 1107 tk.MustInterDirc("alter causet t1 add index idx_b(b)") 1108 tk.MustInterDirc("admin check causet t1") 1109 } 1110 1111 func (s *testSuite6) TestSetDBSReorgWorkerCnt(c *C) { 1112 tk := testkit.NewTestKit(c, s.causetstore) 1113 tk.MustInterDirc("use test") 1114 err := dbsutil.LoadDBSReorgVars(tk.Se) 1115 c.Assert(err, IsNil) 1116 c.Assert(variable.GetDBSReorgWorkerCounter(), Equals, int32(variable.DefMilevaDBDBSReorgWorkerCount)) 1117 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = 1") 1118 err = dbsutil.LoadDBSReorgVars(tk.Se) 1119 c.Assert(err, IsNil) 1120 c.Assert(variable.GetDBSReorgWorkerCounter(), Equals, int32(1)) 1121 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = 100") 1122 err = dbsutil.LoadDBSReorgVars(tk.Se) 1123 c.Assert(err, IsNil) 1124 c.Assert(variable.GetDBSReorgWorkerCounter(), Equals, int32(100)) 1125 _, err = tk.InterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = invalid_val") 1126 c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) 1127 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = 100") 1128 err = dbsutil.LoadDBSReorgVars(tk.Se) 1129 c.Assert(err, IsNil) 1130 c.Assert(variable.GetDBSReorgWorkerCounter(), Equals, int32(100)) 1131 _, err = tk.InterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = -1") 1132 c.Assert(terror.ErrorEqual(err, variable.ErrWrongValueForVar), IsTrue, Commentf("err %v", err)) 1133 1134 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = 100") 1135 res := tk.MustQuery("select @@global.milevadb_dbs_reorg_worker_cnt") 1136 res.Check(testkit.Events("100")) 1137 1138 res = tk.MustQuery("select @@global.milevadb_dbs_reorg_worker_cnt") 1139 res.Check(testkit.Events("100")) 1140 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_worker_cnt = 100") 1141 res = tk.MustQuery("select @@global.milevadb_dbs_reorg_worker_cnt") 1142 res.Check(testkit.Events("100")) 1143 } 1144 1145 func (s *testSuite6) TestSetDBSReorgBatchSize(c *C) { 1146 tk := testkit.NewTestKit(c, s.causetstore) 1147 tk.MustInterDirc("use test") 1148 err := dbsutil.LoadDBSReorgVars(tk.Se) 1149 c.Assert(err, IsNil) 1150 c.Assert(variable.GetDBSReorgBatchSize(), Equals, int32(variable.DefMilevaDBDBSReorgBatchSize)) 1151 1152 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_batch_size = 1") 1153 tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect milevadb_dbs_reorg_batch_size value: '1'")) 1154 err = dbsutil.LoadDBSReorgVars(tk.Se) 1155 c.Assert(err, IsNil) 1156 c.Assert(variable.GetDBSReorgBatchSize(), Equals, variable.MinDBSReorgBatchSize) 1157 tk.MustInterDirc(fmt.Sprintf("set @@global.milevadb_dbs_reorg_batch_size = %v", variable.MaxDBSReorgBatchSize+1)) 1158 tk.MustQuery("show warnings;").Check(testkit.Events(fmt.Sprintf("Warning 1292 Truncated incorrect milevadb_dbs_reorg_batch_size value: '%d'", variable.MaxDBSReorgBatchSize+1))) 1159 err = dbsutil.LoadDBSReorgVars(tk.Se) 1160 c.Assert(err, IsNil) 1161 c.Assert(variable.GetDBSReorgBatchSize(), Equals, variable.MaxDBSReorgBatchSize) 1162 _, err = tk.InterDirc("set @@global.milevadb_dbs_reorg_batch_size = invalid_val") 1163 c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) 1164 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_batch_size = 100") 1165 err = dbsutil.LoadDBSReorgVars(tk.Se) 1166 c.Assert(err, IsNil) 1167 c.Assert(variable.GetDBSReorgBatchSize(), Equals, int32(100)) 1168 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_batch_size = -1") 1169 tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect milevadb_dbs_reorg_batch_size value: '-1'")) 1170 1171 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_batch_size = 100") 1172 res := tk.MustQuery("select @@global.milevadb_dbs_reorg_batch_size") 1173 res.Check(testkit.Events("100")) 1174 1175 res = tk.MustQuery("select @@global.milevadb_dbs_reorg_batch_size") 1176 res.Check(testkit.Events(fmt.Sprintf("%v", 100))) 1177 tk.MustInterDirc("set @@global.milevadb_dbs_reorg_batch_size = 1000") 1178 res = tk.MustQuery("select @@global.milevadb_dbs_reorg_batch_size") 1179 res.Check(testkit.Events("1000")) 1180 } 1181 1182 func (s *testSuite6) TestIllegalFunctionCall4GeneratedDeferredCausets(c *C) { 1183 tk := testkit.NewTestKit(c, s.causetstore) 1184 tk.MustInterDirc("use test") 1185 // Test create an exist database 1186 _, err := tk.InterDirc("CREATE database test") 1187 c.Assert(err, NotNil) 1188 1189 _, err = tk.InterDirc("create causet t1 (b double generated always as (rand()) virtual);") 1190 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) 1191 1192 _, err = tk.InterDirc("create causet t1 (a varchar(64), b varchar(1024) generated always as (load_file(a)) virtual);") 1193 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) 1194 1195 _, err = tk.InterDirc("create causet t1 (a datetime generated always as (curdate()) virtual);") 1196 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) 1197 1198 _, err = tk.InterDirc("create causet t1 (a datetime generated always as (current_time()) virtual);") 1199 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) 1200 1201 _, err = tk.InterDirc("create causet t1 (a datetime generated always as (current_timestamp()) virtual);") 1202 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) 1203 1204 _, err = tk.InterDirc("create causet t1 (a datetime, b varchar(10) generated always as (localtime()) virtual);") 1205 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) 1206 1207 _, err = tk.InterDirc("create causet t1 (a varchar(1024) generated always as (uuid()) virtual);") 1208 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) 1209 1210 _, err = tk.InterDirc("create causet t1 (a varchar(1024), b varchar(1024) generated always as (is_free_lock(a)) virtual);") 1211 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) 1212 1213 tk.MustInterDirc("create causet t1 (a bigint not null primary key auto_increment, b bigint, c bigint as (b + 1));") 1214 1215 _, err = tk.InterDirc("alter causet t1 add defCausumn d varchar(1024) generated always as (database());") 1216 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("d").Error()) 1217 1218 tk.MustInterDirc("alter causet t1 add defCausumn d bigint generated always as (b + 1); ") 1219 1220 _, err = tk.InterDirc("alter causet t1 modify defCausumn d bigint generated always as (connection_id());") 1221 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("d").Error()) 1222 1223 _, err = tk.InterDirc("alter causet t1 change defCausumn c cc bigint generated always as (connection_id());") 1224 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetFunctionIsNotAllowed.GenWithStackByArgs("cc").Error()) 1225 } 1226 1227 func (s *testSuite6) TestGeneratedDeferredCausetRelatedDBS(c *C) { 1228 tk := testkit.NewTestKit(c, s.causetstore) 1229 tk.MustInterDirc("use test") 1230 // Test create an exist database 1231 _, err := tk.InterDirc("CREATE database test") 1232 c.Assert(err, NotNil) 1233 1234 _, err = tk.InterDirc("create causet t1 (a bigint not null primary key auto_increment, b bigint as (a + 1));") 1235 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetRefAutoInc.GenWithStackByArgs("b").Error()) 1236 1237 tk.MustInterDirc("create causet t1 (a bigint not null primary key auto_increment, b bigint, c bigint as (b + 1));") 1238 1239 _, err = tk.InterDirc("alter causet t1 add defCausumn d bigint generated always as (a + 1);") 1240 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetRefAutoInc.GenWithStackByArgs("d").Error()) 1241 1242 tk.MustInterDirc("alter causet t1 add defCausumn d bigint generated always as (b + 1);") 1243 1244 _, err = tk.InterDirc("alter causet t1 modify defCausumn d bigint generated always as (a + 1);") 1245 c.Assert(err.Error(), Equals, dbs.ErrGeneratedDeferredCausetRefAutoInc.GenWithStackByArgs("d").Error()) 1246 1247 _, err = tk.InterDirc("alter causet t1 add defCausumn e bigint as (z + 1);") 1248 c.Assert(err.Error(), Equals, dbs.ErrBadField.GenWithStackByArgs("z", "generated defCausumn function").Error()) 1249 1250 tk.MustInterDirc("drop causet t1;") 1251 } 1252 1253 func (s *testSuite6) TestSetDBSErrorCountLimit(c *C) { 1254 tk := testkit.NewTestKit(c, s.causetstore) 1255 tk.MustInterDirc("use test") 1256 err := dbsutil.LoadDBSVars(tk.Se) 1257 c.Assert(err, IsNil) 1258 c.Assert(variable.GetDBSErrorCountLimit(), Equals, int64(variable.DefMilevaDBDBSErrorCountLimit)) 1259 1260 tk.MustInterDirc("set @@global.milevadb_dbs_error_count_limit = -1") 1261 tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect milevadb_dbs_error_count_limit value: '-1'")) 1262 err = dbsutil.LoadDBSVars(tk.Se) 1263 c.Assert(err, IsNil) 1264 c.Assert(variable.GetDBSErrorCountLimit(), Equals, int64(0)) 1265 tk.MustInterDirc(fmt.Sprintf("set @@global.milevadb_dbs_error_count_limit = %v", uint64(math.MaxInt64)+1)) 1266 tk.MustQuery("show warnings;").Check(testkit.Events(fmt.Sprintf("Warning 1292 Truncated incorrect milevadb_dbs_error_count_limit value: '%d'", uint64(math.MaxInt64)+1))) 1267 err = dbsutil.LoadDBSVars(tk.Se) 1268 c.Assert(err, IsNil) 1269 c.Assert(variable.GetDBSErrorCountLimit(), Equals, int64(math.MaxInt64)) 1270 _, err = tk.InterDirc("set @@global.milevadb_dbs_error_count_limit = invalid_val") 1271 c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err)) 1272 tk.MustInterDirc("set @@global.milevadb_dbs_error_count_limit = 100") 1273 err = dbsutil.LoadDBSVars(tk.Se) 1274 c.Assert(err, IsNil) 1275 c.Assert(variable.GetDBSErrorCountLimit(), Equals, int64(100)) 1276 res := tk.MustQuery("select @@global.milevadb_dbs_error_count_limit") 1277 res.Check(testkit.Events("100")) 1278 } 1279 1280 // Test issue #9205, fix the precision problem for time type default values 1281 // See https://github.com/whtcorpsinc/milevadb/issues/9205 for details 1282 func (s *testSuite6) TestIssue9205(c *C) { 1283 tk := testkit.NewTestKit(c, s.causetstore) 1284 tk.MustInterDirc("use test") 1285 tk.MustInterDirc(`drop causet if exists t;`) 1286 tk.MustInterDirc(`create causet t(c time DEFAULT '12:12:12.8');`) 1287 tk.MustQuery("show create causet `t`").Check(solitonutil.EventsWithSep("|", 1288 ""+ 1289 "t CREATE TABLE `t` (\n"+ 1290 " `c` time DEFAULT '12:12:13'\n"+ 1291 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", 1292 )) 1293 tk.MustInterDirc(`alter causet t add defCausumn c1 time default '12:12:12.000000';`) 1294 tk.MustQuery("show create causet `t`").Check(solitonutil.EventsWithSep("|", 1295 ""+ 1296 "t CREATE TABLE `t` (\n"+ 1297 " `c` time DEFAULT '12:12:13',\n"+ 1298 " `c1` time DEFAULT '12:12:12'\n"+ 1299 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", 1300 )) 1301 1302 tk.MustInterDirc(`alter causet t alter defCausumn c1 set default '2020-02-01 12:12:10.4';`) 1303 tk.MustQuery("show create causet `t`").Check(solitonutil.EventsWithSep("|", 1304 ""+ 1305 "t CREATE TABLE `t` (\n"+ 1306 " `c` time DEFAULT '12:12:13',\n"+ 1307 " `c1` time DEFAULT '12:12:10'\n"+ 1308 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", 1309 )) 1310 1311 tk.MustInterDirc(`alter causet t modify c1 time DEFAULT '770:12:12.000000';`) 1312 tk.MustQuery("show create causet `t`").Check(solitonutil.EventsWithSep("|", 1313 ""+ 1314 "t CREATE TABLE `t` (\n"+ 1315 " `c` time DEFAULT '12:12:13',\n"+ 1316 " `c1` time DEFAULT '770:12:12'\n"+ 1317 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", 1318 )) 1319 } 1320 1321 func (s *testSuite6) TestCheckDefaultFsp(c *C) { 1322 tk := testkit.NewTestKit(c, s.causetstore) 1323 tk.MustInterDirc("use test") 1324 tk.MustInterDirc(`drop causet if exists t;`) 1325 1326 _, err := tk.InterDirc("create causet t ( tt timestamp default now(1));") 1327 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tt'") 1328 1329 _, err = tk.InterDirc("create causet t ( tt timestamp(1) default current_timestamp);") 1330 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tt'") 1331 1332 _, err = tk.InterDirc("create causet t ( tt timestamp(1) default now(2));") 1333 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tt'") 1334 1335 tk.MustInterDirc("create causet t ( tt timestamp(1) default now(1));") 1336 tk.MustInterDirc("create causet t2 ( tt timestamp default current_timestamp());") 1337 tk.MustInterDirc("create causet t3 ( tt timestamp default current_timestamp(0));") 1338 1339 _, err = tk.InterDirc("alter causet t add defCausumn ttt timestamp default now(2);") 1340 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'ttt'") 1341 1342 _, err = tk.InterDirc("alter causet t add defCausumn ttt timestamp(5) default current_timestamp;") 1343 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'ttt'") 1344 1345 _, err = tk.InterDirc("alter causet t add defCausumn ttt timestamp(5) default now(2);") 1346 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'ttt'") 1347 1348 _, err = tk.InterDirc("alter causet t modify defCausumn tt timestamp(1) default now();") 1349 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tt'") 1350 1351 _, err = tk.InterDirc("alter causet t modify defCausumn tt timestamp(4) default now(5);") 1352 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tt'") 1353 1354 _, err = tk.InterDirc("alter causet t change defCausumn tt tttt timestamp(4) default now(5);") 1355 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tttt'") 1356 1357 _, err = tk.InterDirc("alter causet t change defCausumn tt tttt timestamp(1) default now();") 1358 c.Assert(err.Error(), Equals, "[dbs:1067]Invalid default value for 'tttt'") 1359 } 1360 1361 func (s *testSuite6) TestTimestampMinDefaultValue(c *C) { 1362 tk := testkit.NewTestKit(c, s.causetstore) 1363 tk.MustInterDirc("use test") 1364 tk.MustInterDirc("drop causet if exists tdv;") 1365 tk.MustInterDirc("create causet tdv(a int);") 1366 tk.MustInterDirc("ALTER TABLE tdv ADD COLUMN ts timestamp DEFAULT '1970-01-01 08:00:01';") 1367 } 1368 1369 // this test will change the fail-point `mockAutoIDChange`, so we move it to the `testRecoverBlock` suite 1370 func (s *testRecoverBlock) TestRenameBlock(c *C) { 1371 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/spacetime/autoid/mockAutoIDChange", `return(true)`), IsNil) 1372 defer func() { 1373 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/spacetime/autoid/mockAutoIDChange"), IsNil) 1374 }() 1375 tk := testkit.NewTestKit(c, s.causetstore) 1376 1377 tk.MustInterDirc("create database rename1") 1378 tk.MustInterDirc("create database rename2") 1379 tk.MustInterDirc("create database rename3") 1380 tk.MustInterDirc("create causet rename1.t (a int primary key auto_increment)") 1381 tk.MustInterDirc("insert rename1.t values ()") 1382 tk.MustInterDirc("rename causet rename1.t to rename2.t") 1383 // Make sure the drop old database doesn't affect the rename3.t's operations. 1384 tk.MustInterDirc("drop database rename1") 1385 tk.MustInterDirc("insert rename2.t values ()") 1386 tk.MustInterDirc("rename causet rename2.t to rename3.t") 1387 tk.MustInterDirc("insert rename3.t values ()") 1388 tk.MustQuery("select * from rename3.t").Check(testkit.Events("1", "5001", "10001")) 1389 // Make sure the drop old database doesn't affect the rename3.t's operations. 1390 tk.MustInterDirc("drop database rename2") 1391 tk.MustInterDirc("insert rename3.t values ()") 1392 tk.MustQuery("select * from rename3.t").Check(testkit.Events("1", "5001", "10001", "10002")) 1393 tk.MustInterDirc("drop database rename3") 1394 1395 tk.MustInterDirc("create database rename1") 1396 tk.MustInterDirc("create database rename2") 1397 tk.MustInterDirc("create causet rename1.t (a int primary key auto_increment)") 1398 tk.MustInterDirc("rename causet rename1.t to rename2.t1") 1399 tk.MustInterDirc("insert rename2.t1 values ()") 1400 result := tk.MustQuery("select * from rename2.t1") 1401 result.Check(testkit.Events("1")) 1402 // Make sure the drop old database doesn't affect the t1's operations. 1403 tk.MustInterDirc("drop database rename1") 1404 tk.MustInterDirc("insert rename2.t1 values ()") 1405 result = tk.MustQuery("select * from rename2.t1") 1406 result.Check(testkit.Events("1", "2")) 1407 // Rename a causet to another causet in the same database. 1408 tk.MustInterDirc("rename causet rename2.t1 to rename2.t2") 1409 tk.MustInterDirc("insert rename2.t2 values ()") 1410 result = tk.MustQuery("select * from rename2.t2") 1411 result.Check(testkit.Events("1", "2", "5001")) 1412 tk.MustInterDirc("drop database rename2") 1413 1414 tk.MustInterDirc("create database rename1") 1415 tk.MustInterDirc("create database rename2") 1416 tk.MustInterDirc("create causet rename1.t (a int primary key auto_increment)") 1417 tk.MustInterDirc("insert rename1.t values ()") 1418 tk.MustInterDirc("rename causet rename1.t to rename2.t1") 1419 // Make sure the value is greater than autoid.step. 1420 tk.MustInterDirc("insert rename2.t1 values (100000)") 1421 tk.MustInterDirc("insert rename2.t1 values ()") 1422 result = tk.MustQuery("select * from rename2.t1") 1423 result.Check(testkit.Events("1", "100000", "100001")) 1424 _, err := tk.InterDirc("insert rename1.t values ()") 1425 c.Assert(err, NotNil) 1426 tk.MustInterDirc("drop database rename1") 1427 tk.MustInterDirc("drop database rename2") 1428 }