MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
subquery.inc
1 # Basic subquery tests
2 #
3 # NOTE. Please do not switch connection inside this test.
4 # subquery.inc is included from several other test cases which set
5 # explicit session properties that must be preserved throughout the test.
6 # If you need to use a dedicated connection for a test case,
7 # close the new connection and switch back to "default" as soon
8 # as possible.
9 #
10 
11 # This portion of the file vas developed when subquery materialization
12 # was rule-based; to preserve the intended test scenarios, we switch
13 # off cost-based choice for them.
14 set @old_opt_switch=@@optimizer_switch;
15 set optimizer_switch='subquery_materialization_cost_based=off';
16 
17 # Initialise
18 --disable_warnings
19 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
20 --enable_warnings
21 select (select 2);
22 explain extended select (select 2);
23 SELECT (SELECT 1) UNION SELECT (SELECT 2);
24 explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
25 SELECT (SELECT (SELECT 0 UNION SELECT 0));
26 explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
27 -- error ER_ILLEGAL_REFERENCE
28 SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
29 -- error ER_ILLEGAL_REFERENCE
30 SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
31 SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
32 -- error ER_ILLEGAL_REFERENCE
33 SELECT (SELECT a) as a;
34 EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
35 SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
36 -- error ER_BAD_FIELD_ERROR
37 SELECT (SELECT 1), a;
38 SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
39 -- error ER_BAD_FIELD_ERROR
40 SELECT 1 FROM (SELECT (SELECT a) b) c;
41 SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
42 -- error ER_OPERAND_COLUMNS
43 SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
44 SELECT 1 IN (SELECT 1);
45 SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
46 -- error ER_WRONG_USAGE
47 select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
48 -- error ER_PARSE_ERROR
49 SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
50 -- error ER_BAD_FIELD_ERROR
51 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
52 -- error ER_BAD_FIELD_ERROR
53 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
54 SELECT (SELECT 1,2,3) = ROW(1,2,3);
55 SELECT (SELECT 1,2,3) = ROW(1,2,1);
56 SELECT (SELECT 1,2,3) < ROW(1,2,1);
57 SELECT (SELECT 1,2,3) > ROW(1,2,1);
58 SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
59 SELECT ROW(1,2,3) = (SELECT 1,2,3);
60 SELECT ROW(1,2,3) = (SELECT 1,2,1);
61 SELECT ROW(1,2,3) < (SELECT 1,2,1);
62 SELECT ROW(1,2,3) > (SELECT 1,2,1);
63 SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
64 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
65 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
66 SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
67 SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
68 SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
69 SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
70 
71 -- error ER_OPERAND_COLUMNS
72 SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
73 
74 SELECT 1 as a,(SELECT a+a) b,(SELECT b);
75 
76 create table t1 (a int);
77 create table t2 (a int, b int);
78 create table t3 (a int);
79 create table t4 (a int not null, b int not null);
80 insert into t1 values (2);
81 insert into t2 values (1,7),(2,7);
82 insert into t4 values (4,8),(3,8),(5,9);
83 -- error ER_ILLEGAL_REFERENCE
84 select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
85 select (select a from t1 where t1.a=t2.a), a from t2;
86 select (select a from t1 where t1.a=t2.b), a from t2;
87 select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
88 select (select a from t3), a from t2;
89 select * from t2 where t2.a=(select a from t1);
90 insert into t3 values (6),(7),(3);
91 select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
92 (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 order by a limit 2) limit 3;
93 (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
94 explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
95 select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
96 select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
97 (select * from t2 where a>1) as tt;
98 explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
99 (select * from t2 where a>1) as tt;
100 select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
101 select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
102 select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
103 select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
104 explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
105 select * from t3 where exists (select * from t2 where t2.b=t3.a);
106 select * from t3 where not exists (select * from t2 where t2.b=t3.a);
107 select * from t3 where a in (select b from t2);
108 select * from t3 where a not in (select b from t2);
109 select * from t3 where a = some (select b from t2);
110 select * from t3 where a <> any (select b from t2);
111 
112 # Rewrite: select * from t3 where not exists (select b from t2 where a <> b);
113 select * from t3 where a = all (select b from t2);
114 
115 select * from t3 where a <> all (select b from t2);
116 insert into t2 values (100, 5);
117 select * from t3 where a < any (select b from t2);
118 select * from t3 where a < all (select b from t2);
119 select * from t3 where a >= any (select b from t2);
120 explain extended select * from t3 where a >= any (select b from t2);
121 select * from t3 where a >= all (select b from t2);
122 delete from t2 where a=100;
123 -- error ER_OPERAND_COLUMNS
124 select * from t3 where a in (select a,b from t2);
125 -- error ER_OPERAND_COLUMNS
126 select * from t3 where a in (select * from t2);
127 insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
128 # empty set
129 select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
130 insert into t2 values (2,10);
131 select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
132 delete from t2 where a=2 and b=10;
133 select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
134 create table t5 (a int);
135 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
136 insert into t5 values (5);
137 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
138 insert into t5 values (2);
139 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
140 explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
141 -- error ER_SUBQUERY_NO_1_ROW
142 select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
143 create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
144 create table t7( uq int primary key, name char(25));
145 insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
146 insert into t6 values (1,1),(1,2),(2,2),(1,3);
147 select * from t6 where exists (select * from t7 where uq = clinic_uq);
148 explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
149 
150 # not unique fields
151 -- error ER_NON_UNIQ_ERROR
152 select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
153 
154 # different tipes & group functions
155 drop table t1,t2,t3;
156 
157 CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
158 INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
159 CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
160 INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
161 CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
162 INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
163 SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
164 SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
165 SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
166 
167 CREATE TABLE `t8` (
168  `pseudo` varchar(35) character set latin1 NOT NULL default '',
169  `email` varchar(60) character set latin1 NOT NULL default '',
170  PRIMARY KEY (`pseudo`),
171  UNIQUE KEY `email` (`email`)
172 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
173 
174 INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
175 INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
176 INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
177 EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
178 -- error ER_OPERAND_COLUMNS
179 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
180 t8 WHERE pseudo='joce');
181 -- error ER_OPERAND_COLUMNS
182 SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
183 pseudo='joce');
184 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
185 -- error ER_SUBQUERY_NO_1_ROW
186 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
187 
188 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
189 
190 #searchconthardwarefr3 forumconthardwarefr7
191 CREATE TABLE `t1` (
192  `topic` mediumint(8) unsigned NOT NULL default '0',
193  `date` date NOT NULL default '0000-00-00',
194  `pseudo` varchar(35) character set latin1 NOT NULL default '',
195  PRIMARY KEY (`pseudo`,`date`,`topic`),
196  KEY `topic` (`topic`)
197 ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
198 INSERT INTO t1 (topic,date,pseudo) VALUES
199 ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
200 EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
201 EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
202 SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
203 SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
204 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
205 -- error ER_SUBQUERY_NO_1_ROW
206 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
207 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
208 drop table t1;
209 
210 #forumconthardwarefr7 searchconthardwarefr7
211 CREATE TABLE `t1` (
212  `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
213  `maxnumrep` int(10) unsigned NOT NULL default '0',
214  PRIMARY KEY (`numeropost`),
215  UNIQUE KEY `maxnumrep` (`maxnumrep`)
216 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
217 
218 INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
219 
220 CREATE TABLE `t2` (
221  `mot` varchar(30) NOT NULL default '',
222  `topic` mediumint(8) unsigned NOT NULL default '0',
223  `date` date NOT NULL default '0000-00-00',
224  `pseudo` varchar(35) NOT NULL default '',
225  PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
226  ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
227 
228 INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
229 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
230 SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20;
231 -- error ER_BAD_FIELD_ERROR
232 SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
233 -- error ER_BAD_FIELD_ERROR
234 SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
235 
236 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
237 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
238 SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
239 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
240 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
241 SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
242 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
243 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
244 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
245 SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
246 SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
247 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
248 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
249 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
250 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
251 drop table t1,t2;
252 
253 #forumconthardwarefr7
254 CREATE TABLE `t1` (
255  `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
256  `maxnumrep` int(10) unsigned NOT NULL default '0',
257  PRIMARY KEY (`numeropost`),
258  UNIQUE KEY `maxnumrep` (`maxnumrep`)
259 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
260 
261 INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
262 -- error ER_SUBQUERY_NO_1_ROW
263 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
264 -- error ER_SUBQUERY_NO_1_ROW
265 select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
266 drop table t1;
267 
268 create table t1 (a int);
269 insert into t1 values (1),(2),(3);
270 (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
271 drop table t1;
272 
273 #iftest
274 CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
275 INSERT INTO t1 VALUES ();
276 -- error ER_SUBQUERY_NO_1_ROW
277 SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
278 drop table t1;
279 
280 # threadhardwarefr7
281 CREATE TABLE `t1` (
282  `numeropost` mediumint(8) unsigned NOT NULL default '0',
283  `numreponse` int(10) unsigned NOT NULL auto_increment,
284  `pseudo` varchar(35) NOT NULL default '',
285  PRIMARY KEY (`numeropost`,`numreponse`),
286  UNIQUE KEY `numreponse` (`numreponse`),
287  KEY `pseudo` (`pseudo`,`numeropost`)
288 ) ENGINE=MyISAM;
289 -- error ER_ILLEGAL_REFERENCE
290 SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
291 -- error ER_BAD_FIELD_ERROR
292 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
293 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
294 INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
295 -- error ER_SUBQUERY_NO_1_ROW
296 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
297 EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
298 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
299 drop table t1;
300 
301 CREATE TABLE t1 (a int(1));
302 INSERT INTO t1 VALUES (1);
303 SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
304 drop table t1;
305 
306 #update with subselects
307 create table t1 (a int NOT NULL, b int, primary key (a));
308 create table t2 (a int NOT NULL, b int, primary key (a));
309 insert into t1 values (0, 10),(1, 11),(2, 12);
310 insert into t2 values (1, 21),(2, 22),(3, 23);
311 select * from t1;
312 -- error ER_UPDATE_TABLE_USED
313 update t1 set b= (select b from t1);
314 -- error ER_SUBQUERY_NO_1_ROW
315 update t1 set b= (select b from t2);
316 update t1 set b= (select b from t2 where t1.a = t2.a);
317 select * from t1;
318 drop table t1, t2;
319 
320 #delete with subselects
321 create table t1 (a int NOT NULL, b int, primary key (a));
322 create table t2 (a int NOT NULL, b int, primary key (a));
323 insert into t1 values (0, 10),(1, 11),(2, 12);
324 insert into t2 values (1, 21),(2, 12),(3, 23);
325 select * from t1;
326 select * from t1 where b = (select b from t2 where t1.a = t2.a);
327 -- error ER_UPDATE_TABLE_USED
328 delete from t1 where b = (select b from t1);
329 -- error ER_SUBQUERY_NO_1_ROW
330 delete from t1 where b = (select b from t2);
331 delete from t1 where b = (select b from t2 where t1.a = t2.a);
332 select * from t1;
333 drop table t1, t2;
334 
335 #multi-delete with subselects
336 
337 create table t11 (a int NOT NULL, b int, primary key (a));
338 create table t12 (a int NOT NULL, b int, primary key (a));
339 create table t2 (a int NOT NULL, b int, primary key (a));
340 insert into t11 values (0, 10),(1, 11),(2, 12);
341 insert into t12 values (33, 10),(22, 11),(2, 12);
342 insert into t2 values (1, 21),(2, 12),(3, 23);
343 select * from t11;
344 select * from t12;
345 -- error ER_UPDATE_TABLE_USED
346 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
347 -- error ER_SUBQUERY_NO_1_ROW
348 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
349 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
350 select * from t11;
351 select * from t12;
352 drop table t11, t12, t2;
353 
354 #insert with subselects
355 CREATE TABLE t1 (x int);
356 create table t2 (a int);
357 create table t3 (b int);
358 insert into t2 values (1);
359 insert into t3 values (1),(2);
360 -- error ER_UPDATE_TABLE_USED
361 INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
362 -- error ER_SUBQUERY_NO_1_ROW
363 INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
364 INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
365 select * from t1;
366 insert into t2 values (1);
367 let $row_count_before= `SELECT COUNT(*) FROM t1`;
368 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
369 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
370 --source include/wait_condition.inc
371 select * from t1;
372 INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
373 select * from t1;
374 # After this, only data based on old t1 records should have been added.
375 INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
376 select * from t1;
377 -- error ER_BAD_FIELD_ERROR
378 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
379 let $row_count_before= `SELECT COUNT(*) FROM t1`;
380 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
381 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
382 --source include/wait_condition.inc
383 select * from t1;
384 #
385 #TODO: should be uncommented after Bug#380 fix pushed
386 #INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
387 #select * from t1;
388 drop table t1, t2, t3;
389 
390 #replace with subselects
391 CREATE TABLE t1 (x int not null, y int, primary key (x));
392 create table t2 (a int);
393 create table t3 (a int);
394 insert into t2 values (1);
395 insert into t3 values (1),(2);
396 select * from t1;
397 -- error ER_UPDATE_TABLE_USED
398 replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
399 -- error ER_SUBQUERY_NO_1_ROW
400 replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
401 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
402 select * from t1;
403 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
404 select * from t1;
405 let $row_count_before= `SELECT COUNT(*) FROM t1`;
406 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
407 # We get one additional row
408 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
409 --source include/wait_condition.inc
410 select * from t1;
411 let $row_count_before= `SELECT COUNT(*) FROM t1 WHERE y = 2`;
412 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
413 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1 WHERE y = 2;
414 --source include/wait_condition.inc
415 select * from t1;
416 replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
417 select * from t1;
418 drop table t1, t2, t3;
419 
420 -- error ER_NO_TABLES_USED
421 SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
422 
423 CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
424 INSERT INTO t2 VALUES (1),(2);
425 SELECT * FROM t2 WHERE id IN (SELECT 1);
426 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
427 SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
428 SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
429 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
430 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
431 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
432 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
433 -- error ER_UPDATE_TABLE_USED
434 INSERT INTO t2 VALUES ((SELECT * FROM t2));
435 -- error ER_UPDATE_TABLE_USED
436 INSERT INTO t2 VALUES ((SELECT id FROM t2));
437 SELECT * FROM t2;
438 CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
439 INSERT INTO t1 values (1),(1);
440 -- error ER_SUBQUERY_NO_1_ROW
441 UPDATE t2 SET id=(SELECT * FROM t1);
442 drop table t2, t1;
443 
444 #NULL test
445 create table t1 (a int);
446 insert into t1 values (1),(2),(3);
447 select 1 IN (SELECT * from t1);
448 select 10 IN (SELECT * from t1);
449 select NULL IN (SELECT * from t1);
450 update t1 set a=NULL where a=2;
451 select 1 IN (SELECT * from t1);
452 select 3 IN (SELECT * from t1);
453 select 10 IN (SELECT * from t1);
454 select 1 > ALL (SELECT * from t1);
455 select 10 > ALL (SELECT * from t1);
456 select 1 > ANY (SELECT * from t1);
457 select 10 > ANY (SELECT * from t1);
458 drop table t1;
459 create table t1 (a varchar(20));
460 insert into t1 values ('A'),('BC'),('DEF');
461 select 'A' IN (SELECT * from t1);
462 select 'XYZS' IN (SELECT * from t1);
463 select NULL IN (SELECT * from t1);
464 update t1 set a=NULL where a='BC';
465 select 'A' IN (SELECT * from t1);
466 select 'DEF' IN (SELECT * from t1);
467 select 'XYZS' IN (SELECT * from t1);
468 select 'A' > ALL (SELECT * from t1);
469 select 'XYZS' > ALL (SELECT * from t1);
470 select 'A' > ANY (SELECT * from t1);
471 select 'XYZS' > ANY (SELECT * from t1);
472 drop table t1;
473 create table t1 (a float);
474 insert into t1 values (1.5),(2.5),(3.5);
475 select 1.5 IN (SELECT * from t1);
476 select 10.5 IN (SELECT * from t1);
477 select NULL IN (SELECT * from t1);
478 update t1 set a=NULL where a=2.5;
479 select 1.5 IN (SELECT * from t1);
480 select 3.5 IN (SELECT * from t1);
481 select 10.5 IN (SELECT * from t1);
482 select 1.5 > ALL (SELECT * from t1);
483 select 10.5 > ALL (SELECT * from t1);
484 select 1.5 > ANY (SELECT * from t1);
485 select 10.5 > ANY (SELECT * from t1);
486 explain extended select (select a+1) from t1;
487 select (select a+1) from t1;
488 drop table t1;
489 
490 #
491 # Null with keys
492 #
493 
494 CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));
495 CREATE TABLE t2 (a int(11) default '0', INDEX (a));
496 INSERT INTO t1 VALUES (1),(2),(3),(4);
497 INSERT INTO t2 VALUES (1),(2),(3);
498 SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
499 explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
500 CREATE TABLE t3 (a int(11) default '0');
501 INSERT INTO t3 VALUES (1),(2),(3);
502 SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
503 explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
504 drop table t1,t2,t3;
505 
506 #LIMIT is not supported now
507 create table t1 (a float);
508 -- error ER_NOT_SUPPORTED_YET
509 select 10.5 IN (SELECT * from t1 LIMIT 1);
510 -- error ER_NOT_SUPPORTED_YET
511 select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
512 drop table t1;
513 
514 create table t1 (a int, b int, c varchar(10));
515 create table t2 (a int);
516 insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
517 insert into t2 values (1),(2),(NULL);
518 select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t1 where a=t2.a) from t2;
519 select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2;
520 select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t1 where a=t2.a) from t2;
521 drop table t1,t2;
522 
523 create table t1 (a int, b real, c varchar(10));
524 insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
525 select ROW(1, 1, 'a') IN (select a,b,c from t1);
526 select ROW(1, 2, 'a') IN (select a,b,c from t1);
527 select ROW(1, 1, 'a') IN (select b,a,c from t1);
528 select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
529 select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
530 select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
531 select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
532 select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
533 select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
534 -- error ER_NOT_SUPPORTED_YET
535 select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
536 drop table t1;
537 
538 #
539 # DO & SET
540 #
541 create table t1 (a int);
542 insert into t1 values (1);
543 do @a:=(SELECT a from t1);
544 select @a;
545 set @a:=2;
546 set @a:=(SELECT a from t1);
547 select @a;
548 drop table t1;
549 -- error ER_NO_SUCH_TABLE
550 do (SELECT a from t1);
551 -- error ER_NO_SUCH_TABLE
552 set @a:=(SELECT a from t1);
553 
554 CREATE TABLE t1 (a int, KEY(a));
555 HANDLER t1 OPEN;
556 -- error ER_PARSE_ERROR
557 HANDLER t1 READ a=((SELECT 1));
558 HANDLER t1 CLOSE;
559 drop table t1;
560 
561 create table t1 (a int);
562 create table t2 (b int);
563 insert into t1 values (1),(2);
564 insert into t2 values (1);
565 select a from t1 where a in (select a from t1 where a in (select b from t2));
566 drop table t1, t2;
567 
568 create table t1 (a int, b int);
569 create table t2 like t1;
570 insert into t1 values (1,2),(1,3),(1,4),(1,5);
571 insert into t2 values (1,2),(1,3);
572 select * from t1 where row(a,b) in (select a,b from t2);
573 drop table t1, t2;
574 
575 CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1;
576 INSERT INTO t1 VALUES (1);
577 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
578 select * from t1;
579 drop table t1;
580 
581 #test of uncacheable subqueries
582 CREATE TABLE t1 (a int(1));
583 EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
584 EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
585 EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
586 drop table t1;
587 
588 
589 CREATE TABLE `t1` (
590  `mot` varchar(30) character set latin1 NOT NULL default '',
591  `topic` mediumint(8) unsigned NOT NULL default '0',
592  `date` date NOT NULL default '0000-00-00',
593  `pseudo` varchar(35) character set latin1 NOT NULL default '',
594  PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
595  KEY `pseudo` (`pseudo`,`date`,`topic`),
596  KEY `topic` (`topic`)
597 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
598 
599 CREATE TABLE `t2` (
600  `mot` varchar(30) character set latin1 NOT NULL default '',
601  `topic` mediumint(8) unsigned NOT NULL default '0',
602  `date` date NOT NULL default '0000-00-00',
603  `pseudo` varchar(35) character set latin1 NOT NULL default '',
604  PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
605  KEY `pseudo` (`pseudo`,`date`,`topic`),
606  KEY `topic` (`topic`)
607 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
608 
609 CREATE TABLE `t3` (
610  `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
611  `maxnumrep` int(10) unsigned NOT NULL default '0',
612  PRIMARY KEY (`numeropost`),
613  UNIQUE KEY `maxnumrep` (`maxnumrep`)
614 ) ENGINE=MyISAM CHARSET=latin1;
615 INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
616 
617 INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
618 
619 INSERT INTO t3 VALUES (1,1);
620 
621 SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
622 numeropost=topic);
623 select * from t1;
624 DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
625 EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
626 select * from t1;
627 
628 drop table t1, t2, t3;
629 
630 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
631 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
632 SHOW CREATE TABLE t1;
633 drop table t1;
634 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
635 SHOW CREATE TABLE t1;
636 drop table t1;
637 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
638 SHOW CREATE TABLE t1;
639 drop table t1;
640 CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
641 select * from t1;
642 SHOW CREATE TABLE t1;
643 drop table t1;
644 
645 create table t1 (a int);
646 insert into t1 values (1), (2), (3);
647 explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
648 from t1;
649 drop table t1;
650 
651 #
652 # error in IN
653 #
654 -- error ER_NO_SUCH_TABLE
655 select t1.Continent, t2.Name, t2.Population from t1 LEFT JOIN t2 ON t1.Code = t2.Country where t2.Population IN (select max(t2.Population) AS Population from t2, t1 where t2.Country = t1.Code group by Continent);
656 
657 #
658 # complex subquery
659 #
660 
661 CREATE TABLE t1 (
662  ID int(11) NOT NULL auto_increment,
663  name char(35) NOT NULL default '',
664  t2 char(3) NOT NULL default '',
665  District char(20) NOT NULL default '',
666  Population int(11) NOT NULL default '0',
667  PRIMARY KEY (ID)
668 ) ENGINE=MyISAM;
669 
670 INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
671 INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
672 INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
673 
674 CREATE TABLE t2 (
675  Code char(3) NOT NULL default '',
676  Name char(52) NOT NULL default '',
677  Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
678  Region char(26) NOT NULL default '',
679  SurfaceArea float(10,2) NOT NULL default '0.00',
680  IndepYear smallint(6) default NULL,
681  Population int(11) NOT NULL default '0',
682  LifeExpectancy float(3,1) default NULL,
683  GNP float(10,2) default NULL,
684  GNPOld float(10,2) default NULL,
685  LocalName char(45) NOT NULL default '',
686  GovernmentForm char(45) NOT NULL default '',
687  HeadOfState char(60) default NULL,
688  Capital int(11) default NULL,
689  Code2 char(2) NOT NULL default '',
690  PRIMARY KEY (Code)
691 ) ENGINE=MyISAM;
692 
693 INSERT INTO t2 VALUES ('AUS','Australia','Oceania','Australia and New Zealand',7741220.00,1901,18886000,79.8,351182.00,392911.00,'Australia','Constitutional Monarchy, Federation','Elisabeth II',135,'AU');
694 INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Azärbaycan','Federal Republic','Heydär Äliyev',144,'AZ');
695 
696 select t2.Continent, t1.Name, t1.Population from t2 LEFT JOIN t1 ON t2.Code = t1.t2 where t1.Population IN (select max(t1.Population) AS Population from t1, t2 where t1.t2 = t2.Code group by Continent);
697 
698 drop table t1, t2;
699 
700 #
701 # constants in IN
702 #
703 CREATE TABLE `t1` (
704  `id` mediumint(8) unsigned NOT NULL auto_increment,
705  `pseudo` varchar(35) character set latin1 NOT NULL default '',
706  PRIMARY KEY (`id`),
707  UNIQUE KEY `pseudo` (`pseudo`)
708 ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
709 INSERT INTO t1 (pseudo) VALUES ('test');
710 SELECT 0 IN (SELECT 1 FROM t1 a);
711 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
712 INSERT INTO t1 (pseudo) VALUES ('test1');
713 SELECT 0 IN (SELECT 1 FROM t1 a);
714 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
715 drop table t1;
716 
717 CREATE TABLE `t1` (
718  `i` int(11) NOT NULL default '0',
719  PRIMARY KEY (`i`)
720 ) ENGINE=MyISAM CHARSET=latin1;
721 
722 INSERT INTO t1 VALUES (1);
723 UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
724 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
725 -- error ER_BAD_FIELD_ERROR
726 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
727 select * from t1;
728 drop table t1;
729 
730 #
731 # Multi update test
732 #
733 CREATE TABLE t1 (
734  id int(11) default NULL
735 ) ENGINE=MyISAM CHARSET=latin1;
736 INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
737 CREATE TABLE t2 (
738  id int(11) default NULL,
739  name varchar(15) default NULL
740 ) ENGINE=MyISAM CHARSET=latin1;
741 
742 INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
743 update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
744 select * from t2;
745 drop table t1,t2;
746 
747 #
748 # correct NULL in <CONSTANT> IN (SELECT ...)
749 #
750 create table t1 (a int, unique index indexa (a));
751 insert into t1 values (-1), (-4), (-2), (NULL);
752 select -10 IN (select a from t1 FORCE INDEX (indexa));
753 drop table t1;
754 
755 #
756 # Test optimization for sub selects
757 #
758 create table t1 (id int not null auto_increment primary key, salary int, key(salary));
759 insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
760 explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
761 drop table t1;
762 
763 CREATE TABLE t1 (
764  ID int(10) unsigned NOT NULL auto_increment,
765  SUB_ID int(3) unsigned NOT NULL default '0',
766  REF_ID int(10) unsigned default NULL,
767  REF_SUB int(3) unsigned default '0',
768  PRIMARY KEY (ID,SUB_ID),
769  UNIQUE KEY t1_PK (ID,SUB_ID),
770  KEY t1_FK (REF_ID,REF_SUB),
771  KEY t1_REFID (REF_ID)
772 ) ENGINE=MyISAM CHARSET=cp1251;
773 INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
774 SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
775 DROP TABLE t1;
776 
777 #
778 # uninterruptable update
779 #
780 create table t1 (a int, b int);
781 create table t2 (a int, b int);
782 
783 insert into t1 values (1,0), (2,0), (3,0);
784 insert into t2 values (1,1), (2,1), (3,1), (2,2);
785 
786 update ignore t1 set b=(select b from t2 where t1.a=t2.a);
787 select * from t1;
788 
789 drop table t1, t2;
790 
791 #
792 # reduced subselect in ORDER BY & GROUP BY clauses
793 #
794 
795 CREATE TABLE `t1` (
796  `id` mediumint(8) unsigned NOT NULL auto_increment,
797  `pseudo` varchar(35) NOT NULL default '',
798  `email` varchar(60) NOT NULL default '',
799  PRIMARY KEY (`id`),
800  UNIQUE KEY `email` (`email`),
801  UNIQUE KEY `pseudo` (`pseudo`)
802 ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
803 INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
804 SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
805 drop table if exists t1;
806 
807 (SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
808 
809 #
810 # alloc_group_fields() working
811 #
812 create table t1 (a int, b int);
813 create table t2 (a int, b int);
814 create table t3 (a int, b int);
815 insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
816 insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
817 insert into t3 values (3,3), (2,2), (1,1);
818 select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
819 drop table t1,t2,t3;
820 
821 #
822 # aggregate functions in HAVING test
823 #
824 create table t1 (s1 int);
825 create table t2 (s1 int);
826 insert into t1 values (1);
827 insert into t2 values (1);
828 select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
829 drop table t1,t2;
830 
831 #
832 # update subquery with wrong field (to force name resolving
833 # in UPDATE name space)
834 #
835 create table t1 (s1 int);
836 create table t2 (s1 int);
837 insert into t1 values (1);
838 insert into t2 values (1);
839 -- error ER_BAD_FIELD_ERROR
840 update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
841 DROP TABLE t1, t2;
842 
843 #
844 # collation test
845 #
846 CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
847  s2 CHAR(5) COLLATE latin1_swedish_ci);
848 INSERT INTO t1 VALUES ('z','?');
849 -- error ER_CANT_AGGREGATE_2COLLATIONS
850 select * from t1 where s1 > (select max(s2) from t1);
851 -- error ER_CANT_AGGREGATE_2COLLATIONS
852 select * from t1 where s1 > any (select max(s2) from t1);
853 drop table t1;
854 
855 #
856 # aggregate functions reinitialization
857 #
858 create table t1(toid int,rd int);
859 create table t2(userid int,pmnew int,pmtotal int);
860 insert into t2 values(1,0,0),(2,0,0);
861 insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2);
862 select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1);
863 drop table t1, t2;
864 
865 #
866 # row union
867 #
868 create table t1 (s1 char(5));
869 -- error ER_OPERAND_COLUMNS
870 select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
871 insert into t1 values ('tttt');
872 select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
873 explain extended (select * from t1);
874 (select * from t1);
875 drop table t1;
876 
877 #
878 # IN optimisation test results
879 #
880 create table t1 (s1 char(5), index s1(s1));
881 create table t2 (s1 char(5), index s1(s1));
882 insert into t1 values ('a1'),('a2'),('a3');
883 insert into t2 values ('a1'),('a2');
884 select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
885 select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
886 select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
887 select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
888 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
889 explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
890 explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
891 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
892 drop table t1,t2;
893 
894 #
895 # correct ALL optimisation
896 #
897 create table t2 (a int, b int);
898 create table t3 (a int);
899 insert into t3 values (6),(7),(3);
900 select * from t3 where a >= all (select b from t2);
901 explain extended select * from t3 where a >= all (select b from t2);
902 select * from t3 where a >= some (select b from t2);
903 explain extended select * from t3 where a >= some (select b from t2);
904 select * from t3 where a >= all (select b from t2 group by 1);
905 explain extended select * from t3 where a >= all (select b from t2 group by 1);
906 select * from t3 where a >= some (select b from t2 group by 1);
907 explain extended select * from t3 where a >= some (select b from t2 group by 1);
908 select * from t3 where NULL >= any (select b from t2);
909 explain extended select * from t3 where NULL >= any (select b from t2);
910 select * from t3 where NULL >= any (select b from t2 group by 1);
911 explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
912 select * from t3 where NULL >= some (select b from t2);
913 explain extended select * from t3 where NULL >= some (select b from t2);
914 select * from t3 where NULL >= some (select b from t2 group by 1);
915 explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
916 #
917 # optimized static ALL/ANY with grouping
918 #
919 insert into t2 values (2,2), (2,1), (3,3), (3,1);
920 select * from t3 where a > all (select max(b) from t2 group by a);
921 explain extended select * from t3 where a > all (select max(b) from t2 group by a);
922 drop table t2, t3;
923 
924 #
925 # correct used_tables()
926 #
927 
928 CREATE TABLE `t1` ( `id` mediumint(9) NOT NULL auto_increment, `taskid` bigint(20) NOT NULL default '0', `dbid` int(11) NOT NULL default '0', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=3 ;
929 INSERT INTO `t1` (`id`, `taskid`, `dbid`, `create_date`,`last_update`) VALUES (1, 1, 15, '2003-09-29 10:31:36', '2003-09-29 10:31:36'), (2, 1, 21, now(), now());
930 CREATE TABLE `t2` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;
931 INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0);
932 CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;
933 INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
934 CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
935 INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
936 select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
937 SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;
938 drop table t1,t2,t3,t4;
939 
940 #
941 # cardinality check
942 #
943 CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
944 INSERT INTO t1 VALUES (1),(5);
945 CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
946 INSERT INTO t2 VALUES (2),(6);
947 -- error ER_OPERAND_COLUMNS
948 select * from t1 where (1,2,6) in (select * from t2);
949 DROP TABLE t1,t2;
950 
951 #
952 # DO and SET with errors
953 #
954 create table t1 (s1 int);
955 insert into t1 values (1);
956 insert into t1 values (2);
957 -- error ER_SUBQUERY_NO_1_ROW
958 set sort_buffer_size = (select s1 from t1);
959 do (select * from t1);
960 drop table t1;
961 
962 #
963 # optimized ALL/ANY with union
964 #
965 create table t1 (s1 char);
966 insert into t1 values ('e');
967 select * from t1 where 'f' > any (select s1 from t1);
968 select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
969 explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
970 drop table t1;
971 
972 #
973 # filesort in subquery (restoring join_tab)
974 #
975 CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
976 INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
977 CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
978 INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
979 select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c;
980 drop table t1, t2;
981 
982 #
983 # unresolved field error
984 #
985 create table t1 (s1 int);
986 create table t2 (s1 int);
987 -- error ER_BAD_FIELD_ERROR
988 select * from t1 where (select count(*) from t2 where t1.s2) = 1;
989 -- error ER_BAD_FIELD_ERROR
990 select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
991 -- error ER_BAD_FIELD_ERROR
992 select count(*) from t2 group by t1.s2;
993 drop table t1, t2;
994 
995 #
996 # fix_fields() in add_ref_to_table_cond()
997 #
998 CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
999 CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1000 INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1001 INSERT INTO t2 VALUES (100, 200, 'C');
1002 SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1003 DROP TABLE t1, t2;
1004 
1005 CREATE TABLE t1 (a int(1));
1006 INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1007 SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1008 DROP TABLE t1;
1009 
1010 
1011 #
1012 # Bug#2198 SELECT INTO OUTFILE (with Sub-Select) Problem
1013 #
1014 
1015 create table t1 (a int, b decimal(13, 3));
1016 insert into t1 values (1, 0.123);
1017 let $outfile_abs= $MYSQLTEST_VARDIR/tmp/subselect.out.file.1;
1018 let $outfile_rel= ../../tmp/subselect.out.file.1;
1019 --error 0,1
1020 --remove_file $outfile_abs
1021 eval select a, (select max(b) from t1) into outfile "$outfile_rel" from t1;
1022 delete from t1;
1023 eval load data infile "$outfile_rel" into table t1;
1024 --remove_file $outfile_abs
1025 select * from t1;
1026 drop table t1;
1027 
1028 
1029 #
1030 # Bug#2479 dependant subquery with limit crash
1031 #
1032 
1033 CREATE TABLE `t1` (
1034  `id` int(11) NOT NULL auto_increment,
1035  `id_cns` tinyint(3) unsigned NOT NULL default '0',
1036  `tipo` enum('','UNO','DUE') NOT NULL default '',
1037  `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1038  `particolare` mediumint(8) unsigned NOT NULL default '0',
1039  `generale` mediumint(8) unsigned NOT NULL default '0',
1040  `bis` tinyint(3) unsigned NOT NULL default '0',
1041  PRIMARY KEY (`id`),
1042  UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1043  UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1044 );
1045 INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0);
1046 CREATE TABLE `t2` (
1047  `id` tinyint(3) unsigned NOT NULL auto_increment,
1048  `max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1049  PRIMARY KEY (`id`)
1050 );
1051 INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1052 
1053 SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns;
1054 
1055 DROP TABLE t1, t2;
1056 
1057 #
1058 # GLOBAL LIMIT
1059 #
1060 create table t1 (a int);
1061 insert into t1 values (1), (2), (3);
1062 SET SQL_SELECT_LIMIT=1;
1063 select sum(a) from (select * from t1) as a;
1064 select 2 in (select * from t1);
1065 SET SQL_SELECT_LIMIT=default;
1066 drop table t1;
1067 
1068 
1069 #
1070 # Bug#3118 subselect + order by
1071 #
1072 
1073 CREATE TABLE t1 (a int, b int, INDEX (a));
1074 INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1075 SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1076 DROP TABLE t1;
1077 
1078 # Item_cond fix field
1079 #
1080 create table t1(val varchar(10));
1081 insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1082 select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');
1083 drop table t1;
1084 
1085 #
1086 # ref_or_null replacing with ref
1087 #
1088 create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1089 insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12');
1090 select * from t1 where id not in (select id from t1 where id < 8);
1091 select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
1092 explain extended select * from t1 where id not in (select id from t1 where id < 8);
1093 explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
1094 insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1095 create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1096 insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
1097 select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
1098 explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
1099 drop table t1,t2;
1100 
1101 #
1102 # Static tables & rund() in subqueries
1103 #
1104 create table t1 (a int);
1105 insert into t1 values (1);
1106 explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1107 drop table t1;
1108 
1109 
1110 #
1111 # Bug#3188 Ambiguous Column in Subselect crashes server
1112 #
1113 create table t1(id int);
1114 create table t2(id int);
1115 create table t3(flag int);
1116 -- error ER_PARSE_ERROR
1117 select (select * from t3 where id not null) from t1, t2;
1118 drop table t1,t2,t3;
1119 
1120 
1121 #
1122 # aggregate functions (Bug#3505 Wrong results on use of ORDER BY with subqueries)
1123 #
1124 CREATE TABLE t1 (id INT);
1125 CREATE TABLE t2 (id INT);
1126 INSERT INTO t1 VALUES (1), (2);
1127 INSERT INTO t2 VALUES (1);
1128 SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1129 SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1130 SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
1131 SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
1132 DROP TABLE t1,t2;
1133 
1134 #
1135 # ALL/ANY test
1136 #
1137 CREATE TABLE t1 ( a int, b int );
1138 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1139 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1140 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1141 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1142 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1143 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1144 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1145 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1146 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1147 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1148 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1149 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1150 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1151 # with index
1152 ALTER TABLE t1 ADD INDEX (a);
1153 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1154 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1155 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1156 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1157 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1158 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1159 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1160 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1161 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1162 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1163 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1164 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1165 # having clause test
1166 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
1167 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
1168 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
1169 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
1170 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
1171 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
1172 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
1173 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
1174 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
1175 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
1176 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
1177 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
1178 # union test
1179 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1180 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1181 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1182 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1183 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1184 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1185 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1186 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1187 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1188 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1189 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1190 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1191 # union + having test
1192 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1193 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1194 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1195 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1196 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1197 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1198 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1199 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1200 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1201 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1202 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1203 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1204 # row tests
1205 # < > >= <= and = ALL/ <> ANY do not support row operation
1206 -- error ER_OPERAND_COLUMNS
1207 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
1208 -- error ER_OPERAND_COLUMNS
1209 SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1210 -- error ER_OPERAND_COLUMNS
1211 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1212 -- error ER_OPERAND_COLUMNS
1213 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
1214 -- error ER_OPERAND_COLUMNS
1215 SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1216 -- error ER_OPERAND_COLUMNS
1217 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1218 -- error ER_OPERAND_COLUMNS
1219 SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
1220 -- error ER_OPERAND_COLUMNS
1221 SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
1222 # following should be converted to IN
1223 -- error ER_OPERAND_COLUMNS
1224 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
1225 -- error ER_OPERAND_COLUMNS
1226 SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1227 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1228 -- error ER_OPERAND_COLUMNS
1229 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
1230 -- error ER_OPERAND_COLUMNS
1231 SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1232 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1233 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
1234 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
1235 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
1236 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
1237 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
1238 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
1239 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
1240 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
1241 # without optimisation
1242 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1243 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1244 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1245 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1246 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1247 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1248 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1249 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1250 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1251 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1252 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1253 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1254 # without optimisation + having
1255 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
1256 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
1257 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
1258 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1259 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1260 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
1261 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
1262 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
1263 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
1264 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1265 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1266 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
1267 # EXISTS in string contence
1268 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
1269 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
1270 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
1271 DROP TABLE t1;
1272 CREATE TABLE t1 ( a double, b double );
1273 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1274 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
1275 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
1276 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
1277 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
1278 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
1279 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
1280 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
1281 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
1282 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
1283 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
1284 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
1285 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
1286 DROP TABLE t1;
1287 CREATE TABLE t1 ( a char(1), b char(1));
1288 INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
1289 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
1290 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
1291 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
1292 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
1293 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
1294 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
1295 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
1296 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
1297 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
1298 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
1299 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
1300 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
1301 DROP TABLE t1;
1302 
1303 
1304 #
1305 # SELECT(EXISTS * ...)optimisation
1306 #
1307 create table t1 (a int, b int);
1308 insert into t1 values (1,2),(3,4);
1309 select * from t1 up where exists (select * from t1 where t1.a=up.a);
1310 explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
1311 drop table t1;
1312 
1313 
1314 #
1315 # Bug#4102 subselect in HAVING
1316 #
1317 
1318 CREATE TABLE t1 (t1_a int);
1319 INSERT INTO t1 VALUES (1);
1320 CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
1321 INSERT INTO t2 VALUES (1, 1), (1, 2);
1322 SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
1323  HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
1324 DROP TABLE t1, t2;
1325 
1326 
1327 #
1328 # Test problem with NULL and derived tables
1329 # (Bug#4097 JOIN with subquery causes entire column to report NULL)
1330 #
1331 
1332 CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
1333 INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
1334 CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
1335 INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
1336 SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
1337 drop table t1,t2;
1338 
1339 
1340 #
1341 # outer fields resolving in INSERT/REPLACE and CRETE with SELECT
1342 #
1343 CREATE TABLE t1 ( a int, b int );
1344 CREATE TABLE t2 ( c int, d int );
1345 INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
1346 SELECT a AS abc, b FROM t1 outr WHERE b =
1347  (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1348 INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
1349  (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1350 select * from t2;
1351 CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
1352  (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1353 select * from t3;
1354 prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
1355 execute stmt1;
1356 deallocate prepare stmt1;
1357 select * from t2;
1358 drop table t3;
1359 prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
1360 execute stmt1;
1361 select * from t3;
1362 deallocate prepare stmt1;
1363 DROP TABLE t1, t2, t3;
1364 
1365 #
1366 # Aggregate function comparation with ALL/ANY/SOME subselect
1367 #
1368 CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1369 insert into t1 values (1);
1370 CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1371 insert into t2 values (1,2);
1372 select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
1373 drop table t1,t2;
1374 
1375 
1376 #
1377 # Bug#4769 - fulltext in subselect
1378 #
1379 create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
1380 insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
1381 create table t2 (a int);
1382 insert into t2 values (1),(3),(2),(7);
1383 select a,b from t1 where match(b) against ('Ball') > 0;
1384 select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
1385 drop table t1,t2;
1386 
1387 
1388 #
1389 # Bug#5003 - like in subselect
1390 #
1391 CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`KUERZEL` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin,`IZAANALYSEART_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`IZAPMKZ_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin);
1392 CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
1393 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
1394 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
1395 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
1396 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
1397 SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
1398 drop table t1;
1399 
1400 #
1401 # Optimized IN with compound index
1402 #
1403 CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
1404 CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
1405 insert into t1 values (1,1),(1,2),(2,1),(2,2);
1406 insert into t2 values (1,2),(2,2);
1407 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1408 alter table t2 drop primary key;
1409 alter table t2 add key KEY1 (aid, bid);
1410 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1411 alter table t2 drop key KEY1;
1412 alter table t2 add primary key (bid, aid);
1413 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1414 drop table t1,t2;
1415 
1416 #
1417 # resolving fields of grouped outer SELECT
1418 #
1419 CREATE TABLE t1 (howmanyvalues bigint, avalue int);
1420 INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
1421 SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
1422 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1423 CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
1424 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
1425 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1426 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
1427 drop table t1;
1428 
1429 create table t1 (x int);
1430 select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
1431 drop table t1;
1432 
1433 #
1434 # Test of correct maybe_null flag returning by subquwery for temporary table
1435 # creation
1436 #
1437 CREATE TABLE `t1` ( `master` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `slave` int(10) unsigned NOT NULL default '0', `access` int(10) unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
1438 INSERT INTO `t1` VALUES (1,0,0,700),(1,1,1,400),(1,5,5,400),(1,12,12,400),(1,12,32,400),(4,12,32,400);
1439 CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL default '0', `pid` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `level` tinyint(4) unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
1440 INSERT INTO `t2` VALUES (6,5,12,7,'a'),(12,0,0,7,'a'),(12,1,0,7,'a'),(12,5,5,7,'a'),(12,5,12,7,'a');
1441 -- error ER_BAD_FIELD_ERROR
1442 SELECT b.sc FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
1443 SELECT b.ac FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
1444 drop tables t1,t2;
1445 
1446 
1447 #
1448 # Test for Bug#6462 Same request on same data returns different results
1449 # a.k.a. "Proper cleanup of subqueries is missing for SET and DO statements".
1450 #
1451 create table t1 (a int not null, b int not null, c int, primary key (a,b));
1452 insert into t1 values (1,1,1), (2,2,2), (3,3,3);
1453 set @b:= 0;
1454 # Let us check that subquery will use covering index
1455 explain select sum(a) from t1 where b > @b;
1456 # This should not crash -debug server due to failing assertion
1457 set @a:= (select sum(a) from t1 where b > @b);
1458 # And this should not falsely report index usage
1459 explain select a from t1 where c=2;
1460 # Same for DO statement
1461 do @a:= (select sum(a) from t1 where b > @b);
1462 explain select a from t1 where c=2;
1463 drop table t1;
1464 
1465 #
1466 # Subselect in non-select command just after connection
1467 # Disconnect new connection and switch back when test is finished
1468 #
1469 connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
1470 connection root;
1471 set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
1472 connection default;
1473 disconnect root;
1474 
1475 #
1476 # primary query with temporary table and subquery with grouping
1477 #
1478 create table t1 (a int, b int);
1479 create table t2 (a int, b int);
1480 insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
1481 insert into t2 values (1,3),(2,1);
1482 select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
1483 drop table t1, t2;
1484 
1485 #
1486 # subqueries with full text search
1487 #
1488 create table t1 (id int);
1489 create table t2 (id int, body text, fulltext (body));
1490 insert into t1 values(1),(2),(3);
1491 insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
1492 select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
1493 drop table t2,t1;
1494 
1495 #
1496 # Equal operation under row and empty subquery
1497 #
1498 create table t1 (s1 int,s2 int);
1499 insert into t1 values (20,15);
1500 select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
1501 drop table t1;
1502 
1503 #
1504 # ALL/ANY with NULL
1505 #
1506 create table t1 (s1 int);
1507 insert into t1 values (1),(null);
1508 select * from t1 where s1 < all (select s1 from t1);
1509 select s1, s1 < all (select s1 from t1) from t1;
1510 drop table t1;
1511 
1512 #
1513 # reference on changable fields from subquery
1514 #
1515 CREATE TABLE t1 (
1516  Code char(3) NOT NULL default '',
1517  Name char(52) NOT NULL default '',
1518  Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1519  Region char(26) NOT NULL default '',
1520  SurfaceArea float(10,2) NOT NULL default '0.00',
1521  IndepYear smallint(6) default NULL,
1522  Population int(11) NOT NULL default '0',
1523  LifeExpectancy float(3,1) default NULL,
1524  GNP float(10,2) default NULL,
1525  GNPOld float(10,2) default NULL,
1526  LocalName char(45) NOT NULL default '',
1527  GovernmentForm char(45) NOT NULL default '',
1528  HeadOfState char(60) default NULL,
1529  Capital int(11) default NULL,
1530  Code2 char(2) NOT NULL default ''
1531 ) ENGINE=MyISAM;
1532 INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
1533 INSERT INTO t1 VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,0,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS');
1534 INSERT INTO t1 VALUES ('ATF','French Southern territories','Antarctica','Antarctica',7780.00,0,0,NULL,0.00,NULL,'Terres australes françaises','Nonmetropolitan Territory of France','Jacques Chirac',NULL,'TF');
1535 INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','Micronesia/Caribbean',16.00,0,0,NULL,0.00,NULL,'United States Minor Outlying Islands','Dependent Territory of the US','George W. Bush',NULL,'UM');;
1537 SELECT DISTINCT Continent AS c FROM t1 outr WHERE
1538  Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
1539  Population < 200);
1540 drop table t1;
1541 
1542 
1543 #
1544 # Test for Bug#7885 Server crash when 'any' subselect compared to
1545 # non-existant field.
1546 #
1547 create table t1 (a1 int);
1548 create table t2 (b1 int);
1549 --error ER_BAD_FIELD_ERROR
1550 select * from t1 where a2 > any(select b1 from t2);
1551 select * from t1 where a1 > any(select b1 from t2);
1552 drop table t1,t2;
1553 
1554 
1555 #
1556 # Comparison subquery with * and row
1557 #
1558 create table t1 (a integer, b integer);
1559 select (select * from t1) = (select 1,2);
1560 select (select 1,2) = (select * from t1);
1561 # queries whih can be converted to IN
1562 select row(1,2) = ANY (select * from t1);
1563 select row(1,2) != ALL (select * from t1);
1564 drop table t1;
1565 
1566 #
1567 # Comparison subquery and row with nested rows
1568 #
1569 create table t1 (a integer, b integer);
1570 -- error ER_OPERAND_COLUMNS
1571 select row(1,(2,2)) in (select * from t1 );
1572 -- error ER_OPERAND_COLUMNS
1573 select row(1,(2,2)) = (select * from t1 );
1574 -- error ER_OPERAND_COLUMNS
1575 select (select * from t1) = row(1,(2,2));
1576 drop table t1;
1577 
1578 #
1579 # Forward reference detection
1580 #
1581 create table t1 (a integer);
1582 insert into t1 values (1);
1583 -- error ER_ILLEGAL_REFERENCE
1584 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1585 -- error ER_ILLEGAL_REFERENCE
1586 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1587 select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
1588 -- error ER_ILLEGAL_REFERENCE
1589 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1590 -- error ER_ILLEGAL_REFERENCE
1591 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
1592 drop table t1;
1593 
1594 
1595 #
1596 # Test for Bug#8218 Join does not pass string from right table
1597 #
1598 CREATE TABLE t1 (
1599  categoryId int(11) NOT NULL,
1600  courseId int(11) NOT NULL,
1601  startDate datetime NOT NULL,
1602  endDate datetime NOT NULL,
1603  createDate datetime NOT NULL,
1604  modifyDate timestamp NOT NULL,
1605  attributes text NOT NULL
1606 );
1607 INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1608 (1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1609 (1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1610 (2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
1611 (2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
1612 (2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1613 (2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1614 (3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1615 (5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
1616 
1617 CREATE TABLE t2 (
1618  userId int(11) NOT NULL,
1619  courseId int(11) NOT NULL,
1620  date datetime NOT NULL
1621 );
1622 INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
1623 (5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
1624 (5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
1625 (5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
1626 (5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
1627 (5141,89,'2004-10-22'),(5141,51,'2004-10-26');
1628 
1629 
1630 CREATE TABLE t3 (
1631  groupId int(11) NOT NULL,
1632  parentId int(11) NOT NULL,
1633  startDate datetime NOT NULL,
1634  endDate datetime NOT NULL,
1635  createDate datetime NOT NULL,
1636  modifyDate timestamp NOT NULL,
1637  ordering int(11)
1638 );
1639 INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
1640 
1641 CREATE TABLE t4 (
1642  id int(11) NOT NULL,
1643  groupTypeId int(11) NOT NULL,
1644  groupKey varchar(50) NOT NULL,
1645  name text,
1646  ordering int(11),
1647  description text,
1648  createDate datetime NOT NULL,
1649  modifyDate timestamp NOT NULL
1650 );
1651 INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
1652 (12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
1653 
1654 CREATE TABLE t5 (
1655  userId int(11) NOT NULL,
1656  groupId int(11) NOT NULL,
1657  createDate datetime NOT NULL,
1658  modifyDate timestamp NOT NULL
1659 );
1660 INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
1661 
1662 select
1663  count(distinct t2.userid) pass,
1664  groupstuff.*,
1665  count(t2.courseid) crse,
1666  t1.categoryid,
1667  t2.courseid,
1668  date_format(date, '%b%y') as colhead
1669 from t2
1670 join t1 on t2.courseid=t1.courseid
1671 join
1672 (
1673  select
1674  t5.userid,
1675  parentid,
1676  parentgroup,
1677  childid,
1678  groupname,
1679  grouptypeid
1680  from t5
1681  join
1682  (
1683  select t4.id as parentid,
1684  t4.name as parentgroup,
1685  t4.id as childid,
1686  t4.name as groupname,
1687  t4.grouptypeid
1688  from t4
1689  ) as gin on t5.groupid=gin.childid
1690 ) as groupstuff on t2.userid = groupstuff.userid
1691 group by
1692  groupstuff.groupname, colhead , t2.courseid;
1693 
1694 drop table t1, t2, t3, t4, t5;
1695 
1696 
1697 #
1698 # Transformation in left expression of subquery (Bug#8888)
1699 #
1700 create table t1 (a int);
1701 insert into t1 values (1), (2), (3);
1702 SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
1703 drop table t1;
1704 #
1705 # subselect into HAVING clause (code covarage improvement)
1706 #
1707 create table t1 (a int);
1708 create table t2 (a int);
1709 insert into t1 values (1),(2);
1710 insert into t2 values (0),(1),(2),(3);
1711 select a from t2 where a in (select a from t1);
1712 select a from t2 having a in (select a from t1);
1713 prepare stmt1 from "select a from t2 where a in (select a from t1)";
1714 execute stmt1;
1715 execute stmt1;
1716 deallocate prepare stmt1;
1717 prepare stmt1 from "select a from t2 having a in (select a from t1)";
1718 execute stmt1;
1719 execute stmt1;
1720 deallocate prepare stmt1;
1721 drop table t1, t2;
1722 
1723 #
1724 # single row subqueries and row operations (code covarage improvement)
1725 #
1726 create table t1 (a int, b int);
1727 insert into t1 values (1,2);
1728 -- error ER_OPERAND_COLUMNS
1729 select 1 = (select * from t1);
1730 -- error ER_OPERAND_COLUMNS
1731 select (select * from t1) = 1;
1732 -- error ER_OPERAND_COLUMNS
1733 select (1,2) = (select a from t1);
1734 -- error ER_OPERAND_COLUMNS
1735 select (select a from t1) = (1,2);
1736 -- error ER_OPERAND_COLUMNS
1737 select (1,2,3) = (select * from t1);
1738 -- error ER_OPERAND_COLUMNS
1739 select (select * from t1) = (1,2,3);
1740 drop table t1;
1741 
1742 
1743 #
1744 # Item_int_with_ref check (Bug#10020)
1745 #
1746 CREATE TABLE `t1` (
1747  `itemid` bigint(20) unsigned NOT NULL auto_increment,
1748  `sessionid` bigint(20) unsigned default NULL,
1749  `time` int(10) unsigned NOT NULL default '0',
1750  `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
1751 NULL default '',
1752  `data` text collate latin1_general_ci NOT NULL,
1753  PRIMARY KEY (`itemid`)
1754 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1755 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
1756 CREATE TABLE `t2` (
1757  `sessionid` bigint(20) unsigned NOT NULL auto_increment,
1758  `pid` int(10) unsigned NOT NULL default '0',
1759  `date` int(10) unsigned NOT NULL default '0',
1760  `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1761  PRIMARY KEY (`sessionid`)
1762 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1763 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1764 SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
1765 drop tables t1,t2;
1766 
1767 
1768 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
1769 create table t1 (fld enum('0','1'));
1770 insert into t1 values ('1');
1771 select * from (select max(fld) from t1) as foo;
1772 drop table t1;
1773 
1774 
1775 #
1776 # Bug#11479 subquery over left join with an empty inner table
1777 #
1778 
1779 CREATE TABLE t1 (a int);
1780 CREATE TABLE t2 (a int, b int);
1781 CREATE TABLE t3 (b int NOT NULL);
1782 INSERT INTO t1 VALUES (1), (2), (3), (4);
1783 INSERT INTO t2 VALUES (1,10), (3,30);
1784 
1785 SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1786  WHERE t3.b IS NOT NULL OR t2.a > 10;
1787 SELECT * FROM t1
1788  WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1789  WHERE t3.b IS NOT NULL OR t2.a > 10);
1790 
1791 DROP TABLE t1,t2,t3;
1792 
1793 
1794 #
1795 # Bug#18503 Queries with a quantified subquery returning empty set may
1796 # return a wrong result.
1797 #
1798 CREATE TABLE t1 (f1 INT);
1799 CREATE TABLE t2 (f2 INT);
1800 INSERT INTO t1 VALUES (1);
1801 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
1802 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
1803 INSERT INTO t2 VALUES (1);
1804 INSERT INTO t2 VALUES (2);
1805 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
1806 DROP TABLE t1, t2;
1807 
1808 
1809 #
1810 # Bug#16302 Quantified subquery without any tables gives wrong results
1811 #
1812 select 1 from dual where 1 < any (select 2);
1813 select 1 from dual where 1 < all (select 2);
1814 select 1 from dual where 2 > any (select 1);
1815 select 1 from dual where 2 > all (select 1);
1816 select 1 from dual where 1 < any (select 2 from dual);
1817 select 1 from dual where 1 < all (select 2 from dual where 1!=1);
1818 
1819 
1820 # Bug#20975 Wrong query results for subqueries within NOT
1821 create table t1 (s1 char);
1822 insert into t1 values (1),(2);
1823 
1824 select * from t1 where (s1 < any (select s1 from t1));
1825 select * from t1 where not (s1 < any (select s1 from t1));
1826 
1827 select * from t1 where (s1 < ALL (select s1+1 from t1));
1828 select * from t1 where not(s1 < ALL (select s1+1 from t1));
1829 
1830 select * from t1 where (s1+1 = ANY (select s1 from t1));
1831 select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
1832 
1833 select * from t1 where (s1 = ALL (select s1/s1 from t1));
1834 select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
1835 drop table t1;
1836 
1837 
1838 #
1839 # Bug#16255 Subquery in where
1840 #
1841 create table t1 (
1842  retailerID varchar(8) NOT NULL,
1843  statusID int(10) unsigned NOT NULL,
1844  changed datetime NOT NULL,
1845  UNIQUE KEY retailerID (retailerID, statusID, changed)
1846 );
1847 
1848 INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
1849 INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
1850 INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
1851 INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
1852 INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
1853 INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
1854 
1855 select * from t1 r1
1856  where (r1.retailerID,(r1.changed)) in
1857  (SELECT r2.retailerId,(max(changed)) from t1 r2
1858  group by r2.retailerId);
1859 drop table t1;
1860 
1861 
1862 #
1863 # Bug#21180 Subselect with index for both WHERE and ORDER BY
1864 # produces empty result
1865 #
1866 create table t1(a int, primary key (a));
1867 insert into t1 values (10);
1868 
1869 create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
1870 insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
1871 
1872 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1873  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1874  ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1875 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1876  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1877  ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1878 
1879 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1880  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1881  ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1882 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1883  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1884  ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1885 
1886 drop table t1,t2;
1887 
1888 
1889 #
1890 # Bug#21853 assert failure for a grouping query with
1891 # an ALL/ANY quantified subquery in HAVING
1892 #
1893 
1894 CREATE TABLE t1 (
1895  field1 int NOT NULL,
1896  field2 int NOT NULL,
1897  field3 int NOT NULL,
1898  PRIMARY KEY (field1,field2,field3)
1899 );
1900 CREATE TABLE t2 (
1901  fieldA int NOT NULL,
1902  fieldB int NOT NULL,
1903  PRIMARY KEY (fieldA,fieldB)
1904 );
1905 
1906 INSERT INTO t1 VALUES
1907  (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
1908 INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
1909 
1910 SELECT field1, field2, COUNT(*)
1911  FROM t1 GROUP BY field1, field2;
1912 
1913 SELECT field1, field2
1914  FROM t1
1915  GROUP BY field1, field2
1916  HAVING COUNT(*) >= ALL (SELECT fieldB
1917  FROM t2 WHERE fieldA = field1);
1918 SELECT field1, field2
1919  FROM t1
1920  GROUP BY field1, field2
1921  HAVING COUNT(*) < ANY (SELECT fieldB
1922  FROM t2 WHERE fieldA = field1);
1923 
1924 DROP TABLE t1, t2;
1925 
1926 
1927 #
1928 # Bug#23478 not top-level IN subquery returning a non-empty result set
1929 # with possible NULL values by index access from the outer query
1930 #
1931 
1932 CREATE TABLE t1(a int, INDEX (a));
1933 INSERT INTO t1 VALUES (1), (3), (5), (7);
1934 INSERT INTO t1 VALUES (NULL);
1935 
1936 CREATE TABLE t2(a int);
1937 INSERT INTO t2 VALUES (1),(2),(3);
1938 
1939 EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
1940 SELECT a, a IN (SELECT a FROM t1) FROM t2;
1941 
1942 DROP TABLE t1,t2;
1943 
1944 
1945 #
1946 # Bug#11302 getObject() returns a String for a sub-query of type datetime
1947 #
1948 CREATE TABLE t1 (a DATETIME);
1949 INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
1950 
1951 CREATE TABLE t2 AS SELECT
1952  (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
1953  FROM t1 WHERE a > '2000-01-01';
1954 SHOW CREATE TABLE t2;
1955 
1956 CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
1957 SHOW CREATE TABLE t3;
1958 
1959 DROP TABLE t1,t2,t3;
1960 
1961 
1962 #
1963 # Bug#24670 subquery witout tables but with a WHERE clause
1964 #
1965 
1966 CREATE TABLE t1 (a int);
1967 INSERT INTO t1 VALUES (1), (2);
1968 
1969 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
1970 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
1971 EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
1972 
1973 DROP TABLE t1;
1974 
1975 
1976 #
1977 # Bug#24653 sorting by expressions containing subselects
1978 # that return more than one row
1979 #
1980 
1981 CREATE TABLE t1 (a int);
1982 INSERT INTO t1 VALUES (2), (4), (1), (3);
1983 
1984 CREATE TABLE t2 (b int, c int);
1985 INSERT INTO t2 VALUES
1986  (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
1987 
1988 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
1989 --error ER_SUBQUERY_NO_1_ROW
1990 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
1991 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
1992 --error ER_SUBQUERY_NO_1_ROW
1993 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
1994 
1995 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
1996 --error ER_SUBQUERY_NO_1_ROW
1997 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
1998 
1999 
2000 SELECT a FROM t1 GROUP BY a
2001  HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
2002  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2003 --error ER_SUBQUERY_NO_1_ROW
2004 SELECT a FROM t1 GROUP BY a
2005  HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
2006  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2007 
2008 SELECT a FROM t1 GROUP BY a
2009  HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2010  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2011 --error ER_SUBQUERY_NO_1_ROW
2012 SELECT a FROM t1 GROUP BY a
2013  HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2014  (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
2015 
2016 SELECT a FROM t1
2017  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
2018  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
2019 --error ER_SUBQUERY_NO_1_ROW
2020 SELECT a FROM t1
2021  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
2022  (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
2023 
2024 SELECT a FROM t1
2025  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2026  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
2027 --error ER_SUBQUERY_NO_1_ROW
2028 SELECT a FROM t1
2029  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2030  (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
2031 
2032 DROP TABLE t1,t2;
2033 
2034 # End of 4.1 tests
2035 
2036 #
2037 #decimal-related tests
2038 #
2039 create table t1 (df decimal(5,1));
2040 insert into t1 values(1.1);
2041 insert into t1 values(2.2);
2042 
2043 select * from t1 where df <= all (select avg(df) from t1 group by df);
2044 select * from t1 where df >= all (select avg(df) from t1 group by df);
2045 drop table t1;
2046 
2047 create table t1 (df decimal(5,1));
2048 insert into t1 values(1.1);
2049 select 1.1 * exists(select * from t1);
2050 drop table t1;
2051 
2052 CREATE TABLE t1 (
2053  grp int(11) default NULL,
2054  a decimal(10,2) default NULL);
2055 
2056 insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
2057 select * from t1;
2058 select min(a) from t1 group by grp;
2059 drop table t1;
2060 
2061 
2062 #
2063 # Test for Bug#9338 lame substitution of c1 instead of c2
2064 #
2065 
2066 CREATE table t1 ( c1 integer );
2067 INSERT INTO t1 VALUES ( 1 );
2068 INSERT INTO t1 VALUES ( 2 );
2069 INSERT INTO t1 VALUES ( 3 );
2070 
2071 CREATE TABLE t2 ( c2 integer );
2072 INSERT INTO t2 VALUES ( 1 );
2073 INSERT INTO t2 VALUES ( 4 );
2074 INSERT INTO t2 VALUES ( 5 );
2075 
2076 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
2077 
2078 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
2079  WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
2080 
2081 DROP TABLE t1,t2;
2082 
2083 
2084 #
2085 # Test for Bug#9516 wrong evaluation of not_null_tables attribute in SQ
2086 #
2087 CREATE TABLE t1 ( c1 integer );
2088 INSERT INTO t1 VALUES ( 1 );
2089 INSERT INTO t1 VALUES ( 2 );
2090 INSERT INTO t1 VALUES ( 3 );
2091 INSERT INTO t1 VALUES ( 6 );
2092 
2093 CREATE TABLE t2 ( c2 integer );
2094 INSERT INTO t2 VALUES ( 1 );
2095 INSERT INTO t2 VALUES ( 4 );
2096 INSERT INTO t2 VALUES ( 5 );
2097 INSERT INTO t2 VALUES ( 6 );
2098 
2099 CREATE TABLE t3 ( c3 integer );
2100 INSERT INTO t3 VALUES ( 7 );
2101 INSERT INTO t3 VALUES ( 8 );
2102 
2103 SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
2104  WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
2105 
2106 DROP TABLE t1,t2,t3;
2107 
2108 
2109 #
2110 # Item_int_with_ref check (Bug#10020)
2111 #
2112 CREATE TABLE `t1` (
2113  `itemid` bigint(20) unsigned NOT NULL auto_increment,
2114  `sessionid` bigint(20) unsigned default NULL,
2115  `time` int(10) unsigned NOT NULL default '0',
2116  `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2117 NULL default '',
2118  `data` text collate latin1_general_ci NOT NULL,
2119  PRIMARY KEY (`itemid`)
2120 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2121 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2122 CREATE TABLE `t2` (
2123  `sessionid` bigint(20) unsigned NOT NULL auto_increment,
2124  `pid` int(10) unsigned NOT NULL default '0',
2125  `date` int(10) unsigned NOT NULL default '0',
2126  `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2127  PRIMARY KEY (`sessionid`)
2128 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2129 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2130 SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
2131 drop tables t1,t2;
2132 
2133 
2134 #
2135 # Correct building of equal fields list (do not include outer
2136 # fields) (Bug#6384)
2137 #
2138 CREATE TABLE t1 (EMPNUM CHAR(3));
2139 CREATE TABLE t2 (EMPNUM CHAR(3) );
2140 INSERT INTO t1 VALUES ('E1'),('E2');
2141 INSERT INTO t2 VALUES ('E1');
2142 DELETE FROM t1
2143 WHERE t1.EMPNUM NOT IN
2144  (SELECT t2.EMPNUM
2145  FROM t2
2146  WHERE t1.EMPNUM = t2.EMPNUM);
2147 select * from t1;
2148 DROP TABLE t1,t2;
2149 
2150 
2151 #
2152 # Test for Bug#11487 range access in a subquery
2153 #
2154 
2155 CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
2156 INSERT INTO t1 VALUES (1, 1);
2157 CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
2158  PRIMARY KEY(select_id,values_id));
2159 INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
2160 
2161 SELECT values_id FROM t1
2162 WHERE values_id IN (SELECT values_id FROM t2
2163  WHERE select_id IN (1, 0));
2164 SELECT values_id FROM t1
2165 WHERE values_id IN (SELECT values_id FROM t2
2166  WHERE select_id BETWEEN 0 AND 1);
2167 SELECT values_id FROM t1
2168 WHERE values_id IN (SELECT values_id FROM t2
2169  WHERE select_id = 0 OR select_id = 1);
2170 
2171 DROP TABLE t1, t2;
2172 
2173 
2174 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
2175 create table t1 (fld enum('0','1'));
2176 insert into t1 values ('1');
2177 select * from (select max(fld) from t1) as foo;
2178 drop table t1;
2179 
2180 
2181 #
2182 # Test for Bug#11762 subquery with an aggregate function in HAVING
2183 #
2184 
2185 CREATE TABLE t1 (a int, b int);
2186 CREATE TABLE t2 (c int, d int);
2187 CREATE TABLE t3 (e int);
2188 
2189 INSERT INTO t1 VALUES
2190  (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
2191 INSERT INTO t2 VALUES
2192  (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
2193 INSERT INTO t3 VALUES (10), (30), (10), (20) ;
2194 
2195 SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
2196 SELECT * FROM t2;
2197 SELECT * FROM t3;
2198 
2199 SELECT a FROM t1 GROUP BY a
2200  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
2201 SELECT a FROM t1 GROUP BY a
2202  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
2203 SELECT a FROM t1 GROUP BY a
2204  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
2205 SELECT a FROM t1 GROUP BY a
2206  HAVING a IN (SELECT c FROM t2
2207  WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2208 SELECT a FROM t1 GROUP BY a
2209  HAVING a IN (SELECT c FROM t2
2210  WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2211 SELECT a FROM t1 GROUP BY a
2212  HAVING a IN (SELECT c FROM t2
2213  WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2214 SELECT a FROM t1 GROUP BY a
2215  HAVING a IN (SELECT c FROM t2
2216  WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
2217 SELECT a FROM t1 GROUP BY a
2218  HAVING a IN (SELECT c FROM t2
2219  WHERE MIN(b) < d AND
2220  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2221 
2222 SELECT a, SUM(a) FROM t1 GROUP BY a;
2223 
2224 SELECT a FROM t1
2225  WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
2226 SELECT a FROM t1 GROUP BY a
2227  HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
2228 
2229 SELECT a FROM t1
2230  WHERE a < 3 AND
2231  EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
2232 SELECT a FROM t1
2233  WHERE a < 3 AND
2234  EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
2235 
2236 SELECT t1.a FROM t1 GROUP BY t1.a
2237  HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
2238  HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2239  HAVING SUM(t1.a+t2.c) < t3.e/4));
2240 SELECT t1.a FROM t1 GROUP BY t1.a
2241  HAVING t1.a > ALL(SELECT t2.c FROM t2
2242  WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2243  HAVING SUM(t1.a+t2.c) < t3.e/4));
2244 -- error ER_INVALID_GROUP_FUNC_USE
2245 SELECT t1.a FROM t1 GROUP BY t1.a
2246  HAVING t1.a > ALL(SELECT t2.c FROM t2
2247  WHERE EXISTS(SELECT t3.e FROM t3
2248  WHERE SUM(t1.a+t2.c) < t3.e/4));
2249 -- error ER_INVALID_GROUP_FUNC_USE
2250 SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
2251 
2252 SELECT t1.a FROM t1 GROUP BY t1.a
2253  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2254  HAVING AVG(t2.c+SUM(t1.b)) > 20);
2255 SELECT t1.a FROM t1 GROUP BY t1.a
2256  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2257  HAVING AVG(SUM(t1.b)) > 20);
2258 
2259 SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
2260  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2261  HAVING t2.c+sum > 20);
2262 
2263 DROP TABLE t1,t2,t3;
2264 
2265 
2266 #
2267 # Bug#17366 Unchecked Item_int results in server crash
2268 #
2269 create table t1( f1 int,f2 int);
2270 insert into t1 values (1,1),(2,2);
2271 select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1';
2272 drop table t1;
2273 
2274 
2275 #
2276 # Bug#18306 server crash on delete using subquery.
2277 #
2278 
2279 create table t1 (c int, key(c));
2280 insert into t1 values (1142477582), (1142455969);
2281 create table t2 (a int, b int);
2282 insert into t2 values (2, 1), (1, 0);
2283 delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
2284 drop table t1, t2;
2285 
2286 
2287 #
2288 # Bug#7549 Missing error message for invalid view selection with subquery
2289 #
2290 
2291 CREATE TABLE t1 (a INT);
2292 
2293 --error ER_BAD_FIELD_ERROR
2294 CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2295 --error ER_BAD_FIELD_ERROR
2296 CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
2297 --error ER_BAD_FIELD_ERROR
2298 SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2299 
2300 DROP TABLE t1;
2301 
2302 
2303 #
2304 # Bug#19077 A nested materialized derived table is used before being populated.
2305 #
2306 create table t1 (i int, j bigint);
2307 insert into t1 values (1, 2), (2, 2), (3, 2);
2308 select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
2309 drop table t1;
2310 
2311 
2312 #
2313 # Bug#19700 subselect returning BIGINT always returned it as SIGNED
2314 #
2315 CREATE TABLE t1 (i BIGINT UNSIGNED);
2316 INSERT INTO t1 VALUES (10000000000000000000); # > MAX SIGNED BIGINT 9323372036854775807
2317 INSERT INTO t1 VALUES (1);
2318 
2319 CREATE TABLE t2 (i BIGINT UNSIGNED);
2320 INSERT INTO t2 VALUES (10000000000000000000); # same as first table
2321 INSERT INTO t2 VALUES (1);
2322 
2323 /* simple test */
2324 SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
2325 
2326 /* subquery test */
2327 SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
2328 
2329 /* subquery test with cast*/
2330 SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
2331 
2332 DROP TABLE t1;
2333 DROP TABLE t2;
2334 
2335 
2336 #
2337 # Bug#20519 subselect with LIMIT M, N
2338 #
2339 
2340 CREATE TABLE t1 (
2341  id bigint(20) unsigned NOT NULL auto_increment,
2342  name varchar(255) NOT NULL,
2343  PRIMARY KEY (id)
2344 );
2345 INSERT INTO t1 VALUES
2346  (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
2347 
2348 CREATE TABLE t2 (
2349  id bigint(20) unsigned NOT NULL auto_increment,
2350  mid bigint(20) unsigned NOT NULL,
2351  date date NOT NULL,
2352  PRIMARY KEY (id)
2353 );
2354 INSERT INTO t2 VALUES
2355  (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
2356  (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
2357 
2358 SELECT *,
2359  (SELECT date FROM t2 WHERE mid = t1.id
2360  ORDER BY date DESC LIMIT 0, 1) AS date_last,
2361  (SELECT date FROM t2 WHERE mid = t1.id
2362  ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
2363  FROM t1;
2364 SELECT *,
2365  (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
2366  ORDER BY date DESC LIMIT 1, 1) AS date_count
2367  FROM t1;
2368 SELECT *,
2369  (SELECT date FROM t2 WHERE mid = t1.id
2370  ORDER BY date DESC LIMIT 0, 1) AS date_last,
2371  (SELECT date FROM t2 WHERE mid = t1.id
2372  ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
2373  FROM t1;
2374 DROP TABLE t1,t2;
2375 
2376 
2377 #
2378 # Bug#20869 subselect with range access by DESC
2379 #
2380 
2381 CREATE TABLE t1 (
2382  i1 int(11) NOT NULL default '0',
2383  i2 int(11) NOT NULL default '0',
2384  t datetime NOT NULL default '0000-00-00 00:00:00',
2385  PRIMARY KEY (i1,i2,t)
2386 );
2387 INSERT INTO t1 VALUES
2388 (24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
2389 (24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
2390 (24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
2391 (24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
2392 (24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
2393 (24,2,'2005-05-27 12:40:06');
2394 
2395 CREATE TABLE t2 (
2396  i1 int(11) NOT NULL default '0',
2397  i2 int(11) NOT NULL default '0',
2398  t datetime default NULL,
2399  PRIMARY KEY (i1)
2400 );
2401 INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
2402 
2403 EXPLAIN
2404 SELECT * FROM t1,t2
2405  WHERE t1.t = (SELECT t1.t FROM t1
2406  WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
2407  ORDER BY t1.t DESC LIMIT 1);
2408 SELECT * FROM t1,t2
2409  WHERE t1.t = (SELECT t1.t FROM t1
2410  WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
2411  ORDER BY t1.t DESC LIMIT 1);
2412 
2413 DROP TABLE t1, t2;
2414 
2415 
2416 #
2417 # Bug#14654 Cannot select from the same table twice within a UNION statement
2418 #
2419 CREATE TABLE t1 (i INT);
2420 
2421 (SELECT i FROM t1) UNION (SELECT i FROM t1);
2422 SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
2423  (
2424  (SELECT i FROM t1) UNION
2425  (SELECT i FROM t1)
2426  );
2427 
2428 #TODO:not supported
2429 --error ER_PARSE_ERROR
2430 SELECT * FROM t1
2431 WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
2432 
2433 #TODO:not supported
2434 --error ER_PARSE_ERROR
2435 explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
2436  from t1;
2437 
2438 explain select * from t1 where not exists
2439  ((select t11.i from t1 t11) union (select t12.i from t1 t12));
2440 
2441 DROP TABLE t1;
2442 
2443 
2444 #
2445 # Bug#21798 memory leak during query execution with subquery in column
2446 # list using a function
2447 #
2448 CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
2449 insert into t1 (a) values (FLOOR(rand() * 100));
2450 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2451 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2452 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2453 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2454 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2455 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2456 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2457 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2458 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2459 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2460 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2461 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2462 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2463 
2464 SELECT a,
2465  (SELECT REPEAT(' ',250) FROM t1 i1
2466  WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
2467 FROM t1 ORDER BY a LIMIT 5;
2468 DROP TABLE t1;
2469 
2470 
2471 #
2472 # Bug#21540 Subqueries with no from and aggregate functions return
2473 # wrong results
2474 CREATE TABLE t1 (a INT, b INT);
2475 CREATE TABLE t2 (a INT);
2476 INSERT INTO t2 values (1);
2477 INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
2478 SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2479 SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
2480  FROM t1 GROUP BY t1.a;
2481 SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
2482 SELECT COUNT(DISTINCT t1.b),
2483  (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
2484  FROM t1 GROUP BY t1.a;
2485 SELECT (
2486  SELECT (
2487  SELECT COUNT(DISTINCT t1.b)
2488  )
2489 )
2490 FROM t1 GROUP BY t1.a;
2491 SELECT (
2492  SELECT (
2493  SELECT (
2494  SELECT COUNT(DISTINCT t1.b)
2495  )
2496  )
2497  FROM t1 GROUP BY t1.a LIMIT 1)
2498 FROM t1 t2
2499 GROUP BY t2.a;
2500 DROP TABLE t1,t2;
2501 
2502 
2503 #
2504 # Bug#21727 Correlated subquery that requires filesort:
2505 # slow with big sort_buffer_size
2506 #
2507 
2508 CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
2509 CREATE TABLE t2 (x int auto_increment, y int, z int,
2510  PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
2511 create table t3 (a int);
2512 insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
2513 
2514 insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
2515 from t3 A, t3 B, t3 C, t3 D where D.a<3;
2516 insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
2517 enable_query_log;
2518 
2519 SET SESSION sort_buffer_size = 32 * 1024;
2520 SELECT SQL_NO_CACHE COUNT(*)
2521  FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2522  FROM t1) t;
2523 
2524 SET SESSION sort_buffer_size = 8 * 1024 * 1024;
2525 SELECT SQL_NO_CACHE COUNT(*)
2526  FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2527  FROM t1) t;
2528 
2529 DROP TABLE t1,t2,t3;
2530 
2531 
2532 #
2533 # Bug#25219 EXIST subquery with UNION over a mix of
2534 # correlated and uncorrelated selects
2535 #
2536 
2537 CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
2538 CREATE TABLE t2 (c int);
2539 
2540 INSERT INTO t1 VALUES ('aa', 1);
2541 INSERT INTO t2 VALUES (1);
2542 
2543 SELECT * FROM t1
2544  WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2545  UNION
2546  SELECT c from t2 WHERE c=t1.c);
2547 
2548 INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
2549 
2550 SELECT * FROM t1
2551  WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2552  UNION
2553  SELECT c from t2 WHERE c=t1.c);
2554 
2555 INSERT INTO t2 VALUES (2);
2556 CREATE TABLE t3 (c int);
2557 INSERT INTO t3 VALUES (1);
2558 
2559 SELECT * FROM t1
2560  WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
2561  UNION
2562  SELECT c from t2 WHERE c=t1.c);
2563 
2564 DROP TABLE t1,t2,t3;
2565 
2566 
2567 #
2568 # Bug#23800 Outer fields in correlated subqueries is used in a temporary
2569 # table created for sorting.
2570 #
2571 CREATE TABLE t1(f1 int);
2572 CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
2573 INSERT INTO t1 VALUES (1),(1),(2),(2);
2574 INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
2575 SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
2576 SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
2577 PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
2578 EXECUTE stmt1;
2579 EXECUTE stmt1;
2580 DEALLOCATE PREPARE stmt1;
2581 SELECT f2, AVG(f21),
2582  (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
2583  FROM t2 GROUP BY f2;
2584 DROP TABLE t1,t2;
2585 CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
2586 INSERT INTO t1 VALUES
2587  (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
2588  (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
2589  (3,2,'k'), (3,1,'l'), (1,9,'m');
2590 SELECT a, MAX(b),
2591  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
2592  FROM t1 GROUP BY a;
2593 DROP TABLE t1;
2594 
2595 
2596 #
2597 # Bug#21904 (parser problem when using IN with a double "(())")
2598 #
2599 
2600 --disable_warnings
2601 DROP TABLE IF EXISTS t1;
2602 DROP TABLE IF EXISTS t2;
2603 DROP TABLE IF EXISTS t1xt2;
2604 --enable_warnings
2605 
2606 CREATE TABLE t1 (
2607  id_1 int(5) NOT NULL,
2608  t varchar(4) DEFAULT NULL
2609 );
2610 
2611 CREATE TABLE t2 (
2612  id_2 int(5) NOT NULL,
2613  t varchar(4) DEFAULT NULL
2614 );
2615 
2616 CREATE TABLE t1xt2 (
2617  id_1 int(5) NOT NULL,
2618  id_2 int(5) NOT NULL
2619 );
2620 
2621 INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
2622 
2623 INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
2624 
2625 INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
2626 
2627 # subselect returns 0 rows
2628 
2629 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2630 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2631 
2632 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2633 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2634 
2635 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2636 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2637 
2638 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2639 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2640 
2641 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2642 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
2643 
2644 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2645 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
2646 
2647 insert INTO t1xt2 VALUES (1, 12);
2648 
2649 # subselect returns 1 row
2650 
2651 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2652 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2653 
2654 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2655 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2656 
2657 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2658 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2659 
2660 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2661 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2662 
2663 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2664 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2665 
2666 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2667 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2668 
2669 insert INTO t1xt2 VALUES (2, 12);
2670 
2671 # subselect returns more than 1 row
2672 
2673 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2674 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2675 
2676 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2677 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2678 
2679 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2680 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2681 
2682 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2683 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2684 
2685 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2686 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2687 
2688 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2689 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2690 
2691 DROP TABLE t1;
2692 DROP TABLE t2;
2693 DROP TABLE t1xt2;
2694 
2695 
2696 #
2697 # Bug#26728 derived table with concatanation of literals in select list
2698 #
2699 
2700 CREATE TABLE t1 (a int);
2701 INSERT INTO t1 VALUES (3), (1), (2);
2702 
2703 SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
2704 SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
2705 
2706 DROP table t1;
2707 
2708 
2709 #
2710 # Bug#27257 COUNT(*) aggregated in outer query
2711 #
2712 
2713 CREATE TABLE t1 (a int, b int);
2714 CREATE TABLE t2 (m int, n int);
2715 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2716 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2717 
2718 SELECT COUNT(*), a,
2719  (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
2720  FROM t1 GROUP BY a;
2721 
2722 SELECT COUNT(*), a,
2723  (SELECT MIN(m) FROM t2 WHERE m = count(*))
2724  FROM t1 GROUP BY a;
2725 
2726 SELECT COUNT(*), a
2727  FROM t1 GROUP BY a
2728  HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
2729 
2730 DROP TABLE t1,t2;
2731 
2732 
2733 #
2734 # Bug#27229 GROUP_CONCAT in subselect with COUNT() as an argument
2735 #
2736 
2737 CREATE TABLE t1 (a int, b int);
2738 CREATE TABLE t2 (m int, n int);
2739 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2740 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2741 
2742 SELECT COUNT(*) c, a,
2743  (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
2744  FROM t1 GROUP BY a;
2745 
2746 SELECT COUNT(*) c, a,
2747  (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
2748  FROM t1 GROUP BY a;
2749 
2750 DROP table t1,t2;
2751 
2752 
2753 #
2754 # Bug#27321 Wrong subquery result in a grouping select
2755 #
2756 CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
2757 INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
2758 (1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
2759 (3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
2760 
2761 SELECT a, MAX(b),
2762  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
2763  FROM t1 GROUP BY a;
2764 SELECT a x, MAX(b),
2765  (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
2766  FROM t1 GROUP BY a;
2767 SELECT a, AVG(b),
2768  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
2769  FROM t1 WHERE t1.d=0 GROUP BY a;
2770 
2771 SELECT tt.a,
2772  (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2773  LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2774  FROM t1 as tt;
2775 
2776 SELECT tt.a,
2777  (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2778  LIMIT 1)
2779  FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2780  FROM t1 as tt GROUP BY tt.a;
2781 
2782 SELECT tt.a, MAX(
2783  (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2784  LIMIT 1)
2785  FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
2786  FROM t1 as tt GROUP BY tt.a;
2787 
2788 DROP TABLE t1;
2789 
2790 
2791 #
2792 # Bug#27348 SET FUNCTION used in a subquery from WHERE condition
2793 #
2794 
2795 CREATE TABLE t1 (a int, b int);
2796 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
2797 
2798 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2799 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2800 
2801 SELECT a FROM t1 t0
2802  WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2803 
2804 SET @@sql_mode='ansi';
2805 --error ER_INVALID_GROUP_FUNC_USE
2806 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2807 --error ER_INVALID_GROUP_FUNC_USE
2808 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2809 
2810 --error ER_INVALID_GROUP_FUNC_USE
2811 SELECT a FROM t1 t0
2812  WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2813 
2814 SET @@sql_mode=default;
2815 
2816 DROP TABLE t1;
2817 
2818 
2819 #
2820 # Bug#27363 nested aggregates in outer, subquery / sum(select count(outer))
2821 #
2822 CREATE TABLE t1 (a INT); INSERT INTO t1 values (1),(1),(1),(1);
2823 CREATE TABLE t2 (x INT); INSERT INTO t1 values (1000),(1001),(1002);
2824 
2825 --error ER_INVALID_GROUP_FUNC_USE
2826 SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
2827 --error ER_INVALID_GROUP_FUNC_USE
2828 SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
2829 SELECT COUNT(1) FROM DUAL;
2830 
2831 --error ER_INVALID_GROUP_FUNC_USE
2832 SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
2833 
2834 --error ER_INVALID_GROUP_FUNC_USE
2835 SELECT
2836  SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
2837 FROM t1;
2838 
2839 --error ER_INVALID_GROUP_FUNC_USE
2840 SELECT t1.a as XXA,
2841  SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
2842 FROM t1;
2843 
2844 DROP TABLE t1,t2;
2845 
2846 
2847 #
2848 # Bug#27807 Server crash when executing subquery with EXPLAIN
2849 #
2850 CREATE TABLE t1 (a int, b int, KEY (a));
2851 INSERT INTO t1 VALUES (1,1),(2,1);
2852 EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
2853 DROP TABLE t1;
2854 
2855 
2856 #
2857 # Bug#28377 grouping query with a correlated subquery in WHERE condition
2858 #
2859 
2860 CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
2861 INSERT INTO t1 VALUES
2862  (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
2863 CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
2864 INSERT INTO t2 VALUES (7), (5), (1), (3);
2865 
2866 SELECT id, st FROM t1
2867  WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2868 SELECT id, st FROM t1
2869  WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2870  GROUP BY id;
2871 
2872 SELECT id, st FROM t1
2873  WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2874 SELECT id, st FROM t1
2875  WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2876  GROUP BY id;
2877 
2878 DROP TABLE t1,t2;
2879 
2880 
2881 #
2882 # Bug#28728 crash with EXPLAIN EXTENDED for a query with a derived table
2883 # over a grouping subselect
2884 #
2885 
2886 CREATE TABLE t1 (a int);
2887 
2888 INSERT INTO t1 VALUES (1), (2);
2889 
2890 EXPLAIN EXTENDED
2891 SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
2892 
2893 DROP TABLE t1;
2894 
2895 
2896 #
2897 # Bug#28811 crash for query containing subquery with ORDER BY and LIMIT 1
2898 #
2899 
2900 CREATE TABLE t1 (
2901  a varchar(255) default NULL,
2902  b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2903  INDEX idx(a,b)
2904 );
2905 CREATE TABLE t2 (
2906  a varchar(255) default NULL
2907 );
2908 
2909 INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
2910 INSERT INTO t1 SELECT * FROM t1;
2911 INSERT INTO t1 SELECT * FROM t1;
2912 INSERT INTO t1 SELECT * FROM t1;
2913 INSERT INTO t1 SELECT * FROM t1;
2914 INSERT INTO t1 SELECT * FROM t1;
2915 INSERT INTO t1 SELECT * FROM t1;
2916 INSERT INTO t1 SELECT * FROM t1;
2917 INSERT INTO t1 SELECT * FROM t1;
2918 INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
2919 INSERT INTO `t2` VALUES ('abcdefghijk');
2920 INSERT INTO `t2` VALUES ('asdf');
2921 
2922 SET session sort_buffer_size=8192;
2923 
2924 SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
2925 
2926 DROP TABLE t1,t2;
2927 
2928 
2929 #
2930 # Bug#27333 subquery grouped for aggregate of outer query / no aggregate
2931 # of subquery
2932 #
2933 CREATE TABLE t1 (a INTEGER, b INTEGER);
2934 CREATE TABLE t2 (x INTEGER);
2935 INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
2936 INSERT INTO t2 VALUES (1), (2);
2937 
2938 # wasn't failing, but should
2939 --error ER_SUBQUERY_NO_1_ROW
2940 SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
2941 
2942 # fails as it should
2943 --error ER_SUBQUERY_NO_1_ROW
2944 SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
2945 
2946 SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
2947 DROP TABLE t1,t2;
2948 
2949 # second test case from 27333
2950 CREATE TABLE t1 (a INT, b INT);
2951 INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
2952 
2953 # returns no rows, when it should
2954 SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
2955 AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
2956 GROUP BY a1.a;
2957 DROP TABLE t1;
2958 
2959 #test cases from 29297
2960 CREATE TABLE t1 (a INT);
2961 CREATE TABLE t2 (a INT);
2962 INSERT INTO t1 VALUES (1),(2);
2963 INSERT INTO t2 VALUES (1),(2);
2964 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
2965 --error ER_SUBQUERY_NO_1_ROW
2966 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
2967 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
2968 DROP TABLE t1,t2;
2969 
2970 #
2971 # Bug#31884 Assertion + crash in subquery in the SELECT clause.
2972 #
2973 
2974 CREATE TABLE t1 (a1 INT, a2 INT);
2975 CREATE TABLE t2 (b1 INT, b2 INT);
2976 
2977 INSERT INTO t1 VALUES (100, 200);
2978 INSERT INTO t1 VALUES (101, 201);
2979 INSERT INTO t2 VALUES (101, 201);
2980 INSERT INTO t2 VALUES (103, 203);
2981 
2982 SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
2983 DROP TABLE t1, t2;
2984 
2985 #
2986 # Bug#28076 inconsistent binary/varbinary comparison
2987 #
2988 
2989 CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
2990 INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
2991 
2992 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
2993 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
2994 
2995 CREATE INDEX I1 ON t1 (s1);
2996 CREATE INDEX I2 ON t1 (s2);
2997 
2998 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
2999 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3000 
3001 TRUNCATE t1;
3002 INSERT INTO t1 VALUES (0x41,0x41);
3003 SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
3004 
3005 DROP TABLE t1;
3006 
3007 CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
3008 CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
3009 CREATE TABLE t3 (a3 BINARY(2) default '0');
3010 INSERT INTO t1 VALUES (1),(2),(3),(4);
3011 INSERT INTO t2 VALUES (1),(2),(3);
3012 INSERT INTO t3 VALUES (1),(2),(3);
3013 SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
3014 SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
3015 DROP TABLE t1,t2,t3;
3016 
3017 CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
3018 CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
3019 CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
3020 INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
3021 INSERT INTO t2 VALUES (2), (3), (4), (5);
3022 INSERT INTO t3 VALUES (10), (20), (30);
3023 SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
3024 SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
3025 DROP TABLE t1, t2, t3;
3026 
3027 
3028 #
3029 # Bug#32400 Complex SELECT query returns correct result only on some occasions
3030 #
3031 
3032 CREATE TABLE t1(a INT, b INT);
3033 INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
3034 
3035 --error ER_BAD_FIELD_ERROR
3036 EXPLAIN
3037 SELECT a AS out_a, MIN(b) FROM t1
3038 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3039 GROUP BY a;
3040 
3041 --error ER_BAD_FIELD_ERROR
3042 SELECT a AS out_a, MIN(b) FROM t1
3043 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3044 GROUP BY a;
3045 
3046 EXPLAIN
3047 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3048 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3049 GROUP BY a;
3050 
3051 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3052 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3053 GROUP BY a;
3054 
3055 DROP TABLE t1;
3056 
3057 
3058 #
3059 # Bug#32036 EXISTS within a WHERE clause with a UNION crashes MySQL 5.122
3060 #
3061 
3062 CREATE TABLE t1 (a INT);
3063 CREATE TABLE t2 (a INT);
3064 
3065 INSERT INTO t1 VALUES (1),(2);
3066 INSERT INTO t2 VALUES (1),(2);
3067 
3068 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3069 EXPLAIN EXTENDED
3070 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3071 
3072 
3073 EXPLAIN EXTENDED
3074 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
3075  (SELECT 1 FROM t2 WHERE t1.a = t2.a));
3076 
3077 DROP TABLE t1,t2;
3078 
3079 
3080 #
3081 # Bug#33675 Usage of an uninitialized memory by filesort in a subquery
3082 # caused server crash.
3083 #
3084 create table t0(a int);
3085 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3086 create table t1(f11 int, f12 int);
3087 create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
3088 insert into t1 values(1,1),(2,2), (3, 3);
3089 --disable_warnings
3090 insert into t2
3091 select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
3092 from t0 A, t0 B, t0 C, t0 D;
3093 --enable_warnings
3094 set session sort_buffer_size= 33*1024;
3095 select count(*) from t1 where f12 =
3096 (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
3097 
3098 drop table t0,t1,t2;
3099 
3100 
3101 #
3102 # Bug#33794 "MySQL crashes executing specific query on specific dump"
3103 #
3104 CREATE TABLE t4 (
3105  f7 varchar(32) collate utf8_bin NOT NULL default '',
3106  f10 varchar(32) collate utf8_bin default NULL,
3107  PRIMARY KEY (f7)
3108 );
3109 INSERT INTO t4 VALUES(1,1), (2,null);
3110 
3111 CREATE TABLE t2 (
3112  f4 varchar(32) collate utf8_bin NOT NULL default '',
3113  f2 varchar(50) collate utf8_bin default NULL,
3114  f3 varchar(10) collate utf8_bin default NULL,
3115  PRIMARY KEY (f4),
3116  UNIQUE KEY uk1 (f2)
3117 );
3118 INSERT INTO t2 VALUES(1,1,null), (2,2,null);
3119 
3120 CREATE TABLE t1 (
3121  f8 varchar(32) collate utf8_bin NOT NULL default '',
3122  f1 varchar(10) collate utf8_bin default NULL,
3123  f9 varchar(32) collate utf8_bin default NULL,
3124  PRIMARY KEY (f8)
3125 );
3126 INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
3127 
3128 CREATE TABLE t3 (
3129  f6 varchar(32) collate utf8_bin NOT NULL default '',
3130  f5 varchar(50) collate utf8_bin default NULL,
3131  PRIMARY KEY (f6)
3132 );
3133 INSERT INTO t3 VALUES (1,null), (2,null);
3134 
3135 SELECT
3136  IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
3137  IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
3138  SUM(
3139  IF(
3140  (SELECT VPC.f2
3141  FROM t2 VPC, t4 a2, t2 a3
3142  WHERE
3143  VPC.f4 = a2.f10 AND a3.f2 = a4
3144  LIMIT 1) IS NULL,
3145  0,
3146  t3.f5
3147  )
3148  ) AS a6
3149 FROM
3150  t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
3151 GROUP BY a4;
3152 
3153 DROP TABLE t1, t2, t3, t4;
3154 
3155 
3156 #
3157 # Bug#36139 "float, zerofill, crash with subquery"
3158 #
3159 create table t1 (a float(5,4) zerofill);
3160 create table t2 (a float(5,4),b float(2,0));
3161 
3162 select t1.a from t1 where
3163  t1.a= (select b from t2 limit 1) and not
3164  t1.a= (select a from t2 limit 1) ;
3165 
3166 drop table t1, t2;
3167 
3168 
3169 --echo #
3170 --echo # Bug#45061: Incorrectly market field caused wrong result.
3171 --echo #
3172 CREATE TABLE `C` (
3173  `int_nokey` int(11) NOT NULL,
3174  `int_key` int(11) NOT NULL,
3175  KEY `int_key` (`int_key`)
3176 );
3177 
3178 INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
3179 (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
3180 (5,2), (1,8), (7,0), (0,9), (9,5);
3181 
3182 --disable_warnings
3183 SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3184 EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3185 --enable_warnings
3186 
3187 DROP TABLE C;
3188 --echo # End of test for bug#45061.
3189 
3190 
3191 --echo #
3192 --echo # Bug #46749: Segfault in add_key_fields() with outer subquery level
3193 --echo # field references
3194 --echo #
3195 
3196 CREATE TABLE t1 (
3197  a int,
3198  b int,
3199  UNIQUE (a), KEY (b)
3200 );
3201 INSERT INTO t1 VALUES (1,1), (2,1);
3202 
3203 CREATE TABLE st1 like t1;
3204 INSERT INTO st1 VALUES (1,1), (2,1);
3205 
3206 CREATE TABLE st2 like t1;
3207 INSERT INTO st2 VALUES (1,1), (2,1);
3208 
3209 # should have "impossible where"
3210 EXPLAIN
3211 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3212 FROM t1
3213 WHERE a = 230;
3214 
3215 # should not crash
3216 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3217 FROM t1
3218 WHERE a = 230;
3219 
3220 DROP TABLE t1, st1, st2;
3221 
3222 --echo #
3223 --echo # Bug #48709: Assertion failed in sql_select.cc:11782:
3224 --echo # int join_read_key(JOIN_TAB*)
3225 --echo #
3226 
3227 CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
3228 INSERT INTO t1 VALUES (10,1), (14,1);
3229 
3230 CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
3231 INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
3232 
3233 --echo # should have eq_ref for t1, unless subquery materialization is used
3234 --replace_column 1 x 2 x 5 x 6 x 7 x 8 x 9 x 10 x
3235 EXPLAIN
3236 SELECT * FROM t2 outr
3237 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
3238 ORDER BY outr.pk;
3239 
3240 --echo # should not crash on debug binaries
3241 SELECT * FROM t2 outr
3242 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
3243 ORDER BY outr.pk;
3244 
3245 DROP TABLE t1,t2;
3246 
3247 #
3248 # 1. Subquery with GROUP/HAVING
3249 #
3250 create table t1 (oref int, grp int, ie int) ;
3251 insert into t1 (oref, grp, ie) values
3252  (1, 1, 1),
3253  (1, 1, 1),
3254  (1, 2, NULL),
3255 
3256  (2, 1, 3),
3257 
3258  (3, 1, 4),
3259  (3, 2, NULL);
3260 
3261 # Ok, for
3262 # select max(ie) from t1 where oref=PARAM group by grp
3263 # we'll have:
3264 # PARAM subquery result
3265 # 1 -> {(1), (NULL)} matching + NULL
3266 # 2 -> {(3)} non-matching
3267 # 3 -> {(3), (NULL)} non-matching + NULL
3268 # 4 -> {} empty set
3269 
3270 create table t2 (oref int, a int);
3271 insert into t2 values
3272  (1, 1),
3273  (2, 2),
3274  (3, 3),
3275  (4, NULL),
3276  (2, NULL);
3277 
3278 # true, false, null, false, null
3279 select a, oref, a in (select max(ie)
3280  from t1 where oref=t2.oref group by grp) Z from t2;
3281 
3282 # This must have a trigcond
3283 explain extended
3284 select a, oref, a in (select max(ie)
3285  from t1 where oref=t2.oref group by grp) Z from t2;
3286 
3287 # This must not have a trigcond:
3288 explain extended
3289 select a, oref from t2
3290 where a in (select max(ie) from t1 where oref=t2.oref group by grp);
3291 select a, oref, a in (
3292  select max(ie) from t1 where oref=t2.oref group by grp union
3293  select max(ie) from t1 where oref=t2.oref group by grp
3294  ) Z from t2;
3295 
3296 # Non-correlated subquery, 2 NULL evaluations
3297 create table t3 (a int);
3298 insert into t3 values (NULL), (NULL);
3299 flush status;
3300 select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
3301 show status like 'Handler_read_rnd_next';
3302 select ' ^ This must show 11' Z;
3303 
3304 # This must show trigcond:
3305 explain extended select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
3306 
3307 drop table t1, t2, t3;
3308 
3309 #
3310 # 2. Subquery handled with 'index_subquery':
3311 #
3312 create table t1 (a int, oref int, key(a));
3313 insert into t1 values
3314  (1, 1),
3315  (1, NULL),
3316  (2, 3),
3317  (2, NULL),
3318  (3, NULL);
3319 
3320 create table t2 (a int, oref int);
3321 insert into t2 values (1, 1), (2,2), (NULL, 3), (NULL, 4);
3322 
3323 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3324 
3325 # The next explain shows "using index" but that is just incorrect display
3326 # (there is a bug filed about this).
3327 explain extended
3328 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3329 
3330 flush status;
3331 select oref, a from t2 where a in (select a from t1 where oref=t2.oref);
3332 # This will only show access to t2:
3333 show status like '%Handler_read_rnd_next';
3334 
3335 # Check that repeated NULL-scans are not cached (subq. is not correlated):
3336 delete from t2;
3337 insert into t2 values (NULL, 0),(NULL, 0), (NULL, 0), (NULL, 0);
3338 
3339 flush status;
3340 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3341 show status like '%Handler_read%';
3342 select 'No key lookups, seq reads: 29= 5 reads from t2 + 4 * 6 reads from t1.' Z;
3343 
3344 drop table t1, t2;
3345 
3346 #
3347 # 3. Subquery handled with 'unique_index_subquery':
3348 #
3349 create table t1 (a int, b int, primary key (a));
3350 insert into t1 values (1,1), (3,1),(100,1);
3351 
3352 create table t2 (a int, b int);
3353 insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
3354 
3355 select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
3356 
3357 drop table t1, t2;
3358 
3359 #
3360 # 4. Subquery that is a join, with ref access
3361 #
3362 create table t1 (a int, b int, key(a));
3363 insert into t1 values
3364  (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
3365 
3366 create table t2 like t1;
3367 insert into t2 select * from t1;
3368 update t2 set b=1;
3369 
3370 create table t3 (a int, oref int);
3371 insert into t3 values (1, 1), (NULL,1), (NULL,0);
3372 select a, oref,
3373  t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3374 from t3;
3375 
3376 # This must have trigcond in WHERE and HAVING:
3377 explain extended
3378 select a, oref,
3379  t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3380 from t3;
3381 
3382 drop table t1, t2, t3;
3383 
3384 
3385 #
3386 # BUG#24085: Wrong query result for "NULL IN (SELECT ... UNION SELECT ...)"
3387 #
3388 
3389 # case 1: NULL IN (SELECT not_null_val FROM ...) w/o HAVING/GROUP-BY/etc
3390 create table t1 (a int NOT NULL, b int NOT NULL, key(a));
3391 insert into t1 values
3392  (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
3393 
3394 create table t2 like t1;
3395 insert into t2 select * from t1;
3396 update t2 set b=1;
3397 
3398 create table t3 (a int, oref int);
3399 insert into t3 values (1, 1), (NULL,1), (NULL,0);
3400 select a, oref,
3401  t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3402 from t3;
3403 
3404 --echo This must show a trig_cond:
3405 explain extended
3406 select a, oref,
3407  t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3408 from t3;
3409 drop table t1,t2,t3;
3410 
3411 
3412 # case 2: NULL IN (SELECT not_null_val FROM) where SELECT has GROUP BY
3413 create table t1 (oref int, grp int);
3414 insert into t1 (oref, grp) values
3415  (1, 1),
3416  (1, 1);
3417 
3418 # Ok, for
3419 # select count(*) from t1 group by grp having grp=PARAM
3420 # we'll have:
3421 # PARAM subuqery result
3422 # 1 -> {(2)}
3423 # 2 -> {} - empty set
3424 create table t2 (oref int, a int);
3425 insert into t2 values
3426  (1, NULL),
3427  (2, NULL);
3428 
3429 select a, oref,
3430  a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
3431 
3432 --echo This must show a trig_cond:
3433 explain extended
3434 select a, oref,
3435  a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
3436 
3437 drop table t1, t2;
3438 
3439 create table t1 (a int, b int, primary key (a));
3440 insert into t1 values (1,1), (3,1),(100,1);
3441 create table t2 (a int, b int);
3442 insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
3443 
3444 select a,b, a in (select a from t1 where t1.b = t2.b union select a from
3445 t1 where t1.b = t2.b) Z from t2 ;
3446 select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
3447 drop table t1, t2;
3448 
3449 
3450 #
3451 # BUG#24127: Incorrect results of row-based subqueries with NULLs on the left side.
3452 #
3453 create table t3 (a int);
3454 insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3455 create table t2 (a int, b int, oref int);
3456 insert into t2 values (NULL,1, 100), (NULL,2, 100);
3457 
3458 create table t1 (a int, b int, c int, key(a,b));
3459 insert into t1 select 2*A, 2*A, 100 from t3;
3460 
3461 # First test index subquery engine
3462 explain extended select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
3463 select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
3464 
3465 # Then check that we do turn off 'ref' scans in the subquery
3466 create table t4 (x int);
3467 insert into t4 select A.a + 10*B.a from t1 A, t1 B;
3468 explain extended
3469  select a,b, oref,
3470  (a,b) in (select a,b from t1,t4 where c=t2.oref) Z
3471  from t2;
3472 select a,b, oref,
3473  (a,b) in (select a,b from t1,t4 where c=t2.oref) Z
3474 from t2;
3475 
3476 drop table t1,t2,t3,t4;
3477 
3478 # More tests for tricky multi-column cases, where some of pushed-down
3479 # equalities are used for index lookups and some arent.
3480 create table t1 (oref char(4), grp int, ie1 int, ie2 int);
3481 insert into t1 (oref, grp, ie1, ie2) values
3482  ('aa', 10, 2, 1),
3483  ('aa', 10, 1, 1),
3484  ('aa', 20, 2, 1),
3485  ('bb', 10, 3, 1),
3486  ('cc', 10, 4, 2),
3487  ('cc', 20, 3, 2),
3488 
3489  ('ee', 10, 2, 1),
3490  ('ee', 10, 1, 2),
3491 
3492  ('ff', 20, 2, 2),
3493  ('ff', 20, 1, 2);
3494 create table t2 (oref char(4), a int, b int);
3495 insert into t2 values
3496  ('ee', NULL, 1),
3497  ('bb', 2, 1),
3498  ('ff', 2, 2),
3499  ('cc', 3, NULL),
3500  ('bb', NULL, NULL),
3501  ('aa', 1, 1),
3502  ('dd', 1, NULL);
3503 alter table t1 add index idx(ie1,ie2);
3504 
3505 # cc 3 NULL NULL
3506 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 ;
3507 insert into t2 values ('new1', 10,10);
3508 insert into t1 values ('new1', 1234, 10, NULL);
3509 # new1, 10, 10, NULL,
3510 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;
3511 explain extended
3512 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;
3513 drop table t1, t2;
3514 
3515 # Now test different column types:
3516 create table t1 (oref char(4), grp int, ie int);
3517 insert into t1 (oref, grp, ie) values
3518  ('aa', 10, 2),
3519  ('aa', 10, 1),
3520  ('aa', 20, NULL),
3521 
3522  ('bb', 10, 3),
3523 
3524  ('cc', 10, 4),
3525  ('cc', 20, NULL),
3526 
3527  ('ee', 10, NULL),
3528  ('ee', 10, NULL),
3529 
3530  ('ff', 20, 2),
3531  ('ff', 20, 1);
3532 
3533 create table t2 (oref char(4), a int);
3534 insert into t2 values
3535  ('ee', NULL),
3536  ('bb', 2),
3537  ('ff', 2),
3538  ('cc', 3),
3539  ('aa', 1),
3540  ('dd', NULL),
3541  ('bb', NULL);
3542 
3543 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3544 
3545 --sorted_result
3546 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3547 
3548 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3549 
3550 
3551 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
3552 
3553 select oref, a from t2 where
3554  a in (select min(ie) from t1 where oref=t2.oref group by grp);
3555 
3556 select oref, a from t2 where
3557  a not in (select min(ie) from t1 where oref=t2.oref group by grp);
3558 
3559 #
3560 update t1 set ie=3 where oref='ff' and ie=1;
3561 
3562 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
3563 grp) Z from t2;
3564 
3565 
3566 select oref, a from t2 where a in (select min(ie) from t1 where
3567 oref=t2.oref group by grp);
3568 
3569 select oref, a from t2 where a not in (select min(ie) from t1 where
3570 oref=t2.oref group by grp);
3571 
3572 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
3573 grp having min(ie) > 1) Z from t2;
3574 
3575 select oref, a from t2 where a in (select min(ie) from t1 where
3576 oref=t2.oref group by grp having min(ie) > 1);
3577 
3578 select oref, a from t2 where a not in (select min(ie) from t1 where
3579 oref=t2.oref group by grp having min(ie) > 1);
3580 
3581 #
3582 alter table t1 add index idx(ie);
3583 
3584 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3585 
3586 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3587 
3588 --sorted_result
3589 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3590 
3591 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3592 
3593 
3594 alter table t1 drop index idx;
3595 alter table t1 add index idx(oref,ie);
3596 
3597 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3598 
3599 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3600 
3601 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3602 
3603 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3604 
3605 explain
3606 select oref, a,
3607  a in (select min(ie) from t1 where oref=t2.oref
3608  group by grp having min(ie) > 1) Z
3609 from t2;
3610 
3611 select oref, a,
3612  a in (select min(ie) from t1 where oref=t2.oref
3613  group by grp having min(ie) > 1) Z
3614 from t2;
3615 
3616 select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref
3617  group by grp having min(ie) > 1);
3618 
3619 select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref
3620  group by grp having min(ie) > 1);
3621 
3622 drop table t1,t2;
3623 
3624 create table t1 (oref char(4), grp int, ie1 int, ie2 int);
3625 insert into t1 (oref, grp, ie1, ie2) values
3626  ('aa', 10, 2, 1),
3627  ('aa', 10, 1, 1),
3628  ('aa', 20, 2, 1),
3629 
3630  ('bb', 10, 3, 1),
3631 
3632  ('cc', 10, 4, 2),
3633  ('cc', 20, 3, 2),
3634 
3635  ('ee', 10, 2, 1),
3636  ('ee', 10, 1, 2),
3637 
3638  ('ff', 20, 2, 2),
3639  ('ff', 20, 1, 2);
3640 
3641 create table t2 (oref char(4), a int, b int);
3642 insert into t2 values
3643  ('ee', NULL, 1),
3644  ('bb', 2, 1),
3645  ('ff', 2, 2),
3646  ('cc', 3, NULL),
3647  ('bb', NULL, NULL),
3648  ('aa', 1, 1),
3649  ('dd', 1, NULL);
3650 
3651 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3652 
3653 --sorted_result
3654 select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
3655 
3656 select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
3657 
3658 select oref, a, b,
3659  (a,b) in (select min(ie1),max(ie2) from t1
3660  where oref=t2.oref group by grp) Z
3661 from t2;
3662 
3663 select oref, a, b from t2 where
3664  (a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
3665 
3666 select oref, a, b from t2 where
3667  (a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
3668 
3669 alter table t1 add index idx(ie1,ie2);
3670 
3671 explain select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3672 
3673 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3674 
3675 --sorted_result
3676 select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
3677 
3678 select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
3679 
3680 explain extended
3681 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3682 
3683 drop table t1,t2;
3684 
3685 create table t1 (oref char(4), grp int, ie int primary key);
3686 insert into t1 (oref, grp, ie) values
3687  ('aa', 10, 2),
3688  ('aa', 10, 1),
3689 
3690  ('bb', 10, 3),
3691 
3692  ('cc', 10, 4),
3693  ('cc', 20, 5),
3694  ('cc', 10, 6);
3695 
3696 create table t2 (oref char(4), a int);
3697 insert into t2 values
3698  ('ee', NULL),
3699  ('bb', 2),
3700  ('cc', 5),
3701  ('cc', 2),
3702  ('cc', NULL),
3703  ('aa', 1),
3704  ('bb', NULL);
3705 
3706 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3707 
3708 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3709 
3710 --sorted_result
3711 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3712 
3713 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3714 
3715 explain
3716 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
3717 
3718 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
3719 
3720 drop table t1,t2;
3721 
3722 #
3723 # BUG#24420: row-based IN suqueries with aggregation when the left operand
3724 # of the subquery predicate may contain NULL values
3725 #
3726 
3727 create table t1 (a int, b int);
3728 insert into t1 values (0,0), (2,2), (3,3);
3729 create table t2 (a int, b int);
3730 insert into t2 values (1,1), (3,3);
3731 
3732 select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
3733 
3734 insert into t2 values (NULL,4);
3735 select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
3736 
3737 drop table t1,t2;
3738 
3739 #
3740 # Bug #24484: Aggregate function used in column list subquery gives erroneous
3741 # error
3742 #
3743 CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
3744 INSERT INTO t1 VALUES (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
3745  (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'),(3,3,'j'), (3,2,'k'), (3,1,'l'),
3746  (1,9,'m');
3747 CREATE TABLE t2 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
3748 INSERT INTO t2 SELECT * FROM t1;
3749 
3750 # Gives error, but should work since it is (a, b) is the PK so only one
3751 # given match possible
3752 SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
3753  as test FROM t1 GROUP BY a;
3754 SELECT * FROM t1 GROUP by t1.a
3755  HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
3756  HAVING MAX(t2.b+t1.a) < 10));
3757 
3758 SELECT a,b,c FROM t1 WHERE b in (9,3,4) ORDER BY b,c;
3759 
3760 SELECT a, MAX(b),
3761  (SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
3762  LIMIT 1)
3763  as cnt,
3764  (SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
3765  as t_b,
3766  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
3767  as t_b,
3768  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) ORDER BY t.c LIMIT 1)
3769  as t_b
3770  FROM t1 GROUP BY a;
3771 
3772 SELECT a, MAX(b),
3773  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
3774  FROM t1 GROUP BY a;
3775 
3776 
3777 DROP TABLE t1, t2;
3778 
3779 
3780 #
3781 # Bug #27870: crash of an equijoin query with WHERE condition containing
3782 # a subquery predicate of the form <join attr> NOT IN (SELECT ...)
3783 #
3784 
3785 CREATE TABLE t1 (a int);
3786 CREATE TABLE t2 (b int, PRIMARY KEY(b));
3787 INSERT INTO t1 VALUES (1), (NULL), (4);
3788 INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6);
3789 
3790 EXPLAIN EXTENDED
3791 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
3792 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
3793 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4));
3794 
3795 DROP TABLE t1,t2;
3796 
3797 #
3798 # Bug #28375: crash for NOT IN subquery predicate when left operand becomes NULL
3799 #
3800 
3801 CREATE TABLE t1 (id int);
3802 CREATE TABLE t2 (id int PRIMARY KEY);
3803 CREATE TABLE t3 (id int PRIMARY KEY, name varchar(10));
3804 INSERT INTO t1 VALUES (2), (NULL), (3), (1);
3805 INSERT INTO t2 VALUES (234), (345), (457);
3806 INSERT INTO t3 VALUES (222,'bbb'), (333,'ccc'), (111,'aaa');
3807 
3808 EXPLAIN
3809 SELECT * FROM t1
3810  WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
3811  WHERE t3.name='xxx' AND t2.id=t3.id);
3812 SELECT * FROM t1
3813  WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
3814  WHERE t3.name='xxx' AND t2.id=t3.id);
3815 
3816 SELECT (t1.id IN (SELECT t2.id FROM t2,t3
3817  WHERE t3.name='xxx' AND t2.id=t3.id)) AS x
3818  FROM t1;
3819 
3820 DROP TABLE t1,t2,t3;
3821 
3822 #
3823 # Bug #22855: Optimizer doesn't rewrite NOT IN subselects to a correlated
3824 # subquery
3825 #
3826 CREATE TABLE t1 (a INT NOT NULL);
3827 INSERT INTO t1 VALUES (1),(-1), (65),(66);
3828 
3829 CREATE TABLE t2 (a INT UNSIGNED NOT NULL PRIMARY KEY);
3830 INSERT INTO t2 VALUES (65),(66);
3831 
3832 SELECT a FROM t1 WHERE a NOT IN (65,66);
3833 SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
3834 EXPLAIN SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
3835 
3836 DROP TABLE t1, t2;
3837 
3838 #
3839 # Bug #34763: item_subselect.cc:1235:Item_in_subselect::row_value_transformer:
3840 # Assertion failed, unexpected error message:
3841 # ERROR 1247 (42S22): Reference '<list ref>' not supported (forward
3842 # reference in item list)
3843 #
3844 CREATE TABLE t1 (a INT);
3845 INSERT INTO t1 VALUES(1);
3846 
3847 CREATE TABLE t2 (placeholder CHAR(11));
3848 INSERT INTO t2 VALUES("placeholder");
3849 
3850 SELECT ROW(1, 2) IN (SELECT t1.a, 2) FROM t1 GROUP BY t1.a;
3851 SELECT ROW(1, 2) IN (SELECT t1.a, 2 FROM t2) FROM t1 GROUP BY t1.a;
3852 
3853 DROP TABLE t1, t2;
3854 
3855 #
3856 # Bug #36005: crash in subselect with single row
3857 # (subselect_single_select_engine::exec)
3858 #
3859 
3860 CREATE TABLE t1 (a INT);
3861 INSERT INTO t1 VALUES (1),(2),(3);
3862 CREATE TABLE t2 SELECT * FROM t1;
3863 
3864 SELECT 1 FROM t1 WHERE t1.a NOT IN (SELECT 1 FROM t1, t2 WHERE 0);
3865 
3866 DROP TABLE t1, t2;
3867 
3868 #
3869 # Bug 2198
3870 #
3871 
3872 create table t1 (a int, b decimal(13, 3));
3873 insert into t1 values (1, 0.123);
3874 select a, (select max(b) from t1) into outfile "subselect.out.file.1" from t1;
3875 delete from t1;
3876 load data infile "subselect.out.file.1" into table t1;
3877 select * from t1;
3878 drop table t1;
3879 let $datadir=`select @@datadir`;
3880 --remove_file $datadir/test/subselect.out.file.1
3881 
3882 #
3883 # Bug #37894: Assertion in init_read_record_seq in handler.h line 1444
3884 #
3885 
3886 CREATE TABLE t1 (
3887  pk INT PRIMARY KEY,
3888  int_key INT,
3889  varchar_key VARCHAR(5) UNIQUE,
3890  varchar_nokey VARCHAR(5)
3891 );
3892 INSERT INTO t1 VALUES (9, 7,NULL,NULL), (10,8,'p' ,'p');
3893 
3894 SELECT varchar_nokey
3895 FROM t1
3896 WHERE NULL NOT IN (
3897  SELECT INNR.pk FROM t1 AS INNR2
3898  LEFT JOIN t1 AS INNR ON ( INNR2.int_key = INNR.int_key )
3899  WHERE INNR.varchar_key > 'n{'
3900 );
3901 
3902 DROP TABLE t1;
3903 
3904 #
3905 # Bug #39069: <row constructor> IN <table-subquery> seriously messed up
3906 #
3907 
3908 CREATE TABLE t1 (a INT);
3909 INSERT INTO t1 VALUES (1), (2), (11);
3910 
3911 --echo # 2nd and 3rd columns should be same
3912 SELECT a, ROW(11, 12) = (SELECT a, 22), ROW(11, 12) IN (SELECT a, 22) FROM t1 GROUP BY t1.a;
3913 SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1 GROUP BY t1.a;
3914 SELECT a, ROW(11, 12) = (SELECT a, 22), ROW(11, 12) IN (SELECT a, 22) FROM t1;
3915 SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1;
3916 
3917 # The x alias is used below to workaround bug #40674.
3918 # Regression tests for sum function on outer column in subselect from dual:
3919 SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 22), ROW(11, 12) IN (SELECT MAX(x), 22) FROM t1;
3920 SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 12), ROW(11, 12) IN (SELECT MAX(x), 12) FROM t1;
3921 
3922 DROP TABLE t1;
3923 
3924 --echo # both columns should be same
3925 SELECT ROW(1,2) = (SELECT NULL, NULL), ROW(1,2) IN (SELECT NULL, NULL);
3926 SELECT ROW(1,2) = (SELECT 1, NULL), ROW(1,2) IN (SELECT 1, NULL);
3927 SELECT ROW(1,2) = (SELECT NULL, 2), ROW(1,2) IN (SELECT NULL, 2);
3928 SELECT ROW(1,2) = (SELECT NULL, 1), ROW(1,2) IN (SELECT NULL, 1);
3929 SELECT ROW(1,2) = (SELECT 1, 1), ROW(1,2) IN (SELECT 1, 1);
3930 SELECT ROW(1,2) = (SELECT 1, 2), ROW(1,2) IN (SELECT 1, 2);
3931 
3932 #
3933 # Bug #37362 Crash in do_field_eq
3934 #
3935 CREATE TABLE t1 (a INT, b INT, c INT);
3936 INSERT INTO t1 VALUES (1,1,1), (1,1,1);
3937 
3938 --error 1054
3939 EXPLAIN EXTENDED
3940  SELECT c FROM
3941  ( SELECT
3942  (SELECT COUNT(a) FROM
3943  (SELECT COUNT(b) FROM t1) AS x GROUP BY c
3944  ) FROM t1 GROUP BY b
3945  ) AS y;
3946 SHOW WARNINGS;
3947 
3948 DROP TABLE t1;
3949 
3950 --echo #
3951 --echo # Bug #46791: Assertion failed:(table->key_read==0),function unknown
3952 --echo # function,file sql_base.cc
3953 --echo #
3954 
3955 CREATE TABLE t1 (a INT, b INT, KEY(a));
3956 INSERT INTO t1 VALUES (1,1),(2,2);
3957 CREATE TABLE t2 LIKE t1;
3958 INSERT INTO t2 VALUES (1,1),(2,2);
3959 CREATE TABLE t3 LIKE t1;
3960 
3961 --echo # should have 1 impossible where and 2 dependent subqueries
3962 EXPLAIN
3963 SELECT 1 FROM t1
3964 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3965 ORDER BY count(*);
3966 
3967 --echo # should not crash the next statement
3968 SELECT 1 FROM t1
3969 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3970 ORDER BY count(*);
3971 
3972 --echo # should not crash: the crash is caused by the previous statement
3973 SELECT 1;
3974 
3975 DROP TABLE t1,t2,t3;
3976 
3977 --echo #
3978 --echo # Bug #47106: Crash / segfault on adding EXPLAIN to a non-crashing
3979 --echo # query
3980 --echo #
3981 
3982 CREATE TABLE t1 (
3983  a INT,
3984  b INT,
3985  PRIMARY KEY (a),
3986  KEY b (b)
3987 );
3988 INSERT INTO t1 VALUES (1, 1), (2, 1);
3989 
3990 CREATE TABLE t2 LIKE t1;
3991 INSERT INTO t2 SELECT * FROM t1;
3992 
3993 CREATE TABLE t3 LIKE t1;
3994 INSERT INTO t3 SELECT * FROM t1;
3995 
3996 --echo # Should not crash.
3997 --echo # Should have 1 impossible where and 2 dependent subqs.
3998 EXPLAIN
3999 SELECT
4000  (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
4001 FROM t3 WHERE 1 = 0 GROUP BY 1;
4002 
4003 --echo # should return 0 rows
4004 SELECT
4005  (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
4006 FROM t3 WHERE 1 = 0 GROUP BY 1;
4007 
4008 DROP TABLE t1,t2,t3;
4009 --echo #
4010 --echo # Bug#12329653
4011 --echo # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4012 --echo #
4013 
4014 CREATE TABLE t1(a1 int);
4015 INSERT INTO t1 VALUES (1),(2);
4016 
4017 SELECT @@session.sql_mode INTO @old_sql_mode;
4018 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4019 
4020 ## First a simpler query, illustrating the transformation
4021 ## '1 < some (...)' => '1 < max(...)'
4022 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4023 
4024 ## The query which made the server crash.
4025 PREPARE stmt FROM
4026 'SELECT 1 UNION ALL
4027 SELECT 1 FROM t1
4028 ORDER BY
4029 (SELECT 1 FROM t1 AS t1_0
4030  WHERE 1 < SOME (SELECT a1 FROM t1)
4031 )' ;
4032 
4033 --error ER_SUBQUERY_NO_1_ROW
4034 EXECUTE stmt ;
4035 --error ER_SUBQUERY_NO_1_ROW
4036 EXECUTE stmt ;
4037 
4038 SET SESSION sql_mode=@old_sql_mode;
4039 
4040 DEALLOCATE PREPARE stmt;
4041 DROP TABLE t1;
4042 
4043 --echo #
4044 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4045 --echo #
4046 
4047 CREATE TABLE t1(a1 int);
4048 INSERT INTO t1 VALUES (1),(2);
4049 
4050 CREATE TABLE t2(a1 int);
4051 INSERT INTO t2 VALUES (3);
4052 
4053 SELECT @@session.sql_mode INTO @old_sql_mode;
4054 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4055 
4056 ## All these are subject to the transformation
4057 ## '1 < some (...)' => '1 < max(...)'
4058 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
4059 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
4060 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
4061 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
4062 
4063 SET SESSION sql_mode=@old_sql_mode;
4064 
4065 DROP TABLE t1, t2;
4066 
4067 --echo #
4068 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4069 --echo #
4070 
4071 create table t2(i int);
4072 insert into t2 values(0);
4073 
4074 SELECT @@session.sql_mode INTO @old_sql_mode;
4075 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4076 
4077 CREATE VIEW v1 AS
4078 SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
4079 ;
4080 
4081 CREATE TABLE t1 (
4082  pk int NOT NULL,
4083  col_varchar_key varchar(1) DEFAULT NULL,
4084  PRIMARY KEY (pk),
4085  KEY col_varchar_key (col_varchar_key)
4086 );
4087 
4088 SELECT t1.pk
4089 FROM t1
4090 WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4091 ;
4092 
4093 SET SESSION sql_mode=@old_sql_mode;
4094 
4095 drop table t2, t1;
4096 drop view v1;
4097 
4098 --echo End of 5.0 tests.
4099 
4100 #
4101 # Test [NOT] IN truth table (both as top-level and general predicate).
4102 #
4103 
4104 create table t_out (subcase char(3),
4105  a1 char(2), b1 char(2), c1 char(2));
4106 create table t_in (a2 char(2), b2 char(2), c2 char(2));
4107 
4108 insert into t_out values ('A.1','2a', NULL, '2a');
4109 # -------------------------- A.2 - impossible
4110 insert into t_out values ('A.3', '2a', NULL, '2a');
4111 insert into t_out values ('A.4', '2a', NULL, 'xx');
4112 insert into t_out values ('B.1', '2a', '2a', '2a');
4113 insert into t_out values ('B.2', '2a', '2a', '2a');
4114 insert into t_out values ('B.3', '3a', 'xx', '3a');
4115 insert into t_out values ('B.4', 'xx', '3a', '3a');
4116 
4117 insert into t_in values ('1a', '1a', '1a');
4118 insert into t_in values ('2a', '2a', '2a');
4119 insert into t_in values (NULL, '2a', '2a');
4120 insert into t_in values ('3a', NULL, '3a');
4121 -- echo
4122 -- echo Test general IN semantics (not top-level)
4123 -- echo
4124 -- echo case A.1
4125 select subcase,
4126  (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
4127  (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4128 from t_out where subcase = 'A.1';
4129 
4130 -- echo case A.2 - impossible
4131 
4132 -- echo case A.3
4133 select subcase,
4134  (a1, b1, c1) IN (select * from t_in) pred_in,
4135  (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4136 from t_out where subcase = 'A.3';
4137 
4138 -- echo case A.4
4139 select subcase,
4140  (a1, b1, c1) IN (select * from t_in) pred_in,
4141  (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4142 from t_out where subcase = 'A.4';
4143 
4144 -- echo case B.1
4145 select subcase,
4146  (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
4147  (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4148 from t_out where subcase = 'B.1';
4149 
4150 -- echo case B.2
4151 select subcase,
4152  (a1, b1, c1) IN (select * from t_in) pred_in,
4153  (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4154 from t_out where subcase = 'B.2';
4155 
4156 -- echo case B.3
4157 select subcase,
4158  (a1, b1, c1) IN (select * from t_in) pred_in,
4159  (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4160 from t_out where subcase = 'B.3';
4161 
4162 -- echo case B.4
4163 select subcase,
4164  (a1, b1, c1) IN (select * from t_in) pred_in,
4165  (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4166 from t_out where subcase = 'B.4';
4167 
4168 -- echo
4169 -- echo Test IN as top-level predicate, and
4170 -- echo as non-top level for cases A.3, B.3 (the only cases with NULL result).
4171 -- echo
4172 -- echo case A.1
4173 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4174 where subcase = 'A.1' and
4175  (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4176 
4177 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4178 where subcase = 'A.1' and
4179  (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4180 
4181 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4182 where subcase = 'A.1' and
4183  NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4184 
4185 -- echo case A.3
4186 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4187 where subcase = 'A.3' and
4188  (a1, b1, c1) IN (select * from t_in);
4189 
4190 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4191 where subcase = 'A.3' and
4192  (a1, b1, c1) NOT IN (select * from t_in);
4193 
4194 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4195 where subcase = 'A.3' and
4196  NOT((a1, b1, c1) IN (select * from t_in));
4197 -- echo test non-top level result indirectly
4198 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4199 where subcase = 'A.3' and
4200  ((a1, b1, c1) IN (select * from t_in)) is NULL and
4201  ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4202 
4203 -- echo case A.4
4204 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4205 where subcase = 'A.4' and
4206  (a1, b1, c1) IN (select * from t_in);
4207 
4208 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4209 where subcase = 'A.4' and
4210  (a1, b1, c1) NOT IN (select * from t_in);
4211 
4212 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4213 where subcase = 'A.4' and
4214  NOT((a1, b1, c1) IN (select * from t_in));
4215 
4216 -- echo case B.1
4217 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4218 where subcase = 'B.1' and
4219  (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4220 
4221 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4222 where subcase = 'B.1' and
4223  (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4224 
4225 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4226 where subcase = 'B.1' and
4227  NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4228 
4229 -- echo case B.2
4230 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4231 where subcase = 'B.2' and
4232  (a1, b1, c1) IN (select * from t_in);
4233 
4234 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4235 where subcase = 'B.2' and
4236  (a1, b1, c1) NOT IN (select * from t_in);
4237 
4238 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4239 where subcase = 'B.2' and
4240  NOT((a1, b1, c1) IN (select * from t_in));
4241 
4242 -- echo case B.3
4243 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4244 where subcase = 'B.3' and
4245  (a1, b1, c1) IN (select * from t_in);
4246 
4247 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4248 where subcase = 'B.3' and
4249  (a1, b1, c1) NOT IN (select * from t_in);
4250 
4251 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4252 where subcase = 'B.3' and
4253  NOT((a1, b1, c1) IN (select * from t_in));
4254 -- echo test non-top level result indirectly
4255 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4256 where subcase = 'B.3' and
4257  ((a1, b1, c1) IN (select * from t_in)) is NULL and
4258  ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4259 
4260 -- echo case B.4
4261 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4262 where subcase = 'B.4' and
4263  (a1, b1, c1) IN (select * from t_in);
4264 
4265 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4266 where subcase = 'B.4' and
4267  (a1, b1, c1) NOT IN (select * from t_in);
4268 
4269 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4270 where subcase = 'B.4' and
4271  NOT((a1, b1, c1) IN (select * from t_in));
4272 
4273 drop table t_out;
4274 drop table t_in;
4275 
4276 
4277 #
4278 # Bug#27348 SET FUNCTION used in a subquery from WHERE condition
4279 #
4280 
4281 CREATE TABLE t1 (a INT, b INT);
4282 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4283 
4284 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4285 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4286 
4287 SELECT a FROM t1 t0
4288  WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4289 
4290 SET @@sql_mode='ansi';
4291 --error ER_INVALID_GROUP_FUNC_USE
4292 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4293 --error ER_INVALID_GROUP_FUNC_USE
4294 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4295 
4296 --error ER_INVALID_GROUP_FUNC_USE
4297 SELECT a FROM t1 t0
4298  WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4299 
4300 SET @@sql_mode=default;
4301 DROP TABLE t1;
4302 
4303 #
4304 # Bug#20835 (literal string with =any values)
4305 #
4306 CREATE TABLE t1 (s1 CHAR(1));
4307 INSERT INTO t1 VALUES ('a');
4308 SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
4309 DROP TABLE t1;
4310 
4311 #
4312 # Bug#37004 NOT IN subquery with MAX over an empty set
4313 #
4314 
4315 CREATE TABLE t1(c INT, KEY(c));
4316 CREATE TABLE t2(a INT, b INT);
4317 INSERT INTO t2 VALUES (1, 10), (2, NULL);
4318 INSERT INTO t1 VALUES (1), (3);
4319 
4320 SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4321 
4322 DROP TABLE t1,t2;
4323 
4324 #
4325 # Bug#38191 Server crash with subquery containing DISTINCT and ORDER BY
4326 #
4327 
4328 CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4329 INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4330 CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4331 INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4332 SELECT * FROM t1
4333  WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
4334 DROP TABLE t1,t2;
4335 
4336 #
4337 # Bug#37548 result value erronously reported being NULL in certain subqueries
4338 #
4339 
4340 CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4341 
4342 INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4343 
4344 CREATE TABLE t2 (
4345  a INT,
4346  b INT,
4347  c INT,
4348  d INT,
4349  PRIMARY KEY (a),
4350  UNIQUE KEY b (b,c,d),
4351  KEY b_2 (b),
4352  KEY c (c),
4353  KEY d (d)
4354 );
4355 
4356 INSERT INTO t2 VALUES
4357  (43, 2, 11 ,30),
4358  (44, 2, 12 ,30),
4359  (45, 1, 1 ,10000),
4360  (46, 1, 2 ,10000),
4361  (556,1, 32 ,10000);
4362 
4363 CREATE TABLE t3 (
4364  a INT,
4365  b INT,
4366  c INT,
4367  PRIMARY KEY (a),
4368  UNIQUE KEY b (b,c),
4369  KEY c (c),
4370  KEY b_2 (b)
4371 );
4372 
4373 INSERT INTO t3 VALUES (1,1,1), (2,32,1);
4374 
4375 explain
4376 SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
4377 SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
4378 
4379 DROP TABLE t1,t2,t3;
4380 
4381 #
4382 # Bug#37460 Assertion failed:
4383 # !table->file || table->file->inited == handler::NONE
4384 #
4385 CREATE TABLE t1 (id int);
4386 CREATE TABLE t2 (id int, c int);
4387 
4388 INSERT INTO t1 (id) VALUES (1);
4389 INSERT INTO t2 (id) VALUES (1);
4390 INSERT INTO t1 (id) VALUES (1);
4391 INSERT INTO t2 (id) VALUES (1);
4392 
4393 CREATE VIEW v1 AS
4394 SELECT t2.c AS c FROM t1, t2
4395 WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
4396 UPDATE v1 SET c=1;
4397 
4398 CREATE VIEW v2 (a,b) AS
4399 SELECT t2.id, t2.c AS c FROM t1, t2
4400 WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
4401 
4402 --error ER_VIEW_CHECK_FAILED
4403 INSERT INTO v2(a,b) VALUES (2,2);
4404 INSERT INTO v2(a,b) VALUES (1,2);
4405 SELECT * FROM v1;
4406 
4407 CREATE VIEW v3 AS
4408 SELECT t2.c AS c FROM t2
4409 WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
4410 
4411 DELETE FROM v3;
4412 
4413 DROP VIEW v1,v2,v3;
4414 DROP TABLE t1,t2;
4415 
4416 --echo #
4417 --echo # Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
4418 --echo #
4419 create table t1(id integer primary key, g integer, v integer, s char(1));
4420 create table t2(id integer primary key, g integer, v integer, s char(1));
4421 insert into t1 values
4422  (10, 10, 10, 'l'),
4423  (20, 20, 20, 'l'),
4424  (40, 40, 40, 'l'),
4425  (41, 40, null, 'l'),
4426  (50, 50, 50, 'l'),
4427  (51, 50, null, 'l'),
4428  (60, 60, 60, 'l'),
4429  (61, 60, null, 'l'),
4430  (70, 70, 70, 'l'),
4431  (90, 90, null, 'l');
4432 insert into t2 values
4433  (10, 10, 10, 'r'),
4434  (30, 30, 30, 'r'),
4435  (50, 50, 50, 'r'),
4436  (60, 60, 60, 'r'),
4437  (61, 60, null, 'r'),
4438  (70, 70, 70, 'r'),
4439  (71, 70, null, 'r'),
4440  (80, 80, 80, 'r'),
4441  (81, 80, null, 'r'),
4442  (100,100,null, 'r');
4443 
4444 select *
4445 from t1
4446 where v in(select v
4447  from t2
4448  where t1.g=t2.g) is unknown;
4449 drop table t1, t2;
4450 
4451 -- echo #
4452 -- echo # Bug#33204: INTO is allowed in subselect, causing inconsistent results
4453 -- echo #
4454 CREATE TABLE t1( a INT );
4455 INSERT INTO t1 VALUES (1),(2);
4456 
4457 CREATE TABLE t2( a INT, b INT );
4458 
4459 --error ER_PARSE_ERROR
4460 SELECT *
4461 FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
4462 --error ER_PARSE_ERROR
4463 SELECT *
4464 FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
4465 --error ER_PARSE_ERROR
4466 SELECT *
4467 FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
4468 
4469 --error ER_PARSE_ERROR
4470 SELECT * FROM (
4471  SELECT 1 a
4472  UNION
4473  SELECT a INTO @var FROM t1 WHERE a = 2
4474 ) t1a;
4475 
4476 --error ER_PARSE_ERROR
4477 SELECT * FROM (
4478  SELECT 1 a
4479  UNION
4480  SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
4481 ) t1a;
4482 
4483 --error ER_PARSE_ERROR
4484 SELECT * FROM (
4485  SELECT 1 a
4486  UNION
4487  SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
4488 ) t1a;
4489 
4490 SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
4491 
4492 SELECT * FROM (
4493  SELECT a FROM t1 WHERE a = 2
4494  UNION
4495  SELECT a FROM t1 WHERE a = 2
4496 ) t1a;
4497 
4498 SELECT * FROM (
4499  SELECT 1 a
4500  UNION
4501  SELECT a FROM t1 WHERE a = 2
4502  UNION
4503  SELECT a FROM t1 WHERE a = 2
4504 ) t1a;
4505 
4506 # This was not allowed previously. Possibly, it should be allowed on the future.
4507 # For now, the intent is to keep the fix as non-intrusive as possible.
4508 --error ER_PARSE_ERROR
4509 SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
4510 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
4511 SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
4512 --error ER_PARSE_ERROR
4513 SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
4514 --error ER_PARSE_ERROR
4515 SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
4516 --error ER_PARSE_ERROR
4517 SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
4518 
4519 --error ER_PARSE_ERROR
4520 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
4521 --error ER_PARSE_ERROR
4522 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
4523 --error ER_PARSE_ERROR
4524 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
4525 
4526 --error ER_PARSE_ERROR
4527 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
4528 --error ER_PARSE_ERROR
4529 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
4530 --error ER_PARSE_ERROR
4531 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
4532 
4533 SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
4534 SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
4535 SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
4536 SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
4537 
4538 # Test of rule
4539 # table_factor: '(' get_select_lex query_expression_body ')' opt_table_alias
4540 # UNION should not be allowed inside the parentheses, nor should
4541 # aliases after.
4542 #
4543 SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1;
4544 --error ER_PARSE_ERROR
4545 SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
4546 --error ER_PARSE_ERROR
4547 SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
4548 --error ER_PARSE_ERROR
4549 SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
4550 --error ER_PARSE_ERROR
4551 SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
4552 --error ER_PARSE_ERROR
4553 SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
4554 
4555 SELECT * FROM t1 JOIN (t1 t1a) ON 1;
4556 SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
4557 
4558 SELECT * FROM (t1 t1a);
4559 SELECT * FROM ((t1 t1a));
4560 
4561 SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1;
4562 SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
4563 
4564 SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1;
4565 SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
4566 
4567 # For the join, TABLE_LIST::select_lex == NULL
4568 # Check that we handle this.
4569 --error ER_PARSE_ERROR
4570 SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
4571 
4572 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
4573 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
4574 SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
4575 
4576 --error ER_PARSE_ERROR
4577 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
4578 --error ER_PARSE_ERROR
4579 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
4580 --error ER_PARSE_ERROR
4581 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
4582 
4583 SELECT * FROM t1 WHERE a = ( SELECT 1 );
4584 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
4585 --error ER_PARSE_ERROR
4586 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
4587 --error ER_PARSE_ERROR
4588 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
4589 --error ER_PARSE_ERROR
4590 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
4591 
4592 --error ER_PARSE_ERROR
4593 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
4594 --error ER_PARSE_ERROR
4595 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
4596 --error ER_PARSE_ERROR
4597 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
4598 
4599 --error ER_PARSE_ERROR
4600 SELECT ( SELECT 1 INTO @v );
4601 --error ER_PARSE_ERROR
4602 SELECT ( SELECT 1 INTO OUTFILE 'file' );
4603 --error ER_PARSE_ERROR
4604 SELECT ( SELECT 1 INTO DUMPFILE 'file' );
4605 
4606 --error ER_PARSE_ERROR
4607 SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
4608 --error ER_PARSE_ERROR
4609 SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
4610 --error ER_PARSE_ERROR
4611 SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
4612 
4613 # Make sure context is popped when we leave the nested select
4614 SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
4615 SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
4616 
4617 # Make sure we have feature F561 (see .yy file)
4618 SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
4619 
4620 # Make sure the parser does not allow nested UNIONs anywhere
4621 
4622 --error ER_PARSE_ERROR
4623 SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
4624 --error ER_PARSE_ERROR
4625 ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
4626 
4627 --error ER_PARSE_ERROR
4628 SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4629 --error ER_PARSE_ERROR
4630 SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
4631 SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4632 SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
4633 
4634 --error ER_PARSE_ERROR
4635 SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4636 --error ER_PARSE_ERROR
4637 SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4638 SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
4639 
4640 --error ER_PARSE_ERROR
4641 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4642 --error ER_PARSE_ERROR
4643 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4644 --error ER_PARSE_ERROR
4645 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4646 --error ER_PARSE_ERROR
4647 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4648 
4649 --error ER_PARSE_ERROR
4650 SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4651 --error ER_PARSE_ERROR
4652 SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4653 --error ER_PARSE_ERROR
4654 SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4655 --error ER_PARSE_ERROR
4656 SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4657 
4658 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4659 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4660 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4661 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4662 
4663 --error ER_PARSE_ERROR
4664 SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
4665 SELECT EXISTS(SELECT 1+1);
4666 --error ER_PARSE_ERROR
4667 SELECT EXISTS(SELECT 1+1 INTO @test);
4668 --error ER_PARSE_ERROR
4669 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
4670 
4671 --error ER_PARSE_ERROR
4672 SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
4673 --error ER_PARSE_ERROR
4674 SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
4675 
4676 DROP TABLE t1, t2;
4677 #
4678 # Bug #31157: Crash when select+order by the avg of some field within the
4679 # group by
4680 #
4681 CREATE TABLE t1 (a ENUM('rainbow'));
4682 INSERT INTO t1 VALUES (),(),(),(),();
4683 SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
4684 DROP TABLE t1;
4685 CREATE TABLE t1 (a LONGBLOB);
4686 INSERT INTO t1 SET a = 'aaaa';
4687 INSERT INTO t1 SET a = 'aaaa';
4688 SELECT 1 FROM t1 GROUP BY
4689  (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
4690 DROP TABLE t1;
4691 
4692 --echo #
4693 --echo # Bug #49512 : subquery with aggregate function crash
4694 --echo # subselect_single_select_engine::exec()
4695 
4696 CREATE TABLE t1(a INT);
4697 INSERT INTO t1 VALUES();
4698 
4699 --echo # should not crash
4700 SELECT 1 FROM t1 WHERE a <> SOME
4701 (
4702  SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
4703  FROM t1,t1 a
4704 );
4705 DROP TABLE t1;
4706 
4707 --echo #
4708 --echo # Bug #45989 take 2 : memory leak after explain encounters an
4709 --echo # error in the query
4710 --echo #
4711 
4712 CREATE TABLE t1(a LONGTEXT);
4713 INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
4714 INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
4715 
4716 --error ER_BAD_FIELD_ERROR
4717 EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
4718 (SELECT a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
4719 WHERE t1.a = d1.a;
4720 --error ER_WRONG_USAGE
4721 EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
4722 (SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
4723 WHERE t1.a = d1.a;
4724 
4725 DROP TABLE t1;
4726 
4727 #
4728 # BUG#36135 "void Diagnostics_area::set_eof_status(THD*): Assertion `!is_set()' failed."
4729 #
4730 create table t0 (a int);
4731 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4732 
4733 create table t1 (
4734  a int(11) default null,
4735  b int(11) default null,
4736  key (a)
4737 );
4738 # produce numbers 0..999
4739 insert into t1 select A.a+10*(B.a+10*C.a),A.a+10*(B.a+10*C.a) from t0 A, t0 B, t0 C;
4740 
4741 create table t2 (a int(11) default null);
4742 insert into t2 values (0),(1);
4743 
4744 create table t3 (a int(11) default null);
4745 insert into t3 values (0),(1);
4746 
4747 create table t4 (a int(11) default null);
4748 insert into t4 values (0),(1);
4749 
4750 create table t5 (a int(11) default null);
4751 insert into t5 values (0),(1),(0),(1);
4752 
4753 # this must not fail assertion
4754 --error 1242
4755 select * from t2, t3
4756 where
4757  t2.a < 10 and
4758  t3.a+1 = 2 and
4759  t3.a in (select t1.b from t1
4760  where t1.a+1=t1.a+1 and
4761  t1.a < (select t4.a+10
4762  from t4, t5 limit 2));
4763 
4764 drop table t0, t1, t2, t3, t4, t5;
4765 
4766 --echo #
4767 --echo # BUG#48177 - SELECTs with NOT IN subqueries containing NULL
4768 --echo # values return too many records
4769 --echo #
4770 
4771 CREATE TABLE t1 (
4772  i1 int DEFAULT NULL,
4773  i2 int DEFAULT NULL
4774 ) ;
4775 
4776 INSERT INTO t1 VALUES (1, NULL);
4777 INSERT INTO t1 VALUES (2, 3);
4778 INSERT INTO t1 VALUES (4, NULL);
4779 INSERT INTO t1 VALUES (4, 0);
4780 INSERT INTO t1 VALUES (NULL, NULL);
4781 
4782 CREATE TABLE t2 (
4783  i1 int DEFAULT NULL,
4784  i2 int DEFAULT NULL
4785 ) ;
4786 
4787 INSERT INTO t2 VALUES (4, NULL);
4788 INSERT INTO t2 VALUES (5, 0);
4789 
4790 --echo
4791 --echo Data in t1
4792 SELECT i1, i2 FROM t1;
4793 
4794 --echo
4795 --echo Data in subquery (should be filtered out)
4796 SELECT i1, i2 FROM t2 ORDER BY i1;
4797 
4798 FLUSH STATUS;
4799 
4800 --echo
4801 SELECT i1, i2
4802 FROM t1
4803 WHERE (i1, i2)
4804  NOT IN (SELECT i1, i2 FROM t2);
4805 
4806 --echo
4807 --echo # Check that the subquery only has to be evaluated once
4808 --echo # for all-NULL values even though there are two (NULL,NULL) records
4809 --echo # Baseline:
4810 SHOW STATUS LIKE '%Handler_read_rnd_next';
4811 
4812 --echo
4813 INSERT INTO t1 VALUES (NULL, NULL);
4814 FLUSH STATUS;
4815 
4816 --echo
4817 SELECT i1, i2
4818 FROM t1
4819 WHERE (i1, i2)
4820  NOT IN (SELECT i1, i2 FROM t2);
4821 
4822 --echo
4823 --echo # Handler_read_rnd_next should be one more than baseline
4824 --echo # (read record from t1, but do not read from t2)
4825 SHOW STATUS LIKE '%Handler_read_rnd_next';
4826 
4827 
4828 DROP TABLE t1,t2;
4829 
4830 --echo #
4831 --echo # Bug#54568: create view cause Assertion failed: 0,
4832 --echo # file .\item_subselect.cc, line 836
4833 --echo #
4834 EXPLAIN SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4835 DESCRIBE SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4836 --echo # None of the below should crash
4837 CREATE VIEW v1 AS SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4838 CREATE VIEW v2 AS SELECT 1 LIKE '%' ESCAPE ( 1 IN ( SELECT 1 ) );
4839 DROP VIEW v1, v2;
4840 
4841 #
4842 # Bug #47904 Incorrect results w/ table subquery, derived SQs, and LEFT JOIN on index
4843 #
4844 
4845 --echo Set up test tables.
4846 CREATE TABLE t1 (
4847  t1_id INT UNSIGNED,
4848 
4849  PRIMARY KEY(t1_id)
4850 ) Engine=MyISAM;
4851 
4852 INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
4853 
4854 CREATE TABLE t2 SELECT * FROM t1;
4855 
4856 CREATE TABLE t3 (
4857  t3_id INT UNSIGNED AUTO_INCREMENT,
4858  t1_id INT UNSIGNED,
4859  amount DECIMAL(16,2),
4860 
4861  PRIMARY KEY(t3_id),
4862  KEY(t1_id)
4863 ) Engine=MyISAM;
4864 
4865 INSERT INTO t3 (t1_id, t3_id, amount)
4866  VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
4867 
4868 --echo This is the 'inner query' running by itself.
4869 --echo Produces correct results.
4870 SELECT
4871  t1.t1_id,
4872  IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4873 FROM
4874  t1
4875  LEFT JOIN t2 ON t2.t1_id=t1.t1_id
4876 GROUP BY
4877  t1.t1_id
4878 ;
4879 
4880 --echo SELECT * FROM (the same inner query)
4881 --echo Produces correct results.
4882 SELECT * FROM (
4883 SELECT
4884  t1.t1_id,
4885  IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4886 FROM
4887  t1
4888  LEFT JOIN t2 ON t2.t1_id=t1.t1_id
4889 GROUP BY
4890  t1.t1_id
4891 ) AS t;
4892 
4893 --echo Now make t2.t1_id part of a key.
4894 ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
4895 
4896 --echo Same inner query by itself.
4897 --echo Still correct results.
4898 SELECT
4899  t1.t1_id,
4900  IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4901 FROM
4902  t1
4903  LEFT JOIN t2 ON t2.t1_id=t1.t1_id
4904 GROUP BY
4905  t1.t1_id;
4906 
4907 --echo SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
4908 SELECT * FROM (
4909 SELECT
4910  t1.t1_id,
4911  IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4912 FROM
4913  t1
4914  LEFT JOIN t2 ON t2.t1_id=t1.t1_id
4915 GROUP BY
4916  t1.t1_id
4917 ) AS t;
4918 
4919 
4920 DROP TABLE t3;
4921 DROP TABLE t2;
4922 DROP TABLE t1;
4923 
4924 #
4925 # BUG#36135 "void Diagnostics_area::set_eof_status(THD*): Assertion `!is_set()' failed."
4926 #
4927 create table t0 (a int);
4928 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4929 
4930 create table t1 (
4931  a int(11) default null,
4932  b int(11) default null,
4933  key (a)
4934 );
4935 # produce numbers 0..999
4936 insert into t1 select A.a+10*(B.a+10*C.a),A.a+10*(B.a+10*C.a) from t0 A, t0 B, t0 C;
4937 
4938 create table t2 (a int(11) default null);
4939 insert into t2 values (0),(1);
4940 
4941 create table t3 (a int(11) default null);
4942 insert into t3 values (0),(1);
4943 
4944 create table t4 (a int(11) default null);
4945 insert into t4 values (0),(1);
4946 
4947 create table t5 (a int(11) default null);
4948 insert into t5 values (0),(1),(0),(1);
4949 
4950 # this must not fail assertion
4951 --error 1242
4952 select * from t2, t3
4953 where
4954  t2.a < 10 and
4955  t3.a+1 = 2 and
4956  t3.a in (select t1.b from t1
4957  where t1.a+1=t1.a+1 and
4958  t1.a < (select t4.a+10
4959  from t4, t5 limit 2));
4960 
4961 drop table t0, t1, t2, t3, t4, t5;
4962 
4963 --echo #
4964 --echo # BUG#48177 - SELECTs with NOT IN subqueries containing NULL
4965 --echo # values return too many records
4966 --echo #
4967 
4968 CREATE TABLE t1 (
4969  i1 int DEFAULT NULL,
4970  i2 int DEFAULT NULL
4971 ) ;
4972 
4973 INSERT INTO t1 VALUES (1, NULL);
4974 INSERT INTO t1 VALUES (2, 3);
4975 INSERT INTO t1 VALUES (4, NULL);
4976 INSERT INTO t1 VALUES (4, 0);
4977 INSERT INTO t1 VALUES (NULL, NULL);
4978 
4979 CREATE TABLE t2 (
4980  i1 int DEFAULT NULL,
4981  i2 int DEFAULT NULL
4982 ) ;
4983 
4984 INSERT INTO t2 VALUES (4, NULL);
4985 INSERT INTO t2 VALUES (5, 0);
4986 
4987 --echo
4988 --echo Data in t1
4989 SELECT i1, i2 FROM t1;
4990 
4991 --echo
4992 --echo Data in subquery (should be filtered out)
4993 SELECT i1, i2 FROM t2 ORDER BY i1;
4994 
4995 FLUSH STATUS;
4996 
4997 --echo
4998 SELECT i1, i2
4999 FROM t1
5000 WHERE (i1, i2)
5001  NOT IN (SELECT i1, i2 FROM t2);
5002 
5003 --echo
5004 --echo # Check that the subquery only has to be evaluated once
5005 --echo # for all-NULL values even though there are two (NULL,NULL) records
5006 --echo # Baseline:
5007 SHOW STATUS LIKE '%Handler_read_rnd_next';
5008 
5009 --echo
5010 INSERT INTO t1 VALUES (NULL, NULL);
5011 FLUSH STATUS;
5012 
5013 --echo
5014 SELECT i1, i2
5015 FROM t1
5016 WHERE (i1, i2)
5017  NOT IN (SELECT i1, i2 FROM t2);
5018 
5019 --echo
5020 --echo # Handler_read_rnd_next should be one more than baseline
5021 --echo # (read record from t1, but do not read from t2)
5022 SHOW STATUS LIKE '%Handler_read_rnd_next';
5023 DROP TABLE t1,t2;
5024 
5025 
5026 
5027 --echo #
5028 --echo # Bug #52711: Segfault when doing EXPLAIN SELECT with
5029 --echo # union...order by (select... where...)
5030 --echo #
5031 
5032 CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5033 INSERT INTO t1 VALUES (1),(2);
5034 CREATE TABLE t2 (b INT);
5035 INSERT INTO t2 VALUES (1),(2);
5036 
5037 --echo # Should not crash
5038 --disable_result_log
5039 EXPLAIN
5040 SELECT * FROM t2 UNION SELECT * FROM t2
5041  ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5042 
5043 --echo # Should not crash
5044 SELECT * FROM t2 UNION SELECT * FROM t2
5045  ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5046 DROP TABLE t1,t2;
5047 --enable_result_log
5048 
5049 --echo #
5050 --echo # Bug #58818: Incorrect result for IN/ANY subquery
5051 --echo # with HAVING condition
5052 --echo #
5053 
5054 CREATE TABLE t1(i INT);
5055 INSERT INTO t1 VALUES (1), (2), (3);
5056 CREATE TABLE t1s(i INT);
5057 INSERT INTO t1s VALUES (10), (20), (30);
5058 CREATE TABLE t2s(i INT);
5059 INSERT INTO t2s VALUES (100), (200), (300);
5060 
5061 SELECT * FROM t1
5062 WHERE t1.i NOT IN
5063 (
5064  SELECT STRAIGHT_JOIN t2s.i
5065  FROM
5066  t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5067  HAVING t2s.i = 999
5068 );
5069 
5070 SELECT * FROM t1
5071 WHERE t1.I IN
5072 (
5073  SELECT STRAIGHT_JOIN t2s.i
5074  FROM
5075  t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5076  HAVING t2s.i = 999
5077 ) IS UNKNOWN;
5078 
5079 SELECT * FROM t1
5080 WHERE NOT t1.I = ANY
5081 (
5082  SELECT STRAIGHT_JOIN t2s.i
5083  FROM
5084  t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5085  HAVING t2s.i = 999
5086 );
5087 
5088 SELECT * FROM t1
5089  WHERE t1.i = ANY (
5090  SELECT STRAIGHT_JOIN t2s.i
5091  FROM
5092  t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5093  HAVING t2s.i = 999
5094  ) IS UNKNOWN;
5095 
5096 DROP TABLE t1,t1s,t2s;
5097 
5098 --echo #
5099 --echo # Bug #56690 Wrong results with subquery with
5100 --echo # GROUP BY inside < ANY clause
5101 --echo #
5102 
5103 CREATE TABLE t1 (
5104  pk INT NOT NULL PRIMARY KEY,
5105  number INT,
5106  KEY key_number (number)
5107 );
5108 INSERT INTO t1 VALUES (8,8);
5109 
5110 CREATE TABLE t2 (
5111  pk INT NOT NULL PRIMARY KEY,
5112  number INT,
5113  KEY key_number (number)
5114 );
5115 
5116 INSERT INTO t2 VALUES (1,2);
5117 INSERT INTO t2 VALUES (2,8);
5118 INSERT INTO t2 VALUES (3,NULL);
5119 INSERT INTO t2 VALUES (4,166);
5120 
5121 SELECT * FROM t1 WHERE t1.number < ANY(SELECT number FROM t2 GROUP BY number);
5122 SELECT * FROM t1 WHERE t1.number < ANY(SELECT number FROM t2);
5123 
5124 DROP TABLE t1,t2;
5125 
5126 --echo End of 5.1 tests
5127 
5128 --echo #
5129 --echo # BUG#50257: Missing info in REF column of the EXPLAIN
5130 --echo # lines for subselects
5131 --echo #
5132 
5133 CREATE TABLE t1 (a INT, b INT, INDEX (a));
5134 INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5135 
5136 --echo
5137 EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5138 --echo
5139 EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5140 
5141 --echo
5142 DROP TABLE t1;
5143 
5144 
5145 --echo #
5146 --echo # BUG#52317: Assertion failing in Field_varstring::store()
5147 --echo # at field.cc:6833
5148 --echo #
5149 
5150 CREATE TABLE t1 (i INTEGER);
5151 INSERT INTO t1 VALUES (1);
5152 CREATE TABLE t2 (i INTEGER, KEY k(i));
5153 INSERT INTO t2 VALUES (1), (2);
5154 
5155 EXPLAIN
5156 SELECT i FROM t1 WHERE (1) NOT IN (SELECT i FROM t2);
5157 
5158 DROP TABLE t2;
5159 DROP TABLE t1;
5160 
5161 
5162 --echo #
5163 --echo # Bug #11765713 58705:
5164 --echo # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5165 --echo # CREATED BY OPT_SUM_QUERY
5166 --echo #
5167 
5168 CREATE TABLE t1(a INT NOT NULL, KEY (a));
5169 INSERT INTO t1 VALUES (0), (1);
5170 
5171 --error ER_SUBQUERY_NO_1_ROW
5172 SELECT 1 as foo FROM t1 WHERE a < SOME
5173  (SELECT a FROM t1 WHERE a <=>
5174  (SELECT a FROM t1)
5175  );
5176 
5177 SELECT 1 as foo FROM t1 WHERE a < SOME
5178  (SELECT a FROM t1 WHERE a <=>
5179  (SELECT a FROM t1 where a is null)
5180  );
5181 
5182 DROP TABLE t1;
5183 
5184 --echo #
5185 --echo # Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
5186 --echo # Assertion `file' failed.
5187 --echo #
5188 
5189 CREATE TABLE t1 (a INT);
5190 
5191 --error ER_OPERAND_COLUMNS
5192 SELECT 1 FROM
5193  (SELECT ROW(
5194  (SELECT 1 FROM t1 RIGHT JOIN
5195  (SELECT 1 FROM t1, t1 t2) AS d ON 1),
5196  1) FROM t1) AS e;
5197 
5198 DROP TABLE t1;
5199 
5200 --echo #
5201 --echo # Bug#11764086: Null left operand to NOT IN in WHERE clause
5202 --echo # behaves differently than real NULL
5203 --echo #
5204 
5205 CREATE TABLE parent (id int);
5206 INSERT INTO parent VALUES (1), (2);
5207 
5208 CREATE TABLE child (parent_id int, other int);
5209 INSERT INTO child VALUES (1,NULL);
5210 
5211 --echo # Offending query (c.parent_id is NULL for null-complemented rows only)
5212 
5213 SELECT p.id, c.parent_id
5214 FROM parent p
5215 LEFT JOIN child c
5216 ON p.id = c.parent_id
5217 WHERE c.parent_id NOT IN (
5218  SELECT parent_id
5219  FROM child
5220  WHERE parent_id = 3
5221  );
5222 
5223 --echo # Some syntactic variations with IS FALSE and IS NOT TRUE
5224 
5225 SELECT p.id, c.parent_id
5226 FROM parent p
5227 LEFT JOIN child c
5228 ON p.id = c.parent_id
5229 WHERE c.parent_id IN (
5230  SELECT parent_id
5231  FROM child
5232  WHERE parent_id = 3
5233  ) IS NOT TRUE;
5234 
5235 SELECT p.id, c.parent_id
5236 FROM parent p
5237 LEFT JOIN child c
5238 ON p.id = c.parent_id
5239 WHERE c.parent_id IN (
5240  SELECT parent_id
5241  FROM child
5242  WHERE parent_id = 3
5243  ) IS FALSE;
5244 
5245 DROP TABLE parent, child;
5246 
5247 --echo # End of test for bug#11764086.
5248 
5249 --echo End of 5.5 tests.
5250 
5251 --echo #
5252 --echo # BUG#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery
5253 --echo # in the select list
5254 --echo #
5255 
5256 --echo
5257 CREATE TABLE t1 (
5258  i int(11) DEFAULT NULL,
5259  v varchar(1) DEFAULT NULL
5260 );
5261 
5262 --echo
5263 INSERT INTO t1 VALUES (8,'v');
5264 INSERT INTO t1 VALUES (9,'r');
5265 INSERT INTO t1 VALUES (NULL,'y');
5266 
5267 --echo
5268 CREATE TABLE t2 (
5269  i int(11) DEFAULT NULL,
5270  v varchar(1) DEFAULT NULL,
5271  KEY i_key (i)
5272 );
5273 
5274 --echo
5275 INSERT INTO t2 VALUES (NULL,'r');
5276 INSERT INTO t2 VALUES (0,'c');
5277 INSERT INTO t2 VALUES (0,'o');
5278 INSERT INTO t2 VALUES (2,'v');
5279 INSERT INTO t2 VALUES (7,'c');
5280 
5281 --echo
5282 SELECT i, v, (SELECT COUNT(DISTINCT i)
5283  FROM t1
5284  WHERE v = t2.v) as subsel
5285 FROM t2;
5286 
5287 --echo
5288 EXPLAIN EXTENDED
5289 SELECT i, v, (SELECT COUNT(DISTINCT i)
5290  FROM t1
5291  WHERE v = t2.v) as subsel
5292 FROM t2;
5293 
5294 DROP TABLE t1,t2;
5295 
5296 
5297 --echo #
5298 --echo # BUG#50257: Missing info in REF column of the EXPLAIN
5299 --echo # lines for subselects
5300 --echo #
5301 
5302 CREATE TABLE t1 (a INT, b INT, INDEX (a));
5303 INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5304 
5305 --echo
5306 EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5307 --echo
5308 EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5309 
5310 --echo
5311 DROP TABLE t1;
5312 
5313 --echo #
5314 --echo # BUG#58561: Server Crash with correlated subquery and MyISAM tables
5315 --echo #
5316 
5317 CREATE TABLE cc (
5318  pk INT,
5319  col_int_key INT,
5320  col_varchar_key VARCHAR(1),
5321  PRIMARY KEY (pk),
5322  KEY col_int_key (col_int_key),
5323  KEY col_varchar_key (col_varchar_key,col_int_key)
5324 ) ENGINE=MyISAM;
5325 INSERT INTO cc VALUES (10,7,'v');
5326 INSERT INTO cc VALUES (11,1,'r');
5327 
5328 CREATE TABLE bb (
5329  pk INT,
5330  col_date_key DATE,
5331  PRIMARY KEY (pk),
5332  KEY col_date_key (col_date_key)
5333 ) ENGINE=MyISAM;
5334 INSERT INTO bb VALUES (10,'2002-02-21');
5335 
5336 CREATE TABLE c (
5337  pk INT,
5338  col_int_key INT,
5339  col_varchar_key VARCHAR(1),
5340  PRIMARY KEY (pk),
5341  KEY col_int_key (col_int_key),
5342  KEY col_varchar_key (col_varchar_key,col_int_key)
5343 ) ENGINE=MyISAM;
5344 INSERT INTO c VALUES (1,NULL,'w');
5345 INSERT INTO c VALUES (19,NULL,'f');
5346 
5347 CREATE TABLE b (
5348  pk INT,
5349  col_int_key INT,
5350  col_varchar_key VARCHAR(1),
5351  PRIMARY KEY (pk),
5352  KEY col_int_key (col_int_key),
5353  KEY col_varchar_key (col_varchar_key,col_int_key)
5354 ) ENGINE=MyISAM;
5355 INSERT INTO b VALUES (1,7,'f');
5356 
5357 SELECT col_int_key
5358 FROM b granparent1
5359 WHERE (col_int_key, col_int_key) IN (
5360  SELECT parent1.pk, parent1.pk
5361  FROM bb parent1 JOIN cc parent2
5362  ON parent2.col_varchar_key = parent2.col_varchar_key
5363  WHERE granparent1.col_varchar_key IN (
5364  SELECT col_varchar_key
5365  FROM c)
5366  AND parent1.pk = granparent1.col_int_key
5367  ORDER BY parent1.col_date_key
5368 );
5369 
5370 DROP TABLE bb, b, cc, c;
5371 
5372 --echo End of 5.6 tests
5373 
5374 --echo #
5375 --echo # BUG#46743 "Azalea processing correlated, aggregate SELECT
5376 --echo # subqueries incorrectly"
5377 --echo #
5378 # To see the bug, one would have to undo both the fix for BUG#46743
5379 # and the one for BUG#47123.
5380 CREATE TABLE t1 (c int);
5381 INSERT INTO t1 VALUES (NULL);
5382 CREATE TABLE t2 (d int , KEY (d)); # index is needed for bug
5383 INSERT INTO t2 VALUES (NULL),(NULL); # two rows needed for bug
5384 # we see that subquery returns 0 rows
5385 --echo 0 rows in subquery
5386 SELECT 1 AS RESULT FROM t2,t1 WHERE d = c;
5387 # so here it ends up as NULL
5388 --echo base query
5389 SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
5390 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
5391 --echo first equivalent variant
5392 SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c ;
5393 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c;
5394 --echo second equivalent variant
5395 # used to fail with 1242: Subquery returns more than 1 row
5396 SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
5397 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
5398 
5399 DROP TABLE t1,t2;
5400 
5401 --echo
5402 --echo BUG#37842: Assertion in DsMrr_impl::dsmrr_init, at handler.cc:4307
5403 --echo
5404 CREATE TABLE t1 (
5405  `pk` int(11) NOT NULL AUTO_INCREMENT,
5406  `int_key` int(11) DEFAULT NULL,
5407  PRIMARY KEY (`pk`),
5408  KEY `int_key` (`int_key`)
5409 ) ENGINE=MyISAM;
5410 
5411 INSERT INTO t1 VALUES (1,9),(2,3),(3,8),(4,6),(5,9),(6,5),(7,5),(8,9),(9,1),(10,10);
5412 
5413 SELECT `pk` FROM t1 AS OUTR WHERE `int_key` = ALL (
5414  SELECT `int_key` FROM t1 AS INNR WHERE INNR . `pk` >= 9
5415 );
5416 DROP TABLE t1;
5417 
5418 --echo #
5419 --echo # Bug#53236 Segfault in DTCollation::set(DTCollation&)
5420 --echo #
5421 
5422 CREATE TABLE t1 (
5423  pk INTEGER AUTO_INCREMENT,
5424  col_varchar VARCHAR(1),
5425  PRIMARY KEY (pk)
5426 )
5427 ;
5428 
5429 INSERT INTO t1 (col_varchar)
5430 VALUES
5431 ('w'),
5432 ('m')
5433 ;
5434 
5435 SELECT table1.pk
5436 FROM ( t1 AS table1 JOIN t1 AS table2 ON (table1.col_varchar =
5437  table2.col_varchar) )
5438 WHERE ( 1, 2 ) IN ( SELECT SUBQUERY1_t1.pk AS SUBQUERY1_field1,
5439  SUBQUERY1_t1.pk AS SUBQUERY1_field2
5440  FROM ( t1 AS SUBQUERY1_t1 JOIN t1 AS SUBQUERY1_t2
5441  ON (SUBQUERY1_t2.col_varchar =
5442  SUBQUERY1_t1.col_varchar) ) )
5443 ;
5444 
5445 drop table t1;
5446 --echo #
5447 --echo # Bug#58207: invalid memory reads when using default column value and
5448 --echo # tmptable needed
5449 --echo #
5450 CREATE TABLE t(a VARCHAR(245) DEFAULT
5451 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
5452 INSERT INTO t VALUES (''),(''),(''),(''),(''),(''),(''),(''),(''),(''),('');
5453 SELECT * FROM (SELECT default(a) FROM t GROUP BY a) d;
5454 DROP TABLE t;
5455 
5456 --echo #
5457 --echo # Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
5458 --echo # BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
5459 --echo #
5460 
5461 CREATE TABLE t1(a INT);
5462 INSERT INTO t1 VALUES (0), (1);
5463 
5464 CREATE TABLE t2(
5465  b TEXT,
5466  c INT,
5467  PRIMARY KEY (b(1))
5468 );
5469 INSERT INTO t2 VALUES ('a', 2), ('b', 3);
5470 
5471 SELECT 1 FROM t1 WHERE a =
5472  (SELECT 1 FROM t2 WHERE b =
5473  (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
5474  ORDER BY b
5475  );
5476 
5477 SELECT 1 FROM t1 WHERE a =
5478  (SELECT 1 FROM t2 WHERE b =
5479  (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
5480  GROUP BY b
5481  );
5482 
5483 DROP TABLE t1, t2;
5484 
5485 --echo #
5486 --echo # BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
5487 --echo #
5488 
5489 CREATE TABLE t1 (f1 varchar(1));
5490 INSERT INTO t1 VALUES ('v'),('s');
5491 
5492 CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
5493 INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
5494 ('d'),('y'),('t'),('d'),('s');
5495 
5496 let $query=SELECT table1.f1, table2.f1_key
5497 FROM t1 AS table1, t2 AS table2
5498 WHERE EXISTS
5499 (
5500 SELECT DISTINCT f1_key
5501 FROM t2
5502 WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
5503 
5504 eval $query;
5505 eval explain $query;
5506 
5507 DROP TABLE t1,t2;
5508 
5509 --echo #
5510 --echo # BUG#12616477 - 0 VS NULL DIFFERENCES WITH OUTER JOIN, SUBQUERY
5511 --echo #
5512 
5513 CREATE TABLE t1 (
5514 col_int_key int,
5515 col_varchar_key varchar(1),
5516 col_varchar_nokey varchar(1),
5517 KEY (col_int_key)
5518 );
5519 INSERT INTO t1 VALUES (224,'p','p'),(9,'e','e');
5520 
5521 CREATE TABLE t3 (
5522 col_int_key int,
5523 col_varchar_key varchar(1),
5524 KEY col_int_key (col_int_key),
5525 KEY col_varchar_key (col_varchar_key,col_int_key))
5526 ;
5527 INSERT INTO t3 VALUES (4,'p'),(8,'e'),(10,'a');
5528 
5529 # At jcl>=1, if join buffering is on t1 bug doesn't happen, so we
5530 # force join order so that join buffering is rather on t3.
5531 # Reverse join order if you want to see bug at jcl=0.
5532 
5533 let $query=SELECT t1f.*,t3f.*,(
5534 SELECT MIN(t3s.col_int_key)
5535 FROM t3 AS t3s JOIN
5536 t1 AS t1s ON t1s.col_int_key = 9 and
5537 t1s.col_varchar_key = 'e'
5538 WHERE 'e' <> t1f.col_varchar_nokey )
5539 FROM
5540 t1 AS t1f STRAIGHT_JOIN t3 AS t3f;
5541 
5542 eval $query;
5543 eval explain $query;
5544 
5545 DROP TABLE t1,t3;
5546 
5547 --echo #
5548 --echo # Bug#12795555: Missing rows with ALL/ANY subquery
5549 --echo #
5550 # Test the case when Item_maxmin_subselect is used
5551 CREATE TABLE t1 (f1 INT);
5552 INSERT INTO t1 VAlUES (NULL),(1),(NULL),(2);
5553 SELECT f1 FROM t1 WHERE f1 < ALL (SELECT 1 FROM DUAL WHERE 0);
5554 DROP TABLE t1;
5555 # Test the case when Item_sum_[max|min] is used
5556 CREATE TABLE t1 (k VARCHAR(1), KEY k(k));
5557 INSERT INTO t1 VALUES ('r'), (NULL), (NULL);
5558 
5559 CREATE TABLE t2 (c VARCHAR(1));
5560 INSERT INTO t2 VALUES ('g'), (NULL);
5561 
5562 CREATE TABLE t3 (c VARCHAR(1));
5563 
5564 SELECT COUNT(*)
5565 FROM t1 JOIN t2
5566 WHERE t1.k < ALL(
5567  SELECT c
5568  FROM t3
5569 );
5570 
5571 DROP TABLE t1, t2, t3;
5572 
5573 --echo #
5574 --echo # Bug#12838171: 51VS56: TRANSFORMED IN()+SUBQ QUERY
5575 --echo # PRODUCES EMPTY RESULT ON 5.6, 1 ROW ON 5.1
5576 --echo #
5577 CREATE TABLE ot (
5578  col_int_nokey int(11),
5579  col_varchar_nokey varchar(1)
5580 ) ;
5581 
5582 INSERT INTO ot VALUES (1,'x');
5583 
5584 CREATE TABLE it (
5585  col_int_key int(11),
5586  col_varchar_key varchar(1),
5587  KEY idx_cvk_cik (col_varchar_key,col_int_key)
5588 ) ;
5589 
5590 INSERT INTO it VALUES (NULL,'x'), (NULL,'f');
5591 
5592 --echo
5593 SELECT col_int_nokey
5594 FROM ot
5595 WHERE col_varchar_nokey IN
5596  (SELECT col_varchar_key
5597  FROM it
5598  WHERE col_int_key IS NULL);
5599 
5600 --echo
5601 EXPLAIN EXTENDED
5602 SELECT col_int_nokey
5603 FROM ot
5604 WHERE col_varchar_nokey IN
5605  (SELECT col_varchar_key
5606  FROM it
5607  WHERE col_int_key IS NULL);
5608 
5609 --echo
5610 SELECT col_int_nokey
5611 FROM ot
5612 WHERE col_varchar_nokey IN
5613  (SELECT col_varchar_key
5614  FROM it
5615  WHERE coalesce(col_int_nokey, 1) );
5616 
5617 --echo
5618 EXPLAIN EXTENDED
5619 SELECT col_int_nokey
5620 FROM ot
5621 WHERE col_varchar_nokey IN
5622  (SELECT col_varchar_key
5623  FROM it
5624  WHERE coalesce(col_int_nokey, 1) );
5625 
5626 DROP TABLE it;
5627 
5628 CREATE TABLE it (
5629  col_int_key int(11),
5630  col_varchar_key varchar(1),
5631  col_varchar_key2 varchar(1),
5632  KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
5633  KEY idx_cvk_cik (col_varchar_key, col_int_key)
5634 );
5635 
5636 INSERT INTO it VALUES (NULL,'x','x'), (NULL,'f','f');
5637 
5638 SELECT col_int_nokey
5639 FROM ot
5640 WHERE (col_varchar_nokey, 'x') IN
5641  (SELECT col_varchar_key, col_varchar_key2
5642  FROM it
5643  WHERE col_int_key IS NULL);
5644 
5645 --echo
5646 EXPLAIN EXTENDED
5647 SELECT col_int_nokey
5648 FROM ot
5649 WHERE (col_varchar_nokey, 'x') IN
5650  (SELECT col_varchar_key, col_varchar_key2
5651  FROM it
5652  WHERE col_int_key IS NULL);
5653 
5654 --echo
5655 DROP TABLE it, ot;
5656 
5657 --echo #
5658 --echo # Bug #11829691: Pure virtual method called in Item_bool_func2::fix...()
5659 --echo #
5660 
5661 CREATE TABLE t1(a INTEGER);
5662 CREATE TABLE t2(b INTEGER);
5663 
5664 PREPARE stmt FROM "
5665 SELECT SUM(b) FROM t2 GROUP BY b HAVING b IN (SELECT b FROM t1)";
5666 
5667 EXECUTE stmt;
5668 EXECUTE stmt;
5669 
5670 DEALLOCATE PREPARE stmt;
5671 
5672 DROP TABLE t1, t2;
5673 
5674 --echo #
5675 --echo # Bug #13595212 EXTRA ROWS RETURNED ON RIGHT JOIN WITH VIEW AND
5676 --echo # IN-SUBQUERY IN WHERE
5677 --echo #
5678 
5679 CREATE TABLE t1 (
5680  pk int(11) NOT NULL AUTO_INCREMENT,
5681  col_int_key int(11) NOT NULL,
5682  col_varchar_key varchar(1) NOT NULL,
5683  col_varchar_nokey varchar(1) NOT NULL,
5684  PRIMARY KEY (pk),
5685  KEY col_int_key (col_int_key),
5686  KEY col_varchar_key (col_varchar_key,col_int_key)
5687 );
5688 
5689 INSERT INTO t1 VALUES (1,0,'j','j'),(2,8,'v','v'),
5690 (3,1,'c','c'),(4,8,'m','m'),(5,9,'d','d');
5691 
5692 CREATE VIEW v1 AS SELECT * FROM t1;
5693 
5694 # RIGHT JOIN
5695 
5696 let $query=
5697 SELECT alias2.col_varchar_nokey
5698 FROM v1 AS alias1
5699  RIGHT JOIN t1 AS alias2 ON 1
5700 WHERE alias2.col_varchar_key IN (
5701  SELECT sq2_alias1.col_varchar_nokey
5702  FROM v1 AS sq2_alias1
5703  LEFT JOIN t1 AS sq2_alias2
5704  ON (sq2_alias2.col_int_key = sq2_alias1.pk)
5705  WHERE sq2_alias1.pk != alias1.col_int_key
5706  AND sq2_alias1.col_varchar_key > alias1.col_varchar_key
5707 )
5708 ;
5709 
5710 eval EXPLAIN EXTENDED $query;
5711 --sorted_result
5712 eval $query;
5713 
5714 # Same, but in view:
5715 
5716 eval CREATE VIEW v2 AS $query;
5717 
5718 EXPLAIN EXTENDED SELECT * FROM v2;
5719 --sorted_result
5720 SELECT * FROM v2;
5721 
5722 # Same, with LEFT JOIN
5723 
5724 let $query=SELECT alias2.col_varchar_nokey
5725 FROM t1 AS alias2
5726  LEFT JOIN v1 AS alias1 ON 1
5727 WHERE alias2.col_varchar_key IN (
5728  SELECT sq2_alias1.col_varchar_nokey
5729  FROM v1 AS sq2_alias1
5730  LEFT JOIN t1 AS sq2_alias2
5731  ON (sq2_alias2.col_int_key = sq2_alias1.pk)
5732  WHERE sq2_alias1.pk != alias1.col_int_key
5733  AND sq2_alias1.col_varchar_key > alias1.col_varchar_key
5734 );
5735 
5736 eval EXPLAIN EXTENDED $query;
5737 --sorted_result
5738 eval $query;
5739 
5740 DROP TABLE t1;
5741 DROP VIEW v1,v2;
5742 
5743 --echo #
5744 --echo # Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
5745 --echo # HAS AN EMPTY RESULT
5746 --echo #
5747 
5748 CREATE TABLE t1 (
5749  pk int NOT NULL,
5750  col_int_nokey int NOT NULL,
5751  col_int_key int NOT NULL,
5752  col_time_key time NOT NULL,
5753  col_varchar_key varchar(1) NOT NULL,
5754  col_varchar_nokey varchar(1) NOT NULL,
5755  PRIMARY KEY (pk),
5756  KEY col_int_key (col_int_key),
5757  KEY col_time_key (col_time_key),
5758  KEY col_varchar_key (col_varchar_key,col_int_key)
5759 ) ENGINE=MyISAM;
5760 
5761 CREATE TABLE t2 (
5762  pk int NOT NULL AUTO_INCREMENT,
5763  col_int_nokey int NOT NULL,
5764  col_int_key int NOT NULL,
5765  col_time_key time NOT NULL,
5766  col_varchar_key varchar(1) NOT NULL,
5767  col_varchar_nokey varchar(1) NOT NULL,
5768  PRIMARY KEY (pk),
5769  KEY col_int_key (col_int_key),
5770  KEY col_time_key (col_time_key),
5771  KEY col_varchar_key (col_varchar_key,col_int_key)
5772 ) ENGINE=MyISAM;
5773 
5774 INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
5775 
5776 SET @var2:=4, @var3:=8;
5777 
5778 --echo
5779 --echo Testcase without inner subquery
5780 
5781 let $subq=
5782 SELECT @var3:=12, sq4_alias1.*
5783 FROM t1 AS sq4_alias1
5784 WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
5785  sq4_alias1.col_varchar_key = @var3;
5786 
5787 eval EXPLAIN $subq;
5788 eval $subq;
5789 SELECT @var3;
5790 
5791 # Now as derived table:
5792 eval EXPLAIN SELECT * FROM ( $subq ) AS alias3;
5793 eval SELECT * FROM ( $subq ) AS alias3;
5794 SELECT @var3;
5795 
5796 --echo
5797 --echo Testcase with inner subquery; crashed WL#6095
5798 SET @var3=8;
5799 let $subq=
5800 SELECT sq4_alias1.*
5801 FROM t1 AS sq4_alias1
5802 WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
5803  NOT IN
5804  (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
5805  c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
5806  FROM t2 AS c_sq1_alias1
5807  WHERE (c_sq1_alias1.col_int_nokey != @var2
5808  OR c_sq1_alias1.pk != @var3));
5809 
5810 eval EXPLAIN $subq;
5811 eval $subq;
5812 # Now as derived table:
5813 eval EXPLAIN SELECT * FROM ( $subq ) AS alias3;
5814 eval SELECT * FROM ( $subq ) AS alias3;
5815 
5816 DROP TABLE t1,t2;
5817 
5818 --echo #
5819 --echo # Test that indexsubquery_engine only does one lookup if
5820 --echo # the technique is unique_subquery: does not try to read the
5821 --echo # next row if the first row failed the subquery's WHERE
5822 --echo # condition (here: b=3).
5823 --echo #
5824 
5825 create table t1(a int);
5826 insert into t1 values(1),(2);
5827 create table t2(a int primary key, b int);
5828 insert into t2 values(1,10),(2,10);
5829 let $query=select * from t1 where a in (select a from t2 where b=3);
5830 eval explain $query;
5831 flush status;
5832 eval $query;
5833 show status like "handler_read%";
5834 drop table t1,t2;
5835 
5836 --echo #
5837 --echo # Bug#13735980 Difference in number of rows when using subqueries
5838 --echo #
5839 
5840 CREATE TABLE m (c1 VARCHAR(1), c2 INTEGER, c3 INTEGER);
5841 INSERT INTO m VALUES ('',6,8), ('',75,NULL);
5842 
5843 CREATE TABLE o (c1 VARCHAR(1));
5844 INSERT INTO o VALUES ('S'), ('S'), ('S');
5845 
5846 CREATE VIEW v1 AS
5847 SELECT m.c1 AS c1,m.c2 AS c2,m.c3 AS c3 FROM m;
5848 
5849 CREATE VIEW v2 AS
5850 SELECT o.c1 AS c1 FROM o;
5851 
5852 SELECT t1.c3
5853 FROM v1 AS t1 JOIN v2 AS t2 ON t1.c1 < t2.c1
5854 WHERE t1.c3 BETWEEN 2 AND 6 OR
5855  (t1.c3 IN(4) AND t1.c3 <> 2) OR t1.c2 >= 8;
5856 
5857 CREATE TABLE integers (i1 INTEGER);
5858 INSERT IGNORE INTO integers VALUES (2),(4),(6),(8);
5859 
5860 SELECT t1.c3
5861 FROM v1 AS t1 JOIN v2 AS t2 ON t1.c1 < t2.c1
5862 WHERE t1.c3 BETWEEN (SELECT i1 FROM integers WHERE i1 = 2) AND
5863  (SELECT i1 FROM integers WHERE i1 = 6) OR
5864  t1.c3 IN((SELECT i1 FROM integers WHERE i1 = 4)) AND
5865  t1.c3 <>(SELECT i1 FROM integers WHERE i1 = 2) OR
5866  t1.c2 >=(SELECT i1 FROM integers WHERE i1 = 8);
5867 
5868 # Outcome is unaffected when replacing views with tables:
5869 
5870 SELECT t1.c3
5871 FROM m AS t1 JOIN o AS t2 ON t1.c1 < t2.c1
5872 WHERE t1.c3 BETWEEN (SELECT i1 FROM integers WHERE i1 = 2) AND
5873  (SELECT i1 FROM integers WHERE i1 = 6) OR
5874  t1.c3 IN((SELECT i1 FROM integers WHERE i1 = 4)) AND
5875  t1.c3 <>(SELECT i1 FROM integers WHERE i1 = 2) OR
5876  t1.c2 >=(SELECT i1 FROM integers WHERE i1 = 8);
5877 
5878 DROP VIEW v1, v2;
5879 DROP TABLE m, o, integers;
5880 
5881 --echo #
5882 --echo # Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5883 --echo #
5884 
5885 CREATE TABLE t1(a TIME NOT NULL);
5886 INSERT INTO t1 VALUES ('00:00:32');
5887 
5888 SELECT 1 FROM t1 WHERE a >
5889 (SELECT timestamp(a) AS a FROM t1);
5890 
5891 DROP TABLE t1;
5892 
5893 --echo #
5894 --echo # Bug #13736664 RIGHT JOIN+WHERE QUERY GIVES DIFF RESULTS WHEN USING
5895 --echo # SELECT * SUBQ FOR TABLES
5896 --echo #
5897 
5898 CREATE TABLE t1(a INT, b CHAR(1));
5899 INSERT INTO t1 VALUES (NULL, 'x');
5900 
5901 CREATE TABLE t2(c INT, d CHAR(1));
5902 INSERT INTO t2 VALUES (NULL, 'y'), (9, 'z');
5903 
5904 SELECT d
5905 FROM t2 AS a2 RIGHT JOIN t1 AS a1 ON c = a
5906 WHERE d LIKE '_'
5907 ORDER BY d;
5908 
5909 SELECT d
5910 FROM (SELECT * FROM t2) AS a2 RIGHT JOIN (SELECT * FROM t1) AS a1 ON c = a
5911 WHERE d LIKE '_'
5912 ORDER BY d;
5913 
5914 DROP TABLE t1, t2;
5915 
5916 --echo #
5917 --echo # Bug#13468414 Query shows different results when literals are selected
5918 --echo # from separate table
5919 --echo #
5920 
5921 CREATE TABLE t1 (
5922  col_varchar_key varchar(1) NOT NULL,
5923  col_varchar_nokey varchar(1) NOT NULL,
5924  KEY col_varchar_key (col_varchar_key)
5925 ) ENGINE=InnoDB;
5926 
5927 INSERT INTO t1 VALUES
5928  ('v','v'), ('s','s'), ('l','l'), ('y','y'), ('c','c'),
5929  ('i','i'), ('h','h'), ('q','q'), ('a','a'), ('v','v'),
5930  ('u','u'), ('s','s'), ('y','y'), ('z','z'), ('h','h'),
5931  ('p','p'), ('e','e'), ('i','i'), ('y','y'), ('w','w');
5932 
5933 CREATE TABLE t2 (
5934  col_int_nokey INT NOT NULL,
5935  col_varchar_nokey varchar(1) NOT NULL
5936 ) ENGINE=InnoDB;
5937 
5938 INSERT INTO t2 VALUES
5939  (4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
5940  (3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
5941  (1,'q'), (6,'w'), (2,'d'), (9,'e');
5942 
5943 CREATE TABLE t0 (
5944  i1 INTEGER NOT NULL PRIMARY KEY
5945 );
5946 INSERT INTO t0 VALUES (7);
5947 
5948 -- disable_query_log
5949 -- disable_result_log
5950 ANALYZE TABLE t0;
5951 ANALYZE TABLE t1;
5952 ANALYZE TABLE t2;
5953 -- enable_result_log
5954 -- enable_query_log
5955 
5956 SELECT col_varchar_nokey
5957 FROM t1
5958 WHERE (7) NOT IN
5959  (SELECT it2.col_int_nokey
5960  FROM t2 AS it2 LEFT JOIN t1 AS it1
5961  ON it2.col_varchar_nokey = it1.col_varchar_key
5962  );
5963 
5964 let $query=
5965 SELECT col_varchar_nokey
5966 FROM t1
5967 WHERE (SELECT i1 FROM t0 WHERE i1 = 7) NOT IN
5968  (SELECT it2.col_int_nokey
5969  FROM t2 AS it2 LEFT JOIN t1 AS it1
5970  ON it2.col_varchar_nokey = it1.col_varchar_key
5971  );
5972 
5973 eval $query;
5974 eval explain extended $query;
5975 
5976 DROP TABLE t0, t1, t2;
5977 
5978 --echo #
5979 --echo # Bug#13735704 Memory engine + NOT IN + subquery produces different
5980 --echo # result set
5981 --echo #
5982 
5983 CREATE TABLE t1(
5984  col_varchar_key varchar(1),
5985  KEY col_varchar_key(col_varchar_key)
5986 ) engine=MEMORY;
5987 INSERT INTO t1 VALUES
5988  ('v'), ('s'), ('y'), ('z'), ('h'), ('p');
5989 
5990 CREATE TABLE t2(
5991  col_int_nokey int,
5992  col_int_key int,
5993  KEY col_int_key(col_int_key)
5994 ) engine=MEMORY;
5995 INSERT INTO t2 VALUES
5996  (4,4), (2,7), (2,5), (2,3), (197,188), (4,4), (6,2), (1,4),
5997  (156,231), (2,1), (1,2), (80,194), (3,2), (8,3), (91,98), (6,3),
5998  (7,1), (3,0), (161,189), (7,8), (7,3), (213,120), (248,25), (1,1),
5999  (6,3), (6,1), (3,3), (140,153), (0,5), (7,9), (6,1), (1,2),
6000  (1,7), (9,2), (0,1), (6,5);
6001 
6002 CREATE TABLE t0(i1 INTEGER NOT NULL PRIMARY KEY);
6003 INSERT INTO t0 VALUES(7),(2);
6004 
6005 SELECT col_varchar_key
6006 FROM t1
6007 WHERE (7,2) NOT IN
6008  (SELECT col_int_key,col_int_nokey FROM t2);
6009 
6010 let $query=
6011 SELECT col_varchar_key
6012 FROM t1
6013 WHERE ((SELECT i1 FROM t0 WHERE i1 = 7),
6014  (SELECT i1 FROM t0 WHERE i1 = 2)) NOT IN
6015  (SELECT col_int_key,col_int_nokey FROM t2);
6016 
6017 eval explain extended $query;
6018 eval $query;
6019 
6020 DROP TABLE t0, t1, t2;
6021 
6022 
6023 --echo #
6024 --echo # Bug #13639204 64111: CRASH ON SELECT SUBQUERY WITH NON UNIQUE
6025 --echo # INDEX
6026 --echo #
6027 CREATE TABLE t1 (
6028 id int
6029 ) ENGINE=InnoDB;
6030 INSERT INTO t1 (id) VALUES (11);
6031 
6032 CREATE TABLE t2 (
6033 t1_id int,
6034 position int,
6035 KEY t1_id (t1_id),
6036 KEY t1_id_position (t1_id,position)
6037 ) ENGINE=InnoDB;
6038 
6039 let $query=SELECT
6040 (SELECT position FROM t2
6041 WHERE t2.t1_id = t1.id
6042 ORDER BY t2.t1_id , t2.position
6043 LIMIT 10,1
6044 ) AS maxkey
6045 FROM t1
6046 LIMIT 1;
6047 
6048 eval EXPLAIN $query;
6049 eval $query;
6050 
6051 DROP TABLE t1,t2;
6052 
6053 
6054 --echo #
6055 --echo # Bug#13731417 WRONG RESULT WITH NOT IN (SUBQUERY) AND
6056 --echo # COMPOSITE INDEX ON SUBQUERY'S TABLE
6057 --echo #
6058 
6059 CREATE TABLE t1 (a int, b int);
6060 CREATE TABLE t2 (a int, b int, KEY a_b (a,b));
6061 CREATE TABLE t4 (a int);
6062 INSERT INTO t1 VALUES(0,1);
6063 INSERT INTO t2 VALUES(NULL,1),(NULL,1);
6064 INSERT INTO t4 VALUES(1);
6065 
6066 SELECT ((0,1) NOT IN (SELECT NULL,1)) IS NULL;
6067 
6068 let $query=SELECT * FROM t1
6069 WHERE ((a,b) NOT IN (SELECT t2.a,t2.b FROM
6070  t4 STRAIGHT_JOIN t2 WHERE t4.a=t2.b)) IS NULL;
6071 
6072 eval EXPLAIN EXTENDED $query;
6073 eval $query;
6074 
6075 ALTER TABLE t2 DROP INDEX a_b;
6076 
6077 EVAL EXPLAIN EXTENDED $query;
6078 eval $query;
6079 
6080 DROP TABLE t1,t2,t4;
6081 
6082 --echo #
6083 --echo # Bug#13725821 ASSERT NULL_REF_KEY == __NULL FAILED IN
6084 --echo # CREATE_REF_FOR_KEY() IN SQL_SELECT.CC
6085 --echo #
6086 
6087 CREATE TABLE t1 (
6088  pk INT NOT NULL,
6089  col_int_nokey INT,
6090  col_int_key INT,
6091  col_varchar_key VARCHAR(1),
6092  col_varchar_nokey VARCHAR(1),
6093  PRIMARY KEY (pk),
6094  KEY col_int_key (col_int_key),
6095  KEY col_varchar_key (col_varchar_key,col_int_key)
6096 ) ENGINE=MyISAM;
6097 
6098 INSERT INTO t1 VALUES (17,NULL,9,NULL,NULL),
6099 (18,2,2,'o','o'),(19,NULL,9,'w','w'),(20,6,2,'m','m'),(21,7,4,'q','q'),
6100 (22,2,0,NULL,NULL),(23,5,4,'d','d'),(24,7,8,'g','g'),(25,6,NULL,'x','x'),
6101 (26,6,NULL,'f','f'),(27,2,0,'p','p'),(28,9,NULL,'j','j'),(29,6,8,'c','c');
6102 
6103 ALTER TABLE t1 DISABLE KEYS;
6104 ALTER TABLE t1 ENABLE KEYS;
6105 
6106 let $query=
6107 SELECT table1.col_varchar_key AS field1,
6108  table1.col_int_nokey AS field2,
6109  table2.col_varchar_key AS field3,
6110  table1.col_int_nokey AS field4,
6111  table2.col_int_nokey AS field5,
6112  table1.col_varchar_nokey AS field6
6113 FROM ( t1 AS table1
6114  INNER JOIN t1 AS table2
6115  ON (( table2.col_int_nokey = table1.col_int_key )
6116  AND ( table2.col_int_key = table1.col_int_key )
6117  ) )
6118 WHERE ( table1.col_varchar_key = table2.col_varchar_key OR table1.pk = 154 )
6119  OR table1.pk != 201
6120  AND ( table1.col_varchar_key LIKE '%a%' OR table1.col_varchar_key LIKE
6121 '%b%')
6122 ;
6123 
6124 eval CREATE TABLE t2 $query ;
6125 
6126 eval SELECT *
6127  FROM t2
6128  WHERE (field1, field2, field3, field4, field5, field6) NOT IN ( $query
6129 );
6130 
6131 DROP TABLE t2,t1;
6132 
6133 --echo #
6134 --echo # Bug #13735712 SELECT W/ SUBQUERY PRODUCES MORE ROWS WHEN USING
6135 --echo # VARIABLES
6136 --echo #
6137 
6138 CREATE TABLE t1(a INT);
6139 INSERT INTO t1 VALUES(9);
6140 CREATE TABLE t2(b INT);
6141 INSERT INTO t2 VALUES(8);
6142 CREATE TABLE t3(c INT);
6143 INSERT INTO t3 VALUES(3);
6144 
6145 SELECT *
6146 FROM t2 RIGHT JOIN t3 ON(c = b)
6147 WHERE b < ALL(SELECT a FROM t1 WHERE a <= 7);
6148 SET @var = 7;
6149 SELECT *
6150 FROM t2 RIGHT JOIN t3 ON(c = b)
6151 WHERE b < ALL(SELECT a FROM t1 WHERE a <= @var);
6152 
6153 DROP TABLE t1, t2, t3;
6154 
6155 --echo #
6156 --echo # Bug #13330886 TOO MANY ROWS WITH ALL|ANY
6157 --echo #
6158 
6159 CREATE TABLE t1 (a INT, b INT);
6160 INSERT INTO t1 VALUES (NULL,8), (8,7);
6161 CREATE TABLE t2 (c INT);
6162 INSERT INTO t2 VALUES (10);
6163 
6164 SELECT 1 FROM t2 WHERE c > ALL (SELECT a FROM t1 WHERE b >= 3);
6165 SET @var = 3;
6166 SELECT 1 FROM t2 WHERE c > ALL (SELECT a FROM t1 WHERE b >= @var);
6167 
6168 DROP TABLE t1, t2;
6169 
6170 CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL);
6171 INSERT INTO t1 VALUES (0,8), (8,7);
6172 CREATE TABLE t2 (c INT NOT NULL);
6173 INSERT INTO t2 VALUES (10);
6174 
6175 EXPLAIN EXTENDED
6176 SELECT 1 FROM t2 WHERE c > ANY (SELECT a FROM t1 WHERE b >= 3) IS TRUE;
6177 SELECT 1 FROM t2 WHERE c > ANY (SELECT a FROM t1 WHERE b >= 3) IS TRUE;
6178 
6179 DROP TABLE t1, t2;
6180 
6181 set @@optimizer_switch=@old_opt_switch;
6182 # New tests go here.