MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
subquery_mat.inc
1 #
2 # Hash semi-join regression tests
3 # (WL#1110: Subquery optimization: materialization)
4 #
5 
6 # Force the feature, to test it as much as possible:
7 set @old_opt_switch=@@optimizer_switch;
8 set optimizer_switch='subquery_materialization_cost_based=off';
9 
10 --disable_warnings
11 drop table if exists t1, t2, t3, t1i, t2i, t3i;
12 drop view if exists v1, v2, v1m, v2m;
13 --enable_warnings
14 
15 create table t1 (a1 char(8), a2 char(8));
16 create table t2 (b1 char(8), b2 char(8));
17 create table t3 (c1 char(8), c2 char(8));
18 
19 insert into t1 values ('1 - 00', '2 - 00');
20 insert into t1 values ('1 - 01', '2 - 01');
21 insert into t1 values ('1 - 02', '2 - 02');
22 
23 insert into t2 values ('1 - 01', '2 - 01');
24 insert into t2 values ('1 - 01', '2 - 01');
25 insert into t2 values ('1 - 02', '2 - 02');
26 insert into t2 values ('1 - 02', '2 - 02');
27 insert into t2 values ('1 - 03', '2 - 03');
28 
29 insert into t3 values ('1 - 01', '2 - 01');
30 insert into t3 values ('1 - 02', '2 - 02');
31 insert into t3 values ('1 - 03', '2 - 03');
32 insert into t3 values ('1 - 04', '2 - 04');
33 
34 # Indexed columns
35 create table t1i (a1 char(8), a2 char(8));
36 create table t2i (b1 char(8), b2 char(8));
37 create table t3i (c1 char(8), c2 char(8));
38 create index it1i1 on t1i (a1);
39 create index it1i2 on t1i (a2);
40 create index it1i3 on t1i (a1, a2);
41 
42 create index it2i1 on t2i (b1);
43 create index it2i2 on t2i (b2);
44 create index it2i3 on t2i (b1, b2);
45 
46 create index it3i1 on t3i (c1);
47 create index it3i2 on t3i (c2);
48 create index it3i3 on t3i (c1, c2);
49 
50 insert into t1i select * from t1;
51 insert into t2i select * from t2;
52 insert into t3i select * from t3;
53 
54 /******************************************************************************
55 * Simple tests.
56 ******************************************************************************/
57 # non-indexed nullable fields
58 explain extended
59 select * from t1 where a1 in (select b1 from t2 where b1 > '0');
60 select * from t1 where a1 in (select b1 from t2 where b1 > '0');
61 
62 explain extended
63 select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
64 select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
65 
66 explain extended
67 select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
68 select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
69 
70 explain extended
71 select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1);
72 select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1);
73 
74 # indexed columns
75 explain extended
76 select * from t1i where a1 in (select b1 from t2i where b1 > '0');
77 select * from t1i where a1 in (select b1 from t2i where b1 > '0');
78 
79 explain extended
80 select * from t1i where a1 in (select b1 from t2i where b1 > '0' group by b1);
81 select * from t1i where a1 in (select b1 from t2i where b1 > '0' group by b1);
82 
83 explain extended
84 select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0');
85 select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0');
86 
87 explain extended
88 select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0' group by b1, b2);
89 select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0' group by b1, b2);
90 
91 explain extended
92 select * from t1i where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
93 select * from t1i where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
94 
95 # BUG#31639: Wrong plan for uncorrelated subquery when loose scan is applicable.
96 explain extended
97 select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1);
98 select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1);
99 
100 prepare st1 from "explain select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1)";
101 execute st1;
102 execute st1;
103 prepare st2 from "select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1)";
104 execute st2;
105 execute st2;
106 
107 explain extended
108 select * from t1 where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
109 select * from t1 where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
110 -- error 1235
111 select * from t1 where (a1, a2) in (select b1, min(b2) from t2i limit 1,1);
112 
113 # materialize the result of ORDER BY
114 # non-indexed fields
115 explain extended
116 select * from t1 where (a1, a2) in (select b1, b2 from t2 order by b1, b2);
117 select * from t1 where (a1, a2) in (select b1, b2 from t2 order by b1, b2);
118 # indexed fields
119 explain extended
120 select * from t1i where (a1, a2) in (select b1, b2 from t2i order by b1, b2);
121 select * from t1i where (a1, a2) in (select b1, b2 from t2i order by b1, b2);
122 
123 /******************************************************************************
124 * Views, UNIONs, several levels of nesting.
125 ******************************************************************************/
126 # materialize the result of subquery over temp-table view
127 
128 create algorithm=merge view v1 as
129 select b1, c2 from t2, t3 where b2 > c2;
130 
131 create algorithm=merge view v2 as
132 select b1, c2 from t2, t3 group by b2, c2;
133 
134 create algorithm=temptable view v1m as
135 select b1, c2 from t2, t3 where b2 > c2;
136 
137 create algorithm=temptable view v2m as
138 select b1, c2 from t2, t3 group by b2, c2;
139 
140 select * from v1 where (c2, b1) in (select c2, b1 from v2 where b1 is not null);
141 select * from v1 where (c2, b1) in (select distinct c2, b1 from v2 where b1 is not null);
142 
143 select * from v1m where (c2, b1) in (select c2, b1 from v2m where b1 is not null);
144 select * from v1m where (c2, b1) in (select distinct c2, b1 from v2m where b1 is not null);
145 
146 drop view v1, v2, v1m, v2m;
147 
148 # nested subqueries, views
149 explain extended
150 select * from t1
151 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') and
152  (a1, a2) in (select c1, c2 from t3
153  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
154 select * from t1
155 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') and
156  (a1, a2) in (select c1, c2 from t3
157  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
158 
159 explain extended
160 select * from t1i
161 where (a1, a2) in (select b1, b2 from t2i where b1 > '0') and
162  (a1, a2) in (select c1, c2 from t3i
163  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
164 select * from t1i
165 where (a1, a2) in (select b1, b2 from t2i where b1 > '0') and
166  (a1, a2) in (select c1, c2 from t3i
167  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
168 
169 explain extended
170 select * from t1
171 where (a1, a2) in (select b1, b2 from t2
172  where b2 in (select c2 from t3 where c2 LIKE '%02') or
173  b2 in (select c2 from t3 where c2 LIKE '%03')) and
174  (a1, a2) in (select c1, c2 from t3
175  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
176 select * from t1
177 where (a1, a2) in (select b1, b2 from t2
178  where b2 in (select c2 from t3 where c2 LIKE '%02') or
179  b2 in (select c2 from t3 where c2 LIKE '%03')) and
180  (a1, a2) in (select c1, c2 from t3
181  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
182 
183 # as above with correlated innermost subquery
184 explain extended
185 select * from t1
186 where (a1, a2) in (select b1, b2 from t2
187  where b2 in (select c2 from t3 t3a where c1 = a1) or
188  b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
189  (a1, a2) in (select c1, c2 from t3 t3c
190  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
191 select * from t1
192 where (a1, a2) in (select b1, b2 from t2
193  where b2 in (select c2 from t3 t3a where c1 = a1) or
194  b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
195  (a1, a2) in (select c1, c2 from t3 t3c
196  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
197 
198 
199 # multiple levels of nesting subqueries, unions
200 explain extended
201 (select * from t1
202 where (a1, a2) in (select b1, b2 from t2
203  where b2 in (select c2 from t3 where c2 LIKE '%02') or
204  b2 in (select c2 from t3 where c2 LIKE '%03')
205  group by b1, b2) and
206  (a1, a2) in (select c1, c2 from t3
207  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')))
208 UNION
209 (select * from t1i
210 where (a1, a2) in (select b1, b2 from t2i where b1 > '0') and
211  (a1, a2) in (select c1, c2 from t3i
212  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')));
213 
214 (select * from t1
215 where (a1, a2) in (select b1, b2 from t2
216  where b2 in (select c2 from t3 where c2 LIKE '%02') or
217  b2 in (select c2 from t3 where c2 LIKE '%03')
218  group by b1, b2) and
219  (a1, a2) in (select c1, c2 from t3
220  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')))
221 UNION
222 (select * from t1i
223 where (a1, a2) in (select b1, b2 from t2i where b1 > '0') and
224  (a1, a2) in (select c1, c2 from t3i
225  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')));
226 
227 
228 # UNION of subqueries as a subquery (thus it is not computed via materialization)
229 explain extended
230 select * from t1
231 where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
232  (a1, a2) in (select c1, c2 from t3
233  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
234 select * from t1
235 where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
236  (a1, a2) in (select c1, c2 from t3
237  where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
238 # as above, with a join conditon between the outer references
239 explain extended
240 select * from t1, t3
241 where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
242  (c1, c2) in (select c1, c2 from t3
243  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')) and
244  a1 = c1;
245 select * from t1, t3
246 where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
247  (c1, c2) in (select c1, c2 from t3
248  where (c1, c2) in (select b1, b2 from t2i where b2 > '0')) and
249  a1 = c1;
250 
251 
252 /******************************************************************************
253 * Negative tests, where materialization should not be applied.
254 ******************************************************************************/
255 # UNION in a subquery
256 explain extended
257 select * from t3
258 where c1 in (select a1 from t1 where a1 > '0' UNION select b1 from t2 where b1 < '9');
259 select * from t3
260 where c1 in (select a1 from t1 where a1 > '0' UNION select b1 from t2 where b1 < '9');
261 
262 # correlation
263 explain extended
264 select * from t1
265 where (a1, a2) in (select b1, b2 from t2
266  where b2 in (select c2 from t3 t3a where c1 = a1) or
267  b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
268  (a1, a2) in (select c1, c2 from t3 t3c
269  where (c1, c2) in (select b1, b2 from t2i where b2 > '0' or b2 = a2));
270 
271 # subquery has no tables
272 explain extended
273 select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01');
274 select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01');
275 explain extended
276 select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01' from dual);
277 select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01' from dual);
278 
279 
280 /******************************************************************************
281 * Subqueries in other uncovered clauses.
282 ******************************************************************************/
283 
284 /* SELECT clause */
285 select ((a1,a2) IN (select * from t2 where b2 > '0')) IS NULL from t1;
286 
287 /* GROUP BY clause */
288 create table columns (col int key);
289 insert into columns values (1), (2);
290 
291 explain extended
292 select * from t1 group by (select col from columns limit 1);
293 select * from t1 group by (select col from columns limit 1);
294 
295 explain extended
296 select * from t1 group by (a1 in (select col from columns));
297 select * from t1 group by (a1 in (select col from columns));
298 
299 /* ORDER BY clause */
300 explain extended
301 select * from t1 order by (select col from columns limit 1);
302 select * from t1 order by (select col from columns limit 1);
303 
304 /******************************************************************************
305 * Column types/sizes that affect materialization.
306 ******************************************************************************/
307 
308 # test for BIT fields
309 create table t1bit (a1 bit(3), a2 bit(3));
310 create table t2bit (b1 bit(3), b2 bit(3));
311 
312 insert into t1bit values (b'000', b'100');
313 insert into t1bit values (b'001', b'101');
314 insert into t1bit values (b'010', b'110');
315 
316 insert into t2bit values (b'001', b'101');
317 insert into t2bit values (b'010', b'110');
318 insert into t2bit values (b'110', b'111');
319 
320 
321 explain extended select bin(a1), bin(a2)
322 from t1bit
323 where (a1, a2) in (select b1, b2 from t2bit);
324 
325 select bin(a1), bin(a2)
326 from t1bit
327 where (a1, a2) in (select b1, b2 from t2bit);
328 
329 drop table t1bit, t2bit;
330 
331 # test mixture of BIT and BLOB
332 create table t1bb (a1 bit(3), a2 blob(3));
333 create table t2bb (b1 bit(3), b2 blob(3));
334 
335 insert into t1bb values (b'000', '100');
336 insert into t1bb values (b'001', '101');
337 insert into t1bb values (b'010', '110');
338 
339 insert into t2bb values (b'001', '101');
340 insert into t2bb values (b'010', '110');
341 insert into t2bb values (b'110', '111');
342 
343 explain extended select bin(a1), a2
344 from t1bb
345 where (a1, a2) in (select b1, b2 from t2bb);
346 
347 select bin(a1), a2
348 from t1bb
349 where (a1, a2) in (select b1, b2 from t2bb);
350 
351 drop table t1bb, t2bb;
352 drop table t1, t2, t3, t1i, t2i, t3i, columns;
353 
354 /******************************************************************************
355 * Test the cache of the left operand of IN.
356 ******************************************************************************/
357 
358 # Test that default values of Cached_item are not used for comparison
359 create table t1 (s1 int);
360 create table t2 (s2 int);
361 insert into t1 values (5),(1),(0);
362 insert into t2 values (0), (1);
363 --sorted_result
364 select s2 from t2 where s2 in (select s1 from t1);
365 drop table t1, t2;
366 
367 create table t1 (a int not null, b int not null);
368 create table t2 (c int not null, d int not null);
369 create table t3 (e int not null);
370 
371 # the first outer row has no matching inner row
372 insert into t1 values (1,10);
373 insert into t1 values (1,20);
374 insert into t1 values (2,10);
375 insert into t1 values (2,20);
376 insert into t1 values (2,30);
377 insert into t1 values (3,20);
378 insert into t1 values (4,40);
379 
380 insert into t2 values (2,10);
381 insert into t2 values (2,20);
382 insert into t2 values (2,40);
383 insert into t2 values (3,20);
384 insert into t2 values (4,10);
385 insert into t2 values (5,10);
386 
387 insert into t3 values (10);
388 insert into t3 values (10);
389 insert into t3 values (20);
390 insert into t3 values (30);
391 
392 explain extended
393 select a from t1 where a in (select c from t2 where d >= 20);
394 select a from t1 where a in (select c from t2 where d >= 20);
395 
396 create index it1a on t1(a);
397 
398 explain extended
399 select a from t1 where a in (select c from t2 where d >= 20);
400 select a from t1 where a in (select c from t2 where d >= 20);
401 
402 # the first outer row has a matching inner row
403 insert into t2 values (1,10);
404 
405 explain extended
406 select a from t1 where a in (select c from t2 where d >= 20);
407 select a from t1 where a in (select c from t2 where d >= 20);
408 
409 # cacheing for IN predicates inside a having clause - here the cached
410 # items are changed to point to temporary tables.
411 explain extended
412 select a from t1 group by a having a in (select c from t2 where d >= 20);
413 select a from t1 group by a having a in (select c from t2 where d >= 20);
414 
415 # create an index that can be used for the outer query GROUP BY
416 create index iab on t1(a, b);
417 explain extended
418 select a from t1 group by a having a in (select c from t2 where d >= 20);
419 select a from t1 group by a having a in (select c from t2 where d >= 20);
420 
421 explain extended
422 select a from t1 group by a
423 having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
424 select a from t1 group by a
425 having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
426 explain extended
427 select a from t1
428 where a in (select c from t2 where d >= some(select e from t3 where b=e));
429 --sorted_result
430 select a from t1
431 where a in (select c from t2 where d >= some(select e from t3 where b=e));
432 
433 drop table t1, t2, t3;
434 
435 #
436 # BUG#36133 "Assertion `exec_method != MATERIALIZATION || (exec_method == MATERIALIZATION &&"
437 #
438 create table t2 (a int, b int, key(a), key(b));
439 insert into t2 values (3,3),(3,3),(3,3);
440 select 1 from t2 where
441  t2.a > 1
442  or
443  t2.a = 3 and not t2.a not in (select t2.b from t2);
444 drop table t2;
445 
446 #
447 # BUG#37896 Assertion on entry of Item_in_subselect::exec on subquery with AND NOT
448 #
449 create table t1 (a1 int key);
450 create table t2 (b1 int);
451 insert into t1 values (5);
452 
453 explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
454 select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
455 explain select min(a1) from t1 where 7 in (select b1 from t2);
456 select min(a1) from t1 where 7 in (select b1 from t2);
457 drop table t1,t2;
458 
459 #
460 # BUG#36752 "subquery materialization produces wrong results when comparing different types"
461 #
462 create table t1 (a char(2), b varchar(10));
463 insert into t1 values ('a', 'aaa');
464 insert into t1 values ('aa', 'aaaa');
465 
466 explain select a,b from t1 where b in (select a from t1);
467 select a,b from t1 where b in (select a from t1);
468 prepare st1 from "select a,b from t1 where b in (select a from t1)";
469 execute st1;
470 execute st1;
471 drop table t1;
472 
473 #
474 # Test for Bug#16603 GROUP BY in a row subquery with a quantifier
475 # when an index is defined on the grouping field
476 
477 CREATE TABLE t1 (a varchar(5), b varchar(10));
478 INSERT INTO t1 VALUES
479  ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
480  ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
481 
482 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
483 EXPLAIN
484 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
485 
486 ALTER TABLE t1 ADD INDEX(a);
487 
488 --let $query=SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a)
489 FLUSH STATUS;
490 --eval $query
491 SHOW SESSION STATUS LIKE 'Sort_scan%';
492 --eval EXPLAIN $query
493 
494 DROP TABLE t1;
495 
496 
497 #
498 # Bug#36011 Server crash with explain extended on query with dependent
499 # subqueries
500 #
501 
502 CREATE TABLE t1 (a INT);
503 INSERT INTO t1 VALUES (1),(2);
504 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 GROUP BY a);
505 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a);
506 DROP TABLE t1;
507 
508 
509 --echo #
510 --echo # BUG#49630: Segfault in select_describe() with double
511 --echo # nested subquery and materialization
512 --echo #
513 
514 CREATE TABLE t1 (t1i int);
515 CREATE TABLE t2 (t2i int);
516 CREATE TABLE t3 (t3i int);
517 CREATE TABLE t4 (t4i int);
518 
519 INSERT INTO t1 VALUES (1); # Note: t1 must be const table
520 INSERT INTO t2 VALUES (1),(2);
521 INSERT INTO t3 VALUES (1),(2);
522 INSERT INTO t4 VALUES (1),(2);
523 
524 --echo
525 EXPLAIN
526 SELECT t1i
527 FROM t1 JOIN t4 ON t1i=t4i
528 WHERE (t1i) IN (
529  SELECT t2i
530  FROM t2
531  WHERE (t2i) IN (
532  SELECT t3i
533  FROM t3
534  GROUP BY t3i
535  )
536  );
537 
538 DROP TABLE t1,t2,t3,t4;
539 
540 --echo #
541 --echo # BUG#46680 - Assertion failed in file item_subselect.cc,
542 --echo # line 305 crashing on HAVING subquery
543 --echo #
544 
545 --echo # Create tables
546 --echo #
547 
548 CREATE TABLE t1 (
549  pk INT,
550  v VARCHAR(1) DEFAULT NULL,
551  PRIMARY KEY(pk)
552 );
553 CREATE TABLE t2 LIKE t1;
554 CREATE TABLE t3 LIKE t1;
555 CREATE TABLE empty1 (a int);
556 
557 INSERT INTO t1 VALUES (1,'c'),(2,NULL);
558 INSERT INTO t2 VALUES (3,'m'),(4,NULL);
559 INSERT INTO t3 VALUES (1,'n');
560 
561 --echo
562 --echo #
563 --echo # 1) Test that subquery materialization is setup for query with
564 --echo # premature optimize() exit due to "Impossible WHERE"
565 --echo #
566 SELECT MIN(t2.pk)
567 FROM t2 JOIN t1 ON t1.pk=t2.pk
568 WHERE 'j'
569 HAVING ('m') IN (
570 SELECT v
571 FROM t2);
572 
573 --echo
574 EXPLAIN
575 SELECT MIN(t2.pk)
576 FROM t2 JOIN t1 ON t1.pk=t2.pk
577 WHERE 'j'
578 HAVING ('m') IN (
579 SELECT v
580 FROM t2);
581 
582 --echo
583 --echo #
584 --echo # 2) Test that subquery materialization is setup for query with
585 --echo # premature optimize() exit due to "No matching min/max row"
586 --echo #
587 SELECT MIN(t2.pk)
588 FROM t2
589 WHERE t2.pk>10
590 HAVING ('m') IN (
591 SELECT v
592 FROM t2);
593 
594 --echo
595 EXPLAIN
596 SELECT MIN(t2.pk)
597 FROM t2
598 WHERE t2.pk>10
599 HAVING ('m') IN (
600 SELECT v
601 FROM t2);
602 
603 --echo
604 --echo #
605 --echo # 3) Test that subquery materialization is setup for query with
606 --echo # premature optimize() exit due to "Select tables optimized away"
607 --echo #
608 SELECT MIN(pk)
609 FROM t1
610 WHERE pk=NULL
611 HAVING ('m') IN (
612 SELECT v
613 FROM t2);
614 
615 --echo
616 EXPLAIN
617 SELECT MIN(pk)
618 FROM t1
619 WHERE pk=NULL
620 HAVING ('m') IN (
621 SELECT v
622 FROM t2);
623 
624 --echo
625 --echo #
626 --echo # 4) Test that subquery materialization is setup for query with
627 --echo # premature optimize() exit due to "No matching row in const table"
628 --echo #
629 --echo
630 SELECT MIN(a)
631 FROM (SELECT a FROM empty1) tt
632 HAVING ('m') IN (
633 SELECT v
634 FROM t2);
635 
636 --echo
637 EXPLAIN
638 SELECT MIN(a)
639 FROM (SELECT a FROM empty1) tt
640 HAVING ('m') IN (
641 SELECT v
642 FROM t2);
643 
644 --echo
645 --echo #
646 --echo # 5) Test that subquery materialization is setup for query with
647 --echo # premature optimize() exit due to "Impossible WHERE noticed
648 --echo # after reading const tables"
649 --echo #
650 SELECT min(t1.pk)
651 FROM t1
652 WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
653 HAVING ('m') IN (
654 SELECT v
655 FROM t2);
656 
657 --echo
658 EXPLAIN
659 SELECT min(t1.pk)
660 FROM t1
661 WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
662 HAVING ('m') IN (
663 SELECT v
664 FROM t2);
665 
666 --echo #
667 --echo # Cleanup for BUG#46680
668 --echo #
669 DROP TABLE IF EXISTS t1,t2,t3,empty1;
670 
671 
672 --echo #
673 --echo # BUG#52344 - Subquery materialization:
674 --echo # Assertion if subquery in on-clause of outer join
675 --echo #
676 
677 CREATE TABLE t1 (i INTEGER);
678 INSERT INTO t1 VALUES (10);
679 
680 CREATE TABLE t2 (j INTEGER);
681 INSERT INTO t2 VALUES (5);
682 
683 CREATE TABLE t3 (k INTEGER);
684 
685 EXPLAIN
686 SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
687 SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
688 
689 EXPLAIN
690 SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
691 SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
692 
693 DROP TABLE t1, t2, t3;
694 
695 --echo # End BUG#52344
696 
697 
698 #
699 # Bug #52538 Valgrind bug: Item_in_subselect::init_left_expr_cache()
700 #
701 CREATE TABLE t1 (
702  pk INTEGER AUTO_INCREMENT,
703  col_int_nokey INTEGER,
704  col_int_key INTEGER,
705 
706  col_varchar_key VARCHAR(1),
707 
708  PRIMARY KEY (pk),
709  KEY (col_int_key),
710  KEY (col_varchar_key, col_int_key)
711 )
712 ;
713 
714 INSERT INTO t1 (
715  col_int_key, col_int_nokey, col_varchar_key
716 )
717 VALUES
718 (2, NULL, 'w'),
719 (9, 7, 'm'),
720 (3, 9, 'm'),
721 (9, 7, 'k'),
722 (NULL, 4, 'r'),
723 (9, 2, 't'),
724 (3, 6, 'j'),
725 (8, 8, 'u'),
726 (8, NULL, 'h'),
727 (53, 5, 'o'),
728 (0, NULL, NULL),
729 (5, 6, 'k'),
730 (166, 188, 'e'),
731 (3, 2, 'n'),
732 (0, 1, 't'),
733 (1, 1, 'c'),
734 (9, 0, 'm'),
735 (5, 9, 'y'),
736 (6, NULL, 'f'),
737 (2, 4, 'd')
738 ;
739 
740 SELECT table2.col_varchar_key AS field1,
741  table2.col_int_nokey AS field2
742 FROM ( t1 AS table1 LEFT OUTER JOIN t1 AS table2
743  ON (table2.col_varchar_key = table1.col_varchar_key ) )
744 WHERE table1.pk = 6
745 HAVING ( field2 ) IN
746 ( SELECT SUBQUERY2_t2.col_int_nokey AS SUBQUERY2_field2
747  FROM ( t1 AS SUBQUERY2_t1 JOIN t1 AS SUBQUERY2_t2
748  ON (SUBQUERY2_t2.col_varchar_key = SUBQUERY2_t1.col_varchar_key ) ) )
749 ORDER BY field2
750 ;
751 
752 drop table t1;
753 
754 
755 --echo #
756 --echo # BUG#53103: MTR test ps crashes in optimize_cond()
757 --echo # when running with --debug
758 --echo #
759 
760 CREATE TABLE t1(track varchar(15));
761 
762 INSERT INTO t1 VALUES ('CAD'), ('CAD');
763 
764 PREPARE STMT FROM
765 "SELECT 1 FROM t1
766  WHERE
767  track IN (SELECT track FROM t1
768  GROUP BY track
769  HAVING track>='CAD')";
770 EXECUTE STMT ;
771 EXECUTE STMT ;
772 
773 DEALLOCATE PREPARE STMT;
774 DROP TABLE t1;
775 
776 --echo # End of BUG#53103
777 
778 --echo #
779 --echo # BUG#54511 - Assertion failed: cache != 0L in file
780 --echo # sql_select.cc::sub_select_cache on HAVING
781 --echo #
782 
783 CREATE TABLE t1 (i int(11));
784 CREATE TABLE t2 (c char(1));
785 CREATE TABLE t3 (c char(1));
786 
787 # These records are needed for the test to fail with MyISAM. The test
788 # fails with InnoDB without these (difference due to optimization of
789 # aggregates available only in MyISAM)
790 INSERT INTO t1 VALUES (1), (2);
791 INSERT INTO t2 VALUES ('a'), ('b');
792 INSERT INTO t3 VALUES ('x'), ('y');
793 
794 SELECT COUNT( i ),i
795 FROM t1
796 HAVING ('c')
797  IN (SELECT t2.c FROM (t2 JOIN t3));
798 
799 DROP TABLE t1,t2,t3;
800 
801 --echo # End BUG#54511
802 
803 --echo #
804 --echo # BUG#56367 - Assertion exec_method != EXEC_MATERIALIZATION...
805 --echo # on subquery in FROM
806 --echo #
807 
808 CREATE TABLE t1 (a INTEGER);
809 
810 CREATE TABLE t2 (b INTEGER);
811 INSERT INTO t2 VALUES (1);
812 
813 let $query =
814 SELECT a FROM (
815  SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
816 ) table1;
817 eval explain $query;
818 eval $query;
819 
820 DROP TABLE t1, t2;
821 
822 --echo # End BUG#56367
823 
824 --echo #
825 --echo # Bug#59833 - materialization=on/off leads to different result set
826 --echo # when using IN
827 --echo #
828 
829 CREATE TABLE t1 (
830  pk int NOT NULL,
831  f1 int DEFAULT NULL,
832  PRIMARY KEY (pk)
833 ) ENGINE=MyISAM;
834 
835 CREATE TABLE t2 (
836  pk int NOT NULL,
837  f1 int DEFAULT NULL,
838  PRIMARY KEY (pk)
839 ) ENGINE=MyISAM;
840 
841 INSERT INTO t1 VALUES (10,0);
842 INSERT INTO t2 VALUES (10,0),(11,0);
843 
844 let $query=
845 SELECT * FROM t1 JOIN t2 USING (f1)
846 WHERE t1.f1 IN (SELECT t1.pk FROM t1 ORDER BY t1.f1);
847 
848 eval explain $query;
849 eval $query;
850 
851 DROP TABLE t1, t2;
852 
853 --echo # End Bug#59833
854 
855 --echo #
856 --echo # Bug#11852644 - CRASH IN ITEM_REF::SAVE_IN_FIELD ON SELECT DISTINCT
857 --echo #
858 
859 CREATE TABLE t1 (
860  col_varchar_key varchar(1) DEFAULT NULL,
861  col_varchar_nokey varchar(1) DEFAULT NULL,
862  KEY col_varchar_key (col_varchar_key))
863 ;
864 
865 INSERT INTO t1 VALUES
866 ('v','v'),('r','r');
867 
868 CREATE TABLE t2 (
869  col_varchar_key varchar(1) DEFAULT NULL,
870  col_varchar_nokey varchar(1) DEFAULT NULL,
871  KEY col_varchar_key(col_varchar_key))
872 ;
873 
874 INSERT INTO t2 VALUES
875 ('r','r'),('c','c');
876 
877 CREATE VIEW v3 AS SELECT * FROM t2;
878 
879 SELECT DISTINCT alias2.col_varchar_key
880 FROM t1 AS alias1 JOIN v3 AS alias2
881 ON alias2.col_varchar_key = alias1.col_varchar_key
882 HAVING col_varchar_key IN (SELECT col_varchar_nokey FROM t2)
883 ;
884 
885 DROP TABLE t1, t2;
886 DROP VIEW v3;
887 
888 --echo # End Bug#11852644
889 
890 --echo
891 --echo # Bug#12668294 - GROUP BY ON EMPTY RESULT GIVES EMPTY ROW
892 --echo # INSTEAD OF NULL WHEN MATERIALIZATION ON
893 --echo
894 
895 CREATE TABLE t1 (col_int_nokey INT) ENGINE=MEMORY;
896 CREATE TABLE t2 (col_int_nokey INT) ENGINE=MEMORY;
897 INSERT INTO t2 VALUES (8),(7);
898 CREATE TABLE t3 (col_int_nokey INT) ENGINE=MEMORY;
899 INSERT INTO t3 VALUES (7);
900 
901 SELECT MIN(t3.col_int_nokey),t1.col_int_nokey AS field3
902 FROM t3
903  LEFT JOIN t1
904  ON t1.col_int_nokey
905 WHERE (194, 200) IN (
906  SELECT SQ4_alias1.col_int_nokey,
907  SQ4_alias2.col_int_nokey
908  FROM t2 AS SQ4_alias1
909  JOIN
910  t2 AS SQ4_alias2
911  ON SQ4_alias2.col_int_nokey = 5
912  )
913 GROUP BY field3 ;
914 
915 DROP TABLE t1;
916 DROP TABLE t2;
917 DROP TABLE t3;
918 
919 --echo #
920 --echo # Bug#13419028 - SUBQUERY MATERIALIZATION NOT USED IN CREATE
921 --echo # SELECT
922 --echo #
923 
924 CREATE TABLE t1(a int);
925 INSERT INTO t1 values(1),(2);
926 CREATE TABLE t2(a int);
927 INSERT INTO t2 values(1),(2);
928 
929 EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
930 FLUSH STATUS;
931 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
932 CREATE TABLE t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
933 SELECT * FROM t3;
934 # prove that subquery materialization was used:
935 SHOW STATUS LIKE "CREATED_TMP_TABLES";
936 DROP TABLE t1,t2,t3;
937 
938 --echo #
939 --echo # Bug#13552968: Extra row with materialization on join + subquery in
940 --echo #
941 
942 CREATE TABLE t1 (
943  col_varchar_nokey varchar(1) NOT NULL
944 ) ENGINE=MyISAM;
945 
946 INSERT INTO t1 VALUES ('b');
947 
948 CREATE TABLE t2 (
949  col_varchar_nokey varchar(1) NOT NULL
950 ) ENGINE=MyISAM;
951 
952 INSERT INTO t2 VALUES ('k');
953 
954 CREATE TABLE t3 (
955  col_varchar_nokey varchar(1) NOT NULL
956 ) ENGINE=MyISAM;
957 
958 let $query=
959 SELECT STRAIGHT_JOIN *
960 FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
961  FROM t3);
962 
963 eval explain $query;
964 eval $query;
965 
966 DROP TABLE t1, t2, t3;
967 
968 --echo # End of test for bug#13552968
969 
970 --echo #
971 --echo # Bug#13591383: Assertion !(*tab->on_expr_ref && .. && is_expensive())
972 --echo # in join_read_const_table()
973 --echo #
974 
975 CREATE TABLE t1 (v INTEGER) ENGINE=MyISAM;
976 INSERT INTO t1 VALUES(1);
977 
978 CREATE TABLE t2 (v INTEGER) ENGINE=MyISAM;
979 
980 SELECT *
981 FROM t1 LEFT JOIN t2
982  ON t2.v IN(SELECT v FROM t1);
983 
984 DROP TABLE t1, t2;
985 
986 --echo # End of test for bug#13591383.
987 
988 --echo #
989 --echo # Bug#13607423: Assertion !(*tab->on_expr_ref->is_expensive())
990 --echo # in join_read_const_table()
991 --echo #
992 
993 CREATE TABLE t1 (
994  pk int NOT NULL,
995  col_int_nokey int DEFAULT NULL,
996  col_int_key int DEFAULT NULL,
997  PRIMARY KEY (pk),
998  KEY col_int_key (col_int_key)
999 ) ENGINE=MyISAM;
1000 
1001 INSERT INTO t1 VALUES (1,2,4), (2,150,62);
1002 
1003 CREATE TABLE t2 (
1004  pk int NOT NULL,
1005  col_int_key int DEFAULT NULL,
1006  PRIMARY KEY (pk)
1007 ) ENGINE=MyISAM;
1008 
1009 INSERT INTO t2 VALUES (1,7);
1010 
1011 let $query=
1012 SELECT table1.pk, table2.pk
1013 FROM t2 AS table1 LEFT JOIN t2 AS table2
1014  ON table2.pk = table1.pk AND
1015  table2.col_int_key IN
1016  (SELECT col_int_key
1017  FROM t1 AS innr
1018  WHERE innr.col_int_nokey > innr.col_int_nokey
1019  GROUP BY col_int_key
1020  HAVING COUNT(*) > 0
1021  );
1022 
1023 eval explain $query;
1024 FLUSH STATUS;
1025 eval $query;
1026 SHOW SESSION STATUS LIKE 'Sort_scan%';
1027 
1028 DROP TABLE t1, t2;
1029 
1030 --echo # End of test for bug#13607423.
1031 
1032 --echo
1033 --echo Test of WL#6094 "Allow subquery materialization in NOT IN if all
1034 --echo columns are not nullable"
1035 --echo
1036 
1037 # We want to test WL#6094 only, not WL#6095, so we use 2 columns.
1038 
1039 create table t1(a int not null);
1040 create table t2(a int not null);
1041 insert into t1 values(1),(2);
1042 insert into t2 values(1),(2);
1043 
1044 --echo Test in SELECT list
1045 
1046 --echo
1047 let $query=select a, (a,a) in (select a,a from t2) from t1;
1048 
1049 --echo cols not nullable => subq materialization
1050 eval explain extended $query;
1051 eval $query;
1052 
1053 --echo
1054 let $query=select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
1055 from t1 join t2 on t1.a+t2.a=1000;
1056 --echo cols not nullable => subq materialization
1057 eval explain extended $query;
1058 eval $query;
1059 
1060 --echo
1061 let $query=select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
1062 from t1 left join t2 on t1.a+t2.a=1000;
1063 
1064 --echo t2.a is not nullable, but in the query it may appear as NULL
1065 --echo as it's in an outer join. So, no materialization.
1066 eval explain extended $query;
1067 eval $query;
1068 
1069 --echo
1070 alter table t2 modify a int;
1071 let $query=select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
1072 from t1 join t2 on t1.a+t2.a=1000;
1073 --echo two nullable inner cols => no subq materialization
1074 eval explain extended $query;
1075 eval $query;
1076 alter table t2 modify a int not null;
1077 
1078 --echo
1079 --echo Test in WHERE
1080 --echo
1081 let $query=select t1.a, t2.a
1082 from t1 join t2 on t1.a+t2.a=3
1083 where (t2.a,t2.a) in (select a,a from t2 as t3);
1084 --echo top-level => subq materialization. With one exception: if
1085 --echo semijoin is enabled in @@optimizer_switch, semijoin is chosen,
1086 --echo then rejected (due to outer join), and in that case, the
1087 --echo fallback is IN->EXISTS, subq-materialization is not tried...
1088 eval explain extended $query;
1089 eval $query;
1090 
1091 --echo
1092 let $query=select t1.a, t2.a
1093 from t1 join t2 on t1.a+t2.a=3
1094 where (t2.a,t2.a) not in (select a,a from t2 as t3);
1095 --echo cols not nullable => subq materialization
1096 eval explain extended $query;
1097 eval $query;
1098 
1099 drop table t1,t2;
1100 
1101 --echo
1102 --echo Test of WL6095 "Allow subquery materialization in NOT IN if
1103 --echo single-column subquery"
1104 --echo
1105 
1106 # We want to test WL#6095 only, not WL#6094, so we use nullable columns.
1107 
1108 create table t1(a int null);
1109 create table t2(a int null);
1110 insert into t1 values(1),(2);
1111 insert into t2 values(1),(2);
1112 
1113 --echo
1114 let $query=select a, a in (select a from t2) from t1;
1115 
1116 --echo one col => subq materialization
1117 eval explain extended $query;
1118 eval $query;
1119 
1120 --echo
1121 let $query=select t1.a, t2.a, t2.a in (select * from t2 as t3)
1122 from t1 left join t2 on t1.a+t2.a=1000;
1123 
1124 --echo t2.a is not nullable, but in the query it may appear as NULL
1125 --echo as it's in an outer join. But there is only one inner column so
1126 --echo materialization is possible
1127 eval explain extended $query;
1128 eval $query;
1129 
1130 --echo
1131 let $query=select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
1132 from t1 left join t2 on t1.a+t2.a=1000;
1133 
1134 --echo _two_ outer columns, nullable => no materialization
1135 eval explain extended $query;
1136 eval $query;
1137 
1138 drop table t1,t2;
1139 
1140 --echo
1141 --echo Test in HAVING
1142 
1143 create table t1(a int, b int);
1144 create table t2(a int);
1145 insert into t1 values(1,1),(1,2),(1,3),(2,1),(2,2),(2,3);
1146 insert into t2 values(10),(20);
1147 
1148 let $query=select t1.a as z, sum(t1.b) from t1 group by t1.a
1149 having (z in (select * from t2)) is null;
1150 
1151 --echo no NULLs.
1152 
1153 eval explain extended $query;
1154 eval $query;
1155 
1156 --echo one outer NULL
1157 insert into t1 values(null,null);
1158 
1159 eval explain extended $query;
1160 eval $query;
1161 
1162 --echo one outer NULL and one inner NULL
1163 insert into t2 values(null);
1164 
1165 eval explain extended $query;
1166 eval $query;
1167 
1168 --echo one inner NULL
1169 delete from t1 where a is null;
1170 
1171 eval explain extended $query;
1172 eval $query;
1173 
1174 drop table t1,t2;
1175 
1176 --echo
1177 --echo Verify that an inner NULL is looked up only once (result is
1178 --echo cached).
1179 
1180 create table t1(a int);
1181 create table t2(a int);
1182 insert into t1 values(1),(2),(3),(4),(5),(6);
1183 insert into t1 select * from t1; # t1 has 12 rows
1184 insert into t2 values(10),(20),(NULL);
1185 
1186 let $query=select a, (a in (select * from t2)) from t1;
1187 
1188 eval explain extended $query;
1189 flush status;
1190 eval $query;
1191 --echo There will be one look-up in the temporary table for each row
1192 --echo of t1 (12), plus one additional look-up to check whether table
1193 --echo contains a NULL value.
1194 show status like "handler_read_key";
1195 
1196 drop table t1,t2;
1197 
1198 --echo #
1199 --echo # Bug#13495157 - SUBQUERY MATERIALIZATION NOT USED FOR CERTAIN
1200 --echo # STATEMENTS
1201 --echo #
1202 
1203 CREATE TABLE t1(a INT);
1204 INSERT INTO t1 VALUES(1),(2),(3);
1205 CREATE TABLE t2(a INT);
1206 INSERT INTO t2 VALUES(1),(2),(4);
1207 
1208 --echo # subquery materialization used for SELECT:
1209 EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1210 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1211 
1212 --echo # Also used for INSERT SELECT:
1213 # a) all different tables:
1214 CREATE TABLE t3 SELECT * FROM t1;
1215 EXPLAIN INSERT INTO t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1216 INSERT INTO t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1217 --sorted_result
1218 SELECT * FROM t3;
1219 
1220 # b) insert into subquery's selected table
1221 EXPLAIN INSERT INTO t2 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1222 INSERT INTO t2 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1223 --sorted_result
1224 SELECT * FROM t2;
1225 
1226 # c) insert into subquery's and query's selected table
1227 EXPLAIN INSERT INTO t2 SELECT * FROM t2 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1228 INSERT INTO t2 SELECT * FROM t2 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
1229 --sorted_result
1230 SELECT * FROM t2;
1231 
1232 --echo # Not used for single-table UPDATE, DELETE:
1233 # a) all different tables
1234 EXPLAIN SELECT * FROM t2 WHERE a IN (SELECT * FROM t1);
1235 EXPLAIN UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t1);
1236 UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t1);
1237 --sorted_result
1238 SELECT * FROM t2;
1239 EXPLAIN DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
1240 DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
1241 --sorted_result
1242 SELECT * FROM t2;
1243 
1244 # b) update/delete in subquery's selected table: forbidden
1245 --error ER_UPDATE_TABLE_USED
1246 EXPLAIN UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t2);
1247 --error ER_UPDATE_TABLE_USED
1248 EXPLAIN DELETE FROM t2 WHERE a IN (SELECT * FROM t2);
1249 
1250 # Put some content so that future queries have rows to modify:
1251 UPDATE t2 SET a=3 WHERE a=0;
1252 
1253 --echo # Used for multi-table UPDATE, DELETE:
1254 
1255 # a) all different tables
1256 EXPLAIN SELECT * FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
1257 EXPLAIN UPDATE t2,t3 SET t2.a=t2.a-2 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
1258 UPDATE t2,t3 SET t2.a=t2.a-2 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
1259 --sorted_result
1260 SELECT * FROM t2;
1261 EXPLAIN DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
1262 DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
1263 --sorted_result
1264 SELECT * FROM t2;
1265 
1266 # b) update/delete in subquery's selected table: forbidden
1267 --error ER_UPDATE_TABLE_USED
1268 EXPLAIN UPDATE t2,t3 SET t2.a=10 WHERE t2.a IN (SELECT * FROM t2 WHERE a <> 2);
1269 --error ER_UPDATE_TABLE_USED
1270 EXPLAIN DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t2 WHERE a <> 2);
1271 
1272 DROP TABLE t1,t2,t3;
1273 
1274 --echo #
1275 --echo # Test that subquery materialization only does one lookup: does
1276 --echo # not try to read the next row if the first row failed the
1277 --echo # subquery's WHERE. We use a case where index lookup is not
1278 --echo # enough to satisfy IN(), because index has length two when the
1279 --echo # outer value has length three, and thus the post-filtering
1280 --echo # WHERE added by subselect_hash_sj_engine::setup() makes the
1281 --echo # decision.
1282 --echo #
1283 create table t1 (a varchar(3));
1284 create table t2 (a varchar(2));
1285 insert into t1 values('aaa'), ('aaa');
1286 insert into t2 values('aa'), ('aa');
1287 let $query=select * from t1 where a in (select a from t2);
1288 eval explain $query;
1289 flush status;
1290 eval $query;
1291 show status like "handler_read%";
1292 drop table t1,t2;
1293 
1294 --echo #
1295 --echo # Bug#13655791 DIFFERENT RESULT WITH WL6094 ON QUERY WITH XOR
1296 --echo # IN WHERE CLAUSE + MYISAM
1297 --echo #
1298 
1299 CREATE TABLE t1 (
1300  pk int NOT NULL,
1301  col_varchar_nokey varchar(1) DEFAULT NULL,
1302  PRIMARY KEY (pk)
1303 );
1304 
1305 INSERT INTO t1 VALUES (10,'x');
1306 
1307 CREATE TABLE t2 (
1308  pk int NOT NULL,
1309  col_varchar_nokey varchar(1) DEFAULT NULL,
1310  PRIMARY KEY (pk)
1311 );
1312 
1313 INSERT INTO t2 VALUES (1,'v'), (5,'x'), (11,'z'), (12,'c'), (15,'y');
1314 
1315 CREATE TABLE t3 (
1316  pk int NOT NULL,
1317  col_int_key int DEFAULT NULL,
1318  PRIMARY KEY (pk),
1319  KEY col_int_key (col_int_key)
1320 );
1321 
1322 INSERT INTO t3 VALUES (10,8);
1323 
1324 CREATE TABLE t4 (
1325  pk int NOT NULL,
1326  col_varchar_nokey varchar(1) DEFAULT NULL,
1327  PRIMARY KEY (pk)
1328 );
1329 
1330 INSERT INTO t4 VALUES (1,'x');
1331 
1332 let $query=
1333 SELECT OUTR.pk, OUTR.col_varchar_nokey, OUTR2.col_varchar_nokey
1334 FROM t2 AS OUTR2
1335  JOIN t4 AS OUTR
1336  ON (OUTR2.col_varchar_nokey > OUTR.col_varchar_nokey)
1337 WHERE
1338  OUTR.col_varchar_nokey IN (
1339  SELECT INNR.col_varchar_nokey
1340  FROM t3 AS INNR2
1341  LEFT JOIN t1 AS INNR
1342  ON (INNR2.col_int_key >= INNR.pk)
1343  )
1344  XOR OUTR.pk < 6
1345 ;
1346 
1347 eval EXPLAIN $query;
1348 FLUSH STATUS;
1349 eval $query;
1350 SHOW STATUS LIKE "HANDLER_READ%";
1351 
1352 DROP TABLE t1,t2,t3,t4;
1353 
1354 --echo #
1355 --echo # Bug#13727407: Assert !item->const_item() || !item->not_null_tables()
1356 --echo #
1357 
1358 CREATE TABLE t1 (
1359  col_int_key INT,
1360  KEY col_int_key (col_int_key)
1361 );
1362 
1363 INSERT INTO t1 VALUES (1);
1364 
1365 CREATE TABLE t2 (
1366  col_int_key INT,
1367  col_time_key TIME,
1368  col_datetime_nokey DATETIME,
1369  KEY col_int_key (col_int_key),
1370  KEY col_time_key (col_time_key)
1371 );
1372 
1373 INSERT INTO t2 VALUES
1374  (7,'14:03:03','2001-11-28 00:50:27'), (1,'01:46:09','2007-10-09 19:53:04');
1375 
1376 let $query=
1377 SELECT col_datetime_nokey AS x
1378 FROM t2 AS outr
1379 WHERE col_int_key IN (
1380  SELECT STRAIGHT_JOIN col_int_key
1381  FROM t1
1382 ) AND outr.col_int_key = 0
1383 HAVING x = '2000-09-09'
1384 ORDER BY col_time_key;
1385 
1386 eval EXPLAIN $query;
1387 eval $query;
1388 
1389 DROP TABLE t1, t2;
1390 
1391 --echo #
1392 --echo # Bug#13838501 ASSERTION `TABLE->FILE->INITED' FAILED IN
1393 --echo # SUBSELECT_HASH_SJ_ENGINE::EXEC
1394 --echo #
1395 
1396 CREATE TABLE t1
1397 (c1 bigint,c2 char,pk INT,c3 char,c4 int,c5 INT,key (c5))
1398 ENGINE=InnoDB;
1399 INSERT INTO t1 VALUES (763078661862588416,0,1,'',1,'');
1400 CREATE TABLE t2 (c4k int,c4 int,cminnuk INT,key (cminnuk)) ENGINE=InnoDB;
1401 INSERT INTO t2 VALUES(0,'','');
1402 CREATE TABLE t3
1403 (c4 int,pk INT,c1 bigint,cyk year,cy year,key (cyk))
1404 ENGINE=InnoDB;
1405 INSERT INTO t3 VALUES(0,8,'',0,'');
1406 let $query=
1407 SELECT o.c2 AS x FROM t1 AS o
1408 WHERE o.c1 IN
1409  (SELECT innr.c4 AS y
1410  FROM t2 AS innr2 JOIN t3 AS innr
1411  ON (innr2.c4k=innr.c4)
1412  WHERE innr.c1=6 OR NOT innr.c1=innr.pk
1413  ORDER BY innr.c4)
1414  AND o.c4=7 XOR o.pk=3 ORDER BY o.pk;
1415 eval EXPLAIN $query;
1416 eval $query;
1417 
1418 DROP TABLE t1,t2,t3;
1419 
1420 --echo # End of 5.6 tests
1421 
1422 set @@optimizer_switch=@old_opt_switch;