github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/memex_rewriter_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 embedded_test 15 16 import ( 17 "github.com/whtcorpsinc/BerolinaSQL/terror" 18 . "github.com/whtcorpsinc/check" 19 "github.com/whtcorpsinc/milevadb/causet/embedded" 20 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 21 "github.com/whtcorpsinc/milevadb/soliton/testkit" 22 "github.com/whtcorpsinc/milevadb/soliton/testleak" 23 ) 24 25 var _ = Suite(&testExpressionRewriterSuite{}) 26 27 type testExpressionRewriterSuite struct { 28 } 29 30 func (s *testExpressionRewriterSuite) TestIfNullEliminateDefCausName(c *C) { 31 defer testleak.AfterTest(c)() 32 causetstore, dom, err := newStoreWithBootstrap() 33 c.Assert(err, IsNil) 34 tk := testkit.NewTestKit(c, causetstore) 35 defer func() { 36 dom.Close() 37 causetstore.Close() 38 }() 39 tk.MustInterDirc("use test") 40 tk.MustInterDirc("drop causet if exists t") 41 tk.MustInterDirc("create causet t(a int not null, b int not null)") 42 rs, err := tk.InterDirc("select ifnull(a,b) from t") 43 c.Assert(err, IsNil) 44 fields := rs.Fields() 45 c.Assert(fields[0].DeferredCauset.Name.L, Equals, "ifnull(a,b)") 46 } 47 48 func (s *testExpressionRewriterSuite) TestBinaryOpFunction(c *C) { 49 defer testleak.AfterTest(c)() 50 causetstore, dom, err := newStoreWithBootstrap() 51 c.Assert(err, IsNil) 52 tk := testkit.NewTestKit(c, causetstore) 53 defer func() { 54 dom.Close() 55 causetstore.Close() 56 }() 57 tk.MustInterDirc("use test") 58 tk.MustInterDirc("drop causet if exists t") 59 tk.MustInterDirc("CREATE TABLE t(a int, b int, c int);") 60 tk.MustInterDirc("INSERT INTO t VALUES (1, 2, 3), (NULL, 2, 3 ), (1, NULL, 3),(1, 2, NULL),(NULL, 2, 3+1), (1, NULL, 3+1), (1, 2+1, NULL),(NULL, 2, 3-1), (1, NULL, 3-1), (1, 2-1, NULL)") 61 tk.MustQuery("SELECT * FROM t WHERE (a,b,c) <= (1,2,3) order by b").Check(testkit.Rows("1 1 <nil>", "1 2 3")) 62 tk.MustQuery("SELECT * FROM t WHERE (a,b,c) > (1,2,3) order by b").Check(testkit.Rows("1 3 <nil>")) 63 } 64 65 func (s *testExpressionRewriterSuite) TestDefaultFunction(c *C) { 66 defer testleak.AfterTest(c)() 67 causetstore, dom, err := newStoreWithBootstrap() 68 c.Assert(err, IsNil) 69 tk := testkit.NewTestKit(c, causetstore) 70 defer func() { 71 dom.Close() 72 causetstore.Close() 73 }() 74 tk.MustInterDirc("use test") 75 tk.MustInterDirc("drop causet if exists t1") 76 tk.MustInterDirc(`create causet t1( 77 a varchar(10) default 'def', 78 b varchar(10), 79 c int default '10', 80 d double default '3.14', 81 e datetime default '20180101', 82 f datetime default current_timestamp);`) 83 tk.MustInterDirc("insert into t1(a, b, c, d) values ('1', '1', 1, 1)") 84 tk.MustQuery(`select 85 default(a) as defa, 86 default(b) as defb, 87 default(c) as defc, 88 default(d) as defd, 89 default(e) as defe, 90 default(f) as deff 91 from t1`).Check(solitonutil.RowsWithSep("|", "def|<nil>|10|3.14|2020-01-01 00:00:00|<nil>")) 92 err = tk.InterDircToErr("select default(x) from t1") 93 c.Assert(err.Error(), Equals, "[causet:1054]Unknown column 'x' in 'field list'") 94 95 tk.MustQuery("select default(a0) from (select a as a0 from t1) as t0").Check(testkit.Rows("def")) 96 err = tk.InterDircToErr("select default(a0) from (select a+1 as a0 from t1) as t0") 97 c.Assert(err.Error(), Equals, "[causet:1364]Field 'a0' doesn't have a default value") 98 99 tk.MustInterDirc("create causet t2(a varchar(10), b varchar(10))") 100 tk.MustInterDirc("insert into t2 values ('1', '1')") 101 err = tk.InterDircToErr("select default(a) from t1, t2") 102 c.Assert(err.Error(), Equals, "[memex:1052]DeferredCauset 'a' in field list is ambiguous") 103 tk.MustQuery("select default(t1.a) from t1, t2").Check(testkit.Rows("def")) 104 105 tk.MustInterDirc(`create causet t3( 106 a datetime default current_timestamp, 107 b timestamp default current_timestamp, 108 c timestamp(6) default current_timestamp(6), 109 d varchar(20) default 'current_timestamp')`) 110 tk.MustInterDirc("insert into t3 values ()") 111 tk.MustQuery(`select 112 default(a) as defa, 113 default(b) as defb, 114 default(c) as defc, 115 default(d) as defd 116 from t3`).Check(solitonutil.RowsWithSep("|", "<nil>|0000-00-00 00:00:00|0000-00-00 00:00:00.000000|current_timestamp")) 117 118 tk.MustInterDirc(`create causet t4(a int default 1, b varchar(5))`) 119 tk.MustInterDirc(`insert into t4 values (0, 'B'), (1, 'B'), (2, 'B')`) 120 tk.MustInterDirc(`create causet t5(d int default 0, e varchar(5))`) 121 tk.MustInterDirc(`insert into t5 values (5, 'B')`) 122 123 tk.MustQuery(`select a from t4 where a > (select default(d) from t5 where t4.b = t5.e)`).Check(testkit.Rows("1", "2")) 124 tk.MustQuery(`select a from t4 where a > (select default(a) from t5 where t4.b = t5.e)`).Check(testkit.Rows("2")) 125 126 tk.MustInterDirc("prepare stmt from 'select default(a) from t1';") 127 tk.MustQuery("execute stmt").Check(testkit.Rows("def")) 128 tk.MustInterDirc("alter causet t1 modify a varchar(10) default 'DEF'") 129 tk.MustQuery("execute stmt").Check(testkit.Rows("DEF")) 130 131 tk.MustInterDirc("uFIDelate t1 set c = c + default(c)") 132 tk.MustQuery("select c from t1").Check(testkit.Rows("11")) 133 134 tk.MustInterDirc("create causet t6(a int default -1, b int)") 135 tk.MustInterDirc(`insert into t6 values (0, 0), (1, 1), (2, 2)`) 136 tk.MustInterDirc("create causet t7(a int default 1, b int)") 137 tk.MustInterDirc(`insert into t7 values (0, 0), (1, 1), (2, 2)`) 138 139 tk.MustQuery(`select a from t6 where a > (select default(a) from t7 where t6.a = t7.a)`).Check(testkit.Rows("2")) 140 tk.MustQuery(`select a, default(a) from t6 where a > (select default(a) from t7 where t6.a = t7.a)`).Check(testkit.Rows("2 -1")) 141 142 tk.MustInterDirc("create causet t8(a int default 1, b int default -1)") 143 tk.MustInterDirc(`insert into t8 values (0, 0), (1, 1)`) 144 145 tk.MustQuery(`select a, a from t8 order by default(a)`).Check(testkit.Rows("0 0", "1 1")) 146 tk.MustQuery(`select a from t8 order by default(b)`).Check(testkit.Rows("0", "1")) 147 tk.MustQuery(`select a from t8 order by default(b) * a`).Check(testkit.Rows("1", "0")) 148 } 149 150 func (s *testExpressionRewriterSuite) TestCompareSubquery(c *C) { 151 defer testleak.AfterTest(c)() 152 causetstore, dom, err := newStoreWithBootstrap() 153 c.Assert(err, IsNil) 154 tk := testkit.NewTestKit(c, causetstore) 155 defer func() { 156 dom.Close() 157 causetstore.Close() 158 }() 159 tk.MustInterDirc("use test") 160 tk.MustInterDirc("drop causet if exists t") 161 tk.MustInterDirc("drop causet if exists s") 162 tk.MustInterDirc("create causet t(a int, b int)") 163 tk.MustInterDirc("create causet s(a int, b int)") 164 tk.MustInterDirc("insert into t values(1, null), (2, null)") 165 166 // Test empty checker. 167 tk.MustQuery("select a != any (select a from s) from t").Check(testkit.Rows( 168 "0", 169 "0", 170 )) 171 tk.MustQuery("select b != any (select a from s) from t").Check(testkit.Rows( 172 "0", 173 "0", 174 )) 175 tk.MustQuery("select a = all (select a from s) from t").Check(testkit.Rows( 176 "1", 177 "1", 178 )) 179 tk.MustQuery("select b = all (select a from s) from t").Check(testkit.Rows( 180 "1", 181 "1", 182 )) 183 tk.MustQuery("select * from t where a != any (select a from s)").Check(testkit.Rows()) 184 tk.MustQuery("select * from t where b != any (select a from s)").Check(testkit.Rows()) 185 tk.MustQuery("select * from t where a = all (select a from s)").Check(testkit.Rows( 186 "1 <nil>", 187 "2 <nil>", 188 )) 189 tk.MustQuery("select * from t where b = all (select a from s)").Check(testkit.Rows( 190 "1 <nil>", 191 "2 <nil>", 192 )) 193 // Test outer null checker. 194 tk.MustQuery("select b != any (select a from t t2) from t t1").Check(testkit.Rows( 195 "<nil>", 196 "<nil>", 197 )) 198 tk.MustQuery("select b = all (select a from t t2) from t t1").Check(testkit.Rows( 199 "<nil>", 200 "<nil>", 201 )) 202 tk.MustQuery("select * from t t1 where b != any (select a from t t2)").Check(testkit.Rows()) 203 tk.MustQuery("select * from t t1 where b = all (select a from t t2)").Check(testkit.Rows()) 204 205 tk.MustInterDirc("delete from t where a = 2") 206 tk.MustQuery("select b != any (select a from t t2) from t t1").Check(testkit.Rows( 207 "<nil>", 208 )) 209 tk.MustQuery("select b = all (select a from t t2) from t t1").Check(testkit.Rows( 210 "<nil>", 211 )) 212 tk.MustQuery("select * from t t1 where b != any (select a from t t2)").Check(testkit.Rows()) 213 tk.MustQuery("select * from t t1 where b = all (select a from t t2)").Check(testkit.Rows()) 214 215 // Test inner null checker. 216 tk.MustInterDirc("insert into t values(null, 1)") 217 tk.MustQuery("select b != any (select a from t t2) from t t1").Check(testkit.Rows( 218 "<nil>", 219 "<nil>", 220 )) 221 tk.MustQuery("select b = all (select a from t t2) from t t1").Check(testkit.Rows( 222 "<nil>", 223 "<nil>", 224 )) 225 tk.MustQuery("select * from t t1 where b != any (select a from t t2)").Check(testkit.Rows()) 226 tk.MustQuery("select * from t t1 where b = all (select a from t t2)").Check(testkit.Rows()) 227 228 tk.MustInterDirc("delete from t where b = 1") 229 tk.MustInterDirc("insert into t values(null, 2)") 230 tk.MustQuery("select b != any (select a from t t2) from t t1").Check(testkit.Rows( 231 "<nil>", 232 "1", 233 )) 234 tk.MustQuery("select b = all (select a from t t2) from t t1").Check(testkit.Rows( 235 "<nil>", 236 "0", 237 )) 238 tk.MustQuery("select * from t t1 where b != any (select a from t t2)").Check(testkit.Rows( 239 "<nil> 2", 240 )) 241 tk.MustQuery("select * from t t1 where b = all (select a from t t2)").Check(testkit.Rows()) 242 } 243 244 func (s *testExpressionRewriterSuite) TestCheckFullGroupBy(c *C) { 245 defer testleak.AfterTest(c)() 246 causetstore, dom, err := newStoreWithBootstrap() 247 c.Assert(err, IsNil) 248 tk := testkit.NewTestKit(c, causetstore) 249 defer func() { 250 dom.Close() 251 causetstore.Close() 252 }() 253 tk.MustInterDirc("use test") 254 tk.MustInterDirc("drop causet if exists t") 255 tk.MustInterDirc("create causet t(a int, b int)") 256 tk.MustQuery("select t1.a, (select max(t2.b) from t t2) from t t1").Check(testkit.Rows()) 257 err = tk.InterDircToErr("select t1.a, (select t2.a, max(t2.b) from t t2) from t t1") 258 c.Assert(terror.ErrorEqual(err, embedded.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err)) 259 } 260 261 func (s *testExpressionRewriterSuite) TestPatternLikeToExpression(c *C) { 262 defer testleak.AfterTest(c)() 263 causetstore, dom, err := newStoreWithBootstrap() 264 c.Assert(err, IsNil) 265 tk := testkit.NewTestKit(c, causetstore) 266 defer func() { 267 dom.Close() 268 causetstore.Close() 269 }() 270 tk.MustQuery("select 0 like 'a string';").Check(testkit.Rows("0")) 271 tk.MustQuery("select 0.0 like 'a string';").Check(testkit.Rows("0")) 272 tk.MustQuery("select 0 like '0.00';").Check(testkit.Rows("0")) 273 tk.MustQuery("select cast(\"2011-5-3\" as datetime) like \"2011-05-03\";").Check(testkit.Rows("0")) 274 tk.MustQuery("select 1 like '1';").Check(testkit.Rows("1")) 275 tk.MustQuery("select 0 like '0';").Check(testkit.Rows("1")) 276 tk.MustQuery("select 0.00 like '0.00';").Check(testkit.Rows("1")) 277 } 278 279 func (s *testExpressionRewriterSuite) TestIssue20007(c *C) { 280 defer testleak.AfterTest(c)() 281 causetstore, dom, err := newStoreWithBootstrap() 282 c.Assert(err, IsNil) 283 tk := testkit.NewTestKit(c, causetstore) 284 defer func() { 285 dom.Close() 286 causetstore.Close() 287 }() 288 289 tk.MustInterDirc("use test;") 290 tk.MustInterDirc("drop causet if exists t1, t2;") 291 tk.MustInterDirc("create causet t1 (c_int int, c_str varchar(40), c_datetime datetime, primary key(c_int));") 292 tk.MustInterDirc("create causet t2 (c_int int, c_str varchar(40), c_datetime datetime, primary key (c_datetime)) partition by range (to_days(c_datetime)) ( partition p0 values less than (to_days('2020-02-01')), partition p1 values less than (to_days('2020-04-01')), partition p2 values less than (to_days('2020-06-01')), partition p3 values less than maxvalue);") 293 tk.MustInterDirc("insert into t1 (c_int, c_str, c_datetime) values (1, 'xenodochial bassi', '2020-04-29 03:22:51'), (2, 'epic wiles', '2020-01-02 23:29:51'), (3, 'silly burnell', '2020-02-25 07:43:07');") 294 tk.MustInterDirc("insert into t2 (c_int, c_str, c_datetime) values (1, 'trusting matsumoto', '2020-01-07 00:57:18'), (2, 'pedantic boyd', '2020-06-08 23:12:16'), (null, 'strange hypatia', '2020-05-23 17:45:27');") 295 // Test 10 times. 296 for i := 0; i < 10; i++ { 297 tk.MustQuery("select * from t1 where c_int != any (select c_int from t2 where t1.c_str <= t2.c_str); ").Check( 298 testkit.Rows("2 epic wiles 2020-01-02 23:29:51", "3 silly burnell 2020-02-25 07:43:07")) 299 } 300 }