2
drop table if exists t0, t1, t2, t3, t4;
6
# 1. Subquery with GROUP/HAVING
8
create table t1 (oref int, grp int, ie int) ;
9
insert into t1 (oref, grp, ie) values
20
# select max(ie) from t1 where oref=PARAM group by grp
22
# PARAM subquery result
23
# 1 -> {(1), (NULL)} matching + NULL
24
# 2 -> {(3)} non-matching
25
# 3 -> {(3), (NULL)} non-matching + NULL
28
create table t2 (oref int, a int);
36
# true, false, null, false, null
37
select a, oref, a in (select max(ie)
38
from t1 where oref=t2.oref group by grp) Z from t2;
40
# This must have a trigcond
42
select a, oref, a in (select max(ie)
43
from t1 where oref=t2.oref group by grp) Z from t2;
45
# This must not have a trigcond:
47
select a, oref from t2
48
where a in (select max(ie) from t1 where oref=t2.oref group by grp);
49
select a, oref, a in (
50
select max(ie) from t1 where oref=t2.oref group by grp union
51
select max(ie) from t1 where oref=t2.oref group by grp
54
# Non-correlated subquery, 2 NULL evaluations
55
create table t3 (a int);
56
insert into t3 values (NULL), (NULL);
58
select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
60
show status like 'Handler_read_rnd_next';
61
select ' ^ This must show 11' Z;
63
# This must show trigcond:
64
explain extended select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
66
drop table t1, t2, t3;
69
# 2. Subquery handled with 'index_subquery':
71
create table t1 (a int, oref int, key(a));
79
create table t2 (a int, oref int);
80
insert into t2 values (1, 1), (2,2), (NULL, 3), (NULL, 4);
82
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
84
# The next explain shows "using index" but that is just incorrect display
85
# (there is a bug filed about this).
87
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
90
select oref, a from t2 where a in (select a from t1 where oref=t2.oref);
91
# This will only show access to t2:
93
show status like '%Handler_read_rnd_next';
95
# Check that repeated NULL-scans are not cached (subq. is not correlated):
97
insert into t2 values (NULL, 0),(NULL, 0), (NULL, 0), (NULL, 0);
100
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
102
show status like '%Handler_read%';
103
select 'No key lookups, seq reads: 29= 5 reads from t2 + 4 * 6 reads from t1.' Z;
108
# 3. Subquery handled with 'unique_index_subquery':
110
create table t1 (a int, b int, primary key (a));
111
insert into t1 values (1,1), (3,1),(100,1);
113
create table t2 (a int, b int);
114
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
116
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
121
# 4. Subquery that is a join, with ref access
123
create table t1 (a int, b int, key(a));
124
insert into t1 values
125
(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
127
create table t2 like t1;
128
insert into t2 select * from t1;
131
create table t3 (a int, oref int);
132
insert into t3 values (1, 1), (NULL,1), (NULL,0);
134
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
137
# This must have trigcond in WHERE and HAVING:
140
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
143
drop table t1, t2, t3;
147
# BUG#24085: Wrong query result for "NULL IN (SELECT ... UNION SELECT ...)"
150
# case 1: NULL IN (SELECT not_null_val FROM ...) w/o HAVING/GROUP-BY/etc
151
create table t1 (a int NOT NULL, b int NOT NULL, key(a));
152
insert into t1 values
153
(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
155
create table t2 like t1;
156
insert into t2 select * from t1;
159
create table t3 (a int, oref int);
160
insert into t3 values (1, 1), (NULL,1), (NULL,0);
162
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
165
--echo This must show a trig_cond:
168
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
173
# case 2: NULL IN (SELECT not_null_val FROM) where SELECT has GROUP BY
174
create table t1 (oref int, grp int);
175
insert into t1 (oref, grp) values
180
# select count(*) from t1 group by grp having grp=PARAM
182
# PARAM subuqery result
184
# 2 -> {} - empty set
185
create table t2 (oref int, a int);
186
insert into t2 values
191
a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
193
--echo This must show a trig_cond:
196
a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
200
create table t1 (a int, b int, primary key (a));
201
insert into t1 values (1,1), (3,1),(100,1);
202
create table t2 (a int, b int);
203
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
205
select a,b, a in (select a from t1 where t1.b = t2.b union select a from
206
t1 where t1.b = t2.b) Z from t2 ;
207
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
212
# BUG#24127: Incorrect results of row-based subqueries with NULLs on the left side.
214
create table t3 (a int);
215
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
216
create table t2 (a int, b int, oref int);
217
insert into t2 values (NULL,1, 100), (NULL,2, 100);
219
create table t1 (a int, b int, c int, key(a,b));
220
insert into t1 select 2*A, 2*A, 100 from t3;
222
# First test index subquery engine
223
explain extended select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
224
select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
226
# Then check that we do turn off 'ref' scans in the subquery
227
create table t4 (x int);
228
insert into t4 select A.a + 10*B.a from t1 A CROSS JOIN t1 B;
231
(a,b) in (select a,b from t1,t4 where c=t2.oref) Z
234
(a,b) in (select a,b from t1,t4 where c=t2.oref) Z
237
drop table t1,t2,t3,t4;
239
# More tests for tricky multi-column cases, where some of pushed-down
240
# equalities are used for index lookups and some arent.
241
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
242
insert into t1 (oref, grp, ie1, ie2) values
255
create table t2 (oref char(4), a int, b int);
256
insert into t2 values
264
alter table t1 add index idx(ie1,ie2);
267
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=3 and b is null ;
268
insert into t2 values ('new1', 10,10);
269
insert into t1 values ('new1', 1234, 10, NULL);
270
# new1, 10, 10, NULL,
271
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
273
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
276
# Now test different column types:
277
create table t1 (oref char(4), grp int, ie int);
278
insert into t1 (oref, grp, ie) values
294
create table t2 (oref char(4), a int);
295
insert into t2 values
304
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
307
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
309
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
312
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
314
select oref, a from t2 where
315
a in (select min(ie) from t1 where oref=t2.oref group by grp);
317
select oref, a from t2 where
318
a not in (select min(ie) from t1 where oref=t2.oref group by grp);
321
update t1 set ie=3 where oref='ff' and ie=1;
323
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
327
select oref, a from t2 where a in (select min(ie) from t1 where
328
oref=t2.oref group by grp);
330
select oref, a from t2 where a not in (select min(ie) from t1 where
331
oref=t2.oref group by grp);
333
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
334
grp having min(ie) > 1) Z from t2;
336
select oref, a from t2 where a in (select min(ie) from t1 where
337
oref=t2.oref group by grp having min(ie) > 1);
339
select oref, a from t2 where a not in (select min(ie) from t1 where
340
oref=t2.oref group by grp having min(ie) > 1);
343
alter table t1 add index idx(ie);
345
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
347
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
350
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
352
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
355
alter table t1 drop index idx;
356
alter table t1 add index idx(oref,ie);
358
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
360
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
363
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
365
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
369
a in (select min(ie) from t1 where oref=t2.oref
370
group by grp having min(ie) > 1) Z
374
a in (select min(ie) from t1 where oref=t2.oref
375
group by grp having min(ie) > 1) Z
378
select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref
379
group by grp having min(ie) > 1);
381
select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref
382
group by grp having min(ie) > 1);
386
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
387
insert into t1 (oref, grp, ie1, ie2) values
403
create table t2 (oref char(4), a int, b int);
404
insert into t2 values
413
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
415
select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
417
select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
420
(a,b) in (select min(ie1),max(ie2) from t1
421
where oref=t2.oref group by grp) Z
424
select oref, a, b from t2 where
425
(a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
427
select oref, a, b from t2 where
428
(a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
430
alter table t1 add index idx(ie1,ie2);
432
explain select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
434
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
436
select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
438
select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
441
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
445
create table t1 (oref char(4), grp int, ie int primary key);
446
insert into t1 (oref, grp, ie) values
456
create table t2 (oref char(4), a int);
457
insert into t2 values
466
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
468
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
471
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
473
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
476
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
478
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
483
# BUG#24420: row-based IN suqueries with aggregation when the left operand
484
# of the subquery predicate may contain NULL values
487
create table t1 (a int, b int);
488
insert into t1 values (0,0), (2,2), (3,3);
489
create table t2 (a int, b int);
490
insert into t2 values (1,1), (3,3);
492
select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
494
insert into t2 values (NULL,4);
495
select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
500
# Bug #24484: Aggregate function used in column list subquery gives erroneous
503
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
504
INSERT INTO t1 VALUES (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
505
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'),(3,3,'j'), (3,2,'k'), (3,1,'l'),
507
CREATE TABLE t2 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
508
INSERT INTO t2 SELECT * FROM t1;
510
# Gives error, but should work since it is (a, b) is the PK so only one
511
# given match possible
512
SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
513
as test FROM t1 GROUP BY a;
514
SELECT * FROM t1 GROUP by t1.a
515
HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
516
HAVING MAX(t2.b+t1.a) < 10));
518
SELECT a,b,c FROM t1 WHERE b in (9,3,4) ORDER BY b,c;
521
(SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
524
(SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
526
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
528
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) ORDER BY t.c LIMIT 1)
533
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
541
# Bug #27870: crash of an equijoin query with WHERE condition containing
542
# a subquery predicate of the form <join attr> NOT IN (SELECT ...)
545
CREATE TABLE t1 (a int);
546
CREATE TABLE t2 (b int, PRIMARY KEY(b));
547
INSERT INTO t1 VALUES (1), (NULL), (4);
548
INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6);
551
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
552
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
553
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4));
558
# Bug #28375: crash for NOT IN subquery predicate when left operand becomes NULL
561
CREATE TABLE t1 (id int);
562
CREATE TABLE t2 (id int PRIMARY KEY);
563
CREATE TABLE t3 (id int PRIMARY KEY, name varchar(10));
564
INSERT INTO t1 VALUES (2), (NULL), (3), (1);
565
INSERT INTO t2 VALUES (234), (345), (457);
566
INSERT INTO t3 VALUES (222,'bbb'), (333,'ccc'), (111,'aaa');
570
WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
571
WHERE t3.name='xxx' AND t2.id=t3.id);
573
WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
574
WHERE t3.name='xxx' AND t2.id=t3.id);
576
SELECT (t1.id IN (SELECT t2.id FROM t2,t3
577
WHERE t3.name='xxx' AND t2.id=t3.id)) AS x
583
# Bug #22855: Optimizer doesn't rewrite NOT IN subselects to a correlated
586
CREATE TABLE t1 (a INT NOT NULL);
587
INSERT INTO t1 VALUES (1),(-1), (65),(66);
589
CREATE TABLE t2 (a INT NOT NULL PRIMARY KEY);
590
INSERT INTO t2 VALUES (65),(66);
592
SELECT a FROM t1 WHERE a NOT IN (65,66);
593
SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
594
EXPLAIN SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
599
# Bug #34763: item_subselect.cc:1235:Item_in_subselect::row_value_transformer:
600
# Assertion failed, unexpected error message:
601
# ERROR 1247 (42S22): Reference '<list ref>' not supported (forward
602
# reference in item list)
604
CREATE TABLE t1 (a INT);
605
INSERT INTO t1 VALUES(1);
607
CREATE TABLE t2 (placeholder CHAR(11));
608
INSERT INTO t2 VALUES("placeholder");
610
SELECT ROW(1, 2) IN (SELECT t1.a, 2) FROM t1 GROUP BY t1.a;
611
SELECT ROW(1, 2) IN (SELECT t1.a, 2 FROM t2) FROM t1 GROUP BY t1.a;
619
create table t1 (a int, b decimal(13, 3));
620
insert into t1 values (1, 0.123);
621
system rm -f $DRIZZLETEST_VARDIR/subselect.out.file.1;
622
--replace_result $DRIZZLETEST_VARDIR DRIZZLETEST_VARDIR
623
eval select a, (select max(b) from t1) into outfile "$DRIZZLETEST_VARDIR/subselect.out.file.1" from t1;
625
--replace_result $DRIZZLETEST_VARDIR DRIZZLETEST_VARDIR
626
eval load data infile "$DRIZZLETEST_VARDIR/subselect.out.file.1" into table t1;
630
--echo End of 5.0 tests