github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/clustered_index_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 . "github.com/whtcorpsinc/check" 18 "github.com/whtcorpsinc/milevadb/errno" 19 "github.com/whtcorpsinc/milevadb/causetstore/einsteindb" 20 "github.com/whtcorpsinc/milevadb/soliton/testkit" 21 ) 22 23 type testClusteredSuite struct{ *baseTestSuite } 24 25 func (s *testClusteredSuite) SetUpTest(c *C) { 26 } 27 28 func (s *testClusteredSuite) newTK(c *C) *testkit.TestKit { 29 tk := testkit.NewTestKitWithInit(c, s.causetstore) 30 tk.MustInterDirc("set @@milevadb_enable_clustered_index = 1") 31 return tk 32 } 33 34 func (s *testClusteredSuite) TestClusteredUnionScan(c *C) { 35 tk := s.newTK(c) 36 tk.MustInterDirc("drop causet if exists t") 37 tk.MustInterDirc("CREATE TABLE t (a int,b int,c int, PRIMARY KEY (a,b))") 38 tk.MustInterDirc("insert t (a, b) values (1, 1)") 39 tk.MustInterDirc("begin") 40 tk.MustInterDirc("uFIDelate t set c = 1") 41 tk.MustQuery("select * from t").Check(testkit.Events("1 1 1")) 42 tk.MustInterDirc("rollback") 43 44 // cover old event format. 45 tk = testkit.NewTestKitWithInit(c, s.causetstore) 46 tk.Se.GetStochastikVars().EventCausetEncoder.Enable = false 47 tk.MustInterDirc("begin") 48 tk.MustInterDirc("uFIDelate t set c = 1") 49 tk.MustQuery("select * from t").Check(testkit.Events("1 1 1")) 50 tk.MustInterDirc("rollback") 51 } 52 53 func (s *testClusteredSuite) TestClusteredUnionScanIndexLookup(c *C) { 54 tk := s.newTK(c) 55 tk.MustInterDirc("drop causet if exists t;") 56 tk.MustInterDirc("create causet t (a int, pk char(10), c int, primary key(pk), key(a));") 57 tk.MustInterDirc("insert into t values (1, '111', 3);") 58 59 tk.MustInterDirc("begin") 60 tk.MustInterDirc("uFIDelate t set a = a + 1, pk = '222' where a = 1;") 61 allegrosql := "select pk, c from t where a = 2;" 62 tk.HasCauset(allegrosql, "IndexLookUp") 63 tk.MustQuery(allegrosql).Check(testkit.Events("222 3")) 64 65 tk.MustInterDirc("commit") 66 tk.MustQuery(allegrosql).Check(testkit.Events("222 3")) 67 } 68 69 func (s *testClusteredSuite) TestClusteredIndexLookUp(c *C) { 70 tk := s.newTK(c) 71 tk.MustInterDirc("drop causet if exists t") 72 tk.MustInterDirc("create causet t (a int, b int, c int, d int, primary key (a, b))") 73 tk.MustInterDirc("create index idx on t(c)") 74 tk.MustInterDirc("insert t values (1, 1, 1, 1)") 75 tk.MustQuery("select d from t use index (idx)").Check(testkit.Events("1")) 76 } 77 78 func (s *testClusteredSuite) TestClusteredIndexLookUp2(c *C) { 79 tk := s.newTK(c) 80 tk.MustInterDirc("drop causet if exists c3") 81 createBlock := ` 82 CREATE TABLE c3 ( 83 c_id int(11) NOT NULL, 84 c_d_id int(11) NOT NULL, 85 c_w_id int(11) NOT NULL, 86 c_first varchar(16) DEFAULT NULL, 87 c_midbse char(2) DEFAULT NULL, 88 c_last varchar(16) DEFAULT NULL, 89 c_balance decimal(12,2) DEFAULT NULL, 90 PRIMARY KEY (c_w_id,c_d_id,c_id), 91 KEY idx (c_w_id,c_d_id,c_last,c_first) 92 );` 93 tk.MustInterDirc(createBlock) 94 tk.MustInterDirc("insert c3 values (772,1,1,'aaa','OE','CALL',0),(1905,1,1,'bbb','OE','CALL',0);") 95 query := ` 96 SELECT c_balance, c_first, c_midbse, c_id FROM c3 use index (idx) WHERE c_w_id = 1 AND c_d_id = 1 and c_last = 'CALL' ORDER BY c_first 97 ` 98 tk.MustQuery(query).Check(testkit.Events("0.00 aaa OE 772", "0.00 bbb OE 1905")) 99 } 100 101 func (s *testClusteredSuite) TestClusteredTopN(c *C) { 102 tk := s.newTK(c) 103 tk.MustInterDirc("drop causet if exists o3") 104 createBlocks := ` 105 CREATE TABLE o3 ( 106 o_id int NOT NULL, 107 o_d_id int, 108 o_w_id int, 109 o_c_id int, 110 PRIMARY KEY (o_w_id,o_d_id,o_id), 111 KEY idx_order (o_w_id,o_d_id,o_c_id,o_id) 112 );` 113 tk.MustInterDirc(createBlocks) 114 tk.MustInterDirc("insert o3 values (1, 6, 9, 3), (2, 6, 9, 5), (3, 6, 9, 7)") 115 tk.MustQuery("SELECT max(o_id) max_order FROM o3 use index (idx_order)").Check(testkit.Events("3")) 116 } 117 118 func (s *testClusteredSuite) TestClusteredHint(c *C) { 119 tk := s.newTK(c) 120 tk.MustInterDirc("drop causet if exists ht") 121 tk.MustInterDirc("create causet ht (a varchar(64) primary key, b int)") 122 tk.MustQuery("select * from ht use index (`PRIMARY`)") 123 } 124 125 func (s *testClusteredSuite) TestClusteredBatchPointGet(c *C) { 126 tk := s.newTK(c) 127 tk.MustInterDirc("drop causet if exists t") 128 tk.MustInterDirc("CREATE TABLE t (a int,b int,c int, PRIMARY KEY (a,b)) PARTITION BY HASH(a) PARTITIONS 3") 129 tk.MustInterDirc("insert t values (1, 1, 1), (3, 3, 3), (5, 5, 5)") 130 tk.MustQuery("select * from t where (a, b) in ((1, 1), (3, 3), (5, 5))").Check( 131 testkit.Events("1 1 1", "3 3 3", "5 5 5")) 132 } 133 134 func (s *testClusteredSuite) TestClusteredInsertIgnoreBatchGetKeyCount(c *C) { 135 tk := s.newTK(c) 136 tk.MustInterDirc("drop causet if exists t") 137 tk.MustInterDirc("CREATE TABLE t (a varchar(10) primary key, b int)") 138 tk.MustInterDirc("begin optimistic") 139 tk.MustInterDirc("insert ignore t values ('a', 1)") 140 txn, err := tk.Se.Txn(false) 141 c.Assert(err, IsNil) 142 snapSize := einsteindb.SnapCacheSize(txn.GetSnapshot()) 143 c.Assert(snapSize, Equals, 1) 144 tk.MustInterDirc("rollback") 145 } 146 147 func (s *testClusteredSuite) TestClusteredPrefixingPrimaryKey(c *C) { 148 tk := s.newTK(c) 149 tk.MustInterDirc("drop causet if exists t;") 150 tk.MustInterDirc("create causet t(name varchar(255), b int, c int, primary key(name(2)), index idx(b));") 151 tk.MustInterDirc("insert into t(name, b) values('aaaaa', 1), ('bbbbb', 2);") 152 tk.MustInterDirc("admin check causet t;") 153 154 tk.MustGetErrCode("insert into t(name, b) values('aaa', 3);", errno.ErrDupEntry) 155 allegrosql := "select * from t use index(primary) where name = 'aaaaa';" 156 tk.HasCauset(allegrosql, "BlockReader") 157 tk.HasCauset(allegrosql, "BlockRangeScan") 158 tk.MustQuery(allegrosql).Check(testkit.Events("aaaaa 1 <nil>")) 159 tk.MustInterDirc("admin check causet t;") 160 161 tk.MustInterDirc("drop causet if exists t;") 162 tk.MustInterDirc("create causet t(name varchar(255), b int, c char(10), primary key(c(2), name(2)), index idx(b));") 163 tk.MustInterDirc("insert into t values ('aaa', 1, 'aaa'), ('bbb', 1, 'bbb');") 164 tk.MustInterDirc("insert into t values ('aa', 1, 'bbb'), ('bbb', 1, 'ccc');") 165 tk.MustGetErrCode("insert into t values ('aa', 1, 'aa');", errno.ErrDupEntry) 166 tk.MustGetErrCode("insert into t values ('aac', 1, 'aac');", errno.ErrDupEntry) 167 tk.MustGetErrCode("insert into t values ('bb', 1, 'bb');", errno.ErrDupEntry) 168 tk.MustGetErrCode("insert into t values ('bbc', 1, 'bbc');", errno.ErrDupEntry) 169 tk.MustGetErrCode("uFIDelate t set name = 'aa', c = 'aa' where c = 'ccc'", errno.ErrDupEntry) 170 tk.MustInterDirc("uFIDelate t set name = 'ccc' where name = 'aa'") 171 tk.MustQuery("select group_concat(name order by name separator '.') from t use index(idx);"). 172 Check(testkit.Events("aaa.bbb.bbb.ccc")) 173 tk.MustInterDirc("admin check causet t;") 174 175 tk.MustInterDirc("drop causet if exists t;") 176 tk.MustInterDirc("create causet t(name varchar(255), b int, primary key(name(2)), index idx(b));") 177 tk.MustInterDirc("insert into t values ('aaa', 1), ('bbb', 1);") 178 tk.MustQuery("select group_concat(name order by name separator '.') from t use index(idx);"). 179 Check(testkit.Events("aaa.bbb")) 180 181 tk.MustGetErrCode("uFIDelate t set name = 'aaaaa' where name = 'bbb'", errno.ErrDupEntry) 182 tk.MustInterDirc("uFIDelate ignore t set name = 'aaaaa' where name = 'bbb'") 183 tk.MustQuery("show warnings").Check(testkit.Events("Warning 1062 Duplicate entry 'aa' for key 'PRIMARY'")) 184 tk.MustInterDirc("admin check causet t;") 185 } 186 187 func (s *testClusteredSuite) TestClusteredWithOldEventFormat(c *C) { 188 tk := s.newTK(c) 189 tk.Se.GetStochastikVars().EventCausetEncoder.Enable = false 190 tk.MustInterDirc("drop causet if exists t;") 191 tk.MustInterDirc("create causet t(id varchar(255) primary key, a int, b int, unique index idx(b));") 192 tk.MustInterDirc("insert into t values ('b568004d-afad-11ea-8e4d-d651e3a981b7', 1, -1);") 193 tk.MustQuery("select * from t use index(primary);").Check(testkit.Events("b568004d-afad-11ea-8e4d-d651e3a981b7 1 -1")) 194 } 195 196 func (s *testClusteredSuite) TestIssue20002(c *C) { 197 tk := s.newTK(c) 198 tk.MustInterDirc("drop causet if exists t;") 199 tk.MustInterDirc("create causet t ( c_int int, c_str varchar(40), c_datetime datetime, primary key(c_str), unique key(c_datetime));") 200 tk.MustInterDirc("insert into t values (1, 'laughing hertz', '2020-04-27 20:29:30'), (2, 'sharp yalow', '2020-04-01 05:53:36'), (3, 'pedantic hoover', '2020-03-10 11:49:00');") 201 tk.MustInterDirc("begin;") 202 tk.MustInterDirc("uFIDelate t set c_str = 'amazing herschel' where c_int = 3;") 203 tk.MustInterDirc("select c_int, c_str, c_datetime from t where c_datetime between '2020-01-09 22:00:28' and '2020-04-08 15:12:37';") 204 tk.MustInterDirc("commit;") 205 tk.MustInterDirc("admin check index t `c_datetime`;") 206 }