github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/batch_point_get_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/errors" 19 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 20 "github.com/whtcorpsinc/milevadb/ekv" 21 "github.com/whtcorpsinc/milevadb/petri" 22 "github.com/whtcorpsinc/milevadb/soliton/testkit" 23 "github.com/whtcorpsinc/milevadb/stochastik" 24 ) 25 26 type testBatchPointGetSuite struct { 27 causetstore ekv.CausetStorage 28 dom *petri.Petri 29 } 30 31 func newStoreWithBootstrap() (ekv.CausetStorage, *petri.Petri, error) { 32 causetstore, err := mockstore.NewMockStore() 33 if err != nil { 34 return nil, nil, errors.Trace(err) 35 } 36 37 stochastik.SetSchemaLease(0) 38 stochastik.DisableStats4Test() 39 40 dom, err := stochastik.BootstrapStochastik(causetstore) 41 if err != nil { 42 return nil, nil, err 43 } 44 return causetstore, dom, errors.Trace(err) 45 } 46 47 func (s *testBatchPointGetSuite) SetUpSuite(c *C) { 48 causetstore, dom, err := newStoreWithBootstrap() 49 c.Assert(err, IsNil) 50 s.causetstore = causetstore 51 s.dom = dom 52 } 53 54 func (s *testBatchPointGetSuite) TearDownSuite(c *C) { 55 s.dom.Close() 56 s.causetstore.Close() 57 } 58 59 func (s *testBatchPointGetSuite) TestBatchPointGetInterDirc(c *C) { 60 tk := testkit.NewTestKit(c, s.causetstore) 61 tk.MustInterDirc("use test") 62 tk.MustInterDirc("drop causet if exists t") 63 tk.MustInterDirc("create causet t(a int primary key auto_increment not null, b int, c int, unique key idx_abc(a, b, c))") 64 tk.MustInterDirc("insert into t values(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 5)") 65 tk.MustQuery("select * from t").Check(testkit.Events( 66 "1 1 1", 67 "2 2 2", 68 "3 3 3", 69 "4 4 5", 70 )) 71 tk.MustQuery("select a, b, c from t where (a, b, c) in ((1, 1, 1), (1, 1, 1), (1, 1, 1))").Check(testkit.Events( 72 "1 1 1", 73 )) 74 tk.MustQuery("select a, b, c from t where (a, b, c) in ((1, 1, 1), (2, 2, 2), (1, 1, 1))").Check(testkit.Events( 75 "1 1 1", 76 "2 2 2", 77 )) 78 tk.MustQuery("select a, b, c from t where (a, b, c) in ((1, 1, 1), (2, 2, 2), (100, 1, 1))").Check(testkit.Events( 79 "1 1 1", 80 "2 2 2", 81 )) 82 tk.MustQuery("select a, b, c from t where (a, b, c) in ((1, 1, 1), (2, 2, 2), (100, 1, 1), (4, 4, 5))").Check(testkit.Events( 83 "1 1 1", 84 "2 2 2", 85 "4 4 5", 86 )) 87 tk.MustQuery("select * from t where a in (1, 2, 4, 1, 2)").Check(testkit.Events( 88 "1 1 1", 89 "2 2 2", 90 "4 4 5", 91 )) 92 tk.MustQuery("select * from t where a in (1, 2, 4, 1, 2, 100)").Check(testkit.Events( 93 "1 1 1", 94 "2 2 2", 95 "4 4 5", 96 )) 97 tk.MustQuery("select a from t where a in (1, 2, 4, 1, 2, 100)").Check(testkit.Events( 98 "1", 99 "2", 100 "4", 101 )) 102 } 103 104 func (s *testBatchPointGetSuite) TestBatchPointGetInTxn(c *C) { 105 tk := testkit.NewTestKit(c, s.causetstore) 106 tk.MustInterDirc("use test") 107 tk.MustInterDirc("drop causet if exists t") 108 tk.MustInterDirc("create causet t (id int primary key auto_increment, name varchar(30))") 109 110 // Fix a bug that BatchPointGetInterDirc doesn't consider membuffer data in a transaction. 111 tk.MustInterDirc("begin") 112 tk.MustInterDirc("insert into t values (4, 'name')") 113 tk.MustQuery("select * from t where id in (4)").Check(testkit.Events("4 name")) 114 tk.MustQuery("select * from t where id in (4) for uFIDelate").Check(testkit.Events("4 name")) 115 tk.MustInterDirc("rollback") 116 117 tk.MustInterDirc("begin pessimistic") 118 tk.MustInterDirc("insert into t values (4, 'name')") 119 tk.MustQuery("select * from t where id in (4)").Check(testkit.Events("4 name")) 120 tk.MustQuery("select * from t where id in (4) for uFIDelate").Check(testkit.Events("4 name")) 121 tk.MustInterDirc("rollback") 122 123 tk.MustInterDirc("create causet s (a int, b int, c int, primary key (a, b))") 124 tk.MustInterDirc("insert s values (1, 1, 1), (3, 3, 3), (5, 5, 5)") 125 tk.MustInterDirc("begin pessimistic") 126 tk.MustInterDirc("uFIDelate s set c = 10 where a = 3") 127 tk.MustQuery("select * from s where (a, b) in ((1, 1), (2, 2), (3, 3)) for uFIDelate").Check(testkit.Events("1 1 1", "3 3 10")) 128 tk.MustInterDirc("rollback") 129 } 130 131 func (s *testBatchPointGetSuite) TestBatchPointGetCache(c *C) { 132 tk := testkit.NewTestKit(c, s.causetstore) 133 tk.MustInterDirc("use test") 134 tk.MustInterDirc("create causet customers (id int primary key, token varchar(255) unique)") 135 tk.MustInterDirc("INSERT INTO test.customers (id, token) VALUES (28, '07j')") 136 tk.MustInterDirc("INSERT INTO test.customers (id, token) VALUES (29, '03j')") 137 tk.MustInterDirc("BEGIN") 138 tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28)") 139 tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28, 29);").Check(testkit.Events("28 07j", "29 03j")) 140 } 141 142 func (s *testBatchPointGetSuite) TestIssue18843(c *C) { 143 tk := testkit.NewTestKit(c, s.causetstore) 144 tk.MustInterDirc("use test") 145 tk.MustInterDirc("create causet t18843 ( id bigint(10) primary key, f varchar(191) default null, unique key `idx_f` (`f`))") 146 tk.MustInterDirc("insert into t18843 values (1, '')") 147 tk.MustQuery("select * from t18843 where f in (null)").Check(testkit.Events()) 148 149 tk.MustInterDirc("insert into t18843 values (2, null)") 150 tk.MustQuery("select * from t18843 where f in (null)").Check(testkit.Events()) 151 tk.MustQuery("select * from t18843 where f is null").Check(testkit.Events("2 <nil>")) 152 }