2 # Hash semi-join regression tests
3 # (WL#1110: Subquery optimization: materialization)
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';
11 drop
table if exists t1, t2, t3, t1i, t2i, t3i;
12 drop
view if exists v1, v2, v1m, v2m;
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));
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');
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');
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');
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);
42 create
index it2i1 on t2i (b1);
43 create
index it2i2 on t2i (b2);
44 create
index it2i3 on t2i (b1, b2);
46 create
index it3i1 on t3i (c1);
47 create
index it3i2 on t3i (c2);
48 create
index it3i3 on t3i (c1, c2);
50 insert into t1i select * from t1;
51 insert into t2i select * from t2;
52 insert into t3i select * from t3;
57 # non-indexed nullable fields
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');
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);
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);
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);
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');
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);
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');
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);
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);
95 # BUG#31639: Wrong plan for uncorrelated subquery when loose scan is applicable.
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);
100 prepare st1 from "explain select * from t1 where (a1, a2) in (select b1, max(b2) from t2i
group by b1)";
103 prepare st2 from "select * from t1 where (a1, a2) in (select b1, max(b2) from t2i
group by b1)";
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);
111 select * from t1 where (a1, a2) in (select b1, min(b2) from t2i
limit 1,1);
113 # materialize the result of ORDER BY
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);
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);
126 # materialize the result of subquery over temp-table view
128 create algorithm=merge
view v1 as
129 select b1, c2 from t2, t3 where b2 > c2;
131 create algorithm=merge
view v2 as
132 select b1, c2 from t2, t3
group by b2, c2;
134 create algorithm=temptable
view v1m as
135 select b1, c2 from t2, t3 where b2 > c2;
137 create algorithm=temptable
view v2m as
138 select b1, c2 from t2, t3
group by b2, c2;
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);
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);
146 drop
view v1, v2, v1m, v2m;
148 # nested subqueries, views
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'));
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'));
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'));
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'));
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'));
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'));
183 # as above with correlated innermost subquery
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'));
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'));
199 # multiple levels of nesting subqueries, unions
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')
206 (a1, a2) in (select c1, c2 from t3
207 where (c1, c2) in (select b1, b2 from t2i where b2 >
'0')))
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')));
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')
219 (a1, a2) in (select c1, c2 from t3
220 where (c1, c2) in (select b1, b2 from t2i where b2 > '0')))
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')));
228 # UNION of subqueries as a subquery (thus it is not computed via materialization)
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'));
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
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
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
255 # UNION in a subquery
258 where c1 in (select a1 from t1 where a1 >
'0' UNION select b1 from t2 where b1 <
'9');
260 where c1 in (select a1 from t1 where a1 >
'0' UNION select b1 from t2 where b1 <
'9');
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));
271 # subquery has no tables
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');
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);
285 select ((a1,a2) IN (select * from t2 where b2 > '0')) IS NULL from t1;
288 create
table columns (col
int key);
289 insert into columns values (1), (2);
292 select * from t1
group by (select col from columns limit 1);
293 select * from t1
group by (select col from columns limit 1);
296 select * from t1
group by (a1 in (select col from columns));
297 select * from t1
group by (a1 in (select col from columns));
301 select * from t1 order by (select col from columns limit 1);
302 select * from t1 order by (select col from columns limit 1);
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));
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');
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');
321 explain extended select bin(a1), bin(a2)
323 where (a1, a2) in (select b1, b2 from t2bit);
325 select bin(a1), bin(a2)
327 where (a1, a2) in (select b1, b2 from t2bit);
329 drop
table t1bit, t2bit;
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));
335 insert into t1bb values (b
'000',
'100');
336 insert into t1bb values (b
'001',
'101');
337 insert into t1bb values (b
'010',
'110');
339 insert into t2bb values (b
'001',
'101');
340 insert into t2bb values (b
'010',
'110');
341 insert into t2bb values (b
'110',
'111');
343 explain extended select bin(a1), a2
345 where (a1, a2) in (select b1, b2 from t2bb);
349 where (a1, a2) in (select b1, b2 from t2bb);
351 drop
table t1bb, t2bb;
352 drop
table t1, t2, t3, t1i, t2i, t3i, columns;
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);
364 select s2 from t2 where s2 in (select s1 from t1);
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);
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);
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);
387 insert into t3 values (10);
388 insert into t3 values (10);
389 insert into t3 values (20);
390 insert into t3 values (30);
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);
396 create
index it1a on t1(a);
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);
402 # the first outer row has a matching inner row
403 insert into t2 values (1,10);
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);
409 # cacheing for IN predicates inside a having clause - here the cached
410 # items are changed to point to temporary tables.
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);
415 # create an index that can be used for the outer query GROUP BY
416 create
index iab on t1(a, b);
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);
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));
428 where a in (select c from t2 where d >= some(select e from t3 where b=e));
431 where a in (select c from t2 where d >= some(select e from t3 where b=e));
433 drop
table t1, t2, t3;
436 # BUG#36133 "Assertion `exec_method != MATERIALIZATION || (exec_method == MATERIALIZATION &&"
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
443 t2.a = 3 and not t2.a not in (select t2.b from t2);
447 # BUG#37896 Assertion on entry of Item_in_subselect::exec on subquery with AND NOT
449 create
table t1 (a1
int key);
450 create
table t2 (b1
int);
451 insert into t1 values (5);
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);
460 # BUG#36752 "subquery materialization produces wrong results when comparing different types"
462 create
table t1 (a
char(2), b varchar(10));
463 insert into t1 values (
'a',
'aaa');
464 insert into t1 values (
'aa',
'aaaa');
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)";
474 # Test for Bug#16603 GROUP BY in a row subquery with a quantifier
475 # when an index is defined on the grouping field
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);
482 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
484 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
486 ALTER
TABLE t1 ADD INDEX(a);
488 --let $query=SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a)
491 SHOW SESSION STATUS LIKE
'Sort_scan%';
492 --eval EXPLAIN $query
498 # Bug#36011 Server crash with explain extended on query with dependent
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);
510 --echo # BUG#49630: Segfault in select_describe() with
double
511 --echo
# nested subquery and materialization
514 CREATE
TABLE t1 (t1i
int);
515 CREATE
TABLE t2 (t2i
int);
516 CREATE
TABLE t3 (t3i
int);
517 CREATE
TABLE t4 (t4i
int);
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);
527 FROM t1
JOIN t4 ON t1i=t4i
538 DROP
TABLE t1,t2,t3,t4;
541 --echo # BUG#46680 - Assertion failed in
file item_subselect.cc,
542 --echo # line 305 crashing on HAVING subquery
545 --echo # Create tables
550 v VARCHAR(1) DEFAULT NULL,
553 CREATE
TABLE t2 LIKE t1;
554 CREATE
TABLE t3 LIKE t1;
555 CREATE
TABLE empty1 (a
int);
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');
563 --echo # 1)
Test that subquery materialization is setup
for query with
564 --echo # premature optimize() exit due
to "Impossible WHERE"
567 FROM t2
JOIN t1 ON t1.pk=t2.pk
576 FROM t2
JOIN t1 ON t1.pk=t2.pk
584 --echo # 2)
Test that subquery materialization is setup
for query with
585 --echo # premature optimize() exit due
to "No matching min/max row"
605 --echo # 3)
Test that subquery materialization is setup
for query with
606 --echo # premature optimize() exit due
to "Select tables optimized away"
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"
631 FROM (SELECT a FROM empty1) tt
639 FROM (SELECT a FROM empty1) tt
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"
652 WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
661 WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
667 --echo # Cleanup
for BUG#46680
669 DROP
TABLE IF EXISTS t1,t2,t3,empty1;
673 --echo # BUG#52344 - Subquery materialization:
674 --echo # Assertion
if subquery in on-clause of outer join
677 CREATE
TABLE t1 (
i INTEGER);
678 INSERT INTO t1 VALUES (10);
680 CREATE
TABLE t2 (j INTEGER);
681 INSERT INTO t2 VALUES (5);
683 CREATE
TABLE t3 (k INTEGER);
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);
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);
693 DROP
TABLE t1, t2, t3;
695 --echo
# End BUG#52344
699 # Bug #52538 Valgrind bug: Item_in_subselect::init_left_expr_cache()
702 pk INTEGER AUTO_INCREMENT,
703 col_int_nokey INTEGER,
706 col_varchar_key VARCHAR(1),
710 KEY (col_varchar_key, col_int_key)
715 col_int_key, col_int_nokey, col_varchar_key
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 ) )
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 ) ) )
757 --echo # when running with --debug
760 CREATE
TABLE t1(track varchar(15));
762 INSERT INTO t1 VALUES (
'CAD'), (
'CAD');
767 track IN (SELECT track FROM t1
769 HAVING track>='CAD')";
773 DEALLOCATE PREPARE STMT;
776 --echo # End of BUG#53103
779 --echo # BUG#54511 - Assertion failed: cache != 0L in
file
780 --echo # sql_select.cc::sub_select_cache on HAVING
783 CREATE
TABLE t1 (i
int(11));
784 CREATE
TABLE t2 (c
char(1));
785 CREATE
TABLE t3 (c
char(1));
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');
797 IN (SELECT t2.c FROM (t2
JOIN t3));
801 --echo
# End BUG#54511
804 --echo # BUG#56367 - Assertion exec_method != EXEC_MATERIALIZATION...
805 --echo # on subquery in FROM
808 CREATE
TABLE t1 (a INTEGER);
810 CREATE
TABLE t2 (b INTEGER);
811 INSERT INTO t2 VALUES (1);
815 SELECT t1.* FROM t1 LEFT
JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
822 --echo # End BUG#56367
825 --echo # Bug#59833 - materialization=on/off leads
to different result
set
826 --echo # when
using IN
841 INSERT INTO t1 VALUES (10,0);
842 INSERT INTO t2 VALUES (10,0),(11,0);
845 SELECT * FROM t1
JOIN t2 USING (f1)
846 WHERE t1.f1 IN (SELECT t1.pk FROM t1
ORDER BY t1.f1);
853 --echo
# End Bug#59833
856 --echo # Bug#11852644 - CRASH IN ITEM_REF::SAVE_IN_FIELD ON SELECT DISTINCT
860 col_varchar_key varchar(1) DEFAULT NULL,
861 col_varchar_nokey varchar(1) DEFAULT NULL,
862 KEY col_varchar_key (col_varchar_key))
865 INSERT INTO t1 VALUES
869 col_varchar_key varchar(1) DEFAULT NULL,
870 col_varchar_nokey varchar(1) DEFAULT NULL,
871 KEY col_varchar_key(col_varchar_key))
874 INSERT INTO t2 VALUES
877 CREATE VIEW v3 AS SELECT * FROM t2;
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)
888 --echo # End Bug#11852644
891 --echo # Bug#12668294 - GROUP BY ON EMPTY RESULT GIVES EMPTY ROW
892 --echo # INSTEAD OF NULL WHEN MATERIALIZATION ON
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);
901 SELECT MIN(t3.col_int_nokey),t1.col_int_nokey AS field3
905 WHERE (194, 200) IN (
906 SELECT SQ4_alias1.col_int_nokey,
907 SQ4_alias2.col_int_nokey
908 FROM t2 AS SQ4_alias1
911 ON SQ4_alias2.col_int_nokey = 5
920 --echo # Bug#13419028 - SUBQUERY MATERIALIZATION NOT USED IN CREATE
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);
929 EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
931 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
932 CREATE
TABLE t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
934 # prove that subquery materialization was used:
935 SHOW STATUS LIKE
"CREATED_TMP_TABLES";
939 --echo # Bug#13552968: Extra row with materialization on join + subquery in
943 col_varchar_nokey varchar(1) NOT NULL
946 INSERT INTO t1 VALUES (
'b');
949 col_varchar_nokey varchar(1) NOT NULL
952 INSERT INTO t2 VALUES (
'k');
955 col_varchar_nokey varchar(1) NOT NULL
959 SELECT STRAIGHT_JOIN *
960 FROM t1 LEFT
JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
966 DROP
TABLE t1, t2, t3;
968 --echo # End of
test for bug#13552968
971 --echo # Bug#13591383: Assertion !(*tab->on_expr_ref && .. && is_expensive())
975 CREATE
TABLE t1 (v INTEGER) ENGINE=MyISAM;
976 INSERT INTO t1 VALUES(1);
978 CREATE
TABLE t2 (v INTEGER) ENGINE=MyISAM;
982 ON t2.v IN(SELECT v FROM t1);
986 --echo # End of
test for bug#13591383.
989 --echo # Bug#13607423: Assertion !(*tab->on_expr_ref->is_expensive())
995 col_int_nokey
int DEFAULT NULL,
996 col_int_key
int DEFAULT NULL,
998 KEY col_int_key (col_int_key)
1001 INSERT INTO t1 VALUES (1,2,4), (2,150,62);
1005 col_int_key
int DEFAULT NULL,
1009 INSERT INTO t2 VALUES (1,7);
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
1018 WHERE innr.col_int_nokey > innr.col_int_nokey
1019 GROUP BY col_int_key
1023 eval explain $query;
1026 SHOW SESSION STATUS LIKE 'Sort_scan%';
1030 --echo
# End of test for bug#13607423.
1033 --echo
Test of WL#6094
"Allow subquery materialization in NOT IN if all
1034 --echo columns are not nullable"
1037 # We want to test WL#6094 only, not WL#6095, so we use 2 columns.
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);
1044 --echo
Test in SELECT list
1047 let $query=select a, (a,a) in (select a,a from t2) from t1;
1049 --echo cols not nullable => subq materialization
1050 eval explain extended $query;
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;
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;
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;
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;
1076 alter table t2 modify a int not null;
1079 --echo Test in WHERE
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;
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;
1102 --echo Test of WL6095 "Allow subquery materialization in NOT IN if
1103 --echo single-column subquery"
1106 # We want to test WL#6095 only, not WL#6094, so we use nullable columns.
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);
1114 let $query=select a, a in (select a from t2) from t1;
1116 --echo one col => subq materialization
1117 eval explain extended $query;
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;
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;
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;
1134 --echo _two_ outer columns, nullable => no materialization
1135 eval explain extended $query;
1141 --echo
Test in HAVING
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);
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;
1153 eval explain extended $query;
1156 --echo one outer NULL
1157 insert into t1 values(null,null);
1159 eval explain extended $query;
1162 --echo one outer NULL and one inner NULL
1163 insert into t2 values(null);
1165 eval explain extended $query;
1168 --echo one inner NULL
1169 delete from t1 where a is null;
1171 eval explain extended $query;
1177 --echo Verify that an inner NULL is looked up only once (result is
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);
1186 let $query=select a, (a in (select * from t2)) from t1;
1188 eval explain extended $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";
1199 --echo # Bug#13495157 - SUBQUERY MATERIALIZATION NOT USED FOR CERTAIN
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);
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);
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);
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);
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);
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);
1239 EXPLAIN DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
1240 DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
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);
1250 # Put some content so that future queries have rows to modify:
1251 UPDATE t2 SET a=3 WHERE a=0;
1253 --echo # Used
for multi-
table UPDATE, DELETE:
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);
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);
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);
1272 DROP
TABLE t1,t2,t3;
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
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;
1291 show status like "handler_read%";
1295 --echo # Bug#13655791 DIFFERENT RESULT WITH WL6094 ON QUERY WITH XOR
1296 --echo # IN WHERE CLAUSE + MYISAM
1301 col_varchar_nokey varchar(1) DEFAULT NULL,
1305 INSERT INTO t1 VALUES (10,'x
');
1309 col_varchar_nokey varchar(1) DEFAULT NULL,
1313 INSERT INTO t2 VALUES (1,'v
'), (5,'x
'), (11,'z
'), (12,'c
'), (15,'y
');
1317 col_int_key int DEFAULT NULL,
1319 KEY col_int_key (col_int_key)
1322 INSERT INTO t3 VALUES (10,8);
1326 col_varchar_nokey varchar(1) DEFAULT NULL,
1330 INSERT INTO t4 VALUES (1,'x
');
1333 SELECT OUTR.pk, OUTR.col_varchar_nokey, OUTR2.col_varchar_nokey
1336 ON (OUTR2.col_varchar_nokey > OUTR.col_varchar_nokey)
1338 OUTR.col_varchar_nokey IN (
1339 SELECT INNR.col_varchar_nokey
1341 LEFT JOIN t1 AS INNR
1342 ON (INNR2.col_int_key >= INNR.pk)
1347 eval EXPLAIN $query;
1350 SHOW STATUS LIKE "HANDLER_READ%";
1352 DROP TABLE t1,t2,t3,t4;
1355 --echo # Bug#13727407: Assert !item->const_item() || !item->not_null_tables()
1360 KEY col_int_key (col_int_key)
1363 INSERT INTO t1 VALUES (1);
1368 col_datetime_nokey DATETIME,
1369 KEY col_int_key (col_int_key),
1370 KEY col_time_key (col_time_key)
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
');
1377 SELECT col_datetime_nokey AS x
1379 WHERE col_int_key IN (
1380 SELECT STRAIGHT_JOIN col_int_key
1382 ) AND outr.col_int_key = 0
1383 HAVING x = '2000-09-09
'
1384 ORDER BY col_time_key;
1386 eval EXPLAIN $query;
1392 --echo # Bug#13838501 ASSERTION `TABLE->FILE->INITED' FAILED IN
1393 --echo # SUBSELECT_HASH_SJ_ENGINE::EXEC
1397 (c1 bigint,c2
char,pk INT,c3
char,c4
int,c5 INT,key (c5))
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,'','');
1403 (c4
int,pk INT,c1 bigint,cyk year,cy year,key (cyk))
1405 INSERT INTO t3 VALUES(0,8,'',0,'');
1407 SELECT o.c2 AS x FROM t1 AS o
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
1414 AND o.c4=7 XOR o.pk=3
ORDER BY o.pk;
1415 eval EXPLAIN $query;
1418 DROP
TABLE t1,t2,t3;
1420 --echo
# End of 5.6 tests
1422 set @@optimizer_switch=@old_opt_switch;