subselect4.result 105 KB
Newer Older
Sergey Petrunya's avatar
Sergey Petrunya committed
1
drop table if exists t0,t1,t2,t3,t4,t5,t6;
2
drop view if exists v1, v2;
3 4
set @subselect4_tmp= @@optimizer_switch;
set optimizer_switch='semijoin=on,firstmatch=on,loosescan=on';
Igor Babaev's avatar
Igor Babaev committed
5
set optimizer_switch='semijoin_with_cache=on';
6
set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# Bug #46791: Assertion failed:(table->key_read==0),function unknown
#    function,file sql_base.cc
#
CREATE TABLE t1 (a INT, b INT, KEY(a));
INSERT INTO t1 VALUES (1,1),(2,2);
CREATE TABLE t2 LIKE t1;
INSERT INTO t2 VALUES (1,1),(2,2);
CREATE TABLE t3 LIKE t1;
# should have 1 impossible where and 2 dependent subqueries
EXPLAIN
SELECT 1 FROM t1
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
ORDER BY count(*);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
22
1	PRIMARY	t1	index	NULL	a	5	NULL	2	Using index
23
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
24
3	DEPENDENT SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	Const row not found
25 26 27 28 29 30 31 32 33 34 35
# should not crash the next statement
SELECT 1 FROM t1
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
ORDER BY count(*);
1
1
# should not crash: the crash is caused by the previous statement
SELECT 1;
1
1
DROP TABLE t1,t2,t3;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#
# Bug #47106: Crash / segfault on adding EXPLAIN to a non-crashing 
# query
#
CREATE TABLE t1 (
a INT,
b INT,
PRIMARY KEY (a),
KEY b (b)
);
INSERT INTO t1 VALUES (1, 1), (2, 1);
CREATE TABLE t2 LIKE t1;
INSERT INTO t2 SELECT * FROM t1;
CREATE TABLE t3 LIKE t1;
INSERT INTO t3 SELECT * FROM t1;
# Should not crash.
# Should have 1 impossible where and 2 dependent subqs.
EXPLAIN
SELECT
(SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
FROM t3 WHERE 1 = 0 GROUP BY 1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	DEPENDENT SUBQUERY	t1	index	NULL	PRIMARY	4	NULL	2	Using index
60
2	DEPENDENT SUBQUERY	t2	ALL	b	NULL	NULL	NULL	2	Range checked for each record (index map: 0x2)
61 62 63 64 65 66
# should return 0 rows
SELECT
(SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
FROM t3 WHERE 1 = 0 GROUP BY 1;
(SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
DROP TABLE t1,t2,t3;
67
End of 5.0 tests.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
CREATE TABLE t1 (col_int_nokey int(11) NOT NULL, col_varchar_nokey varchar(1) NOT NULL) engine=myisam;
INSERT INTO t1 VALUES (2,'s'),(0,'v'),(2,'s');
CREATE TABLE t2 (
pk int(11) NOT NULL AUTO_INCREMENT,
`col_int_key` int(11) NOT NULL,
col_varchar_key varchar(1) NOT NULL,
PRIMARY KEY (pk),
KEY `col_int_key` (`col_int_key`),
KEY `col_varchar_key` (`col_varchar_key`)
) ENGINE=MyISAM;
INSERT INTO t2 VALUES (4,10,'g'), (5,20,'v');
SELECT t1.col_int_nokey,(SELECT MIN( t2_a.col_int_key ) FROM t2 t2_a, t2 t2_b, t1 t1_a WHERE t1_a.col_varchar_nokey = t2_b.col_varchar_key and t1.col_int_nokey ) as sub FROM t1;
col_int_nokey	sub
2	10
0	NULL
2	10
SELECT t1.col_int_nokey,(SELECT MIN( t2_a.col_int_key ) +1 FROM t2 t2_a, t2 t2_b, t1 t1_a WHERE t1_a.col_varchar_nokey = t2_b.col_varchar_key and t1.col_int_nokey ) as sub FROM t1;
col_int_nokey	sub
2	11
0	NULL
2	11
DROP TABLE t1,t2;
90
#
91 92 93 94 95
# Bug#54568: create view cause Assertion failed: 0, 
# file .\item_subselect.cc, line 836
#
EXPLAIN SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
96
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
97 98 99 100
Warnings:
Note	1249	Select 2 was reduced during optimization
DESCRIBE SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
101
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
102 103 104 105 106 107
Warnings:
Note	1249	Select 2 was reduced during optimization
# None of the below should crash
CREATE VIEW v1 AS SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
CREATE VIEW v2 AS SELECT 1 LIKE '%' ESCAPE ( 1 IN ( SELECT 1 ) );
DROP VIEW v1, v2;
108 109 110 111 112 113 114 115 116 117 118 119 120 121
# 
# Bug#51070: Query with a NOT IN subquery predicate returns a wrong
# result set
# 
CREATE TABLE t1 ( a INT, b INT );
INSERT INTO t1 VALUES ( 1, NULL ), ( 2, NULL );
CREATE TABLE t2 ( c INT, d INT );
INSERT INTO t2 VALUES ( NULL, 3 ), ( NULL, 4 );
CREATE TABLE t3 ( e INT, f INT );
INSERT INTO t3 VALUES ( NULL, NULL ), ( NULL, NULL );
CREATE TABLE t4 ( a INT );
INSERT INTO t4 VALUES (1), (2), (3);
CREATE TABLE t5 ( a INT );
INSERT INTO t5 VALUES (NULL), (2);
122 123
SET @old_optimizer_switch = @@session.optimizer_switch;
SET SESSION optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
124 125 126 127
EXPLAIN
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
x	PRIMARY	x	x	x	x	x	x	x	x
128
x	DEPENDENT SUBQUERY	x	x	x	x	x	x	x	x
129 130 131 132 133 134
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 );
a	b
EXPLAIN
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
135
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS NULL;
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE ( a, b ) IN ( SELECT c, d FROM t2 ) IS NULL;
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS UNKNOWN;
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE (( a, b ) NOT IN ( SELECT c, d FROM t2 )) IS UNKNOWN;
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE 1 = 1 AND ( a, b ) NOT IN ( SELECT c, d FROM t2 );
a	b
EXPLAIN
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT e, f FROM t3 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
x	PRIMARY	x	x	x	x	x	x	x	x
158
x	DEPENDENT SUBQUERY	x	x	x	x	x	x	x	x
159 160 161 162 163 164
SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT e, f FROM t3 );
a	b
EXPLAIN
SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT a, b FROM t1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
x	PRIMARY	x	x	x	x	x	x	x	x
165
x	DEPENDENT SUBQUERY	x	x	x	x	x	x	x	x
166 167 168 169 170 171
SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT a, b FROM t1 );
c	d
EXPLAIN
SELECT * FROM t3 WHERE ( e, f ) NOT IN ( SELECT c, d FROM t2 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
x	PRIMARY	x	x	x	x	x	x	x	x
172
x	DEPENDENT SUBQUERY	x	x	x	x	x	x	x	x
173 174 175 176 177 178
SELECT * FROM t3 WHERE ( e, f ) NOT IN ( SELECT c, d FROM t2 );
e	f
EXPLAIN
SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT e, f FROM t3 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
x	PRIMARY	x	x	x	x	x	x	x	x
179
x	DEPENDENT SUBQUERY	x	x	x	x	x	x	x	x
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT e, f FROM t3 );
c	d
SELECT * FROM t1 WHERE ( a, b ) NOT IN 
( SELECT c, d FROM t2 WHERE c = 1 AND c <> 1 );
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE b NOT IN ( SELECT c FROM t2 WHERE c = 1 );
a	b
1	NULL
2	NULL
SELECT * FROM t1 WHERE NULL NOT IN ( SELECT c FROM t2 WHERE c = 1 AND c <> 1 );
a	b
1	NULL
2	NULL
195
SET SESSION optimizer_switch = @old_optimizer_switch;
196
DROP TABLE t1, t2, t3, t4, t5;
197
#
198 199 200 201 202 203 204 205 206 207 208
# Bug#58207: invalid memory reads when using default column value and 
# tmptable needed
#
CREATE TABLE t(a VARCHAR(245) DEFAULT
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
INSERT INTO t VALUES (''),(''),(''),(''),(''),(''),(''),(''),(''),(''),('');
SELECT * FROM (SELECT default(a) FROM t GROUP BY a) d;
default(a)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
DROP TABLE t;
#
209 210 211 212 213 214 215 216 217 218 219 220 221
# LP BUG#1009187, MDEV-373, MYSQL bug#58628
# Wrong result for a query with [NOT] IN subquery predicate if
# the left part of the predicate is explicit NULL
#
CREATE TABLE t1 (pk INT NOT NULL, i INT NOT NULL);
INSERT INTO t1 VALUES (0,10), (1,20), (2,30), (3,40);
CREATE TABLE t2a (pk INT NOT NULL, i INT NOT NULL, PRIMARY KEY(i,pk));
INSERT INTO t2a VALUES (0,0), (1,1), (2,2), (3,3);
CREATE TABLE t2b (pk INT, i INT);
INSERT INTO t2b VALUES (0,0), (1,1), (2,2), (3,3);
CREATE TABLE t2c (pk INT NOT NULL, i INT NOT NULL);
INSERT INTO t2c VALUES (0,0), (1,1), (2,2), (3,3);
create index it2c on t2c (i,pk);
222 223
CREATE TABLE t2d (pk INT NOT NULL, i INT NOT NULL, PRIMARY KEY(i));
INSERT INTO t2d VALUES (0,0), (1,1), (2,2), (3,3);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
EXPLAIN
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2a	unique_subquery	PRIMARY	PRIMARY	8	const,test.t1.pk	1	Using index; Using where; Full scan on NULL key
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
pk	i
SELECT * FROM t1 WHERE 1+NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
pk	i
SELECT * FROM t1 WHERE NULL     IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk) IS UNKNOWN;
pk	i
0	10
1	20
2	30
3	40
SELECT t1.pk, NULL NOT IN (SELECT t2a.i FROM t2a  WHERE t2a.pk = t1.pk) FROM t1;
pk	NULL NOT IN (SELECT t2a.i FROM t2a  WHERE t2a.pk = t1.pk)
0	NULL
1	NULL
2	NULL
3	NULL
EXPLAIN
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2b	ALL	NULL	NULL	NULL	NULL	4	Using where
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk);
pk	i
SELECT * FROM t1 WHERE NULL     IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk) IS UNKNOWN;
pk	i
0	10
1	20
2	30
3	40
SELECT t1.pk, NULL NOT IN (SELECT t2b.i FROM t2b  WHERE t2b.pk = t1.pk) FROM t1;
pk	NULL NOT IN (SELECT t2b.i FROM t2b  WHERE t2b.pk = t1.pk)
0	NULL
1	NULL
2	NULL
3	NULL
EXPLAIN
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2c	index_subquery	it2c	it2c	8	const,test.t1.pk	2	Using index; Using where; Full scan on NULL key
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk);
pk	i
SELECT * FROM t1 WHERE NULL     IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk) IS UNKNOWN;
pk	i
0	10
1	20
2	30
3	40
SELECT t1.pk, NULL NOT IN (SELECT t2c.i FROM t2c  WHERE t2c.pk = t1.pk) FROM t1;
pk	NULL NOT IN (SELECT t2c.i FROM t2c  WHERE t2c.pk = t1.pk)
0	NULL
1	NULL
2	NULL
3	NULL
EXPLAIN
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2d	const	PRIMARY	PRIMARY	4	const	1	Using where; Full scan on NULL key
SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk);
pk	i
SELECT * FROM t1 WHERE NULL     IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk) IS UNKNOWN;
pk	i
0	10
1	20
2	30
3	40
SELECT t1.pk, NULL NOT IN (SELECT t2d.i FROM t2d  WHERE t2d.pk = t1.pk) FROM t1;
pk	NULL NOT IN (SELECT t2d.i FROM t2d  WHERE t2d.pk = t1.pk)
0	NULL
1	NULL
2	NULL
3	NULL
EXPLAIN
303 304 305
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
306
2	DEPENDENT SUBQUERY	t2a	unique_subquery	PRIMARY	PRIMARY	8	const,test.t1.pk	1	Using index; Using where; Full scan on NULL key
307 308
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk);
pk	i
309 310 311
0	10
2	30
3	40
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
SELECT (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk) from t1;
(NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk)
1
NULL
1
1
EXPLAIN
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2b	ALL	NULL	NULL	NULL	NULL	4	Using where
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk);
pk	i
0	10
2	30
3	40
SELECT (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk) from t1;
(NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk)
1
NULL
1
1
EXPLAIN
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
338
2	DEPENDENT SUBQUERY	t2c	index_subquery	it2c	it2c	8	const,test.t1.pk	2	Using index; Using where; Full scan on NULL key
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk);
pk	i
0	10
2	30
3	40
SELECT (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk) from t1;
(NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk)
1
NULL
1
1
EXPLAIN
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
2	DEPENDENT SUBQUERY	t2d	const	PRIMARY	PRIMARY	4	const	1	Using where; Full scan on NULL key
SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk);
pk	i
0	10
2	30
3	40
SELECT (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk) from t1;
(NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk)
1
NULL
1
1
drop table t1, t2a, t2b, t2c, t2d;
367
#
368 369
# End of 5.1 tests.
#
Georgi Kodinov's avatar
merge  
Georgi Kodinov committed
370
#
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
# BUG#46743 "Azalea processing correlated, aggregate SELECT
# subqueries incorrectly"
#
CREATE TABLE t1 (c int);
INSERT INTO t1 VALUES (NULL);
CREATE TABLE t2 (d int , KEY (d));
INSERT INTO t2 VALUES (NULL),(NULL);
0 rows in subquery
SELECT 1 AS RESULT FROM t2,t1 WHERE d = c;
RESULT
base query
SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
RESULT
NULL
EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00	
388
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
389 390
Warnings:
Note	1276	Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
391
Note	1003	/* select#1 */ select (/* select#2 */ select 1 from `test`.`t2` where 0) AS `RESULT` from dual
392 393 394 395 396 397 398
first equivalent variant
SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c ;
RESULT
NULL
EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00	
399
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
400 401
Warnings:
Note	1276	Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
402
Note	1003	/* select#1 */ select (/* select#2 */ select 1 from `test`.`t2` where 0) AS `RESULT` from dual group by NULL
403 404 405 406 407 408 409
second equivalent variant
SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
RESULT
NULL
EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00	
410
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
411 412
Warnings:
Note	1276	Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
413
Note	1003	/* select#1 */ select (/* select#2 */ select 1 from `test`.`t2` where 0) AS `RESULT` from dual group by NULL
414 415 416 417 418 419 420 421 422 423 424 425 426 427
DROP TABLE t1,t2;
#
# BUG#45928 "Differing query results depending on MRR and
# engine_condition_pushdown settings"
#
CREATE TABLE `t1` (
`pk` int(11) NOT NULL AUTO_INCREMENT,
`time_nokey` time NOT NULL,
`varchar_key` varchar(1) NOT NULL,
`varchar_nokey` varchar(1) NOT NULL,
PRIMARY KEY (`pk`),
KEY `varchar_key` (`varchar_key`)
) AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
INSERT INTO `t1` VALUES (10,'00:00:00','i','i'),(11,'00:00:00','','');
428 429
set @old_optimizer_switch          = @@session.optimizer_switch;
SET SESSION OPTIMIZER_SWITCH = 'materialization=off,semijoin=off,loosescan=off,firstmatch=off,mrr=on,engine_condition_pushdown=on';
430
Warnings:
431
Warning	1681	'engine_condition_pushdown=on' is deprecated and will be removed in a future release
432 433 434 435
SELECT `time_nokey` G1  FROM t1  WHERE ( `varchar_nokey`  , `varchar_key`  )  IN ( 
SELECT `varchar_nokey`  , `varchar_nokey`  )  AND `varchar_key`  >= 'c' HAVING G1  ORDER 
BY `pk`   ;
G1
436
set @@session.optimizer_switch          = @old_optimizer_switch;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
DROP TABLE t1;
#
# During work with BUG#45863 I had problems with a query that was
# optimized differently in regular and prepared mode.
# Because there was a bug in one of the selected strategies, I became
# aware of the problem. Adding an EXPLAIN query to catch this.
DROP TABLE IF EXISTS t1, t2, t3;
CREATE TABLE t1
(EMPNUM   CHAR(3) NOT NULL,
EMPNAME  CHAR(20),
GRADE    DECIMAL(4),
CITY     CHAR(15));
CREATE TABLE t2
(PNUM     CHAR(3) NOT NULL,
PNAME    CHAR(20),
PTYPE    CHAR(6),
BUDGET   DECIMAL(9),
CITY     CHAR(15));
CREATE TABLE t3
(EMPNUM   CHAR(3) NOT NULL,
PNUM     CHAR(3) NOT NULL,
HOURS    DECIMAL(5));
INSERT INTO t1 VALUES ('E1','Alice',12,'Deale');
INSERT INTO t1 VALUES ('E2','Betty',10,'Vienna');
INSERT INTO t1 VALUES ('E3','Carmen',13,'Vienna');
INSERT INTO t1 VALUES ('E4','Don',12,'Deale');
INSERT INTO t1 VALUES ('E5','Ed',13,'Akron');
INSERT INTO t2 VALUES ('P1','MXSS','Design',10000,'Deale');
INSERT INTO t2 VALUES ('P2','CALM','Code',30000,'Vienna');
INSERT INTO t2 VALUES ('P3','SDP','Test',30000,'Tampa');
INSERT INTO t2 VALUES ('P4','SDP','Design',20000,'Deale');
INSERT INTO t2 VALUES ('P5','IRM','Test',10000,'Vienna');
INSERT INTO t2 VALUES ('P6','PAYR','Design',50000,'Deale');
INSERT INTO t3 VALUES  ('E1','P1',40);
INSERT INTO t3 VALUES  ('E1','P2',20);
INSERT INTO t3 VALUES  ('E1','P3',80);
INSERT INTO t3 VALUES  ('E1','P4',20);
INSERT INTO t3 VALUES  ('E1','P5',12);
INSERT INTO t3 VALUES  ('E1','P6',12);
INSERT INTO t3 VALUES  ('E2','P1',40);
INSERT INTO t3 VALUES  ('E2','P2',80);
INSERT INTO t3 VALUES  ('E3','P2',20);
INSERT INTO t3 VALUES  ('E4','P2',20);
INSERT INTO t3 VALUES  ('E4','P4',40);
INSERT INTO t3 VALUES  ('E4','P5',80);
SET @old_optimizer_switch = @@session.optimizer_switch;
SET @old_join_cache_level = @@session.join_cache_level;
unknown's avatar
unknown committed
484
SET SESSION optimizer_switch = 'firstmatch=on,loosescan=on,materialization=on,in_to_exists=off,semijoin=on';
485 486 487 488 489 490 491 492 493 494 495 496 497
SET SESSION join_cache_level = 1;
CREATE UNIQUE INDEX t1_IDX ON t1(EMPNUM);
EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
(SELECT EMPNUM
FROM t3
WHERE PNUM IN
(SELECT PNUM
FROM t2
WHERE PTYPE = 'Design'));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
498
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
499 500
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
501 502 503 504 505 506 507 508 509 510 511 512
PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
   (SELECT EMPNUM
    FROM t3
    WHERE PNUM IN
       (SELECT PNUM
        FROM t2
        WHERE PTYPE = 'Design'))";
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
513
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
514 515
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
516 517
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
518 519
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
520 521
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
522 523 524 525 526 527 528 529 530 531 532 533 534 535
DEALLOCATE PREPARE stmt;
DROP INDEX t1_IDX ON t1;
CREATE INDEX t1_IDX ON t1(EMPNUM);
EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
(SELECT EMPNUM
FROM t3
WHERE PNUM IN
(SELECT PNUM
FROM t2
WHERE PTYPE = 'Design'));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
536
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
537 538
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
539 540 541 542 543 544 545 546 547 548 549 550
PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
   (SELECT EMPNUM
    FROM t3
    WHERE PNUM IN
       (SELECT PNUM
        FROM t2
        WHERE PTYPE = 'Design'))";
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
551
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
552 553
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
554 555
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
556 557
1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
558 559
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
560 561 562 563 564 565 566 567 568 569 570 571 572
DEALLOCATE PREPARE stmt;
DROP INDEX t1_IDX ON t1;
EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
(SELECT EMPNUM
FROM t3
WHERE PNUM IN
(SELECT PNUM
FROM t2
WHERE PTYPE = 'Design'));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
573
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
574 575
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
576 577 578 579 580 581 582 583 584 585 586 587
PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
FROM t1
WHERE EMPNUM IN
   (SELECT EMPNUM
    FROM t3
    WHERE PNUM IN
       (SELECT PNUM
        FROM t2
        WHERE PTYPE = 'Design'))";
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
Sergey Petrunya's avatar
Sergey Petrunya committed
588
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
589 590
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
591 592
EXECUTE stmt;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
593 594
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	3	func	1	
595 596
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
2	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; Using join buffer (flat, BNL join)
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
DEALLOCATE PREPARE stmt;
SET SESSION optimizer_switch = @old_optimizer_switch;
SET SESSION join_cache_level = @old_join_cache_level;
DROP TABLE t1, t2, t3;
#
# BUG#45221 Query SELECT pk FROM C WHERE pk IN (SELECT int_key) failing
#
CREATE TABLE t1 (
i1_key INT,
i2 INT,
i3 INT,
KEY i1_index (i1_key)
);
INSERT INTO t1 VALUES (9,1,2), (9,2,1);
CREATE TABLE t2 (
pk INT NOT NULL,
i1 INT,
PRIMARY KEY (pk)
);
INSERT INTO t2 VALUES (9,1);
# Enable Index condition pushdown
618 619
set @old_icp=@@optimizer_switch;
SET SESSION optimizer_switch="engine_condition_pushdown=on";
620
Warnings:
621
Warning	1681	'engine_condition_pushdown=on' is deprecated and will be removed in a future release
622 623 624 625 626 627 628 629 630 631 632 633

SELECT pk
FROM t2
WHERE 
pk IN ( 
SELECT i1_key
FROM t1
WHERE t1.i2 < t1.i3 XOR t2.i1 > 1
ORDER BY t1.i2 desc);
pk
9
# Restore old value for Index condition pushdown
634
SET SESSION optimizer_switch=@old_icp;
635
DROP TABLE t1,t2;
Sergei Golubchik's avatar
Sergei Golubchik committed
636
#
637 638
# End of 5.3 tests.
#
639
#
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
# Bug#53236 Segfault in DTCollation::set(DTCollation&)
#
CREATE TABLE t1 (
pk INTEGER AUTO_INCREMENT,
col_varchar VARCHAR(1),
PRIMARY KEY (pk)
)
;
INSERT INTO t1 (col_varchar) 
VALUES
('w'),
('m')
;
SELECT  table1.pk
FROM ( t1 AS table1 JOIN t1 AS table2 ON (table1.col_varchar =
table2.col_varchar) ) 
WHERE ( 1, 2 ) IN ( SELECT SUBQUERY1_t1.pk AS SUBQUERY1_field1,
SUBQUERY1_t1.pk AS SUBQUERY1_field2
FROM ( t1 AS SUBQUERY1_t1 JOIN t1 AS SUBQUERY1_t2
ON (SUBQUERY1_t2.col_varchar =
SUBQUERY1_t1.col_varchar) ) ) 
;
pk
drop table t1;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
#
# BUG#716293: "Range checked for each record" is not used if condition refers to outside of subquery
#
create table t1 (a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2 (a int, b int, `filler` char(200), key(a), key (b));
insert into t2 
select A.a + 10*B.a + 100 * C.a, A.a + 10*B.a + 100 * C.a, 'filler' from t1 A, t1 B, t1 C;
# The following must use "Range checked for each record" for table B
explain 
select a, 
(select sum(X.a+B.b) from t1 X, t2 B where B.a=A.a or B.b=A.a)
from t1 A;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	A	ALL	NULL	NULL	NULL	NULL	10	
2	DEPENDENT SUBQUERY	X	ALL	NULL	NULL	NULL	NULL	10	
2	DEPENDENT SUBQUERY	B	ALL	a,b	NULL	NULL	NULL	1000	Range checked for each record (index map: 0x3)
drop table t1, t2;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
#
# BUG#723822: Crash in get_constant_key_infix with EXISTS ( SELECT .. DISTINCT )
#
CREATE TABLE t1 ( f1 int(11), f3 varchar(1)) ;
INSERT INTO t1 VALUES ('8','c'),('5','f');
ALTER TABLE t1 ADD KEY (f3,f1);
CREATE TABLE t2 ( f4 varchar(1)) ;
INSERT INTO t2 VALUES ('f'),('d');
SELECT * FROM t2
WHERE EXISTS (
SELECT DISTINCT f3
FROM t1
WHERE f3 <= t2.f4
);
f4
f
d
drop table t1,t2;
unknown's avatar
unknown committed
700 701 702 703 704 705 706 707
#
# LP BUG#718763 Second crash in select_describe() and materialization
#
CREATE TABLE t1 ( f1 int(11), f3 int(11), f10 varchar(1), KEY (f3)) ;
INSERT INTO t1 VALUES ('28','6','m'),('29','4','c');
CREATE TABLE t2 (f11 varchar(1)) ;
INSERT INTO t2 VALUES ('f'),('d');
SET @old_optimizer_switch = @@session.optimizer_switch;
708
SET SESSION optimizer_switch = 'materialization=on,in_to_exists=off';
unknown's avatar
unknown committed
709 710 711 712 713 714
EXPLAIN
SELECT * FROM t1
WHERE f3 = (
SELECT t1.f3 FROM t1
WHERE ( t1.f10 ) IN ( SELECT f11 FROM t2 GROUP BY f11 ));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
715
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
Sergei Golubchik's avatar
Sergei Golubchik committed
716
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
Igor Babaev's avatar
Igor Babaev committed
717
2	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1	
Sergei Golubchik's avatar
Sergei Golubchik committed
718
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
719 720 721 722 723 724 725 726 727 728 729
SELECT * FROM t1
WHERE f3 = (
SELECT t1.f3 FROM t1
WHERE ( t1.f10 ) IN ( SELECT f11 FROM t2 GROUP BY f11 ));
f1	f3	f10
EXPLAIN
SELECT * FROM t1
WHERE f3 = (
SELECT f3 FROM t1
WHERE ( f10, f10 ) IN ( SELECT f11, f11 FROM t2 GROUP BY f11 ));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
730
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
Sergei Golubchik's avatar
Sergei Golubchik committed
731
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
Igor Babaev's avatar
Igor Babaev committed
732
2	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	8	func,func	1	
Sergei Golubchik's avatar
Sergei Golubchik committed
733
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
734 735 736 737 738
SELECT * FROM t1
WHERE f3 = (
SELECT f3 FROM t1
WHERE ( f10, f10 ) IN ( SELECT f11, f11 FROM t2 GROUP BY f11 ));
f1	f3	f10
unknown's avatar
unknown committed
739
SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
unknown's avatar
unknown committed
740 741 742 743
EXPLAIN
SELECT * FROM t1
WHERE f3 = (
SELECT t1.f3 FROM t1
unknown's avatar
unknown committed
744
WHERE ( t1.f10 ) IN ( SELECT max(f11) FROM t2 GROUP BY f11 ));
unknown's avatar
unknown committed
745
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
Igor Babaev's avatar
Igor Babaev committed
746
1	PRIMARY	t1	ref	f3	f3	5	const	0	Using where
unknown's avatar
unknown committed
747
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
748
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using temporary
unknown's avatar
unknown committed
749 750 751
SELECT * FROM t1
WHERE f3 = (
SELECT t1.f3 FROM t1
unknown's avatar
unknown committed
752
WHERE ( t1.f10 ) IN ( SELECT max(f11) FROM t2 GROUP BY f11 ));
unknown's avatar
unknown committed
753 754 755 756 757
f1	f3	f10
EXPLAIN
SELECT * FROM t1
WHERE f3 = (
SELECT f3 FROM t1
unknown's avatar
unknown committed
758
WHERE ( f10, f10 ) IN ( SELECT max(f11), f11 FROM t2 GROUP BY f11 ));
unknown's avatar
unknown committed
759
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
Igor Babaev's avatar
Igor Babaev committed
760
1	PRIMARY	t1	ref	f3	f3	5	const	0	Using where
unknown's avatar
unknown committed
761
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
762
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using temporary
unknown's avatar
unknown committed
763 764 765
SELECT * FROM t1
WHERE f3 = (
SELECT f3 FROM t1
unknown's avatar
unknown committed
766
WHERE ( f10, f10 ) IN ( SELECT max(f11), f11 FROM t2 GROUP BY f11 ));
unknown's avatar
unknown committed
767 768 769
f1	f3	f10
SET SESSION optimizer_switch = @old_optimizer_switch;
drop table t1,t2;
unknown's avatar
unknown committed
770 771 772 773 774 775
#
# LP BUG#715738: Wrong result with implicit grouping and empty result set
#
CREATE TABLE t1 (f1 int, f2 int);
CREATE TABLE t2 (f3 int, f4 int not null, PRIMARY KEY (f3));
set @save_optimizer_switch=@@optimizer_switch;
unknown's avatar
unknown committed
776
SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
unknown's avatar
unknown committed
777 778 779
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
780 781
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
782 783 784 785 786
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
787 788
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
789 790 791 792 793
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
794 795
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
796 797 798 799 800 801
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
802
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
803 804 805 806 807 808
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
809 810
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
811 812 813 814 815
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
816 817
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
818 819 820 821 822 823
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
824
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
825 826 827 828 829 830 831
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
832
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
833 834 835 836 837 838 839
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
not_in
1
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
840
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
841 842 843 844 845 846 847
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
848
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
849 850 851 852 853 854 855
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
856
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
857 858 859 860 861 862
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
863
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
864
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
unknown's avatar
unknown committed
865 866
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
f1	f2
unknown's avatar
unknown committed
867
SET @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
unknown's avatar
unknown committed
868 869 870
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
871 872
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
873 874 875 876 877
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
878 879
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
880 881 882 883 884
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
885 886
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
887 888 889 890 891 892
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
893
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
894 895 896 897 898 899
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
900 901
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
902 903 904 905 906
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
907 908
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
909 910 911 912 913 914
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
915
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
916 917 918 919 920 921 922
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
923
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
924 925 926 927 928 929 930
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
not_in
1
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
931
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
932 933 934 935 936 937 938
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
939
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
940 941 942 943 944 945 946
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
947
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
948 949 950 951 952 953
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
954
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
955
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
unknown's avatar
unknown committed
956 957 958 959 960 961
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
f1	f2
INSERT INTO t1 VALUES (1, 2);
INSERT INTO t1 VALUES (3, 4);
INSERT INTO t2 VALUES (5, 6);
INSERT INTO t2 VALUES (7, 8);
unknown's avatar
unknown committed
962
SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
unknown's avatar
unknown committed
963 964 965
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
966
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
967
2	MATERIALIZED	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
968 969 970 971 972
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
973
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
974
2	MATERIALIZED	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
975 976 977 978 979
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
980
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
981
2	MATERIALIZED	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
982 983 984 985 986 987
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
988
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
989 990 991 992 993 994
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
995
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
996
2	MATERIALIZED	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
997 998 999 1000 1001
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1002
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
1003
2	MATERIALIZED	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1004 1005 1006 1007 1008 1009
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1010
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1011 1012 1013 1014 1015 1016 1017
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1018
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1019 1020 1021 1022 1023 1024 1025
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
not_in
1
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1026
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1027 1028 1029 1030 1031 1032 1033
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1034
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1035 1036 1037 1038 1039 1040 1041
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1042
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1043 1044 1045 1046 1047 1048
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
1049
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
1050
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
unknown's avatar
unknown committed
1051 1052
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
f1	f2
unknown's avatar
unknown committed
1053
SET @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
unknown's avatar
unknown committed
1054 1055 1056
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1057
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1058
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1059 1060 1061 1062 1063
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1064
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1065
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1066 1067 1068 1069 1070
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1071
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1072
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1073 1074 1075 1076 1077 1078
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1079
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1080 1081 1082 1083 1084 1085
SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1086
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1087
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1088 1089 1090 1091 1092
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1093
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1094
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1095 1096 1097 1098 1099 1100
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
f1	f2
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1101
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1102 1103 1104 1105 1106 1107 1108
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1109
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1110 1111 1112 1113 1114 1115 1116
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
not_in
1
EXPLAIN
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1117
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1118 1119 1120 1121 1122 1123 1124
SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1125
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1126 1127 1128 1129 1130 1131 1132
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1133
2	SUBQUERY	t2	range	PRIMARY	PRIMARY	4	NULL	1	Using index condition; Rowid-ordered scan
unknown's avatar
unknown committed
1134 1135 1136 1137 1138 1139
SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
not_in
NULL
EXPLAIN
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
1140
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
1141
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
unknown's avatar
unknown committed
1142 1143 1144 1145
SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
f1	f2
set @@optimizer_switch=@save_optimizer_switch;
drop table t1,t2;
unknown's avatar
unknown committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
#
# LP BUG#613029 Wrong result with materialization and semijoin, and
# valgrind warnings in Protocol::net_store_data with materialization
# for implicit grouping
#
CREATE TABLE t1 (
pk int(11) NOT NULL AUTO_INCREMENT,
f2 int(11) NOT NULL,
f3 varchar(1) NOT NULL,
PRIMARY KEY (pk),
KEY f2 (f2));
INSERT INTO t1 VALUES (1,9,'x');
INSERT INTO t1 VALUES (2,5,'g');
CREATE TABLE t2 (
pk int(11) NOT NULL AUTO_INCREMENT,
f2 int(11) NOT NULL,
f3 varchar(1) NOT NULL,
PRIMARY KEY (pk),
KEY f2 (f2));
INSERT INTO t2 VALUES (1,7,'p');
set @save_optimizer_switch=@@optimizer_switch;
unknown's avatar
unknown committed
1167
set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
unknown's avatar
unknown committed
1168 1169 1170 1171 1172
EXPLAIN
SELECT t1.f3, MAX(t1.f2)
FROM t1, t2
WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
1173 1174
1	PRIMARY	t2	system	PRIMARY	NULL	NULL	NULL	1	
1	PRIMARY	t1	const	PRIMARY	PRIMARY	4	const	1	
unknown's avatar
unknown committed
1175 1176 1177 1178 1179 1180
2	DEPENDENT SUBQUERY	t1	index_subquery	f2	f2	4	func	2	Using index
SELECT t1.f3, MAX(t1.f2)
FROM t1, t2
WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
f3	MAX(t1.f2)
NULL	NULL
unknown's avatar
unknown committed
1181
set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
unknown's avatar
unknown committed
1182 1183 1184 1185 1186 1187 1188
EXPLAIN
SELECT t1.f3, MAX(t1.f2)
FROM t1, t2
WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	PRIMARY	NULL	NULL	NULL	1	
1	PRIMARY	t1	const	PRIMARY	PRIMARY	4	const	1	
1189
2	MATERIALIZED	t1	index	NULL	f2	4	NULL	2	Using index
unknown's avatar
unknown committed
1190 1191 1192 1193 1194 1195 1196 1197 1198
SELECT t1.f3, MAX(t1.f2)
FROM t1, t2
WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
f3	MAX(t1.f2)
NULL	NULL
TODO: add a test case for semijoin when the wrong result is fixed
set @@optimizer_switch='materialization=off,semijoin=on';
set @@optimizer_switch=@save_optimizer_switch;
drop table t1, t2;
unknown's avatar
unknown committed
1199
#
unknown's avatar
unknown committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
# LP BUG#777691 Wrong result with subqery in select list and subquery cache=off in maria-5.3
#
CREATE TABLE t1 ( f1 varchar(32)) ;
INSERT INTO t1 VALUES ('b'),('x'),('c'),('x');
CREATE TABLE t2 ( f2 int, f3 varchar(32)) ;
INSERT INTO t2 VALUES (1,'x');
set @save_optimizer_switch=@@optimizer_switch;
set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off';
EXPLAIN
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	
2	DEPENDENT SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
f1	max_f2
b	NULL
x	1
c	NULL
x	1
set @@optimizer_switch='materialization=on,in_to_exists=off,subquery_cache=off';
EXPLAIN
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	
2	DEPENDENT SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
f1	max_f2
b	NULL
x	1
c	NULL
x	1
set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off';
Even when t2 is not constant table, the result must be the same.
INSERT INTO t2 VALUES (2,'y');
EXPLAIN
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
f1	max_f2
b	NULL
x	1
c	NULL
x	1
set @@optimizer_switch=@save_optimizer_switch;
unknown's avatar
unknown committed
1246
drop table t1, t2;
unknown's avatar
unknown committed
1247
#
1248 1249 1250 1251 1252 1253 1254 1255 1256
# LP BUG#641203 Query returns rows where no result is expected (impossible WHERE)
#
CREATE TABLE t1 (c1 varchar(1) DEFAULT NULL);
CREATE TABLE t2 (c1 varchar(1) DEFAULT NULL);
INSERT INTO t2 VALUES ('k'), ('d');
CREATE TABLE t3 (c1 varchar(1) DEFAULT NULL);
INSERT INTO t3 VALUES ('a'), ('b'), ('c');
CREATE TABLE t4 (c1 varchar(1) primary key);
INSERT INTO t4 VALUES ('k'), ('d');
1257 1258
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='outer_join_with_cache=off';
1259 1260
SET optimizer_switch='semijoin_with_cache=off';
SET optimizer_switch='materialization=off';
1261 1262 1263
EXPLAIN
SELECT * FROM t1 RIGHT JOIN t2 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1264
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
1265 1266
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(t1)
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
1267 1268 1269 1270 1271
SELECT * FROM t1 RIGHT JOIN t2 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
c1	c1
EXPLAIN
SELECT * FROM t2 LEFT JOIN t1 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1272
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
1273 1274
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(t1)
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
1275 1276
SELECT * FROM t2 LEFT JOIN t1 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
c1	c1
1277
SET optimizer_switch='materialization=on';
1278 1279 1280
EXPLAIN
SELECT * FROM (t2 LEFT JOIN t1 ON t1.c1) LEFT JOIN t3 on t3.c1 WHERE 's' IN (SELECT c1 FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1281
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
Igor Babaev's avatar
Igor Babaev committed
1282
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	
1283
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1284
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	Using where
1285
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
1286 1287 1288 1289 1290
SELECT * FROM (t2 LEFT JOIN t1 ON t1.c1) LEFT JOIN t3 on t3.c1 WHERE 's' IN (SELECT c1 FROM t2);
c1	c1	c1
EXPLAIN
SELECT * FROM t4 LEFT JOIN t2 ON t4.c1 WHERE 's' IN (SELECT c1 FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
Igor Babaev's avatar
Igor Babaev committed
1291
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	
1292
1	PRIMARY	t4	index	NULL	PRIMARY	3	NULL	2	Using index; Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1293
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
1294
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
1295 1296
SELECT * FROM t4 LEFT JOIN t2 ON t4.c1 WHERE 's' IN (SELECT c1 FROM t2);
c1	c1
1297
SET optimizer_switch=@save_optimizer_switch;
1298
drop table t1, t2, t3, t4;
1299
#
unknown's avatar
unknown committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
# LP BUG#675981 Assertion `cache != __null' failed in sub_select_cache()
#               on EXPLAIN
#
CREATE TABLE t1 (f1 int,f2 int) ;
INSERT IGNORE INTO t1 VALUES ('2','5'),('2',NULL);
CREATE TABLE t2 (f1 int, f5 int) ;
INSERT IGNORE INTO t2 VALUES (1,0);
CREATE TABLE t3 (f4 int) ;
INSERT IGNORE INTO t3 VALUES (0),(0);
set @@optimizer_switch='in_to_exists=on,materialization=off,semijoin=off';
EXPLAIN
SELECT * FROM t2
WHERE f1 IN (SELECT t1.f2 FROM t1 JOIN t3 ON t3.f4);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1316
2	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1317 1318
drop table t1, t2, t3;
#
unknown's avatar
unknown committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
# LP BUG#680005 Second assertion `cache != __null' failed in
#                sub_select_cache() on EXPLAIN
#
CREATE TABLE t1 (f1 int,f2 int,f4 int,f6 int,KEY (f4)) ;
INSERT IGNORE INTO t1 VALUES
('1','5','1','0'),('2','1','1','0'),('2','2','2','0'),('0',NULL,'0','0'),
('2','1','2','0'),('2','0','0','0'),('2','2','2','0'),('2','8','2','0'),
('2','7','2','0'),('2','5','2','0'),('2',NULL,'1','0');
CREATE TABLE t2 (f3 int) ;
INSERT IGNORE INTO t2 VALUES ('7');
CREATE TABLE t3 (f3 int) ;
INSERT IGNORE INTO t3 VALUES ('2');
EXPLAIN
SELECT t1.f4
FROM t2 JOIN t1 ON t1.f6
WHERE
( t1.f2 ) IN (SELECT SUBQUERY2_t1.f3
FROM t3 AS SUBQUERY2_t1
JOIN
(t1 AS SUBQUERY2_t2
JOIN
t1 AS SUBQUERY2_t3 ON SUBQUERY2_t3.f1)
ON SUBQUERY2_t3.f2)
GROUP BY t1.f4 ORDER BY t1.f1 LIMIT 10;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	Using temporary; Using filesort
Sergei Golubchik's avatar
Sergei Golubchik committed
1345
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	11	Using where
unknown's avatar
unknown committed
1346
2	DEPENDENT SUBQUERY	SUBQUERY2_t1	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1347 1348
2	DEPENDENT SUBQUERY	SUBQUERY2_t2	index	NULL	f4	5	NULL	11	Using index
2	DEPENDENT SUBQUERY	SUBQUERY2_t3	ALL	NULL	NULL	NULL	NULL	11	Using where; Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1349 1350
drop table t1, t2, t3;
#
unknown's avatar
unknown committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
# LP BUG#680038 bool close_thread_table(THD*, TABLE**):
#               Assertion `table->key_read == 0' failed in EXPLAIN
#
CREATE TABLE t1 (f1 int,f3 int,f4 int) ;
INSERT IGNORE INTO t1 VALUES (NULL,1,0);
CREATE TABLE t2 (f2 int,f4 int,f5 int) ;
INSERT IGNORE INTO t2 VALUES (8,0,0),(5,0,0);
CREATE TABLE t3 (f4 int,KEY (f4)) ;
INSERT IGNORE INTO t3 VALUES (0),(0);
set @@optimizer_switch='semijoin=off';
EXPLAIN
SELECT * FROM t1 WHERE
(SELECT f2 FROM t2
WHERE f4 <= ALL
unknown's avatar
unknown committed
1365
(SELECT max(SQ1_t1.f4)
unknown's avatar
unknown committed
1366 1367 1368 1369 1370
FROM t3 AS SQ1_t1 JOIN t3 AS SQ1_t3 ON SQ1_t3.f4
GROUP BY SQ1_t1.f4));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
2	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1371
3	SUBQUERY	SQ1_t1	index	NULL	f4	5	NULL	2	Using index; Using temporary
unknown's avatar
unknown committed
1372
3	SUBQUERY	SQ1_t3	index	f4	f4	5	NULL	2	Using where; Using index; Using join buffer (flat, BNL join)
1373 1374 1375 1376 1377 1378 1379
SELECT * FROM t1 WHERE
(SELECT f2 FROM t2
WHERE f4 <= ALL
(SELECT max(SQ1_t1.f4)
FROM t3 AS SQ1_t1 JOIN t3 AS SQ1_t3 ON SQ1_t3.f4
GROUP BY SQ1_t1.f4));
ERROR 21000: Subquery returns more than 1 row
unknown's avatar
unknown committed
1380 1381
drop table t1, t2, t3;
#
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
# BUG#52317: Assertion failing in Field_varstring::store() 
# 	    at field.cc:6833
#
CREATE TABLE t1 (i INTEGER);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (i INTEGER, KEY k(i));
INSERT INTO t2 VALUES (1), (2);
EXPLAIN  
SELECT i FROM t1 WHERE (1) NOT IN (SELECT i FROM t2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
1392
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1393
2	SUBQUERY	t2	index_subquery	k	k	5	const	2	Using index
1394 1395
DROP TABLE t2;
DROP TABLE t1;
unknown's avatar
unknown committed
1396
#
unknown's avatar
unknown committed
1397
# LP BUG#680846: Crash in clear_tables() with subqueries
unknown's avatar
unknown committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
#
CREATE TABLE t1 (f3 int) ;
INSERT IGNORE INTO t1 VALUES (0),(0);
CREATE TABLE t2 (f1 int,f3 int,f4 varchar(32)) ;
INSERT IGNORE INTO t2 VALUES (1,0,'f');
EXPLAIN
SELECT COUNT(t2.f3),
(SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
FROM t2 JOIN t1 ON t1.f3
WHERE ('v') IN (SELECT f4 FROM t2)
GROUP BY f9;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1410
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1411
3	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1412
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
SELECT COUNT(t2.f3),
(SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
FROM t2 JOIN t1 ON t1.f3
WHERE ('v') IN (SELECT f4 FROM t2)
GROUP BY f9;
COUNT(t2.f3)	f9
EXPLAIN
SELECT COUNT(t2.f3),
(SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
FROM t2 JOIN t1 ON t1.f3
WHERE ('v') IN (SELECT f4 FROM t2)
ORDER BY f9;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1426
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1427
3	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1428
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1429 1430 1431 1432 1433 1434
SELECT COUNT(t2.f3),
(SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
FROM t2 JOIN t1 ON t1.f3
WHERE ('v') IN (SELECT f4 FROM t2)
ORDER BY f9;
COUNT(t2.f3)	f9
1435
0	NULL
unknown's avatar
unknown committed
1436 1437 1438 1439 1440 1441 1442
EXPLAIN
SELECT COUNT(t2.f3),
(SELECT t2.f1 FROM t1 limit 1) AS f9
FROM t2 JOIN t1
WHERE ('v') IN (SELECT f4 FROM t2)
GROUP BY f9;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1443
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1444
3	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
unknown's avatar
unknown committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
SELECT COUNT(t2.f3),
(SELECT t2.f1 FROM t1 limit 1) AS f9
FROM t2 JOIN t1
WHERE ('v') IN (SELECT f4 FROM t2)
GROUP BY f9;
COUNT(t2.f3)	f9
EXPLAIN
SELECT COUNT(t2.f3),
(SELECT t2.f1 FROM t1 limit 1) AS f9
FROM t2 JOIN t1
WHERE ('v') IN (SELECT f4 FROM t2)
ORDER BY f9;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1459
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1460
3	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
unknown's avatar
unknown committed
1461 1462 1463 1464 1465 1466 1467 1468 1469
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
SELECT COUNT(t2.f3),
(SELECT t2.f1 FROM t1 limit 1) AS f9
FROM t2 JOIN t1
WHERE ('v') IN (SELECT f4 FROM t2)
ORDER BY f9;
COUNT(t2.f3)	f9
0	NULL
drop table t1,t2;
unknown's avatar
unknown committed
1470
#
unknown's avatar
unknown committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
# LP BUG#682683 Crash in create_tmp_table called from
#               JOIN::init_execution
#
CREATE TABLE t2 (f1 int) ;
INSERT INTO t2 VALUES (1),(2);
CREATE TABLE t1 (f1 int) ;
EXPLAIN
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	
1481
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
1482 1483 1484 1485 1486 1487 1488
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
field1
NULL
EXPLAIN
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	
1489
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
field1
NULL
NULL
INSERT INTO t1 VALUES (1),(2);
EXPLAIN
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
ERROR 21000: Subquery returns more than 1 row
EXPLAIN
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
ERROR 21000: Subquery returns more than 1 row
drop table t1,t2;
#
unknown's avatar
unknown committed
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
# LP BUG#680943 Assertion `!table || (!table->read_set ||
# bitmap_is_set(table->read_set, field_index))' failed with subquery
#
CREATE TABLE t1 (f1 int,f3 int) ;
INSERT IGNORE INTO t1 VALUES ('6','0'),('4','0');
CREATE TABLE t2 (f1 int,f2 int,f3 int) ;
INSERT IGNORE INTO t2 VALUES ('6','0','0'),('2','0','0');
SELECT f2
FROM (SELECT * FROM t2) AS alias1
WHERE (SELECT SQ2_t2.f1
FROM t1 JOIN t1 AS SQ2_t2 ON SQ2_t2.f3
WHERE SQ2_t2.f3 AND alias1.f1)
ORDER BY f3 ;
f2
drop table t1,t2;
unknown's avatar
unknown committed
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
#
# LP BUG#715062: Wrong result with VIEW + UNION + subquery in maria-5.3-mwl89
#
create table t1 (f1 int);
create table t2 (f2 int);
create table t3 (f3 int);
insert into t1 values (2);
insert into t2 values (2);
insert into t3 values (7);
CREATE VIEW v1 AS SELECT 2 UNION SELECT 2 ;
CREATE VIEW v2 AS SELECT * from t1 UNION SELECT * from t2 ;
set @save_optimizer_switch=@@optimizer_switch;
SET @@optimizer_switch = 'in_to_exists=off,semijoin=off,materialization=on';
EXPLAIN
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1543
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1544 1545 1546 1547 1548 1549 1550 1551 1552
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
bug
EXPLAIN
SELECT ( 5 ) IN ( SELECT * FROM v1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1553
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT ( 5 ) IN ( SELECT * FROM v1 );
( 5 ) IN ( SELECT * FROM v1 )
0
EXPLAIN
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1564
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1565 1566 1567 1568 1569 1570 1571 1572 1573
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
bug
EXPLAIN
SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
1574
2	MATERIALIZED	<derived3>	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1575 1576 1577 1578 1579 1580 1581 1582 1583
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
bug
EXPLAIN
SELECT ( 5 ) IN ( SELECT * FROM v2 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1584
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT ( 5 ) IN ( SELECT * FROM v2 );
( 5 ) IN ( SELECT * FROM v2 )
0
SET @@optimizer_switch = 'in_to_exists=on,semijoin=off,materialization=off';
EXPLAIN
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1596
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1597 1598 1599 1600 1601 1602 1603 1604 1605
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
bug
EXPLAIN
SELECT ( 5 ) IN ( SELECT * FROM v1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1606
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT ( 5 ) IN ( SELECT * FROM v1 );
( 5 ) IN ( SELECT * FROM v1 )
0
EXPLAIN
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1617
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1618 1619 1620 1621 1622 1623 1624 1625 1626
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
bug
EXPLAIN
SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1627
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1628 1629 1630 1631 1632 1633 1634 1635 1636
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
bug
EXPLAIN
SELECT ( 5 ) IN ( SELECT * FROM v2 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
unknown's avatar
unknown committed
1637
2	SUBQUERY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1638 1639 1640 1641 1642 1643 1644 1645 1646
3	DERIVED	t1	system	NULL	NULL	NULL	NULL	1	
4	UNION	t2	system	NULL	NULL	NULL	NULL	1	
NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT ( 5 ) IN ( SELECT * FROM v2 );
( 5 ) IN ( SELECT * FROM v2 )
0
set @@optimizer_switch=@save_optimizer_switch;
drop table t1,t2,t3;
drop view v1,v2;
unknown's avatar
unknown committed
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
#
# LP BUG#715069 Wrong result with GROUP BY inside subquery and materialization=off
#
CREATE TABLE t0 ( f1 int(11), f2 int(11), f10 varchar(1), PRIMARY KEY (f1)) ;
INSERT INTO t0 VALUES (8,8,'u'),(10,5,'o');
CREATE TABLE t1 (f1a int, f2a int not null, f3a varchar(3) not null, PRIMARY KEY (f1a)) ;
INSERT INTO t1 VALUES
(8,8,'a1a'),
(10,5,'b1b');
CREATE TABLE t2 (f1b int, f2b int not null, f3b varchar(3) not null, PRIMARY KEY (f1b)) ;
INSERT INTO t2 VALUES
(10,5,'d1d');
set @save_optimizer_switch=@@optimizer_switch;
1660
SET optimizer_switch='outer_join_with_cache=off';
unknown's avatar
unknown committed
1661
set @@optimizer_switch = 'in_to_exists=on,materialization=off,semijoin=off';
unknown's avatar
unknown committed
1662 1663 1664 1665
EXPLAIN
SELECT alias2.f1 , alias2.f2
FROM t0 AS alias1
RIGHT JOIN t0 AS alias2 ON alias2.f10
unknown's avatar
unknown committed
1666
WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
unknown's avatar
unknown committed
1667 1668 1669
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	alias1	index	NULL	PRIMARY	4	NULL	2	Using where; Using index
unknown's avatar
unknown committed
1670
2	DEPENDENT SUBQUERY	t0	ALL	NULL	NULL	NULL	NULL	2	Using temporary
unknown's avatar
unknown committed
1671 1672 1673
SELECT alias2.f1 , alias2.f2
FROM t0 AS alias1
RIGHT JOIN t0 AS alias2 ON alias2.f10
unknown's avatar
unknown committed
1674
WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
unknown's avatar
unknown committed
1675 1676
f1	f2
8	8
1677 1678
Warnings:
Warning	1292	Truncated incorrect DOUBLE value: 'u'
unknown's avatar
unknown committed
1679
EXPLAIN
unknown's avatar
unknown committed
1680
SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1681 1682
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1683 1684
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary
SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1685 1686 1687
f1b	f2b	f3b
10	5	d1d
EXPLAIN
unknown's avatar
unknown committed
1688
SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1689 1690
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1691 1692
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary
SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1693 1694
f1b	f2b	f3b
10	5	d1d
unknown's avatar
unknown committed
1695
set @@optimizer_switch = 'in_to_exists=off,materialization=on,semijoin=off';
unknown's avatar
unknown committed
1696 1697 1698 1699
EXPLAIN
SELECT alias2.f1 , alias2.f2
FROM t0 AS alias1
RIGHT JOIN t0 AS alias2 ON alias2.f10
unknown's avatar
unknown committed
1700
WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
unknown's avatar
unknown committed
1701 1702 1703
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	alias1	index	NULL	PRIMARY	4	NULL	2	Using where; Using index
unknown's avatar
unknown committed
1704
2	MATERIALIZED	t0	ALL	NULL	NULL	NULL	NULL	2	Using temporary
unknown's avatar
unknown committed
1705 1706 1707
SELECT alias2.f1 , alias2.f2
FROM t0 AS alias1
RIGHT JOIN t0 AS alias2 ON alias2.f10
unknown's avatar
unknown committed
1708
WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
unknown's avatar
unknown committed
1709 1710
f1	f2
8	8
1711 1712
Warnings:
Warning	1292	Truncated incorrect DOUBLE value: 'u'
unknown's avatar
unknown committed
1713
EXPLAIN
unknown's avatar
unknown committed
1714
SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1715 1716
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1717 1718
2	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary
SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1719 1720 1721
f1b	f2b	f3b
10	5	d1d
EXPLAIN
unknown's avatar
unknown committed
1722
SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1723 1724
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
unknown's avatar
unknown committed
1725 1726
2	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary
SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
unknown's avatar
unknown committed
1727 1728 1729 1730
f1b	f2b	f3b
10	5	d1d
set @@optimizer_switch=@save_optimizer_switch;
drop table t0,t1,t2;
unknown's avatar
unknown committed
1731
#                                                                                                                                                     
unknown's avatar
unknown committed
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
# LP BUG#715759 Wrong result with in_to_exists=on in maria-5.3-mwl89
#                                                                                                                                                     
set @save_optimizer_switch=@@optimizer_switch;
CREATE TABLE t1 (a1 int, a2 int) ;
INSERT INTO t1 VALUES (1, 2);
INSERT INTO t1 VALUES (3, 4);
CREATE TABLE t2 (b1 int, b2 int) ;
INSERT INTO t2 VALUES (1, 2);
SET @@optimizer_switch = 'in_to_exists=on,materialization=off,semijoin=off';
EXPLAIN SELECT * FROM t1 WHERE a1 IN (SELECT b1 FROM t2 WHERE b1 = b2);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
SELECT * FROM t1 WHERE a1 IN (SELECT b1 FROM t2 WHERE b1 = b2);
a1	a2
set @@optimizer_switch=@save_optimizer_switch;
drop table t1, t2;
unknown's avatar
unknown committed
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
#                                                                                                                                                     
# LP BUG#772309 join_tab_cmp_straight(): Assertion `!jt2->emb_sj_nest' failed in maria-5.3-mwl89 with semijoin
#                                                                                                                                                     
CREATE TABLE t1 ( f2 int) ;
INSERT INTO t1 VALUES (0),(0);
CREATE TABLE t2 ( f1 int NOT NULL ) ;
INSERT INTO t2 VALUES (0),(0);
CREATE TABLE t3 ( f1 int NOT NULL , f2 int) ;
INSERT INTO t3 VALUES (0,0), (0,0);
EXPLAIN SELECT STRAIGHT_JOIN (
SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
3	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
SELECT STRAIGHT_JOIN (
SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
);
(
SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
)
NULL
drop table t1, t2, t3;
unknown's avatar
unknown committed
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
#                                                                                                                                                     
# LP BUG#777597 Wrong result with multipart keys, in_to_exists=on, NOT IN in MWL#89
#                                                                                                                                                     
CREATE TABLE t1 ( f4 int);
INSERT IGNORE INTO t1 VALUES (2),(2);
CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3) );
INSERT IGNORE INTO t2 VALUES (6, 1), (6, 1);
CREATE TABLE t3 ( f10 int );
INSERT IGNORE INTO t3 VALUES (1);
SET SESSION optimizer_switch='in_to_exists=on,materialization=off';
EXPLAIN
SELECT * FROM t1 WHERE ( 6 ) NOT IN ( SELECT t2.f3 FROM t2 JOIN t3 ON t3.f10 = t2.f10);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1787
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
unknown's avatar
unknown committed
1788 1789
2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	1	
2	SUBQUERY	t2	ref_or_null	f10	f10	10	const,const	2	Using where; Using index
unknown's avatar
unknown committed
1790 1791 1792
SELECT * FROM t1 WHERE ( 6 ) NOT IN ( SELECT t2.f3 FROM t2 JOIN t3 ON t3.f10 = t2.f10);
f4
drop table t1,t2,t3;
unknown's avatar
unknown committed
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
#                                                                                                                                                     
# LP BUG#778413 Third crash in select_describe() in maria-5.3-mwl89
#                                                                                                                                                     
CREATE TABLE t1 ( f11 int) ;
INSERT INTO t1 VALUES (1),(1);
CREATE TABLE t2 ( f1 int NOT NULL) ;
INSERT INTO t2 VALUES (20);
CREATE TABLE t3 (f3 int) ;
INSERT INTO t3 VALUES (2),(2);
EXPLAIN SELECT * FROM t2
WHERE t2.f1 = (
SELECT MAX( f3 ) FROM t3
WHERE EXISTS (
SELECT DISTINCT f11
FROM t1));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
2	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1811
3	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1812
drop table t1, t2, t3;
unknown's avatar
unknown committed
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
#
# LP BUG#802979 Assertion `table->key_read == 0' in close_thread_table
#
CREATE TABLE t1 ( f1 int, f2 int , KEY (f1)) ;
INSERT IGNORE INTO t1 VALUES (1,0),(5,0);
CREATE TABLE t2 ( f1 int, f2 int , KEY (f1)) ;
INSERT IGNORE INTO t2 VALUES (1,0),(5,0);
CREATE TABLE t3 ( f1 int, f2 int , KEY (f1)) ;
INSERT IGNORE INTO t3 VALUES (1,0),(5,0);
CREATE TABLE t4 ( f1 int, f2 int , KEY (f1)) ;
INSERT IGNORE INTO t4 VALUES (1,0),(5,0);
EXPLAIN
SELECT *
FROM t1, t2
WHERE t2.f2 = (SELECT f2 FROM t3
WHERE EXISTS (SELECT DISTINCT f1 FROM t4))
AND t2.f2 = t1.f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
Igor Babaev's avatar
Igor Babaev committed
1831
1	PRIMARY	t1	ref	f1	f1	5	const	0	Using where
unknown's avatar
unknown committed
1832 1833
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
2	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1834
3	SUBQUERY	t4	index	NULL	f1	5	NULL	2	Using index
unknown's avatar
unknown committed
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
SELECT *
FROM t1, t2
WHERE t2.f2 = (SELECT f2 FROM t3
WHERE EXISTS (SELECT DISTINCT f1 FROM t4))
AND t2.f2 = t1.f1;
ERROR 21000: Subquery returns more than 1 row
EXPLAIN
SELECT *
FROM t1, t2
WHERE t2.f2 = (SELECT f2 FROM t3
WHERE EXISTS (SELECT DISTINCT f1 FROM t4) LIMIT 1)
AND t2.f2 = t1.f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
Igor Babaev's avatar
Igor Babaev committed
1848
1	PRIMARY	t1	ref	f1	f1	5	const	0	Using where
unknown's avatar
unknown committed
1849 1850
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
2	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1851
3	SUBQUERY	t4	index	NULL	f1	5	NULL	2	Using index
unknown's avatar
unknown committed
1852 1853 1854 1855 1856 1857 1858
SELECT *
FROM t1, t2
WHERE t2.f2 = (SELECT f2 FROM t3
WHERE EXISTS (SELECT DISTINCT f1 FROM t4) LIMIT 1)
AND t2.f2 = t1.f1;
f1	f2	f1	f2
drop table t1,t2,t3,t4;
unknown's avatar
unknown committed
1859
#
unknown's avatar
unknown committed
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
# LP BUG#611690 Crash in select_describe() with nested subqueries
#
CREATE TABLE t1 (
col_int_key int(11) DEFAULT NULL,
col_varchar_key varchar(1) DEFAULT NULL,
col_varchar_nokey varchar(1) DEFAULT NULL,
KEY col_int_key (col_int_key),
KEY col_varchar_key (col_varchar_key,col_int_key)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO t1 VALUES (8,'v','v');
INSERT INTO t1 VALUES (9,'r','r');
CREATE TABLE t2 (
col_int_key int(11) DEFAULT NULL,
col_varchar_key varchar(1) DEFAULT NULL,
col_varchar_nokey varchar(1) DEFAULT NULL,
KEY col_int_key (col_int_key),
KEY col_varchar_key (col_varchar_key,col_int_key)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO t2 VALUES (2,'w','w');
INSERT INTO t2 VALUES (9,'m','m');
set @old_optimizer_switch = @@optimizer_switch;
set @@optimizer_switch='subquery_cache=off,materialization=on,in_to_exists=off,semijoin=off';
EXPLAIN
SELECT col_int_key
FROM t2
WHERE (SELECT SUBQUERY2_t1.col_int_key
FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
ON SUBQUERY2_t2.col_varchar_key
WHERE SUBQUERY2_t2.col_varchar_nokey IN
(SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	index	NULL	col_int_key	5	NULL	2	Using index
2	SUBQUERY	SUBQUERY2_t1	index	NULL	col_int_key	5	NULL	2	Using index
unknown's avatar
unknown committed
1893
2	SUBQUERY	SUBQUERY2_t2	ALL	col_varchar_key	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1894
3	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1895 1896 1897 1898 1899 1900 1901 1902
SELECT col_int_key
FROM t2
WHERE (SELECT SUBQUERY2_t1.col_int_key
FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
ON SUBQUERY2_t2.col_varchar_key
WHERE SUBQUERY2_t2.col_varchar_nokey IN
(SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
col_int_key
1903 1904 1905
Warnings:
Warning	1292	Truncated incorrect DOUBLE value: 'v'
Warning	1292	Truncated incorrect DOUBLE value: 'r'
unknown's avatar
unknown committed
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
set @@optimizer_switch='subquery_cache=off,materialization=off,in_to_exists=on,semijoin=off';
EXPLAIN
SELECT col_int_key
FROM t2
WHERE (SELECT SUBQUERY2_t1.col_int_key
FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
ON SUBQUERY2_t2.col_varchar_key
WHERE SUBQUERY2_t2.col_varchar_nokey IN
(SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	index	NULL	col_int_key	5	NULL	2	Using index
2	SUBQUERY	SUBQUERY2_t1	index	NULL	col_int_key	5	NULL	2	Using index
unknown's avatar
unknown committed
1918
2	SUBQUERY	SUBQUERY2_t2	ALL	col_varchar_key	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
unknown's avatar
unknown committed
1919
3	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
1920 1921 1922 1923 1924 1925 1926 1927
SELECT col_int_key
FROM t2
WHERE (SELECT SUBQUERY2_t1.col_int_key
FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
ON SUBQUERY2_t2.col_varchar_key
WHERE SUBQUERY2_t2.col_varchar_nokey IN
(SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
col_int_key
1928 1929 1930
Warnings:
Warning	1292	Truncated incorrect DOUBLE value: 'v'
Warning	1292	Truncated incorrect DOUBLE value: 'r'
unknown's avatar
unknown committed
1931 1932
drop table t1, t2;
set @@optimizer_switch = @old_optimizer_switch;
unknown's avatar
unknown committed
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
#
# LP BUG#612543 Crash in Item_field::used_tables() with view + subquery + prepared statements
#
CREATE TABLE t1 ( f1 int(11), f2 varchar(1));
CREATE TABLE t2 ( f3 varchar(1));
insert into t1 values (2,'x'), (5,'y');
insert into t2 values ('x'), ('z');
CREATE VIEW v2 AS SELECT * FROM t2;
set @old_optimizer_switch = @@optimizer_switch;
set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off,subquery_cache=off';
EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
1947
2	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
PREPARE st1 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
EXECUTE st1;
f1	f2	f3
2	x	x
5	y	x
EXECUTE st1;
f1	f2	f3
2	x	x
5	y	x
set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off,subquery_cache=off';
EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
PREPARE st2 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
EXECUTE st2;
f1	f2	f3
2	x	x
5	y	x
EXECUTE st2;
f1	f2	f3
2	x	x
5	y	x
set @@optimizer_switch='materialization=on,in_to_exists=on,semijoin=off,subquery_cache=off';
EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
1977
2	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
PREPARE st3 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
EXECUTE st3;
f1	f2	f3
2	x	x
5	y	x
EXECUTE st3;
f1	f2	f3
2	x	x
5	y	x
set @@optimizer_switch = @old_optimizer_switch;
drop table t1, t2;
drop view v2;
unknown's avatar
unknown committed
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
#
# LP BUG#611396 RQG: crash in Item_field::register_field_in_read_map with semijoin=off
# and prepared statements and materialization
CREATE TABLE t1 ( f1 int(11), f2 int(11)) ;
CREATE TABLE t2 ( f1 int(11), f4 varchar(1), PRIMARY KEY (f1)) ;
INSERT INTO t2 VALUES ('23','j'),('24','e');
CREATE TABLE t3 ( f1 int(11), f4 varchar(1)) ;
INSERT INTO t3 VALUES ('8','j');
set @old_optimizer_switch = @@optimizer_switch;
set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
EXPLAIN
SELECT t2.f1, (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
FROM t2 JOIN t3 ON t3.f4 = t2.f4
WHERE t3.f1 = 8
GROUP BY 1, 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2006 2007
1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using filesort
2008
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2009
3	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
PREPARE st1 FROM "
SELECT t2.f1, (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
FROM t2 JOIN t3 ON t3.f4 = t2.f4
WHERE t3.f1 = 8
GROUP BY 1, 2";
EXECUTE st1;
f1	(SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
23	NULL
EXECUTE st1;
f1	(SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
23	NULL
set @@optimizer_switch = @old_optimizer_switch;
drop table t1, t2, t3;
unknown's avatar
unknown committed
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
#
# LP BUG#611382 RQG: Query returns extra rows when executed with materialization=on
#
CREATE TABLE t1 ( f4 varchar(1)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (NULL);
CREATE TABLE t2 ( f2 date, f3 varchar(1), f4 varchar(1)) ;
INSERT INTO t2 VALUES ('2005-05-03','c','c'),('1900-01-01','d','d');
CREATE TABLE t3 ( f3 varchar(1)) ;
INSERT INTO t3 VALUES ('c');
set @old_optimizer_switch = @@optimizer_switch;
set @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
EXPLAIN SELECT t1.f4
FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
2041
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	
unknown's avatar
unknown committed
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
SELECT t1.f4
FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
f4
set @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
EXPLAIN SELECT t1.f4
FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
SELECT t1.f4
FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
f4
set @@optimizer_switch = @old_optimizer_switch;
drop table t1, t2, t3;
unknown's avatar
unknown committed
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
#
# LP BUG#782305: Wrong result/valgrind warning in Item_sum_hybrid::any_value()
#
CREATE TABLE t1 ( f1 int) ;
INSERT INTO t1 VALUES (2),(3);
CREATE TABLE t2 (f2 int) ;
INSERT INTO t2 VALUES (2),(3);
PREPARE st1 FROM '
SELECT * FROM t2
WHERE f2 <= SOME ( SELECT f1 FROM t1 );
';
EXECUTE st1;
f2
2
3
EXECUTE st1;
f2
2
3
PREPARE st2 FROM '
SELECT * FROM t2
WHERE f2 <= SOME (SELECT f1-2 FROM t1 UNION SELECT f1-1 FROM t1);
';
EXECUTE st2;
f2
2
EXECUTE st2;
f2
2
drop table t1, t2;
unknown's avatar
unknown committed
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
#
# LP BUG#825018: Crash in check_and_do_in_subquery_rewrites() with corrlated subquery in select list
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (10,1),(11,7);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (2),(3);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (1,1);
CREATE PROCEDURE sp1 () LANGUAGE SQL
SELECT (SELECT t1.a
FROM t1
WHERE t1.b = t3.b
AND t1.b IN ( SELECT a FROM t2 )) sq
FROM t3
GROUP BY 1;
CALL sp1();
sq
NULL
CALL sp1();
sq
NULL
drop procedure sp1;
prepare st1 from "
SELECT (SELECT t1.a
        FROM t1
        WHERE t1.b = t3.b
        AND t1.b IN ( SELECT a FROM t2 )) sq
FROM t3
GROUP BY 1";
execute st1;
sq
NULL
execute st1;
sq
NULL
deallocate prepare st1;
drop table t1, t2, t3;
2129
set optimizer_switch=@subselect4_tmp;
unknown's avatar
unknown committed
2130 2131 2132 2133 2134 2135 2136 2137 2138
#
# LP BUG#833702 Wrong result with nested IN and singlerow subqueries and equality propagation
#
CREATE TABLE t2 (c int , a int, b int);
INSERT INTO t2 VALUES (10,7,0);
CREATE TABLE t3 (a int, b int) ;
INSERT INTO t3 VALUES (5,0),(7,0);
CREATE TABLE t4 (a int);
INSERT INTO t4 VALUES (2),(8);
2139
set @@optimizer_switch='semijoin=off,in_to_exists=on,materialization=off,subquery_cache=off';
unknown's avatar
unknown committed
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
SELECT * FROM t2
WHERE t2.b IN (SELECT b FROM t3 WHERE t3.a = t2.a AND a < SOME (SELECT * FROM t4))
OR ( t2.c > 242 );
c	a	b
10	7	0
EXPLAIN SELECT * FROM t2
WHERE t2.b IN (SELECT t3.b FROM t3 WHERE t3.a < ANY (SELECT t4.a FROM t4) and t3.a = 7);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
2	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
3	SUBQUERY	t4	ALL	NULL	NULL	NULL	NULL	2	
SELECT * FROM t2
WHERE t2.b IN (SELECT t3.b FROM t3 WHERE t3.a < ANY (SELECT t4.a FROM t4) and t3.a = 7);
c	a	b
10	7	0
drop table t2, t3, t4;
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
#
# BUG#934597: Assertion `! is_set()' failed in Diagnostics_area::set_ok_status(THD...
#
CREATE TABLE t1 ( a VARCHAR(1) );
INSERT INTO t1 VALUES ('u'),('k');
CREATE TABLE t2 AS
SELECT a AS field1 FROM t1
WHERE ( SELECT alias1.a
FROM t1 AS alias1
) IS NOT NULL;
ERROR 21000: Subquery returns more than 1 row
DROP TABLE t2;
2168
ERROR 42S02: Unknown table 'test.t2'
2169
DROP TABLE t1;
unknown's avatar
unknown committed
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
#
# LP BUG#1000649 EXPLAIN shows incorrectly a non-correlated constant IN subquery is correlated
#
create table ten (a int);
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, c int);
insert into t1 select a,a,a from ten;
create table five (a int, b int, c int);
insert into five select a,a,a from ten limit 5;
set @@optimizer_switch='semijoin=on,in_to_exists=on,materialization=off';
explain select * from t1 where 33 in (select b from five) or c > 11;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	Using where
2	SUBQUERY	five	ALL	NULL	NULL	NULL	NULL	5	Using where
drop table ten, t1, five;
unknown's avatar
unknown committed
2185 2186 2187 2188 2189 2190 2191 2192 2193
#
# LP BUG#1008773 Wrong result (NULL instead of a value) with no matching rows, subquery in FROM and HAVING
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1);
EXPLAIN
SELECT MAX(a), ( SELECT 1 FROM t2 ) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2194
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
unknown's avatar
unknown committed
2195 2196 2197 2198 2199 2200 2201
2	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT MAX(a), ( SELECT 1 FROM t2 ) AS bb FROM t1;
MAX(a)	bb
NULL	1
EXPLAIN
SELECT MAX(a), 1 in ( SELECT b FROM t2 ) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2202
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
unknown's avatar
unknown committed
2203 2204 2205 2206 2207 2208 2209
2	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT MAX(a), 1 in ( SELECT b FROM t2 ) AS bb FROM t1;
MAX(a)	bb
NULL	1
EXPLAIN
SELECT MAX(a), 1 >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2210
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
unknown's avatar
unknown committed
2211 2212 2213 2214 2215 2216 2217
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
SELECT MAX(a), 1 >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
MAX(a)	bb
NULL	1
EXPLAIN
SELECT MAX(a), ( SELECT 1 FROM t2 where b = a) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2218
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
2219
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
unknown's avatar
unknown committed
2220 2221 2222 2223 2224 2225
SELECT MAX(a), ( SELECT 1 FROM t2 where b = a) AS bb FROM t1;
MAX(a)	bb
NULL	NULL
EXPLAIN
SELECT MAX(a), a in ( SELECT b FROM t2 ) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2226
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
unknown's avatar
unknown committed
2227 2228 2229 2230 2231 2232 2233
2	DEPENDENT SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT MAX(a), a in ( SELECT b FROM t2 ) AS bb FROM t1;
MAX(a)	bb
NULL	NULL
EXPLAIN
SELECT MAX(a), a >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2234
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
unknown's avatar
unknown committed
2235 2236 2237 2238 2239
2	DEPENDENT SUBQUERY	t2	system	NULL	NULL	NULL	NULL	1	
SELECT MAX(a), a >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
MAX(a)	bb
NULL	NULL
drop table t1, t2;
2240
set optimizer_switch=@subselect4_tmp;
2241
#
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
# MDEV-3928  Assertion `example' failed in Item_cache::is_expensive_processor with a 2-level IN subquery 
#
CREATE TABLE t1 (a1 INT, b1 TIME) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4,'21:22:34'),(6,'10:50:38');
CREATE TABLE t2 (a2 INT, b2 TIME) ENGINE=MyISAM;
INSERT INTO t2 VALUES (8, '06:17:39');
CREATE TABLE t3 (a3 INT, b3 TIME) ENGINE=MyISAM;
INSERT INTO t3 VALUES (1,'00:00:01'),(7,'00:00:02');
EXPLAIN
SELECT * FROM t1 WHERE a1 IN ( 
SELECT a2 FROM t2 WHERE a2 IN ( 
SELECT a3 FROM t3 WHERE b2 = b1 AND b2 <= b1 ORDER BY b3 
)
);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
3	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
SELECT * FROM t1 WHERE a1 IN ( 
SELECT a2 FROM t2 WHERE a2 IN ( 
SELECT a3 FROM t3 WHERE b2 = b1 AND b2 <= b1 ORDER BY b3 
)
);
a1	b1
drop table t1, t2, t3;
Sergei Golubchik's avatar
Sergei Golubchik committed
2267
#
unknown's avatar
unknown committed
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
# MDEV-4056:Server crashes in Item_func_trig_cond::val_int
# with FROM and NOT IN subqueries, LEFT JOIN, derived_merge+in_to_exists
#
set @optimizer_switch_MDEV4056 = @@optimizer_switch;
SET optimizer_switch = 'derived_merge=on,in_to_exists=on';
CREATE TABLE t1 (a VARCHAR(1)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('x'),('d');
CREATE TABLE t2 (pk INT PRIMARY KEY, b INT, c VARCHAR(1))  ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,2,'v'),(2,150,'v');
SELECT * FROM t1 LEFT JOIN (  
SELECT * FROM t2 WHERE ( pk, pk ) NOT IN ( 
SELECT MIN(b), SUM(pk) FROM t1
) 
) AS alias1 ON (a = c) 
WHERE b IS NULL OR a < 'u';
a	pk	b	c
x	NULL	NULL	NULL
d	NULL	NULL	NULL
drop table t1,t2;
set @@optimizer_switch = @optimizer_switch_MDEV4056;
Igor Babaev's avatar
Igor Babaev committed
2288
#
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
# MDEV-5103: server crashed on singular Item_equal
#
CREATE TABLE t1 (
a enum('p','r') NOT NULL DEFAULT 'r',
b int NOT NULL DEFAULT '0',
c char(32) NOT NULL,
d varchar(255) NOT NULL,
PRIMARY KEY (a, b), UNIQUE KEY idx(a, c)
);
INSERT INTO t1 VALUES ('r', 1, 'ad18832202b199728921807033a8a515', '001_cbr643');
CREATE TABLE t2 (
a enum('p','r') NOT NULL DEFAULT 'r',
b int NOT NULL DEFAULT '0',
e datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (a, b, e)
);
INSERT INTO t2 VALUES ('r', 1, '2013-10-05 14:25:30');
SELECT * FROM t1 AS t 
WHERE a='r' AND (c,b) NOT IN (SELECT c,b FROM t2 WHERE (c,b)=(t.c,t.b));
a	b	c	d
DROP TABLE t1, t2;
Igor Babaev's avatar
Igor Babaev committed
2310
#
Igor Babaev's avatar
Igor Babaev committed
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
# MDEV-5468: assertion failure with a simplified condition in subselect
#
CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,1);
CREATE TABLE t2 ( pk int PRIMARY KEY, c INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,4), (2,6);
SELECT ( SELECT MAX(b) FROM t1, t2 WHERE pk = a AND b < from_sq.c ) AS select_sq,
COUNT( DISTINCT from_sq.c ) 
FROM ( SELECT DISTINCT t2_1.* FROM t2 AS t2_1, t2 AS t2_2 ) AS from_sq
GROUP BY select_sq ;
select_sq	COUNT( DISTINCT from_sq.c )
1	2
DROP TABLE t1,t2;
CREATE TABLE t1 (id int, a2 char(2), a3 char(3)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,'BE','BEL');
CREATE TABLE t2 (id int, a2 char(2), a3 char(3)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,'BE','BEL'), (2,'MX','MEX');
CREATE VIEW v2 AS SELECT DISTINCT * FROM t2;
SELECT * FROM t1 AS outer_t1, v2  
WHERE v2.a3 = outer_t1.a3 
AND EXISTS ( SELECT * FROM t1 WHERE a2 < v2.a2 AND id = outer_t1.id )
AND outer_t1.a3 < 'J'    
ORDER BY v2.id;
id	a2	a3	id	a2	a3
DROP VIEW v2;
DROP TABLE t1,t2;
Igor Babaev's avatar
Igor Babaev committed
2337
#
Igor Babaev's avatar
Igor Babaev committed
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
# MDEV-5686: degenerate disjunct in NOT IN subquery
#
CREATE TABLE t1 (a int, b int, c varchar(3)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,1,'CAN'),(2,2,'AUS');
CREATE TABLE t2 (f int) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3);
EXPLAIN EXTENDED
SELECT * FROM t2 
WHERE f NOT IN (SELECT b FROM t1
WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	100.00	
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
Warnings:
2352
Note	1003	/* select#1 */ select 3 AS `f` from dual where !<expr_cache><3>(<in_optimizer>(3,<exists>(/* select#2 */ select `test`.`t1`.`b` from `test`.`t1` where (`test`.`t1`.`c` = 'USA' or `test`.`t1`.`c` <> 'USA') and trigcond(<cache>(3) = `test`.`t1`.`b` or `test`.`t1`.`b` is null) and `test`.`t1`.`b` = `test`.`t1`.`a` having trigcond(`test`.`t1`.`b` is null))))
Igor Babaev's avatar
Igor Babaev committed
2353 2354 2355 2356 2357 2358
SELECT * FROM t2 
WHERE f NOT IN (SELECT b FROM t1
WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b);
f
3
DROP TABLE t1,t2;
Sergei Golubchik's avatar
Sergei Golubchik committed
2359
#
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
# MDEV-3899  Valgrind warnings (blocks are definitely lost) in filesort on IN subquery with SUM and DISTINCT
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(9);
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (8);
SELECT * FROM t1 
WHERE (1, 1) IN (SELECT a, SUM(DISTINCT a) FROM t1, t2 GROUP BY a);
a
1
9
drop table t1, t2;
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
#
# MDEV-3902  Assertion `record_length == m_record_length' failed at Filesort_buffer::alloc_sort_buffer 
#
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (pk INT PRIMARY KEY, b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,1),(2,7);
CREATE TABLE t3 (c INT) ENGINE=MyISAM;
INSERT INTO t3 VALUES (8);
SELECT * FROM t1
WHERE (1, 5)  IN (SELECT b, SUM(DISTINCT b) FROM t2, t3 GROUP BY b);
a
SELECT * FROM t2 AS alias1, t2 AS alias2
WHERE EXISTS ( SELECT 1 ) AND (alias2.pk = alias1.b )
ORDER BY alias1.b;
pk	b	pk	b
1	1	1	1
drop table t1, t2, t3;
unknown's avatar
unknown committed
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
#
# MDEV-4144 simple subquery causes full scan instead of range scan
#
CREATE TABLE t1 (id int not null auto_increment, x int not null, primary key(id));
INSERT INTO t1 (x) VALUES (0),(0),(0);
EXPLAIN
SELECT x FROM t1 WHERE id > (SELECT MAX(id) - 1000 FROM t1) ORDER BY x LIMIT 1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	range	PRIMARY	PRIMARY	4	NULL	3	Using where; Using filesort
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
SELECT x FROM t1 WHERE id > (SELECT MAX(id) - 1000 FROM t1) ORDER BY x LIMIT 1;
x
0
drop table t1;
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
#
# MDEV-7691: Assertion `outer_context || !*from_field || *from_field == not_found_field' ...
#
set optimizer_switch=default;
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4),(6);
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1),(8);
PREPARE stmt FROM "
SELECT * FROM t2
HAVING 0 IN (
  SELECT a FROM t1
  WHERE a IN ( 
    SELECT a FROM t1
    WHERE b = a
  )
)
";
EXECUTE stmt;
b
EXECUTE stmt;
b
# Alternative test case, without HAVING
CREATE TABLE t3 (i INT) ENGINE=MyISAM;
INSERT INTO t3 VALUES (4),(6);
PREPARE stmt FROM "
SELECT * FROM t3 AS t10
WHERE EXISTS ( 
  SELECT * FROM t3 AS t20 WHERE t10.i IN ( 
    SELECT i FROM t3 
  ) 
)";
EXECUTE stmt;
i
2438
4
2439 2440 2441
6
EXECUTE stmt;
i
2442
4
2443 2444
6
drop table t1, t2, t3;
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
#
# MDEV-11078: NULL NOT IN (non-empty subquery) should never return results
#
create table t1(a int,b int);
create table t2(a int,b int);
insert into t1 value (1,2);
select (NULL)  in (select 1 from t1);
(NULL)  in (select 1 from t1)
NULL
select (null)  in (select 1 from t2);
(null)  in (select 1 from t2)
0
select 1 in (select 1 from t1);
1 in (select 1 from t1)
1
select 1 in (select 1 from t2);
1 in (select 1 from t2)
0
select 1 from dual where null in (select 1 from t1);
1
select 1 from dual where null in (select 1 from t2);
1
select (null,null) in (select * from t1);
(null,null) in (select * from t1)
NULL
select (null,null) in (select * from t2);
(null,null) in (select * from t2)
0
select 1 from dual where null not in (select 1 from t1);
1
select 1 from dual where null not in (select 1 from t2);
1
1
drop table t1,t2;
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
#
# MDEV-6486: Assertion `!table || (!table->read_set || bitmap_is_set(table->read_set, field_index))'
# failed with SELECT SQ, TEXT field
#
CREATE TABLE t1 (a VARCHAR(8), KEY(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('foo'),( 'bar');
CREATE TABLE t2 (b VARCHAR(8), c TINYTEXT, KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('baz','baz'),('qux', 'qux');
SELECT ( SELECT COUNT(*) FROM t1 WHERE a = c ) AS field, COUNT(DISTINCT c)
FROM t2 WHERE b <= 'quux' GROUP BY field;
field	COUNT(DISTINCT c)
0	1
drop table t1,t2;
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
#
# MDEV-15555: select from DUAL where false yielding wrong result when in a IN
#
explain 
SELECT 2 IN (SELECT 2 from DUAL WHERE 1 != 1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
2	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
SELECT 2 IN (SELECT 2 from DUAL WHERE 1 != 1);
2 IN (SELECT 2 from DUAL WHERE 1 != 1)
0
2503 2504
SET optimizer_switch= @@global.optimizer_switch;
set @@tmp_table_size= @@global.tmp_table_size;
2505
#
Marko Mäkelä's avatar
Marko Mäkelä committed
2506
# MDEV-14515: Wrong results for tableless query with subquery in WHERE
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
#             and implicit aggregation
#
create table t1 (i1 int, i2 int);
insert into t1 values (1314, 1084),(1330, 1084),(1401, 1084),(580, 1084);
create table t2 (cd int);
insert into t2 values
(1330), (1330), (1330), (1330), (1330), (1330), (1330), (1330),
(1330), (1330), (1330), (1330), (1330), (1330), (1330), (1330);
select max(10) from dual
where exists (select 1 from t2 join t1 on t1.i1 = t2.cd  and t1.i2 = 345);
max(10)
NULL
insert into t2 select * from t2;
select max(10) from dual
where exists (select 1 from t2 join t1 on t1.i1 = t2.cd  and t1.i2 = 345);
max(10)
NULL
DROP TABLE t1,t2;
Marko Mäkelä's avatar
Marko Mäkelä committed
2525
#
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
# MDEV-10232 Scalar result of subquery changes after adding an outer select stmt
#
create table t1(c1 int, c2 int, primary key(c2));
insert into t1 values(2,1),(1,2);
select (select c1 from t1 group by c1,c2 order by c1 limit 1) as x;
x
1
(select c1 from t1 group by c1,c2 order by c1 limit 1);
c1
1
drop table t1;
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
#
# MDEV-22498: SIGSEGV in Bitmap<64u>::merge on SELECT
#
set @save_sql_select_limit= @@sql_select_limit;
SET sql_select_limit=0;
CREATE TABLE t1(b INT, c INT);
CREATE TABLE t2(a INT, b INT);
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
INSERT INTO t2 VALUES (1,1),(2,2),(3,3);
EXPLAIN EXTENDED SELECT sum(a), t2.a, t2.b FROM t2 HAVING t2.a IN (SELECT t2.b FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Zero limit
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	
Warnings:
Note	1276	Field or reference 'test.t2.b' of SELECT #2 was resolved in SELECT #1
Note	1276	Field or reference 'test.t2.b' of SELECT #2 was resolved in SELECT #1
Marko Mäkelä's avatar
Marko Mäkelä committed
2553
Note	1003	/* select#1 */ select sum(`test`.`t2`.`a`) AS `sum(a)`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` having <in_optimizer>(`test`.`t2`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`b` from `test`.`t1` where <cache>(`test`.`t2`.`a`) = `test`.`t2`.`b`))
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
SELECT sum(a), t2.a, t2.b FROM t2 HAVING t2.a IN (SELECT t2.b FROM t1);
sum(a)	a	b
SET @@sql_select_limit= @save_sql_select_limit;
EXPLAIN EXTENDED SELECT sum(a), t2.a, t2.b FROM t2 HAVING t2.a IN (SELECT t2.b FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3	100.00	
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	
Warnings:
Note	1276	Field or reference 'test.t2.b' of SELECT #2 was resolved in SELECT #1
Note	1276	Field or reference 'test.t2.b' of SELECT #2 was resolved in SELECT #1
Marko Mäkelä's avatar
Marko Mäkelä committed
2564
Note	1003	/* select#1 */ select sum(`test`.`t2`.`a`) AS `sum(a)`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` having <expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`b` from `test`.`t1` where <cache>(`test`.`t2`.`a`) = `test`.`t2`.`b`)))
2565 2566 2567 2568
SELECT sum(a), t2.a, t2.b FROM t2 HAVING t2.a IN (SELECT t2.b FROM t1);
sum(a)	a	b
6	1	1
DROP TABLE t1,t2;
2569
#
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
# MDEV-17606: Query returns wrong results (while using CHARACTER SET utf8)
#
CREATE TABLE t1(l1 varchar(10), i2 int);
INSERT INTO t1 VALUES ('e',2),('o',6),('x',4);
CREATE TABLE t2 (v1 varchar(10) CHARACTER SET utf8, KEY v1 (v1(3)));
INSERT INTO t2 VALUES  ('k'),('rid'),('f'),('x');
EXPLAIN EXTENDED SELECT * FROM t1 where  ( t1.l1 < ANY (SELECT MAX(t2.v1) FROM t2));
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	
Warnings:
Marko Mäkelä's avatar
Marko Mäkelä committed
2581
Note	1003	/* select#1 */ select `test`.`t1`.`l1` AS `l1`,`test`.`t1`.`i2` AS `i2` from `test`.`t1` where <nop>(<in_optimizer>(`test`.`t1`.`l1`,<max>(/* select#2 */ select max(`test`.`t2`.`v1`) from `test`.`t2`) > convert(<cache>(`test`.`t1`.`l1`) using utf8)))
2582 2583 2584 2585 2586
SELECT * FROM t1 where  ( t1.l1 < ANY (SELECT MAX(t2.v1) FROM t2));
l1	i2
e	2
o	6
DROP TABLE t1, t2;
Marko Mäkelä's avatar
Marko Mäkelä committed
2587
#
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
# MDEV-19232: Floating point precision / value comparison problem
#
CREATE TABLE t1 (region varchar(60), area decimal(10,0), population decimal(11,0));
INSERT INTO t1 VALUES ('Central America and the Caribbean',91,11797);
INSERT INTO t1 VALUES ('Central America and the Caribbean',442,66422);
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='subquery_cache=on';
SELECT
population, area, population/area,
cast(population/area as DECIMAL(20,9)) FROM t1 LIMIT 1;
population	area	population/area	cast(population/area as DECIMAL(20,9))
11797	91	129.6374	129.637400000
SELECT * FROM t1 A
WHERE population/area = (SELECT MAX(population/area) from t1 B where A.region = B.region);
region	area	population
Central America and the Caribbean	442	66422
SET optimizer_switch='subquery_cache=off';
SELECT * FROM t1 A
WHERE population/area = (SELECT MAX(population/area) from t1 B where A.region = B.region);
region	area	population
Central America and the Caribbean	442	66422
SET @@optimizer_switch= @save_optimizer_switch;
DROP TABLE t1;
2611
#
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
# MDEV-9513: Assertion `join->group_list || !join->is_in_subquery()' failed in create_sort_index
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (a INT);
INSERT INTO t2 VALUES (2),(3);
EXPLAIN
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	A	ALL	NULL	NULL	NULL	NULL	2	Using where
3	DEPENDENT UNION	B	ALL	NULL	NULL	NULL	NULL	2	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
a
1
2
EXPLAIN
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
2	SUBQUERY	A	ALL	NULL	NULL	NULL	NULL	2	
3	UNION	B	ALL	NULL	NULL	NULL	NULL	2	
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
a
1
2
EXPLAIN
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	A	ALL	NULL	NULL	NULL	NULL	2	Using where
3	DEPENDENT UNION	B	ALL	NULL	NULL	NULL	NULL	2	Using where
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
a
1
2
DROP TABLE t1,t2;
# end of 10.1 tests
Marko Mäkelä's avatar
Marko Mäkelä committed
2652
#
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
# MDEV-22852: SIGSEGV in sortlength (optimized builds)
#
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='subquery_cache=off';
CREATE TABLE t1 (a INT,b INT);
INSERT INTO t1 VALUES (0,0),(0,0);
SELECT (SELECT DISTINCT t1i.b FROM t1 t1i GROUP BY t1i.a ORDER BY MAX(t1o.b)) FROM t1 AS t1o;
(SELECT DISTINCT t1i.b FROM t1 t1i GROUP BY t1i.a ORDER BY MAX(t1o.b))
0
SET @@optimizer_switch= @save_optimizer_switch;
DROP TABLE t1;
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
#
# MDEV-17066: Bytes lost or Assertion `status_var.local_memory_used == 0 after DELETE
# with subquery with ROLLUP
#
CREATE TABLE t1 (i INT DEFAULT 0, c VARCHAR(2048));
INSERT INTO t1  SELECT 0, seq FROM seq_1_to_6000;
CREATE TABLE t2 (f VARCHAR(2048) DEFAULT '');
INSERT INTO t2 VALUES ('1'),('bar');
EXPLAIN
SELECT * FROM t2 WHERE f IN ( SELECT MAX(c) FROM t1 GROUP BY c WITH ROLLUP);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
2	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	6000	Using filesort
SELECT * FROM t2 WHERE f IN ( SELECT MAX(c) FROM t1 GROUP BY c WITH ROLLUP);
f
1
SELECT * FROM t2;
f
1
bar
DELETE FROM t2 WHERE f IN ( SELECT MAX(c) FROM t1 GROUP BY c WITH ROLLUP );
SELECT * FROM t2;
f
bar
DROP TABLE t1, t2;
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
#
# MDEV-18335: Assertion `!error || error == 137' failed in subselect_rowid_merge_engine::init
#
CREATE TABLE t1 (i1 int,v1 varchar(1),KEY (v1,i1));
INSERT INTO t1 VALUES
(9,'y'),(4,'q'),(0,null),(0,'p'),(null,'j');
CREATE TABLE t2 (pk int);
INSERT INTO t2 VALUES (1),(2);
CREATE TABLE t3 (v2 varchar(1));
INSERT INTO t3 VALUES
('p'),('j'),('y'),('q');
CREATE TABLE t4 (v2 varchar(1));
INSERT INTO t4 VALUES
('a'),('a'),('b'),('b'),('c'),('c'),
('d'),('d'),('e'),('e'),('f'),('f'),
('g'),('g'),('h'),('h'),('i'),('i'),
('j'),('j'),('k'),('k'),('l'),('l'),
('m'),('m'),('n'),('n'),('o'),('o'),
('p'),('p'),('q'),('q'),('r'),('r'),
('s'),('s'),('t'),('t'),('u'),('u'),('v'),('v'),
('w'),('w'),('x'),('x'), (NULL),(NULL);
SET @save_join_cache_level=@@join_cache_level;
SET join_cache_level=0;
select 1
from t2 join t1 on
('i','w') not in (select t1.v1,t4.v2 from t4,t1,t3 where t3.v2 = t1.v1) LIMIT ROWS EXAMINED 500;
1
Warnings:
Warning	1931	Query execution was interrupted. The query examined at least 3020 rows, which exceeds LIMIT ROWS EXAMINED (500). The query result may be incomplete
SET join_cache_level= @save_join_cache_level;
DROP TABLE t1,t2,t3,t4;
Marko Mäkelä's avatar
Marko Mäkelä committed
2720
# End of 10.2 tests