1set @save_optimizer_switch_jcl6=@@optimizer_switch; 2set @@optimizer_switch='optimize_join_buffer_size=on'; 3set @@optimizer_switch='semijoin=on,firstmatch=on,loosescan=on'; 4set @@optimizer_switch='semijoin_with_cache=on'; 5set @@optimizer_switch='outer_join_with_cache=on'; 6set @@optimizer_switch='join_cache_hashed=off'; 7set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; 8set @save_join_cache_level=@@join_cache_level; 9set join_cache_level=6; 10set @optimizer_switch_for_subselect_sj_test=@@optimizer_switch; 11set @join_cache_level_for_subselect_sj_test=@@join_cache_level; 12drop table if exists t0, t1, t2, t3, t4, t5, t10, t11, t12; 13drop view if exists v1, v2, v3, v4; 14drop procedure if exists p1; 15set @subselect_sj_tmp= @@optimizer_switch; 16set optimizer_switch='semijoin=on,firstmatch=on,loosescan=on'; 17set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; 18SET optimizer_switch=ifnull(@optimizer_switch_for_subselect_sj_test,'outer_join_with_cache=off'); 19SET optimizer_switch=ifnull(@optimizer_switch_for_subselect_sj_test,'semijoin_with_cache=off'); 20set join_cache_level=@join_cache_level_for_subselect_sj_test; 21set @local_optimizer_switch=@@optimizer_switch; 22create table t0 (a int); 23insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 24create table t1(a int, b int); 25insert into t1 values (0,0),(1,1),(2,2); 26create table t2 as select * from t1; 27create table t11(a int, b int); 28create table t10 (pk int, a int, primary key(pk)); 29insert into t10 select a,a from t0; 30create table t12 like t10; 31insert into t12 select * from t10; 32Flattened because of dependency, t10=func(t1) 33explain select * from t1 where a in (select pk from t10); 34id select_type table type possible_keys key key_len ref rows Extra 351 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where 361 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index 37select * from t1 where a in (select pk from t10); 38a b 390 0 401 1 412 2 42A confluent case of dependency 43explain select * from t1 where a in (select a from t10 where pk=12); 44id select_type table type possible_keys key key_len ref rows Extra 451 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 46select * from t1 where a in (select a from t10 where pk=12); 47a b 48explain select * from t1 where a in (select a from t10 where pk=9); 49id select_type table type possible_keys key key_len ref rows Extra 501 PRIMARY t10 const PRIMARY PRIMARY 4 const 1 511 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where 52select * from t1 where a in (select a from t10 where pk=9); 53a b 54An empty table inside 55explain select * from t1 where a in (select a from t11); 56id select_type table type possible_keys key key_len ref rows Extra 571 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 58select * from t1 where a in (select a from t11); 59a b 60explain select * from t1 where a in (select pk from t10) and b in (select pk from t10); 61id select_type table type possible_keys key key_len ref rows Extra 621 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where 631 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index 641 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using index 65select * from t1 where a in (select pk from t10) and b in (select pk from t10); 66a b 670 0 681 1 692 2 70flattening a nested subquery 71explain select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12)); 72id select_type table type possible_keys key key_len ref rows Extra 731 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where 741 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 751 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 Using index 76select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12)); 77a b 780 0 791 1 802 2 81flattening subquery w/ several tables 82explain extended select * from t1 where a in (select t10.pk from t10, t12 where t12.pk=t10.a); 83id select_type table type possible_keys key key_len ref rows filtered Extra 841 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where 851 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 861 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 100.00 Using index 87Warnings: 88Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t12` join `test`.`t1` where `test`.`t12`.`pk` = `test`.`t10`.`a` and `test`.`t10`.`pk` = `test`.`t1`.`a` 89subqueries within outer joins go into ON expr. 90explAin extended 91select * from t1 left join (t2 A, t2 B) on ( A.A= t1.A And B.A in (select pk from t10)); 92id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA 931 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 941 PRIMARY A ALL NULL NULL NULL NULL 3 100.00 Using where; Using join Buffer (flAt, BNL join) 951 PRIMARY B ALL NULL NULL NULL NULL 3 100.00 Using where; Using join Buffer (incrementAl, BNL join) 962 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index 97Warnings: 98Note 1003 /* select#1 */ select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`A`.`A` AS `A`,`test`.`A`.`B` AS `B`,`test`.`B`.`A` AS `A`,`test`.`B`.`B` AS `B` from `test`.`t1` left join (`test`.`t2` `A` join `test`.`t2` `B`) on(`test`.`A`.`A` = `test`.`t1`.`A` And <in_optimizer>(`test`.`B`.`A`,`test`.`B`.`A` in ( <mAteriAlize> (/* select#2 */ select `test`.`t10`.`pk` from `test`.`t10` ), <primAry_index_lookup>(`test`.`B`.`A` in <temporAry tABle> on distinct_key where `test`.`B`.`A` = `<suBquery2>`.`pk`)))) where 1 99t2 should be wrapped into OJ-nest, so we have "t1 LJ (t2 J t10)" 100explAin extended 101select * from t1 left join t2 on (t2.A= t1.A And t2.A in (select pk from t10)); 102id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA 1031 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 1041 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join Buffer (flAt, BNL join) 1052 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index 106Warnings: 107Note 1003 /* select#1 */ select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`t2`.`A` AS `A`,`test`.`t2`.`B` AS `B` from `test`.`t1` left join `test`.`t2` on(`test`.`t2`.`A` = `test`.`t1`.`A` And <in_optimizer>(`test`.`t1`.`A`,`test`.`t1`.`A` in ( <mAteriAlize> (/* select#2 */ select `test`.`t10`.`pk` from `test`.`t10` ), <primAry_index_lookup>(`test`.`t1`.`A` in <temporAry tABle> on distinct_key where `test`.`t1`.`A` = `<suBquery2>`.`pk`)))) where 1 108set @save_join_buffer_size=@@join_buffer_size; 109set join_buffer_size=8*1024; 110we shouldn't flatten if we're going to get a join of > MAX_TABLES. 111explain select * from 112t1 s00, t1 s01, t1 s02, t1 s03, t1 s04,t1 s05,t1 s06,t1 s07,t1 s08,t1 s09, 113t1 s10, t1 s11, t1 s12, t1 s13, t1 s14,t1 s15,t1 s16,t1 s17,t1 s18,t1 s19, 114t1 s20, t1 s21, t1 s22, t1 s23, t1 s24,t1 s25,t1 s26,t1 s27,t1 s28,t1 s29, 115t1 s30, t1 s31, t1 s32, t1 s33, t1 s34,t1 s35,t1 s36,t1 s37,t1 s38,t1 s39, 116t1 s40, t1 s41, t1 s42, t1 s43, t1 s44,t1 s45,t1 s46,t1 s47,t1 s48,t1 s49 117where 118s00.a in ( 119select m00.a from 120t1 m00, t1 m01, t1 m02, t1 m03, t1 m04,t1 m05,t1 m06,t1 m07,t1 m08,t1 m09, 121t1 m10, t1 m11, t1 m12, t1 m13, t1 m14,t1 m15,t1 m16,t1 m17,t1 m18,t1 m19 122); 123id select_type table type possible_keys key key_len ref rows Extra 1241 PRIMARY s00 ALL NULL NULL NULL NULL 3 Using where 1251 PRIMARY s01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join) 1261 PRIMARY s02 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1271 PRIMARY s03 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1281 PRIMARY s04 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1291 PRIMARY s05 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1301 PRIMARY s06 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1311 PRIMARY s07 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1321 PRIMARY s08 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1331 PRIMARY s09 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1341 PRIMARY s10 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1351 PRIMARY s11 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1361 PRIMARY s12 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1371 PRIMARY s13 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1381 PRIMARY s14 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1391 PRIMARY s15 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1401 PRIMARY s16 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1411 PRIMARY s17 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1421 PRIMARY s18 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1431 PRIMARY s19 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1441 PRIMARY s20 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1451 PRIMARY s21 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1461 PRIMARY s22 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1471 PRIMARY s23 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1481 PRIMARY s24 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1491 PRIMARY s25 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1501 PRIMARY s26 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1511 PRIMARY s27 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1521 PRIMARY s28 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1531 PRIMARY s29 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1541 PRIMARY s30 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1551 PRIMARY s31 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1561 PRIMARY s32 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1571 PRIMARY s33 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1581 PRIMARY s34 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1591 PRIMARY s35 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1601 PRIMARY s36 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1611 PRIMARY s37 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1621 PRIMARY s38 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1631 PRIMARY s39 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1641 PRIMARY s40 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1651 PRIMARY s41 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1661 PRIMARY s42 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1671 PRIMARY s43 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1681 PRIMARY s44 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1691 PRIMARY s45 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1701 PRIMARY s46 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1711 PRIMARY s47 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1721 PRIMARY s48 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1731 PRIMARY s49 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1742 DEPENDENT SUBQUERY m00 ALL NULL NULL NULL NULL 3 Using where 1752 DEPENDENT SUBQUERY m01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join) 1762 DEPENDENT SUBQUERY m02 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1772 DEPENDENT SUBQUERY m03 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1782 DEPENDENT SUBQUERY m04 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1792 DEPENDENT SUBQUERY m05 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1802 DEPENDENT SUBQUERY m06 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1812 DEPENDENT SUBQUERY m07 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1822 DEPENDENT SUBQUERY m08 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1832 DEPENDENT SUBQUERY m09 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1842 DEPENDENT SUBQUERY m10 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1852 DEPENDENT SUBQUERY m11 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1862 DEPENDENT SUBQUERY m12 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1872 DEPENDENT SUBQUERY m13 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1882 DEPENDENT SUBQUERY m14 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1892 DEPENDENT SUBQUERY m15 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1902 DEPENDENT SUBQUERY m16 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1912 DEPENDENT SUBQUERY m17 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1922 DEPENDENT SUBQUERY m18 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 1932 DEPENDENT SUBQUERY m19 ALL NULL NULL NULL NULL 3 Using join buffer (incremental, BNL join) 194select * from 195t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10)) 196where t1.a < 5; 197a b a b 1980 0 0 0 1991 1 1 1 2002 2 2 2 201set join_buffer_size=@save_join_buffer_size; 202prepare s1 from 203' select * from 204 t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10)) 205 where t1.a < 5'; 206execute s1; 207a b a b 2080 0 0 0 2091 1 1 1 2102 2 2 2 211execute s1; 212a b a b 2130 0 0 0 2141 1 1 1 2152 2 2 2 216insert into t1 select (A.a + 10 * B.a),1 from t0 A, t0 B; 217explain extended select * from t1 where a in (select pk from t10 where pk<3); 218id select_type table type possible_keys key key_len ref rows filtered Extra 2191 PRIMARY t10 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where; Using index 2201 PRIMARY t1 ALL NULL NULL NULL NULL 103 100.00 Using where; Using join buffer (flat, BNL join) 221Warnings: 222Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t1` where `test`.`t1`.`a` = `test`.`t10`.`pk` and `test`.`t10`.`pk` < 3 223drop table t0, t1, t2; 224drop table t10, t11, t12; 225 226Bug#37899: Wrongly checked optimization prerequisite caused failed 227assertion. 228 229CREATE TABLE t1 ( 230`pk` int(11), 231`varchar_nokey` varchar(5) 232); 233INSERT INTO t1 VALUES 234(1,'qk'),(2,'j'),(3,'aew'); 235SELECT * 236FROM t1 237WHERE varchar_nokey IN ( 238SELECT 239varchar_nokey 240FROM 241t1 242) XOR pk = 30; 243pk varchar_nokey 2441 qk 2452 j 2463 aew 247drop table t1; 248# 249# BUG#41842: Semi-join materialization strategy crashes when the upper query has HAVING 250# 251CREATE TABLE t1 ( 252pk int(11) NOT NULL AUTO_INCREMENT, 253int_nokey int(11) NOT NULL, 254time_key time NOT NULL, 255datetime_key datetime NOT NULL, 256datetime_nokey datetime NOT NULL, 257varchar_key varchar(1) NOT NULL, 258varchar_nokey varchar(1) NOT NULL, 259PRIMARY KEY (pk), 260KEY time_key (time_key), 261KEY datetime_key (datetime_key), 262KEY varchar_key (varchar_key) 263); 264INSERT INTO t1 VALUES 265(1,0, '00:16:10','2008-09-03 14:25:40','2008-09-03 14:25:40','h','h'), 266(2,7, '00:00:00','2001-01-13 00:00:00','2001-01-13 00:00:00','',''), 267(3,0, '00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'), 268(4,2, '16:29:24','2000-10-16 01:39:08','2000-10-16 01:39:08','w','w'), 269(5,1, '09:23:32','0000-00-00 00:00:00','0000-00-00 00:00:00','p','p'), 270(6,3, '00:00:00','2007-12-02 00:00:00','2007-12-02 00:00:00','o','o'), 271(7,3, '00:00:00','2008-09-11 00:00:00','2008-09-11 00:00:00','',''), 272(8,0, '13:59:04','0000-00-00 00:00:00','0000-00-00 00:00:00','s','s'), 273(9,7, '09:01:06','0000-00-00 00:00:00','0000-00-00 00:00:00','d','d'), 274(10,5,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','n','n'), 275(11,0,'21:06:46','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'), 276(12,2,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','',''), 277(13,6,'14:45:34','2003-07-28 02:34:08','2003-07-28 02:34:08','w','w'), 278(14,1,'15:04:12','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'), 279(15,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'), 280(16,0,'15:55:23','2004-03-17 00:32:27','2004-03-17 00:32:27','p','p'), 281(17,1,'16:30:00','2004-12-27 19:20:00','2004-12-27 19:20:00','d','d'), 282(18,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','h','h'), 283(19,0,'14:13:26','2008-11-09 05:53:48','2008-11-09 05:53:48','o','o'), 284(20,0,'00:00:00','2009-10-11 06:58:04','2009-10-11 06:58:04','k','k'); 285CREATE TABLE t2 ( 286pk int(11) NOT NULL AUTO_INCREMENT, 287int_nokey int(11) NOT NULL, 288time_key time NOT NULL, 289datetime_key datetime NOT NULL, 290datetime_nokey datetime NOT NULL, 291varchar_key varchar(1) NOT NULL, 292varchar_nokey varchar(1) NOT NULL, 293PRIMARY KEY (pk), 294KEY time_key (time_key), 295KEY datetime_key (datetime_key), 296KEY varchar_key (varchar_key) 297); 298INSERT INTO t2 VALUES 299(10,0,'19:39:13','0000-00-00 00:00:00','0000-00-00 00:00:00','g','g'), 300(11,8,'03:43:53','0000-00-00 00:00:00','0000-00-00 00:00:00','b','b'); 301SELECT OUTR.datetime_nokey AS X FROM t1 AS OUTR 302WHERE 303OUTR.varchar_nokey IN (SELECT 304INNR . varchar_nokey AS Y 305FROM t2 AS INNR 306WHERE 307INNR . datetime_key >= INNR . time_key OR 308INNR . pk = INNR . int_nokey 309) 310AND OUTR . varchar_nokey <= 'w' 311HAVING X > '2012-12-12'; 312X 313drop table t1, t2; 314# 315# Bug#45191: Incorrectly initialized semi-join led to a wrong result. 316# 317CREATE TABLE STAFF (EMPNUM CHAR(3) NOT NULL, 318EMPNAME CHAR(20), GRADE DECIMAL(4), CITY CHAR(15)); 319CREATE TABLE PROJ (PNUM CHAR(3) NOT NULL, 320PNAME CHAR(20), PTYPE CHAR(6), 321BUDGET DECIMAL(9), 322CITY CHAR(15)); 323CREATE TABLE WORKS (EMPNUM CHAR(3) NOT NULL, 324PNUM CHAR(3) NOT NULL, HOURS DECIMAL(5)); 325INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale'); 326INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna'); 327INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna'); 328INSERT INTO STAFF VALUES ('E4','Don',12,'Deale'); 329INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron'); 330INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale'); 331INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna'); 332INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa'); 333INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale'); 334INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna'); 335INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale'); 336INSERT INTO WORKS VALUES ('E1','P1',40); 337INSERT INTO WORKS VALUES ('E1','P2',20); 338INSERT INTO WORKS VALUES ('E1','P3',80); 339INSERT INTO WORKS VALUES ('E1','P4',20); 340INSERT INTO WORKS VALUES ('E1','P5',12); 341INSERT INTO WORKS VALUES ('E1','P6',12); 342INSERT INTO WORKS VALUES ('E2','P1',40); 343INSERT INTO WORKS VALUES ('E2','P2',80); 344INSERT INTO WORKS VALUES ('E3','P2',20); 345INSERT INTO WORKS VALUES ('E4','P2',20); 346INSERT INTO WORKS VALUES ('E4','P4',40); 347INSERT INTO WORKS VALUES ('E4','P5',80); 348set optimizer_switch=@local_optimizer_switch; 349set optimizer_switch='materialization=off'; 350explain SELECT EMPNUM, EMPNAME 351FROM STAFF 352WHERE EMPNUM IN 353(SELECT EMPNUM FROM WORKS 354WHERE PNUM IN 355(SELECT PNUM FROM PROJ)); 356id select_type table type possible_keys key key_len ref rows Extra 3571 PRIMARY STAFF ALL NULL NULL NULL NULL 5 3581 PRIMARY PROJ ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join) 3591 PRIMARY WORKS ALL NULL NULL NULL NULL 12 Using where; FirstMatch(STAFF); Using join buffer (incremental, BNL join) 360SELECT EMPNUM, EMPNAME 361FROM STAFF 362WHERE EMPNUM IN 363(SELECT EMPNUM FROM WORKS 364WHERE PNUM IN 365(SELECT PNUM FROM PROJ)); 366EMPNUM EMPNAME 367E1 Alice 368E2 Betty 369E3 Carmen 370E4 Don 371set optimizer_switch=@local_optimizer_switch; 372drop table STAFF,WORKS,PROJ; 373# End of bug#45191 374# 375# Bug#46550 Azalea returning duplicate results for some IN subqueries 376# w/ semijoin=on 377# 378DROP TABLE IF EXISTS t0, t1, t2; 379CREATE TABLE t0 ( 380int_key int(11) DEFAULT NULL, 381varchar_key varchar(1) DEFAULT NULL, 382varchar_nokey varchar(1) DEFAULT NULL, 383KEY int_key (int_key), 384KEY varchar_key (varchar_key,int_key) 385); 386INSERT INTO t0 VALUES 387(1,'m','m'), 388(40,'h','h'), 389(1,'r','r'), 390(1,'h','h'), 391(9,'x','x'), 392(NULL,'q','q'), 393(NULL,'k','k'), 394(7,'l','l'), 395(182,'k','k'), 396(202,'a','a'), 397(7,'x','x'), 398(6,'j','j'), 399(119,'z','z'), 400(4,'d','d'), 401(5,'h','h'), 402(1,'u','u'), 403(3,'q','q'), 404(7,'a','a'), 405(3,'e','e'), 406(6,'l','l'); 407CREATE TABLE t1 ( 408int_key int(11) DEFAULT NULL, 409varchar_key varchar(1) DEFAULT NULL, 410varchar_nokey varchar(1) DEFAULT NULL, 411KEY int_key (int_key), 412KEY varchar_key (varchar_key,int_key) 413); 414INSERT INTO t1 VALUES (7,NULL,NULL),(4,'x','x'); 415CREATE TABLE t2 ( 416int_key int(11) DEFAULT NULL, 417varchar_key varchar(1) DEFAULT NULL, 418varchar_nokey varchar(1) DEFAULT NULL, 419KEY int_key (int_key), 420KEY varchar_key (varchar_key,int_key) 421); 422INSERT INTO t2 VALUES (123,NULL,NULL); 423SELECT int_key 424FROM t0 425WHERE varchar_nokey IN ( 426SELECT t1 .varchar_key from t1 427); 428int_key 4299 4307 431DROP TABLE t0, t1, t2; 432# End of bug#46550 433# 434# Bug #46744 Crash in optimize_semijoin_nests on empty view 435# with limit and procedure. 436# 437DROP TABLE IF EXISTS t1, t2; 438DROP VIEW IF EXISTS v1; 439DROP PROCEDURE IF EXISTS p1; 440CREATE TABLE t1 ( f1 int ); 441CREATE TABLE t2 ( f1 int ); 442insert into t2 values (5), (7); 443CREATE OR REPLACE VIEW v1 AS SELECT * FROM t1 LIMIT 2; 444create procedure p1() 445select COUNT(*) 446FROM v1 WHERE f1 IN 447(SELECT f1 FROM t2 WHERE f1 = ANY (SELECT f1 FROM v1)); 448SET SESSION optimizer_switch = 'semijoin=on'; 449CALL p1(); 450COUNT(*) 4510 452SET SESSION optimizer_switch = 'semijoin=off'; 453CALL p1(); 454COUNT(*) 4550 456drop table t1, t2; 457drop view v1; 458drop procedure p1; 459set SESSION optimizer_switch=@local_optimizer_switch; 460# End of bug#46744 461 462Bug#46797 "Crash in fix_semijoin_strategies_for_picked_join_order 463with semijoin=on" 464 465CREATE TABLE t1 ( 466varchar_key varchar(1) DEFAULT NULL, 467KEY varchar_key (varchar_key) 468); 469CREATE TABLE t2 ( 470varchar_key varchar(1) DEFAULT NULL, 471KEY varchar_key (varchar_key) 472); 473INSERT INTO t2 VALUES 474(NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'), 475('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'), 476('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'), 477('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'), 478('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'), 479('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'), 480('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'), 481('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z'); 482CREATE TABLE t3 ( 483varchar_key varchar(1) DEFAULT NULL, 484KEY varchar_key (varchar_key) 485) ENGINE=MyISAM DEFAULT CHARSET=latin1; 486INSERT INTO t3 VALUES 487(NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'), 488('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y'); 489SELECT varchar_key FROM t3 490WHERE (SELECT varchar_key FROM t3 491WHERE (varchar_key,varchar_key) 492IN (SELECT t1.varchar_key, t2 .varchar_key 493FROM t1 RIGHT JOIN t2 ON t1.varchar_key 494) 495); 496varchar_key 497DROP TABLE t1, t2, t3; 498# 499# Bug#46556 Returning incorrect, empty results for some IN subqueries 500# w/semijoin=on 501# 502CREATE TABLE t0 ( 503pk INTEGER, 504vkey VARCHAR(1), 505vnokey VARCHAR(1), 506PRIMARY KEY (pk), 507KEY vkey(vkey) 508); 509INSERT INTO t0 510VALUES (1,'g','g'), (2,'v','v'), (3,'t','t'), (4,'u','u'), (5,'n','n'); 511EXPLAIN EXTENDED SELECT vkey FROM t0 WHERE pk IN 512(SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey); 513id select_type table type possible_keys key key_len ref rows filtered Extra 5141 PRIMARY t0 ALL PRIMARY NULL NULL NULL 5 100.00 5151 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t0.pk 1 100.00 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 5161 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 2 100.00 Using index; FirstMatch(t1) 517Warnings: 518Note 1003 select `test`.`t0`.`vkey` AS `vkey` from `test`.`t0` `t1` semi join (`test`.`t0` `t2`) join `test`.`t0` where `test`.`t1`.`pk` = `test`.`t0`.`pk` and `test`.`t2`.`vkey` = `test`.`t1`.`vnokey` 519SELECT vkey FROM t0 WHERE pk IN 520(SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey); 521vkey 522g 523v 524t 525u 526n 527DROP TABLE t0; 528# End of bug#46556 529 530Bug #48073 Subquery on char columns from view crashes Mysql 531 532DROP TABLE IF EXISTS t1, t2; 533DROP VIEW IF EXISTS v1; 534CREATE TABLE t1 ( 535city VARCHAR(50) NOT NULL, 536country_id SMALLINT UNSIGNED NOT NULL 537); 538INSERT INTO t1 VALUES 539('Batna',2), 540('Bchar',2), 541('Skikda',2), 542('Tafuna',3), 543('Algeria',2) ; 544CREATE TABLE t2 ( 545country_id SMALLINT UNSIGNED NOT NULL, 546country VARCHAR(50) NOT NULL 547); 548INSERT INTO t2 VALUES 549(2,'Algeria'), 550(3,'American Samoa') ; 551CREATE VIEW v1 AS 552SELECT country_id, country 553FROM t2 554WHERE LEFT(country,1) = "A" 555; 556SELECT city, country_id 557FROM t1 558WHERE city IN ( 559SELECT country 560FROM t2 561WHERE LEFT(country, 1) = "A" 562); 563city country_id 564Algeria 2 565SELECT city, country_id 566FROM t1 567WHERE city IN ( 568SELECT country 569FROM v1 570); 571city country_id 572Algeria 2 573drop table t1, t2; 574drop view v1; 575# End of bug#48073 576 577Bug#48834: Procedure with view + subquery + semijoin=on 578crashes on second call. 579 580SET SESSION optimizer_switch ='semijoin=on'; 581CREATE TABLE t1 ( t1field integer, primary key (t1field)); 582CREATE TABLE t2 ( t2field integer, primary key (t2field)); 583CREATE VIEW v1 AS 584SELECT t1field as v1field 585FROM t1 A 586WHERE A.t1field IN (SELECT t1field FROM t2 ); 587CREATE VIEW v2 AS 588SELECT t2field as v2field 589FROM t2 A 590WHERE A.t2field IN (SELECT t2field FROM t2 ); 591CREATE PROCEDURE p1 () 592BEGIN 593SELECT v1field 594FROM v1 595WHERE v1field IN ( SELECT v2field as vf_inner FROM v2 ); 596END| 597INSERT INTO t1 VALUES (1),(2),(3); 598INSERT INTO t2 VALUES (2),(3),(4); 599CALL p1; 600v1field 6012 6023 603CALL p1; 604v1field 6052 6063 607DROP TABLE t1,t2; 608DROP VIEW v1,v2; 609DROP PROCEDURE p1; 610set SESSION optimizer_switch=@local_optimizer_switch; 611# End of BUG#48834 612 613Bug#49097 subquery with view generates wrong result with 614non-prepared statement 615 616DROP TABLE IF EXISTS t1, t2; 617DROP VIEW IF EXISTS v1; 618CREATE TABLE t1 ( 619city VARCHAR(50) NOT NULL, 620country_id SMALLINT UNSIGNED NOT NULL 621); 622INSERT INTO t1 VALUES 623('Batna',2), 624('Bchar',2), 625('Skikda',2), 626('Tafuna',3), 627('Algeria',2) ; 628CREATE TABLE t2 ( 629country_id SMALLINT UNSIGNED NOT NULL, 630country VARCHAR(50) NOT NULL 631); 632INSERT INTO t2 VALUES 633(2,'Algeria'), 634(3,'XAmerican Samoa') ; 635CREATE VIEW v1 AS 636SELECT country_id, country 637FROM t2 638WHERE LEFT(country,1) = "A" 639; 640SELECT city, country_id 641FROM t1 642WHERE country_id IN ( 643SELECT country_id 644FROM t2 645WHERE LEFT(country,1) = "A" 646); 647city country_id 648Batna 2 649Bchar 2 650Skikda 2 651Algeria 2 652SELECT city, country_id 653FROM t1 654WHERE country_id IN ( 655SELECT country_id 656FROM v1 657); 658city country_id 659Batna 2 660Bchar 2 661Skikda 2 662Algeria 2 663PREPARE stmt FROM 664" 665SELECT city, country_id 666FROM t1 667WHERE country_id IN ( 668 SELECT country_id 669 FROM v1 670); 671"; 672execute stmt; 673city country_id 674Batna 2 675Bchar 2 676Skikda 2 677Algeria 2 678deallocate prepare stmt; 679drop table t1, t2; 680drop view v1; 681# End of Bug#49097 682# 683# BUG#38075: Wrong result: rows matching a subquery with outer join not returned 684# 685DROP TABLE IF EXISTS ot1, it1, it2; 686CREATE TABLE it2 ( 687int_key int(11) NOT NULL, 688datetime_key datetime NOT NULL, 689KEY int_key (int_key), 690KEY datetime_key (datetime_key) 691); 692INSERT INTO it2 VALUES 693(5,'2002-04-10 14:25:30'), (0,'0000-00-00 00:00:00'), 694(0,'2006-09-14 04:01:02'), (4,'0000-00-00 00:00:00'), 695(8,'0000-00-00 00:00:00'), (5,'0000-00-00 00:00:00'), 696(9,'0000-00-00 00:00:00'), (8,'2007-04-01 11:04:17'), 697(1,'0000-00-00 00:00:00'), (7,'2009-01-12 00:00:00'), 698(0,'2009-06-05 00:00:00'), (3,'2006-02-14 18:06:35'), 699(5,'2006-02-21 07:08:16'), (0,'0000-00-00 00:00:00'), 700(7,'0000-00-00 00:00:00'), (0,'0000-00-00 00:00:00'), 701(0,'2007-02-13 00:00:00'), (1,'0000-00-00 00:00:00'), 702(0,'0000-00-00 00:00:00'), (1,'2003-08-11 00:00:00'); 703CREATE TABLE ot1 ( 704int_nokey int(11) NOT NULL, 705int_key int(11) NOT NULL, 706KEY int_key (int_key) 707); 708INSERT INTO ot1 VALUES 709(5,0), (3,0), (0,2), (3,0), (1,3), (0,0), (1,7), (7,0), (1,7), (0,7), 710(0,9), (8,2), (4,4), (9,3), (0,9), (2,5), (0,5), (8,0), (5,8), (1,5); 711CREATE TABLE it1 ( 712int_nokey int(11) NOT NULL, 713int_key int(11) NOT NULL, 714KEY int_key (int_key) 715); 716INSERT INTO it1 VALUES 717(9,5), (0,4); 718SELECT int_key FROM ot1 719WHERE int_nokey IN (SELECT it2.int_key 720FROM it1 LEFT JOIN it2 ON it2.datetime_key); 721int_key 7220 7230 7240 7250 7260 7270 7282 7292 7303 7315 7325 7337 7347 7357 7368 7379 7389 739EXPLAIN 740SELECT int_key FROM ot1 741WHERE int_nokey IN (SELECT it2.int_key 742FROM it1 LEFT JOIN it2 ON it2.datetime_key); 743id select_type table type possible_keys key key_len ref rows Extra 7441 PRIMARY ot1 ALL NULL NULL NULL NULL 20 7451 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 7462 MATERIALIZED it1 index NULL int_key 4 NULL 2 Using index 7472 MATERIALIZED it2 ALL int_key,datetime_key NULL NULL NULL 20 Using where; Using join buffer (flat, BNL join) 748DROP TABLE ot1, it1, it2; 749# End of BUG#38075 750# 751# BUG#31480: Incorrect result for nested subquery when executed via semi join 752# 753create table t1 (a int not null, b int not null); 754create table t2 (c int not null, d int not null); 755create table t3 (e int not null); 756insert into t1 values (1,10); 757insert into t1 values (2,10); 758insert into t1 values (1,20); 759insert into t1 values (2,20); 760insert into t1 values (3,20); 761insert into t1 values (2,30); 762insert into t1 values (4,40); 763insert into t2 values (2,10); 764insert into t2 values (2,20); 765insert into t2 values (4,10); 766insert into t2 values (5,10); 767insert into t2 values (3,20); 768insert into t2 values (2,40); 769insert into t3 values (10); 770insert into t3 values (30); 771insert into t3 values (10); 772insert into t3 values (20); 773explain extended 774select a from t1 775where a in (select c from t2 where d >= some(select e from t3 where b=e)); 776id select_type table type possible_keys key key_len ref rows filtered Extra 7771 PRIMARY t1 ALL NULL NULL NULL NULL 7 100.00 7781 PRIMARY t2 ALL NULL NULL NULL NULL 6 100.00 Using where; FirstMatch(t1); Using join buffer (flat, BNL join) 7793 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where 780Warnings: 781Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1 782Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`c` = `test`.`t1`.`a` and <nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`>(<in_optimizer>(`test`.`t2`.`d`,<exists>(/* select#3 */ select `test`.`t3`.`e` from `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`e` and <cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`)))) 783show warnings; 784Level Code Message 785Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1 786Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`c` = `test`.`t1`.`a` and <nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`>(<in_optimizer>(`test`.`t2`.`d`,<exists>(/* select#3 */ select `test`.`t3`.`e` from `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`e` and <cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`)))) 787select a from t1 788where a in (select c from t2 where d >= some(select e from t3 where b=e)); 789a 7902 7912 7923 7932 794drop table t1, t2, t3; 795# 796# Bug#48213 Materialized subselect crashes if using GEOMETRY type 797# 798CREATE TABLE t1 ( 799pk int, 800a varchar(1), 801b varchar(4), 802c tinyblob, 803d blob, 804e mediumblob, 805f longblob, 806g tinytext, 807h text, 808i mediumtext, 809j longtext, 810k geometry, 811PRIMARY KEY (pk) 812); 813INSERT INTO t1 VALUES (1,'o','ffff','ffff','ffoo','ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')), (2,'f','ffff','ffff','ffff', 'ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')); 814CREATE TABLE t2 LIKE t1; 815INSERT INTO t2 VALUES (1,'i','iiii','iiii','iiii','iiii','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')), (2,'f','ffff','ffff','ffff','ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')); 816EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0); 817id select_type table type possible_keys key key_len ref rows filtered Extra 8181 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8191 PRIMARY <subquery2> eq_ref distinct_key distinct_key 11 func,func 1 100.00 8202 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan 821Warnings: 822Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`pk` > 0 823SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0); 824pk 8252 826EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0); 827id select_type table type possible_keys key key_len ref rows filtered Extra 8281 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8291 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 830Warnings: 831Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`c` = `test`.`t1`.`c` and `test`.`t2`.`pk` > 0 832SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0); 833pk 8341 8352 836EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0); 837id select_type table type possible_keys key key_len ref rows filtered Extra 8381 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8391 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 840Warnings: 841Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`d` = `test`.`t1`.`d` and `test`.`t2`.`pk` > 0 842SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0); 843pk 8442 845EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0); 846id select_type table type possible_keys key key_len ref rows filtered Extra 8471 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8481 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 849Warnings: 850Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`e` = `test`.`t1`.`e` and `test`.`t2`.`pk` > 0 851SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0); 852pk 8531 8542 855EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0); 856id select_type table type possible_keys key key_len ref rows filtered Extra 8571 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8581 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 859Warnings: 860Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`f` = `test`.`t1`.`f` and `test`.`t2`.`pk` > 0 861SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0); 862pk 8631 8642 865EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0); 866id select_type table type possible_keys key key_len ref rows filtered Extra 8671 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8681 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 869Warnings: 870Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`g` = `test`.`t1`.`g` and `test`.`t2`.`pk` > 0 871SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0); 872pk 8731 8742 875EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0); 876id select_type table type possible_keys key key_len ref rows filtered Extra 8771 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8781 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 879Warnings: 880Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`h` = `test`.`t1`.`h` and `test`.`t2`.`pk` > 0 881SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0); 882pk 8831 8842 885EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0); 886id select_type table type possible_keys key key_len ref rows filtered Extra 8871 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8881 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 889Warnings: 890Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`i` = `test`.`t1`.`i` and `test`.`t2`.`pk` > 0 891SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0); 892pk 8931 8942 895EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0); 896id select_type table type possible_keys key key_len ref rows filtered Extra 8971 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 8981 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 899Warnings: 900Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`j` = `test`.`t1`.`j` and `test`.`t2`.`pk` > 0 901SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0); 902pk 9031 9042 905EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0); 906id select_type table type possible_keys key key_len ref rows filtered Extra 9071 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 9081 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1); Using join buffer (flat, BNL join) 909Warnings: 910Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`b` = `test`.`t1`.`b` and `test`.`t2`.`k` = `test`.`t1`.`k` and `test`.`t2`.`pk` > 0 911SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0); 912pk 9131 9142 915DROP TABLE t1, t2; 916# End of Bug#48213 917# 918# Bug#49198 Wrong result for second call of procedure 919# with view in subselect. 920# 921CREATE TABLE t1 (t1field integer, primary key (t1field)); 922CREATE TABLE t2 (t2field integer, primary key (t2field)); 923CREATE TABLE t3 (t3field integer, primary key (t3field)); 924CREATE VIEW v2 AS SELECT * FROM t2; 925CREATE VIEW v3 AS SELECT * FROM t3; 926INSERT INTO t1 VALUES(1),(2); 927INSERT INTO t2 VALUES(1),(2); 928INSERT INTO t3 VALUES(1),(2); 929PREPARE stmt FROM 930" 931SELECT t1field 932FROM t1 933WHERE t1field IN (SELECT * FROM v2); 934"; 935EXECUTE stmt; 936t1field 9371 9382 939EXECUTE stmt; 940t1field 9411 9422 943PREPARE stmt FROM 944" 945EXPLAIN 946SELECT t1field 947FROM t1 948WHERE t1field IN (SELECT * FROM v2) 949 AND t1field IN (SELECT * FROM v3) 950"; 951EXECUTE stmt; 952id select_type table type possible_keys key key_len ref rows Extra 9531 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index 9541 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index 9551 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index 956EXECUTE stmt; 957id select_type table type possible_keys key key_len ref rows Extra 9581 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index 9591 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index 9601 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index 961DROP TABLE t1, t2, t3; 962DROP VIEW v2, v3; 963# End of Bug#49198 964# 965# Bug#45174: Incorrectly applied equality propagation caused wrong 966# result on a query with a materialized semi-join. 967# 968CREATE TABLE `t1` ( 969`pk` int(11) NOT NULL AUTO_INCREMENT, 970`varchar_key` varchar(1) NOT NULL, 971`varchar_nokey` varchar(1) NOT NULL, 972PRIMARY KEY (`pk`), 973KEY `varchar_key` (`varchar_key`) 974); 975INSERT INTO `t1` VALUES (11,'m','m'),(12,'j','j'),(13,'z','z'),(14,'a','a'),(15,'',''),(16,'e','e'),(17,'t','t'),(19,'b','b'),(20,'w','w'),(21,'m','m'),(23,'',''),(24,'w','w'),(26,'e','e'),(27,'e','e'),(28,'p','p'); 976CREATE TABLE `t2` ( 977`varchar_nokey` varchar(1) NOT NULL 978); 979INSERT INTO `t2` VALUES ('v'),('u'),('n'),('l'),('h'),('u'),('n'),('j'),('k'),('e'),('i'),('u'),('n'),('b'),('x'),(''),('q'),('u'); 980EXPLAIN EXTENDED SELECT varchar_nokey 981FROM t2 982WHERE ( `varchar_nokey` , `varchar_nokey` ) IN ( 983SELECT `varchar_key` , `varchar_nokey` 984FROM t1 985WHERE `varchar_nokey` < 'n' XOR `pk` ) ; 986id select_type table type possible_keys key key_len ref rows filtered Extra 9871 PRIMARY t2 ALL NULL NULL NULL NULL 18 100.00 9881 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 9892 MATERIALIZED t1 ALL varchar_key NULL NULL NULL 15 100.00 Using where 990Warnings: 991Note 1003 select `test`.`t2`.`varchar_nokey` AS `varchar_nokey` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`varchar_nokey` = `test`.`t1`.`varchar_key` and (`test`.`t1`.`varchar_key` < 'n' xor `test`.`t1`.`pk`) 992SELECT varchar_nokey 993FROM t2 994WHERE ( `varchar_nokey` , `varchar_nokey` ) IN ( 995SELECT `varchar_key` , `varchar_nokey` 996FROM t1 997WHERE `varchar_nokey` < 'n' XOR `pk` ) ; 998varchar_nokey 999DROP TABLE t1, t2; 1000# End of the test for bug#45174. 1001# 1002# BUG#43768: Prepared query with nested subqueries core dumps on second execution 1003# 1004create table t1 ( 1005id int(11) unsigned not null primary key auto_increment, 1006partner_id varchar(35) not null, 1007t1_status_id int(10) unsigned 1008); 1009insert into t1 values ("1", "partner1", "10"), ("2", "partner2", "10"), 1010("3", "partner3", "10"), ("4", "partner4", "10"); 1011create table t2 ( 1012id int(11) unsigned not null default '0', 1013t1_line_id int(11) unsigned not null default '0', 1014article_id varchar(20), 1015sequence int(11) not null default '0', 1016primary key (id,t1_line_id) 1017); 1018insert into t2 values ("1", "1", "sup", "0"), ("2", "1", "sup", "1"), 1019("2", "2", "sup", "2"), ("2", "3", "sup", "3"), 1020("2", "4", "imp", "4"), ("3", "1", "sup", "0"), 1021("4", "1", "sup", "0"); 1022create table t3 ( 1023id int(11) not null default '0', 1024preceding_id int(11) not null default '0', 1025primary key (id,preceding_id) 1026); 1027create table t4 ( 1028user_id varchar(50) not null, 1029article_id varchar(20) not null, 1030primary key (user_id,article_id) 1031); 1032insert into t4 values("nicke", "imp"); 1033prepare stmt from 1034'select t1.partner_id 1035from t1 1036where 1037 t1.id in ( 1038 select pl_inner.id 1039 from t2 as pl_inner 1040 where pl_inner.article_id in ( 1041 select t4.article_id from t4 1042 where t4.user_id = \'nicke\' 1043 ) 1044 )'; 1045execute stmt; 1046partner_id 1047partner2 1048execute stmt; 1049partner_id 1050partner2 1051drop table t1,t2,t3,t4; 1052# 1053# Bug#48623 Multiple subqueries are optimized incorrectly 1054# 1055CREATE TABLE t1(val VARCHAR(10)); 1056CREATE TABLE t2(val VARCHAR(10)); 1057CREATE TABLE t3(val VARCHAR(10)); 1058INSERT INTO t1 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp'); 1059INSERT INTO t2 VALUES('aaa'), ('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp'); 1060INSERT INTO t3 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp'); 1061EXPLAIN 1062SELECT * 1063FROM t1 1064WHERE t1.val IN (SELECT t2.val FROM t2 1065WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%') 1066AND t1.val IN (SELECT t3.val FROM t3 1067WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%'); 1068id select_type table type possible_keys key key_len ref rows Extra 10691 PRIMARY t1 ALL NULL NULL NULL NULL 5 10701 PRIMARY <subquery3> eq_ref distinct_key distinct_key 13 func 1 10711 PRIMARY <subquery2> eq_ref distinct_key distinct_key 13 func 1 10723 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using where 10732 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where 1074SELECT * 1075FROM t1 1076WHERE t1.val IN (SELECT t2.val FROM t2 1077WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%') 1078AND t1.val IN (SELECT t3.val FROM t3 1079WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%'); 1080val 1081aaa 1082eee 1083DROP TABLE t1; 1084DROP TABLE t2; 1085DROP TABLE t3; 1086# End of Bug#48623 1087# 1088# LPBUG#602574: RQG: sql_select.cc:5385: bool greedy_search(JOIN*, table_map, uint, 1089# uint): Assertion `join->best_read < 1090# 1091set @tmp_optimizer_switch=@@optimizer_switch; 1092set optimizer_switch='materialization=off'; 1093CREATE TABLE t1 ( 1094varchar_key varchar(1) DEFAULT NULL, 1095KEY varchar_key (varchar_key) 1096); 1097CREATE TABLE t2 ( 1098varchar_key varchar(1) DEFAULT NULL, 1099KEY varchar_key (varchar_key) 1100); 1101INSERT INTO t2 VALUES 1102(NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'), 1103('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'), 1104('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'), 1105('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'), 1106('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'), 1107('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'), 1108('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'), 1109('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z'); 1110CREATE TABLE t3 ( 1111varchar_key varchar(1) DEFAULT NULL, 1112KEY varchar_key (varchar_key) 1113) ENGINE=MyISAM DEFAULT CHARSET=latin1; 1114INSERT INTO t3 VALUES 1115(NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'), 1116('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y'); 1117SELECT varchar_key FROM t3 1118WHERE (SELECT varchar_key FROM t3 1119WHERE (varchar_key,varchar_key) 1120IN (SELECT t1.varchar_key, t2 .varchar_key 1121FROM t1 RIGHT JOIN t2 ON t1.varchar_key 1122) 1123); 1124varchar_key 1125set optimizer_switch=@tmp_optimizer_switch; 1126DROP TABLE t1, t2, t3; 1127# 1128# Bug#46692 "Crash occurring on queries with nested FROM subqueries 1129# using materialization." 1130# 1131CREATE TABLE t1 ( 1132pk INTEGER PRIMARY KEY, 1133int_key INTEGER, 1134KEY int_key(int_key) 1135); 1136INSERT INTO t1 VALUES (10,186),(11,NULL),(12,2),(13,3),(14,0),(15,133),(16,1); 1137CREATE TABLE t2 ( 1138pk INTEGER PRIMARY KEY, 1139int_key INTEGER, 1140KEY int_key(int_key) 1141); 1142INSERT INTO t2 VALUES (1,7),(2,2); 1143SELECT * FROM t1 WHERE (140, 4) IN 1144(SELECT t2.int_key, t2 .pk FROM t2 STRAIGHT_JOIN t1 ON t2.int_key); 1145pk int_key 1146DROP TABLE t1, t2; 1147# 1148# Bug#42353 "SELECT ... WHERE oe IN (SELECT w/ LEFT JOIN) query 1149# causes crash." 1150# 1151CREATE TABLE t1 ( 1152pk INTEGER PRIMARY KEY, 1153int_nokey INTEGER, 1154int_key INTEGER, 1155date_key DATE, 1156datetime_nokey DATETIME, 1157varchar_nokey VARCHAR(1) 1158); 1159CREATE TABLE t2 ( 1160date_nokey DATE 1161); 1162CREATE TABLE t3 ( 1163pk INTEGER PRIMARY KEY, 1164int_nokey INTEGER, 1165date_key date, 1166varchar_key VARCHAR(1), 1167varchar_nokey VARCHAR(1), 1168KEY date_key (date_key) 1169); 1170SELECT date_key FROM t1 1171WHERE (int_key, int_nokey) 1172IN (SELECT t3.int_nokey, t3.pk 1173FROM t2 LEFT JOIN t3 ON (t2.date_nokey < t3.date_key) 1174WHERE t3.varchar_key <= t3.varchar_nokey OR t3.int_nokey <= t3.pk 1175) 1176AND (varchar_nokey <> 'f' OR NOT int_key < 7); 1177date_key 1178# 1179# Bug#45933 "Crash in optimize_semijoin_nests on JOIN in subquery 1180# + AND in outer query". 1181# 1182INSERT INTO t1 VALUES (10,7,5,'2009-06-16','2002-04-10 14:25:30','w'), 1183(11,7,0,'0000-00-00','0000-00-00 00:00:00','s'), 1184(12,4,0,'2003-07-14','2006-09-14 04:01:02','y'), 1185(13,0,4,'2002-07-25','0000-00-00 00:00:00','c'), 1186(14,1,8,'2007-07-03','0000-00-00 00:00:00','q'), 1187(15,6,5,'2001-11-12','0000-00-00 00:00:00',''), 1188(16,2,9,'0000-00-00','0000-00-00 00:00:00','j'), 1189(29,9,1,'0000-00-00','2003-08-11 00:00:00','m'); 1190INSERT INTO t3 VALUES (1,9,'0000-00-00','b','b'), 1191(2,2,'2002-09-17','h','h'); 1192SELECT t1.varchar_nokey FROM t1 JOIN t3 ON t1.datetime_nokey 1193WHERE t1.varchar_nokey 1194IN (SELECT varchar_nokey FROM t1 1195WHERE (pk) 1196IN (SELECT t3.int_nokey 1197FROM t3 LEFT JOIN t1 ON t1.varchar_nokey 1198WHERE t3.date_key BETWEEN '2008-06-07' AND '2006-06-26' 1199 ) 1200); 1201varchar_nokey 1202DROP TABLE t1, t2, t3; 1203# 1204# Bug#45219 "Crash on SELECT DISTINCT query containing a 1205# LEFT JOIN in subquery" 1206# 1207CREATE TABLE t1 ( 1208pk INTEGER NOT NULL, 1209int_nokey INTEGER NOT NULL, 1210datetime_key DATETIME NOT NULL, 1211varchar_key VARCHAR(1) NOT NULL, 1212PRIMARY KEY (pk), 1213KEY datetime_key (datetime_key), 1214KEY varchar_key (varchar_key) 1215); 1216INSERT INTO t1 VALUES 1217(1,9,'0000-00-00 00:00:00','p'),(2,0,'2002-02-09 07:38:13','v'), 1218(3,8,'2001-05-03 12:08:14','t'),(4,3,'0000-00-00 00:00:00','u'), 1219(5,7,'2009-07-28 03:43:30','n'),(6,0,'2009-08-04 00:00:00','l'), 1220(7,1,'0000-00-00 00:00:00','h'),(8,9,'0000-00-00 00:00:00','u'), 1221(9,0,'2005-08-02 17:16:54','n'),(10,9,'2002-12-21 00:00:00','j'), 1222(11,0,'2005-08-15 12:37:35','k'),(12,5,'0000-00-00 00:00:00','e'), 1223(13,0,'2006-03-10 00:00:00','i'),(14,8,'2005-05-16 11:02:36','u'), 1224(15,8,'2008-11-02 00:00:00','n'),(16,5,'2006-03-15 00:00:00','b'), 1225(17,1,'0000-00-00 00:00:00','x'),(18,7,'0000-00-00 00:00:00',''), 1226(19,0,'2008-12-17 20:15:40','q'),(20,9,'0000-00-00 00:00:00','u'); 1227CREATE TABLE t2 LIKE t1; 1228INSERT INTO t2 VALUES 1229(10,0,'2006-07-07 07:26:28','q'),(11,5,'2002-09-23 00:00:00','m'), 1230(12,7,'0000-00-00 00:00:00','j'),(13,1,'2006-06-07 00:00:00','z'), 1231(14,8,'2000-09-16 12:15:34','a'),(15,2,'2007-08-05 15:47:52',''), 1232(16,1,'0000-00-00 00:00:00','e'),(17,8,'2005-12-02 19:34:26','t'), 1233(18,5,'0000-00-00 00:00:00','q'),(19,4,'0000-00-00 00:00:00','b'), 1234(20,5,'2007-12-28 00:00:00','w'),(21,3,'2004-08-02 11:48:43','m'), 1235(22,0,'0000-00-00 00:00:00','x'),(23,8,'2004-04-19 12:18:43',''), 1236(24,0,'2009-04-27 00:00:00','w'),(25,4,'2006-10-20 14:52:15','x'), 1237(26,0,'0000-00-00 00:00:00','e'),(27,0,'2002-03-22 11:48:37','e'), 1238(28,2,'0000-00-00 00:00:00','p'),(29,0,'2001-01-04 03:55:07','x'); 1239CREATE TABLE t3 LIKE t1; 1240INSERT INTO t3 VALUES 1241(10,8,'2007-08-19 08:08:38','i'),(11,0,'2000-05-21 03:51:51',''); 1242SELECT DISTINCT datetime_key FROM t1 1243WHERE (int_nokey, pk) 1244IN (SELECT t3.pk, t3.pk FROM t2 LEFT JOIN t3 ON t3.varchar_key) 1245AND pk = 9; 1246datetime_key 1247DROP TABLE t1, t2, t3; 1248# 1249# BUG#53060: LooseScan semijoin strategy does not return all rows 1250# 1251set @tmp_optimizer_switch=@@optimizer_switch; 1252set optimizer_switch='semijoin=on,materialization=off'; 1253set optimizer_switch='firstmatch=off,loosescan=on'; 1254set @tmp_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity; 1255set optimizer_use_condition_selectivity=4; 1256CREATE TABLE t1 (i INTEGER); 1257INSERT INTO t1 VALUES (1), (2), (3), (4), (5); 1258CREATE TABLE t2 (i INTEGER, j INTEGER, KEY k(i, j)); 1259INSERT INTO t2 VALUES (1, 0), (1, 1), (2, 0), (2, 1); 1260EXPLAIN 1261SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0); 1262id select_type table type possible_keys key key_len ref rows Extra 12631 PRIMARY t2 index k k 10 NULL 4 Using where; Using index; Start temporary 12641 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where; End temporary; Using join buffer (flat, BNL join) 1265SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0); 1266i 12671 12682 1269DROP TABLE t1, t2; 1270set optimizer_switch=@tmp_optimizer_switch; 1271set @@optimizer_use_condition_selectivity=@tmp_optimizer_use_condition_selectivity; 1272# 1273# BUG#49453: re-execution of prepared statement with view 1274# and semijoin crashes 1275# 1276CREATE TABLE t1 (city VARCHAR(50), country_id INT); 1277CREATE TABLE t2 (country_id INT, country VARCHAR(50)); 1278INSERT INTO t1 VALUES 1279('Batna',2),('Bchar',2),('Skikda',2),('Tafuna',3),('Algeria',2) ; 1280INSERT INTO t2 VALUES (2,'Algeria'),(2,'AlgeriaDup'),(3,'XAmerican Samoa'); 1281CREATE VIEW v1 AS 1282SELECT country_id as vf_country_id 1283FROM t2 1284WHERE LEFT(country,1) = "A"; 1285PREPARE stmt FROM " 1286SELECT city, country_id 1287FROM t1 1288WHERE country_id IN (SELECT vf_country_id FROM v1); 1289"; 1290 1291EXECUTE stmt; 1292city country_id 1293Batna 2 1294Bchar 2 1295Skikda 2 1296Algeria 2 1297EXECUTE stmt; 1298city country_id 1299Batna 2 1300Bchar 2 1301Skikda 2 1302Algeria 2 1303DROP TABLE t1,t2; 1304DROP VIEW v1; 1305# 1306# Bug#54437 Extra rows with LEFT JOIN + semijoin 1307# 1308create table t1 (a int); 1309create table t2 (a int); 1310create table t3 (a int); 1311insert into t1 values(1),(1); 1312insert into t2 values(1),(1),(1),(1); 1313insert into t3 values(2),(2); 1314set @tmp_optimizer_switch=@@optimizer_switch; 1315set optimizer_switch='materialization=off'; 1316set optimizer_switch='semijoin=off'; 1317explain 1318select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a); 1319id select_type table type possible_keys key key_len ref rows Extra 13201 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 13212 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where 13222 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 1323select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a); 1324a 13251 13261 1327set optimizer_switch='semijoin=on'; 1328explain 1329select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a); 1330id select_type table type possible_keys key key_len ref rows Extra 13311 PRIMARY t1 ALL NULL NULL NULL NULL 2 13321 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where; Start temporary; Using join buffer (flat, BNL join) 13331 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary; Using join buffer (incremental, BNL join) 1334select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a); 1335a 13361 13371 1338set optimizer_switch=@tmp_optimizer_switch; 1339drop table t1,t2,t3; 1340# 1341# Bug#55955: crash in MEMORY engine with IN(LEFT JOIN (JOIN)) 1342# 1343CREATE TABLE t1 (a INT); 1344CREATE TABLE t2 (a INT); 1345CREATE TABLE t3 (a INT); 1346INSERT INTO t1 VALUES(1),(1); 1347INSERT INTO t2 VALUES(1),(1); 1348INSERT INTO t3 VALUES(2),(2); 1349set @tmp_optimzer_switch=@@optimizer_switch; 1350set optimizer_switch='semijoin=off,materialization=off'; 1351EXPLAIN 1352SELECT * FROM t1 1353WHERE t1.a IN (SELECT t2.a 1354FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1355id select_type table type possible_keys key key_len ref rows Extra 13561 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 13572 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where 13582 DEPENDENT SUBQUERY t2inner ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 13592 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (incremental, BNL join) 1360SELECT * FROM t1 1361WHERE t1.a IN (SELECT t2.a 1362FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1363a 13641 13651 1366set optimizer_switch='semijoin=off,materialization=on'; 1367EXPLAIN 1368SELECT * FROM t1 1369WHERE t1.a IN (SELECT t2.a 1370FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1371id select_type table type possible_keys key key_len ref rows Extra 13721 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 13732 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 13742 MATERIALIZED t2inner ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 13752 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (incremental, BNL join) 1376SELECT * FROM t1 1377WHERE t1.a IN (SELECT t2.a 1378FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1379a 13801 13811 1382set optimizer_switch='semijoin=on,materialization=off'; 1383EXPLAIN 1384SELECT * FROM t1 1385WHERE t1.a IN (SELECT t2.a 1386FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1387id select_type table type possible_keys key key_len ref rows Extra 13881 PRIMARY t1 ALL NULL NULL NULL NULL 2 13891 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Start temporary; Using join buffer (flat, BNL join) 13901 PRIMARY t2inner ALL NULL NULL NULL NULL 2 Using join buffer (incremental, BNL join) 13911 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary; Using join buffer (incremental, BNL join) 1392SELECT * FROM t1 1393WHERE t1.a IN (SELECT t2.a 1394FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a); 1395a 13961 13971 1398set optimizer_switch=@tmp_optimzer_switch; 1399DROP TABLE t1,t2,t3; 1400# 1401# BUG#52329 - Wrong result: subquery materialization, IN, 1402# non-null field followed by nullable 1403# 1404CREATE TABLE t1 (a1 CHAR(8) NOT NULL, a2 char(8) NOT NULL); 1405CREATE TABLE t2a (b1 char(8), b2 char(8)); 1406CREATE TABLE t2b (b1 CHAR(8), b2 char(8) NOT NULL); 1407CREATE TABLE t2c (b1 CHAR(8) NOT NULL, b2 char(8)); 1408INSERT INTO t1 VALUES ('1 - 12', '2 - 22'); 1409INSERT INTO t2a VALUES ('1 - 11', '2 - 21'), 1410('1 - 11', '2 - 21'), 1411('1 - 12', '2 - 22'), 1412('1 - 12', '2 - 22'), 1413('1 - 13', '2 - 23'); 1414INSERT INTO t2b SELECT * FROM t2a; 1415INSERT INTO t2c SELECT * FROM t2a; 1416set @tmp_optimzer_switch=@@optimizer_switch; 1417set optimizer_switch='semijoin=off,materialization=on'; 1418SELECT * FROM t1 1419WHERE (a1, a2) IN ( 1420SELECT b1, b2 FROM t2c WHERE b1 > '0' GROUP BY b1, b2); 1421a1 a2 14221 - 12 2 - 22 1423SELECT * FROM t1 1424WHERE (a1, a2) IN ( 1425SELECT b1, b2 FROM t2a WHERE b1 > '0'); 1426a1 a2 14271 - 12 2 - 22 1428SELECT * FROM t1 1429WHERE (a1, a2) IN ( 1430SELECT b1, b2 FROM t2b WHERE b1 > '0'); 1431a1 a2 14321 - 12 2 - 22 1433SELECT * FROM t1 1434WHERE (a1, a2) IN ( 1435SELECT b1, b2 FROM t2c WHERE b1 > '0'); 1436a1 a2 14371 - 12 2 - 22 1438set optimizer_switch=@tmp_optimzer_switch; 1439DROP TABLE t1,t2a,t2b,t2c; 1440# 1441# Bug#57623: subquery within before insert trigger causes crash (sj=on) 1442# 1443CREATE TABLE ot1(a INT); 1444CREATE TABLE ot2(a INT); 1445CREATE TABLE ot3(a INT); 1446CREATE TABLE it1(a INT); 1447INSERT INTO ot1 VALUES(0),(1),(2),(3),(4),(5),(6),(7); 1448INSERT INTO ot2 VALUES(0),(2),(4),(6); 1449INSERT INTO ot3 VALUES(0),(3),(6); 1450INSERT INTO it1 VALUES(0),(1),(2),(3),(4),(5),(6),(7); 1451set @tmp_optimzer_switch=@@optimizer_switch; 1452set optimizer_switch='semijoin=on'; 1453set optimizer_switch='materialization=off'; 1454explain SELECT * 1455FROM ot1 1456LEFT JOIN 1457(ot2 JOIN ot3 on ot2.a=ot3.a) 1458ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1); 1459id select_type table type possible_keys key key_len ref rows Extra 14601 PRIMARY ot1 ALL NULL NULL NULL NULL 8 14611 PRIMARY ot3 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join) 14621 PRIMARY ot2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (incremental, BNL join) 14632 DEPENDENT SUBQUERY it1 ALL NULL NULL NULL NULL 8 Using where 1464SELECT * 1465FROM ot1 1466LEFT JOIN 1467(ot2 JOIN ot3 on ot2.a=ot3.a) 1468ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1); 1469a a a 14700 0 0 14716 6 6 14721 NULL NULL 14732 NULL NULL 14743 NULL NULL 14754 NULL NULL 14765 NULL NULL 14777 NULL NULL 1478prepare s from 'SELECT * 1479FROM ot1 1480LEFT JOIN 1481(ot2 JOIN ot3 on ot2.a=ot3.a) 1482ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1)'; 1483execute s; 1484a a a 14850 0 0 14866 6 6 14871 NULL NULL 14882 NULL NULL 14893 NULL NULL 14904 NULL NULL 14915 NULL NULL 14927 NULL NULL 1493execute s; 1494a a a 14950 0 0 14966 6 6 14971 NULL NULL 14982 NULL NULL 14993 NULL NULL 15004 NULL NULL 15015 NULL NULL 15027 NULL NULL 1503deallocate prepare s; 1504set optimizer_switch='materialization=on'; 1505explain SELECT * 1506FROM ot1 1507LEFT JOIN 1508(ot2 JOIN ot3 on ot2.a=ot3.a) 1509ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1); 1510id select_type table type possible_keys key key_len ref rows Extra 15111 PRIMARY ot1 ALL NULL NULL NULL NULL 8 15121 PRIMARY ot3 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join) 15131 PRIMARY ot2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (incremental, BNL join) 15142 MATERIALIZED it1 ALL NULL NULL NULL NULL 8 1515SELECT * 1516FROM ot1 1517LEFT JOIN 1518(ot2 JOIN ot3 on ot2.a=ot3.a) 1519ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1); 1520a a a 15210 0 0 15226 6 6 15231 NULL NULL 15242 NULL NULL 15253 NULL NULL 15264 NULL NULL 15275 NULL NULL 15287 NULL NULL 1529prepare s from 'SELECT * 1530FROM ot1 1531LEFT JOIN 1532(ot2 JOIN ot3 on ot2.a=ot3.a) 1533ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1)'; 1534execute s; 1535a a a 15360 0 0 15376 6 6 15381 NULL NULL 15392 NULL NULL 15403 NULL NULL 15414 NULL NULL 15425 NULL NULL 15437 NULL NULL 1544execute s; 1545a a a 15460 0 0 15476 6 6 15481 NULL NULL 15492 NULL NULL 15503 NULL NULL 15514 NULL NULL 15525 NULL NULL 15537 NULL NULL 1554deallocate prepare s; 1555set optimizer_switch=@tmp_optimzer_switch; 1556DROP TABLE ot1, ot2, ot3, it1; 1557# 1558# Bug#59919/11766739: Crash in tmp_table_param::init() with semijoin=on 1559# 1560CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM; 1561CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM; 1562CREATE TABLE t3 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM; 1563INSERT INTO t1 VALUES (1); 1564INSERT INTO t2 VALUES (1,1), (2,1); 1565INSERT INTO t3 VALUES 1566(1,1), (2,1), (5,4), (7,3), (8,2), (8,1), (7,3), 1567(9,5), (4,3), (7,2), (7,7), (3,1), (5,8), (9,7); 1568set @tmp_optimzer_switch=@@optimizer_switch; 1569set optimizer_switch='semijoin=off,materialization=on'; 1570EXPLAIN 1571SELECT * FROM t3 1572WHERE f2 IN (SELECT t1.f1 1573FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE); 1574id select_type table type possible_keys key key_len ref rows Extra 15751 PRIMARY t3 ALL NULL NULL NULL NULL 14 Using where 15762 MATERIALIZED t1 system NULL NULL NULL NULL 1 15772 MATERIALIZED b1 ALL NULL NULL NULL NULL 2 Using where 15782 MATERIALIZED b2 ALL NULL NULL NULL NULL 2 1579SELECT * FROM t3 1580WHERE f2 IN (SELECT t1.f1 1581FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE); 1582f1 f2 15831 1 15842 1 15858 1 15863 1 1587set optimizer_switch='semijoin=on,materialization=on'; 1588EXPLAIN 1589SELECT * FROM t3 1590WHERE f2 IN (SELECT t1.f1 1591FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE); 1592id select_type table type possible_keys key key_len ref rows Extra 15931 PRIMARY t1 system NULL NULL NULL NULL 1 15941 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 1 15951 PRIMARY t3 ALL NULL NULL NULL NULL 14 Using where; Using join buffer (flat, BNL join) 15962 MATERIALIZED b1 ALL NULL NULL NULL NULL 2 Using where 15972 MATERIALIZED b2 ALL NULL NULL NULL NULL 2 1598SELECT * FROM t3 1599WHERE f2 IN (SELECT t1.f1 1600FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE); 1601f1 f2 16021 1 16032 1 16048 1 16053 1 1606set optimizer_switch=@tmp_optimzer_switch; 1607DROP TABLE t1, t2, t3 ; 1608# 1609# 1610# BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3 1611# 1612CREATE TABLE t1 ( t1field integer, primary key (t1field)); 1613CREATE TABLE t2 ( t2field integer, primary key (t2field)); 1614INSERT INTO t1 VALUES (1),(2),(3); 1615INSERT INTO t2 VALUES (2),(3),(4); 1616explain 1617SELECT * FROM t1 A 1618WHERE 1619A.t1field IN (SELECT A.t1field FROM t2 B) AND 1620A.t1field IN (SELECT C.t2field FROM t2 C 1621WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); 1622id select_type table type possible_keys key key_len ref rows Extra 16231 PRIMARY A index PRIMARY PRIMARY 4 NULL 3 Using index 16241 PRIMARY B index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(A); Using join buffer (flat, BNL join) 16251 PRIMARY C eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index 16261 PRIMARY D eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index 1627SELECT * FROM t1 A 1628WHERE 1629A.t1field IN (SELECT A.t1field FROM t2 B) AND 1630A.t1field IN (SELECT C.t2field FROM t2 C 1631WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); 1632t1field 16332 16343 1635drop table t1,t2; 1636# 1637# BUG#787299: Valgrind complains on a join query with two IN subqueries 1638# 1639create table t1 (a int); 1640insert into t1 values (1), (2), (3); 1641create table t2 as select * from t1; 1642select * from t1 A, t1 B 1643where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D); 1644a a 16451 1 16462 2 16473 3 1648explain 1649select * from t1 A, t1 B 1650where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D); 1651id select_type table type possible_keys key key_len ref rows Extra 16521 PRIMARY A ALL NULL NULL NULL NULL 3 16531 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 16541 PRIMARY B ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join) 16551 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 16562 MATERIALIZED C ALL NULL NULL NULL NULL 3 16573 MATERIALIZED D ALL NULL NULL NULL NULL 3 1658drop table t1, t2; 1659# 1660# BUG#784441: Abort on semijoin with a view as the inner table 1661# 1662CREATE TABLE t1 (a int) ; 1663INSERT INTO t1 VALUES (1), (1); 1664CREATE TABLE t2 (a int) ; 1665INSERT INTO t2 VALUES (1), (1); 1666CREATE VIEW v1 AS SELECT 1; 1667EXPLAIN 1668SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1); 1669id select_type table type possible_keys key key_len ref rows Extra 16701 PRIMARY <derived3> system NULL NULL NULL NULL 1 16711 PRIMARY t1 ALL NULL NULL NULL NULL 2 16721 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 16733 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used 1674SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1); 1675a a 16761 1 16771 1 16781 1 16791 1 1680DROP VIEW v1; 1681DROP TABLE t1,t2; 1682# 1683# BUG#751439 Assertion `!table->file || table->file->inited == handler::NONE' failed with subquery 1684# 1685CREATE TABLE t1 ( f10 int, f11 int) ; 1686INSERT IGNORE INTO t1 VALUES (0,0),(0,0); 1687CREATE TABLE t2 ( f11 int); 1688INSERT IGNORE INTO t2 VALUES (0),(0); 1689CREATE TABLE t3 ( f11 int) ; 1690INSERT IGNORE INTO t3 VALUES (0); 1691SELECT alias1.f11 AS field2 1692FROM ( t3 AS alias2 JOIN t1 AS alias3 ON alias3.f10 = 1) 1693LEFT JOIN ( t2 AS alias1 ) ON alias3.f11 = 1 1694WHERE alias2.f11 IN ( SELECT f11 FROM t2 ) 1695GROUP BY field2 ; 1696field2 1697drop table t1, t2, t3; 1698# 1699# BUG#778406 Crash in hp_movelink with Aria engine and subqueries 1700# 1701CREATE TABLE t4 (f10 varchar(32) , KEY (f10)) ENGINE=Aria; 1702INSERT INTO t4 VALUES ('x'),('m'),('c'); 1703CREATE TABLE t1 (f11 int) ENGINE=Aria; 1704INSERT INTO t1 VALUES (0),(0),(0); 1705CREATE TABLE t2 ( f10 int) ENGINE=Aria; 1706INSERT INTO t2 VALUES (0),(0),(0); 1707CREATE TABLE t3 ( f10 int, f11 int) ENGINE=Aria; 1708SELECT * 1709FROM t4 1710WHERE f10 IN 1711( SELECT t1.f11 1712FROM t1 1713LEFT JOIN t2 JOIN t3 ON t3.f10 = t2.f10 ON t3.f11 != 0 ); 1714f10 1715x 1716m 1717c 1718Warnings: 1719Warning 1292 Truncated incorrect DOUBLE value: 'x' 1720Warning 1292 Truncated incorrect DOUBLE value: 'm' 1721Warning 1292 Truncated incorrect DOUBLE value: 'c' 1722Warning 1292 Truncated incorrect DOUBLE value: 'x' 1723Warning 1292 Truncated incorrect DOUBLE value: 'm' 1724Warning 1292 Truncated incorrect DOUBLE value: 'c' 1725Warning 1292 Truncated incorrect DOUBLE value: 'x' 1726Warning 1292 Truncated incorrect DOUBLE value: 'm' 1727Warning 1292 Truncated incorrect DOUBLE value: 'c' 1728drop table t1,t2,t3,t4; 1729# 1730# BUG#751484: Valgrind warning / sporadic crash in evaluate_join_record sql_select.cc:14099 with semijoin 1731# 1732CREATE TABLE t1 ( f10 int, f11 int, KEY (f10)); 1733INSERT IGNORE INTO t1 VALUES (0, 0),(0, 0); 1734CREATE TABLE t3 ( f10 int); 1735INSERT IGNORE INTO t3 VALUES (0); 1736set @tmp_751484= @@optimizer_switch; 1737set optimizer_switch='materialization=on'; 1738SELECT * FROM t1 1739WHERE f11 IN ( 1740SELECT C_SQ1_alias1.f11 1741FROM t1 AS C_SQ1_alias1 1742JOIN t3 AS C_SQ1_alias2 1743ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10 1744); 1745f10 f11 17460 0 17470 0 1748set optimizer_switch='materialization=off'; 1749SELECT * FROM t1 1750WHERE f11 IN ( 1751SELECT C_SQ1_alias1.f11 1752FROM t1 AS C_SQ1_alias1 1753JOIN t3 AS C_SQ1_alias2 1754ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10 1755); 1756f10 f11 17570 0 17580 0 1759set optimizer_switch=@tmp_751484; 1760drop table t1, t3; 1761# BUG#795530 Wrong result with subquery semijoin materialization and outer join 1762# Simplified testcase that uses DuplicateElimination 1763# 1764create table t1 (a int); 1765create table t2 (a int, b char(10)); 1766insert into t1 values (1),(2); 1767insert into t2 values (1, 'one'), (3, 'three'); 1768create table t3 (b char(10)); 1769insert into t3 values('three'),( 'four'); 1770insert into t3 values('three'),( 'four'); 1771insert into t3 values('three'),( 'four'); 1772insert into t3 values('three'),( 'four'); 1773explain select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a); 1774id select_type table type possible_keys key key_len ref rows Extra 17751 PRIMARY t3 ALL NULL NULL NULL NULL 8 17761 PRIMARY <subquery2> eq_ref distinct_key distinct_key 10 func 1 17772 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 17782 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 1779select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a); 1780b 1781drop table t1, t2, t3; 1782# 1783# BUG#600958 RQG: Crash in optimize_semijoin_nests 1784# 1785CREATE TABLE t1 ( 1786pk int(11) NOT NULL AUTO_INCREMENT, 1787col_int_key int(11) DEFAULT NULL, 1788col_date_key date DEFAULT NULL, 1789col_varchar_key varchar(1) DEFAULT NULL, 1790PRIMARY KEY (pk), 1791KEY col_int_key (col_int_key), 1792KEY col_date_key (col_date_key), 1793KEY col_varchar_key (col_varchar_key,col_int_key) 1794) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; 1795INSERT INTO t1 VALUES (10,8,'2002-02-21',NULL); 1796CREATE TABLE t2 ( 1797pk int(11) NOT NULL AUTO_INCREMENT, 1798col_int_key int(11) DEFAULT NULL, 1799col_date_key date DEFAULT NULL, 1800col_varchar_key varchar(1) DEFAULT NULL, 1801PRIMARY KEY (pk), 1802KEY col_int_key (col_int_key), 1803KEY col_date_key (col_date_key), 1804KEY col_varchar_key (col_varchar_key,col_int_key) 1805) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 1806INSERT INTO t2 VALUES (1,7,'1900-01-01','f'); 1807SELECT col_date_key FROM t1 1808WHERE 5 IN ( 1809SELECT SUBQUERY3_t1 .col_int_key 1810FROM t2 SUBQUERY3_t1 1811LEFT JOIN t1 SUBQUERY3_t2 ON SUBQUERY3_t1 .col_varchar_key 1812); 1813col_date_key 1814drop table t2, t1; 1815# 1816# No BUG#: Duplicate weedout check is not done for outer joins 1817# 1818create table t1 (a int); 1819create table t2 (a int); 1820insert into t1 values (1),(1),(2),(2); 1821insert into t2 values (1); 1822create table t0 (a int); 1823insert into t0 values (1),(2); 1824set @tmp_20110622= @@optimizer_switch; 1825set optimizer_switch='firstmatch=off,loosescan=off,materialization=off'; 1826# Check DuplicateWeedout + join buffer 1827explain 1828select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1829id select_type table type possible_keys key key_len ref rows Extra 18301 PRIMARY t0 ALL NULL NULL NULL NULL 2 18311 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary; Using join buffer (flat, BNL join) 18321 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary; Using join buffer (incremental, BNL join) 1833select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1834a 18351 18362 1837# Check DuplicateWeedout without join buffer 1838set @tmp_jcl_20110622= @@join_cache_level; 1839set join_cache_level= 0; 1840explain 1841select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1842id select_type table type possible_keys key key_len ref rows Extra 18431 PRIMARY t0 ALL NULL NULL NULL NULL 2 18441 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary 18451 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary 1846select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1847a 18481 18492 1850# Check FirstMatch without join buffer: 1851set optimizer_switch='firstmatch=on'; 1852explain 1853select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1854id select_type table type possible_keys key key_len ref rows Extra 18551 PRIMARY t0 ALL NULL NULL NULL NULL 2 18561 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary 18571 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary 1858select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a); 1859a 18601 18612 1862# 1863# Now, check the same for multiple inner tables: 1864alter table t2 add b int; 1865update t2 set b=a; 1866create table t3 as select * from t2; 1867set optimizer_switch='firstmatch=off'; 1868set join_cache_level= 0; 1869# DuplicateWeedout without join buffer 1870explain 1871select * from t0 1872where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1873id select_type table type possible_keys key key_len ref rows Extra 18741 PRIMARY t0 ALL NULL NULL NULL NULL 2 18751 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary 18761 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where 18771 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary 1878select * from t0 1879where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1880a 18811 18822 1883set @@join_cache_level=@tmp_jcl_20110622; 1884# DuplicateWeedout + join buffer 1885explain 1886select * from t0 1887where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1888id select_type table type possible_keys key key_len ref rows Extra 18891 PRIMARY t0 ALL NULL NULL NULL NULL 2 18901 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary; Using join buffer (flat, BNL join) 18911 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where; Using join buffer (incremental, BNL join) 18921 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary; Using join buffer (incremental, BNL join) 1893select * from t0 1894where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1895a 18961 18972 1898# Now, let the inner join side have a 'partial' match 1899select * from t3; 1900a b 19011 1 1902insert into t3 values(2,2); 1903explain 1904select * from t0 1905where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1906id select_type table type possible_keys key key_len ref rows Extra 19071 PRIMARY t0 ALL NULL NULL NULL NULL 2 19081 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary; Using join buffer (flat, BNL join) 19091 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using join buffer (incremental, BNL join) 19101 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary; Using join buffer (incremental, BNL join) 1911select * from t0 1912where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a); 1913a 19141 19152 1916set @@optimizer_switch=@tmp_20110622; 1917drop table t0, t1, t2, t3; 1918# 1919# BUG#802965: Crash in do_copy_not_null with semijoin=on in maria-5.3 1920# 1921set @save_802965= @@optimizer_switch; 1922set optimizer_switch='semijoin=on,materialization=off,firstmatch=off,loosescan=off'; 1923CREATE TABLE t2 ( f1 int NOT NULL , PRIMARY KEY (f1)) ; 1924INSERT IGNORE INTO t2 VALUES (19),(20); 1925CREATE TABLE t1 ( f1 int NOT NULL , PRIMARY KEY (f1)) ; 1926INSERT IGNORE INTO t1 VALUES (21),(22),(23),(24); 1927SELECT * 1928FROM t2 , t1 1929WHERE t2.f1 IN 1930( 1931SELECT SQ1_alias1.f1 1932FROM t1 AS SQ1_alias1 LEFT JOIN t2 AS SQ1_alias2 JOIN t2 AS SQ1_alias3 ON SQ1_alias3.f1 ON SQ1_alias3.f1 1933) 1934AND t1.f1 = t2.f1 ; 1935f1 f1 1936DROP TABLE t1, t2; 1937set optimizer_switch=@save_802965; 1938# 1939# BUG#803365: Crash in pull_out_semijoin_tables with outer join + semijoin + derived tables in maria-5.3 with WL#106 1940# 1941CREATE TABLE t1 ( f1 int) ; 1942INSERT INTO t1 VALUES (1),(1); 1943CREATE TABLE t2 ( f2 int) ; 1944INSERT INTO t2 VALUES (1),(1); 1945CREATE TABLE t3 ( f3 int) ; 1946INSERT INTO t3 VALUES (1),(1); 1947SELECT * 1948FROM t1 1949WHERE t1.f1 IN ( 1950SELECT t2.f2 1951FROM t2 1952LEFT JOIN ( 1953SELECT * 1954FROM t3 1955) AS alias1 1956ON alias1.f3 = t2.f2 1957); 1958f1 19591 19601 1961DROP TABLE t1,t2,t3; 1962# 1963# BUG#611704: Crash in replace_where_subcondition with nested subquery and semijoin=on 1964# 1965CREATE TABLE t1 ( f1 int) ; 1966CREATE TABLE t2 ( f1 int) ; 1967CREATE TABLE t3 ( f1 int) ; 1968SELECT * FROM ( 1969SELECT t3.* 1970FROM t2 STRAIGHT_JOIN t3 1971ON t3.f1 1972AND (t3.f1 ) IN ( 1973SELECT t1.f1 1974FROM t1 1975) 1976) AS alias1; 1977f1 1978DROP TABLE t1,t2,t3; 1979# BUG#611704: another testcase: 1980CREATE TABLE t1 ( f1 int(11), f3 varchar(1), f4 varchar(1)) ; 1981CREATE TABLE t2 ( f2 int(11), KEY (f2)); 1982CREATE TABLE t3 ( f4 varchar(1)) ; 1983PREPARE st1 FROM ' 1984SELECT * 1985FROM t1 1986STRAIGHT_JOIN ( t2 STRAIGHT_JOIN t3 ON t2.f2 ) 1987ON (t1.f3) IN ( SELECT f4 FROM t1 ) 1988'; 1989EXECUTE st1; 1990f1 f3 f4 f2 f4 1991DROP TABLE t1,t2,t3; 1992# 1993# BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90 1994# (Original testcase) 1995# 1996CREATE TABLE t1 (f1 int, f2 int ); 1997INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL); 1998CREATE TABLE t2 (f2 int, f3 int ); 1999INSERT INTO t2 VALUES (NULL,NULL),(0,0); 2000CREATE TABLE t3 ( f1 int, f3 int ); 2001INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0); 2002CREATE TABLE t4 ( f2 int, KEY (f2) ); 2003INSERT INTO t4 VALUES (0),(NULL); 2004CREATE VIEW v4 AS SELECT DISTINCT f2 FROM t4 ; 2005# The following must not have outer joins: 2006explain extended 2007SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4); 2008id select_type table type possible_keys key key_len ref rows filtered Extra 20091 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 20101 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 20111 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) 20121 PRIMARY t3 ALL NULL NULL NULL NULL 5 100.00 Using where; Using join buffer (incremental, BNL join) 20132 MATERIALIZED t4 index f2 f2 5 NULL 2 100.00 Using index 2014Warnings: 2015Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t3`.`f3` AS `f3` from `test`.`t1` semi join (`test`.`t4`) join `test`.`t2` join `test`.`t3` where `test`.`t3`.`f1` = `test`.`t1`.`f1` and `test`.`t1`.`f2` = `test`.`t2`.`f2` 2016SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4); 2017f1 f2 f3 f3 20182 0 0 0 20194 0 0 0 20204 0 0 0 2021drop view v4; 2022drop table t1, t2, t3, t4; 2023# 2024# BUG#803303: Wrong result with semijoin=on, outer join in maria-5.3-subqueries-mwl90 2025# 2026# Testcase#1: 2027set @tmp803303= @@optimizer_switch; 2028set optimizer_switch = 'semijoin=on,materialization=off,firstmatch=off,loosescan=off'; 2029CREATE TABLE t2 ( f1 int) ; 2030INSERT IGNORE INTO t2 VALUES (6),(8); 2031CREATE TABLE t1 ( f1 int, f2 int, f3 int) ; 2032INSERT IGNORE INTO t1 VALUES (8,0,0),(7,0,0),(9,0,0); 2033SELECT alias2.f1 2034FROM t2 AS alias1 2035LEFT JOIN ( t1 AS alias2 JOIN t1 AS alias3 ON alias3.f2 = alias2.f3 ) 2036ON alias3.f2 = alias2.f2 2037WHERE alias2.f1 IN ( SELECT f1 FROM t2 AS alias4 ) ; 2038f1 20398 20408 20418 20428 20438 20448 2045drop table t1,t2; 2046set optimizer_switch= @tmp803303; 2047# Testcase #2: 2048CREATE TABLE t1 ( f10 int) ; 2049INSERT INTO t1 VALUES (0),(0); 2050CREATE TABLE t2 ( f10 int, f11 varchar(1)) ; 2051INSERT INTO t2 VALUES (0,'a'),(0,'b'); 2052CREATE TABLE t3 ( f10 int) ; 2053INSERT INTO t3 VALUES (0),(0),(0),(0),(0); 2054CREATE TABLE t4 ( f10 varchar(1), f11 int) ; 2055INSERT INTO t4 VALUES ('a',0),('b',0); 2056SELECT * FROM t1 2057LEFT JOIN ( t2 JOIN t3 ON t3.f10 = t2.f10 ) ON t1.f10 = t2.f10 2058WHERE t2.f10 IN ( 2059SELECT t4.f11 2060FROM t4 2061WHERE t4.f10 != t2.f11 2062); 2063f10 f10 f11 f10 20640 0 b 0 20650 0 b 0 20660 0 a 0 20670 0 a 0 20680 0 b 0 20690 0 b 0 20700 0 a 0 20710 0 a 0 20720 0 b 0 20730 0 b 0 20740 0 a 0 20750 0 a 0 20760 0 b 0 20770 0 b 0 20780 0 a 0 20790 0 a 0 20800 0 b 0 20810 0 b 0 20820 0 a 0 20830 0 a 0 2084drop table t1,t2,t3,t4; 2085# 2086# BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90 2087# 2088set @tmp803457=@@optimizer_switch; 2089set optimizer_switch='materialization=off'; 2090CREATE TABLE t1 (f1 int, f2 int ); 2091INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL); 2092CREATE TABLE t2 (f2 int, f3 int ); 2093INSERT INTO t2 VALUES (NULL,NULL),(0,0); 2094CREATE TABLE t3 ( f1 int, f3 int ); 2095INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0); 2096CREATE TABLE t4 ( f2 int); 2097INSERT INTO t4 VALUES (0),(NULL); 2098# The following uses Duplicate Weedout, and "End temporary" must not be 2099# in the middle of the inner side of an outer join: 2100explain 2101SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4); 2102id select_type table type possible_keys key key_len ref rows Extra 21031 PRIMARY t1 ALL NULL NULL NULL NULL 3 21041 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 21051 PRIMARY t3 ALL NULL NULL NULL NULL 5 Using where; Using join buffer (incremental, BNL join) 21061 PRIMARY t4 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary; Using join buffer (incremental, BNL join) 2107SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3 ) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4); 2108f1 f2 f3 f3 21092 0 0 0 21104 0 0 0 21114 0 0 0 21120 NULL NULL NULL 2113Warnings: 2114Warning 1292 Truncated incorrect DOUBLE value: 'foo' 2115Warning 1292 Truncated incorrect DOUBLE value: 'foo' 2116DROP TABLE t1, t2, t3, t4; 2117set @tmp803457=@@optimizer_switch; 2118# 2119# BUG#818280: crash in do_copy_not_null() in maria-5.3 with semijoin 2120# 2121CREATE TABLE t1 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ; 2122INSERT IGNORE INTO t1 VALUES (2,7),(1,3),(5,6); 2123CREATE TABLE t3 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ; 2124INSERT IGNORE INTO t3 VALUES (2,7),(1,3),(5,6); 2125CREATE TABLE t2 ( c1 int NOT NULL , c5 int NOT NULL ); 2126INSERT IGNORE INTO t2 VALUES (2,2),(2,2),(5,6); 2127SELECT * FROM t1 WHERE c1 IN ( SELECT t3.c1 FROM t3 LEFT JOIN t2 ON t2 .c1 = t3 .c1 WHERE t2.c5 != 0 ); 2128c1 c2 21292 7 21305 6 2131DROP TABLE t1, t2, t3; 2132# 2133# BUG#834534: Assertion `0' failed in replace_where_subcondition with semijoin subquery in HAVING 2134# 2135CREATE TABLE t1 ( d int ); 2136INSERT INTO t1 VALUES (2),(2),(0),(2),(2); 2137CREATE TABLE t2 ( b int ); 2138INSERT INTO t2 VALUES (4),(3),(3); 2139CREATE TABLE t3 ( a int ); 2140SELECT * 2141FROM t3 2142WHERE (t3.a) IN ( 2143SELECT t1.d 2144FROM t1 2145HAVING ( 4 ) IN ( 2146SELECT t2.b 2147FROM t2 2148) 2149); 2150a 2151drop table t1, t2,t3; 2152# 2153# BUG#834758: Wrong result with innner join, LooseScan, two-column IN() predicate 2154# 2155set @tmp835758=@@optimizer_switch; 2156set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off'; 2157CREATE TABLE t1 (b int) ; 2158INSERT INTO t1 VALUES (1),(5); 2159CREATE TABLE t2 (a int, PRIMARY KEY (a)) ; 2160INSERT INTO t2 VALUES (6),(10); 2161CREATE TABLE t3 (a int, b int, KEY (b)) ; 2162INSERT INTO t3 VALUES (6,5),(6,2),(8,0),(9,1),(6,5); 2163# This used to incorrectly pick a join order of (t1, LooseScan(t3), t2): 2164explain 2165SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3); 2166id select_type table type possible_keys key key_len ref rows Extra 21671 PRIMARY t1 ALL NULL NULL NULL NULL 2 21681 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 2 Using index; Using join buffer (flat, BNL join) 21691 PRIMARY t3 ALL b NULL NULL NULL 5 Using where; Start temporary; End temporary; Using join buffer (incremental, BNL join) 2170SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3); 2171b a 21725 6 2173DROP TABLE t1, t2, t3; 2174set @@optimizer_switch= @tmp835758; 2175# 2176# BUG#834739: Wrong result with 3-way inner join, LooseScan,multipart keys 2177# 2178set @tmp834739=@@optimizer_switch; 2179set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off'; 2180CREATE TABLE t2 ( b int, c int, KEY (b)) ; 2181INSERT INTO t2 VALUES (1,0),(1,0),(9,0),(1,0),(5,0); 2182INSERT INTO t2 VALUES (2,0),(3,0),(8,0),(6,0),(5,0); 2183CREATE TABLE t3 ( a int); 2184INSERT INTO t3 VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0); 2185CREATE TABLE t4 ( a int); 2186INSERT INTO t4 VALUES (0),(0),(0); 2187CREATE TABLE t5 ( b int, a int , KEY (a,b)) ; 2188INSERT INTO t5 VALUES (7,0),(9,0); 2189explain 2190SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b); 2191id select_type table type possible_keys key key_len ref rows Extra 21921 PRIMARY t5 index a a 10 NULL 2 Using index; Start temporary 21931 PRIMARY t4 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join) 21941 PRIMARY t2 ALL b NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join) 21951 PRIMARY t3 ALL NULL NULL NULL NULL 15 Using where; End temporary; Using join buffer (incremental, BNL join) 2196SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b); 2197a 21980 21990 22000 22010 22020 22030 22040 22050 22060 22070 22080 22090 22100 22110 22120 2213DROP TABLE t2, t3, t4, t5; 2214set @@optimizer_switch=@tmp834739; 2215# 2216# BUG#830993: Crash in end_read_record with derived table 2217# 2218set @tmp_830993=@@optimizer_switch; 2219set optimizer_switch='semijoin=on,loosescan=off,materialization=off,firstmatch=off'; 2220set @tmp_830993_jbs= @@join_buffer_size; 2221set join_buffer_size=160; 2222CREATE TABLE t1 ( 2223a int(11) NOT NULL AUTO_INCREMENT, 2224b int(11) DEFAULT NULL, 2225c int(11) DEFAULT NULL, 2226d time DEFAULT NULL, 2227e varchar(1) DEFAULT NULL, 2228f varchar(1) DEFAULT NULL, 2229PRIMARY KEY (a), 2230KEY c (c), 2231KEY d (d), 2232KEY e (e,c) 2233); 2234INSERT INTO t1 VALUES (10,NULL,8,'22:55:23','x','x'), 2235(11,8,7,'10:19:31','d','d'),(12,1,1,'14:40:36','r','r'), 2236(13,9,7,'04:37:47','f','f'),(14,4,9,'19:34:06','y','y'), 2237(15,3,NULL,'20:35:33','u','u'),(16,2,1,NULL,'m','m'), 2238(17,NULL,9,'14:43:37',NULL,NULL),(18,2,2,'02:23:09','o','o'), 2239(19,NULL,9,'01:22:45','w','w'),(20,6,2,'00:00:00','m','m'), 2240(21,7,4,'00:13:25','q','q'),(22,2,0,'03:47:16',NULL,NULL), 2241(23,5,4,'01:41:48','d','d'),(24,7,8,'00:00:00','g','g'), 2242(25,6,NULL,'22:32:04','x','x'),(26,6,NULL,'16:44:14','f','f'), 2243(27,2,0,'17:38:37','p','p'),(28,9,NULL,'08:46:48','j','j'), 2244(29,6,8,'14:11:27','c','c'); 2245CREATE TABLE t2 like t1; 2246INSERT INTO t2 VALUES (1,2,4,'22:34:09','v','v'), 2247(2,150,62,'14:26:02','v','v'),(3,NULL,7,'14:03:03','c','c'), 2248(4,2,1,'01:46:09',NULL,NULL),(5,5,0,'16:21:18','x','x'), 2249(6,3,7,'18:56:33','i','i'),(7,1,7,NULL,'e','e'), 2250(8,4,1,'09:29:08','p','p'),(9,NULL,7,'19:11:10','s','s'), 2251(10,2,1,'11:57:26','j','j'),(11,6,5,'00:39:46','z','z'), 2252(12,6,2,'03:28:15','c','c'),(13,8,0,'06:44:18','a','a'), 2253(14,2,1,'14:36:39','q','q'),(15,6,8,'18:42:45','y','y'), 2254(16,8,1,'02:57:29',NULL,NULL),(17,3,1,'16:46:13','r','r'), 2255(18,3,9,'19:39:02','v','v'),(19,9,1,NULL,NULL,NULL), 2256(20,6,5,'20:58:33','r','r'); 2257set @tmp_optimizer_switch=@@optimizer_switch; 2258set optimizer_switch='derived_merge=off,derived_with_keys=off'; 2259explain 2260SELECT 2261alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f, 2262alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d, 2263alias2.e as a2_e, alias2.f as a2_f, 2264t2.a as t2_a, t2.b as t2_b, t2.c as t2_c, t2.d as t2_d, t2.e as t2_e, t2.f as t2_f 2265FROM 2266(SELECT * FROM t2) AS alias1, 2267t1 AS alias2, 2268t2 2269WHERE 2270alias1.c IN (SELECT SQ3_alias1.b 2271FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2) 2272LIMIT 100; 2273id select_type table type possible_keys key key_len ref rows Extra 22741 PRIMARY SQ3_alias1 ALL NULL NULL NULL NULL 20 Start temporary 22751 PRIMARY <derived2> ALL NULL NULL NULL NULL 20 Using where; Using join buffer (flat, BNL join) 22761 PRIMARY SQ3_alias2 index NULL PRIMARY 4 NULL 20 Using index; End temporary; Using join buffer (incremental, BNL join) 22771 PRIMARY alias2 ALL NULL NULL NULL NULL 20 Using join buffer (incremental, BNL join) 22781 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using join buffer (incremental, BNL join) 22792 DERIVED t2 ALL NULL NULL NULL NULL 20 2280create table t3 as 2281SELECT 2282alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f, 2283alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d, 2284alias2.e as a2_e, alias2.f as a2_f, 2285t2.a as t2_a, t2.b as t2_b, t2.c as t2_c, t2.d as t2_d, t2.e as t2_e, t2.f as t2_f 2286FROM 2287(SELECT * FROM t2) AS alias1, 2288t1 AS alias2, 2289t2 2290WHERE 2291alias1.c IN (SELECT SQ3_alias1.b 2292FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2) 2293LIMIT 100; 2294set optimizer_switch=@tmp_optimizer_switch; 2295drop table t1,t2, t3; 2296set optimizer_switch=@tmp_830993; 2297set join_buffer_size= @tmp_830993_jbs; 2298# 2299# BUG##849717: Crash in Item_func::fix_fields on second execution of a prepared statement with semijoin 2300# 2301CREATE TABLE t1 (a int); 2302CREATE TABLE t2 (a int); 2303CREATE TABLE t3 (a int, b int) ; 2304PREPARE st1 FROM "SELECT * FROM t2 LEFT JOIN t1 ON t2.a != 0 AND ('j','r') IN ( SELECT b,a FROM t3)"; 2305EXECUTE st1; 2306a a 2307Warnings: 2308Warning 1292 Truncated incorrect DOUBLE value: 'j' 2309Warning 1292 Truncated incorrect DOUBLE value: 'r' 2310EXECUTE st1; 2311a a 2312Warnings: 2313Warning 1292 Truncated incorrect DOUBLE value: 'j' 2314Warning 1292 Truncated incorrect DOUBLE value: 'r' 2315DROP TABLE t1, t2, t3; 2316# 2317# BUG#849776: Wrong result with semijoin + "Impossible where" 2318# 2319CREATE TABLE t1 ( b varchar(1), a integer) ; 2320INSERT INTO t1 VALUES ('z',8); 2321CREATE TABLE t2 ( a integer, b varchar(1)) ; 2322CREATE TABLE t4 ( a integer, b varchar(1)) ; 2323CREATE TABLE t5 ( a integer) ; 2324INSERT INTO t5 VALUES (8); 2325select * from t5 where (a) in ( 2326SELECT t1.a 2327FROM t1 LEFT JOIN t2 ON t1.a = t2.a 2328WHERE t2.b NOT IN (SELECT t4.b FROM t4 WHERE t4.b < t1.b) 2329); 2330a 23318 2332DROP TABLE t1, t2, t4, t5; 2333# 2334# BUG#861147: Assertion `fixed == 1' failed in Item_func_eq::val_int() with semijoin + materialization + max_join_size 2335# 2336create table t1 (a int); 2337insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 2338set @tmp_mjs_861147= @@max_join_size; 2339SET max_join_size=10; 2340set @tmp_os_861147= @@optimizer_switch; 2341set @@optimizer_switch='semijoin=on,materialization=on'; 2342explain 2343select * from t1 where a in (select max(A.a + B.a + C.a) from t1 A, t1 B, t1 C); 2344ERROR 42000: The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay 2345set max_join_size= @tmp_mjs_861147; 2346set optimizer_switch= @tmp_os_861147; 2347drop table t1; 2348# 2349# BUG#877288: Wrong result with semijoin + materialization + multipart key 2350# 2351set @tmp_877288=@@optimizer_switch; 2352set optimizer_switch='semijoin=ON,materialization=ON'; 2353CREATE TABLE t1 ( a int) ; 2354INSERT INTO t1 VALUES (19),(19),(19),(20),(20),(20),(20),(20),(20); 2355CREATE TABLE t2 ( b int NOT NULL , c int NOT NULL , KEY (b,c)) ; 2356INSERT INTO t2 VALUES (14,1),(15,1),(16,1),(17,1),(18,1),(19,1),(20,1); 2357CREATE TABLE t3 ( a int, d int) ; 2358INSERT INTO t3 VALUES (19,1),(7,1),(3,1),(3,1),(20,1),(3,1),(16,1),(17,1),(9,1),(4,1),(6,1),(15,1),(17,1); 2359explain 2360SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a); 2361id select_type table type possible_keys key key_len ref rows Extra 23621 PRIMARY t1 ALL NULL NULL NULL NULL 9 23631 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 23642 MATERIALIZED t3 ALL NULL NULL NULL NULL 13 Using where 23652 MATERIALIZED t2 ref b b 4 test.t3.a 1 Using index 2366SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a); 2367a 236819 236919 237019 237120 237220 237320 237420 237520 237620 2377DROP TABLE t1,t2,t3; 2378set optimizer_switch=@tmp_877288; 2379# 2380# BUG#878753: Assertion '0' failed in replace_where_subcondition with derived_merge 2381# 2382set @tmp878753= @@optimizer_switch; 2383set optimizer_switch= 'semijoin=on,derived_merge=on'; 2384CREATE TABLE t1 (b int(11)) ; 2385CREATE TABLE t2 (c int, b int, d varchar(52) NOT NULL) ; 2386CREATE TABLE t3 (b int(11)) ; 2387PREPARE st1 FROM ' 2388 SELECT * FROM t1 2389 JOIN ( 2390 SELECT t2.* FROM t2 2391 WHERE t2.d <> "a" 2392 AND t2.c IN ( 2393 SELECT t3.b 2394 FROM t3 2395 ) 2396 ) AS alias2 2397 ON ( alias2.b = t1.b ); 2398'; 2399EXECUTE st1; 2400b c b d 2401DROP TABLE t1,t2,t3; 2402set optimizer_switch=@tmp878753; 2403# 2404# Bug #889750: semijoin=on + firstmatch=off + semijoin_with_cache=off 2405# 2406create table t1 (a int); 2407insert into t1 values (7), (1), (5), (3); 2408create table t2 (a int); 2409insert into t2 values (4), (1), (8), (3), (9), (2); 2410set @tmp_otimizer_switch= @@optimizer_switch; 2411set optimizer_switch='semijoin=on'; 2412set optimizer_switch='firstmatch=off'; 2413set optimizer_switch='semijoin_with_cache=on'; 2414explain 2415select * from t1 where t1.a in (select t2.a from t2); 2416id select_type table type possible_keys key key_len ref rows Extra 24171 PRIMARY t1 ALL NULL NULL NULL NULL 4 24181 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join) 2419select * from t1 where t1.a in (select t2.a from t2); 2420a 24211 24223 2423set optimizer_switch='semijoin_with_cache=off'; 2424explain 2425select * from t1 where t1.a in (select t2.a from t2); 2426id select_type table type possible_keys key key_len ref rows Extra 24271 PRIMARY t1 ALL NULL NULL NULL NULL 4 24281 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary 2429select * from t1 where t1.a in (select t2.a from t2); 2430a 24311 24323 2433set optimizer_switch= @tmp_otimizer_switch; 2434drop table t1,t2; 2435# 2436# Bug #887496: semijoin with IN equality for the second part of an index 2437# 2438CREATE TABLE t1 (a int); 2439INSERT INTO t1 VALUES (9), (0), (8), (5); 2440CREATE TABLE t2 (a int, b varchar(1), INDEX idx (b,a)); 2441INSERT INTO t2 VALUES (5,'r'), (5,'z'); 2442CREATE TABLE t3 (a int, b varchar(1), INDEX idx (b,a)); 2443INSERT INTO t3 VALUES (5,'r'), (5,'z'); 2444set @tmp_otimizer_switch= @@optimizer_switch; 2445SET SESSION optimizer_switch='semijoin=on,firstmatch=on'; 2446SET SESSION optimizer_switch='loosescan=off'; 2447EXPLAIN 2448SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b); 2449id select_type table type possible_keys key key_len ref rows Extra 24501 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary 24511 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index 24521 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join) 2453SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b); 2454a 24555 2456SET SESSION optimizer_switch='loosescan=on'; 2457EXPLAIN 2458SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b); 2459id select_type table type possible_keys key key_len ref rows Extra 24601 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary 24611 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index 24621 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join) 2463SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b); 2464a 24655 2466set optimizer_switch= @tmp_otimizer_switch; 2467DROP TABLE t1,t2,t3; 2468# 2469# Bug #901312: materialized semijoin + right join 2470# 2471CREATE TABLE t1 (a int); 2472INSERT INTO t1 VALUES (4), (1); 2473CREATE TABLE t2 (b int); 2474INSERT INTO t2 VALUES (4), (1); 2475CREATE TABLE t3 (c int); 2476INSERT INTO t3 VALUES (4), (1); 2477set @tmp_otimizer_switch= @@optimizer_switch; 2478SET SESSION optimizer_switch='semijoin=on,materialization=on'; 2479EXPLAIN 2480SELECT * FROM t1 RIGHT JOIN t2 ON b = a WHERE t2.b IN (SELECT c FROM t3); 2481id select_type table type possible_keys key key_len ref rows Extra 24821 PRIMARY t2 ALL NULL NULL NULL NULL 2 24831 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 24841 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 24852 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 2486SELECT * FROM t1 RIGHT JOIN t2 ON b = a WHERE t2.b IN (SELECT c FROM t3); 2487a b 24884 4 24891 1 2490set optimizer_switch= @tmp_otimizer_switch; 2491DROP TABLE t1,t2,t3; 2492# 2493# Bug #901709: assertion failure with record count == 0 2494# 2495CREATE TABLE t1 (a int, KEY (a)); 2496INSERT INTO t1 VALUES (4), (6); 2497CREATE TABLE t2 (a int, KEY (a)); 2498INSERT INTO t2 VALUES (4), (6); 2499CREATE TABLE t3 (b int); 2500INSERT INTO t3 VALUES (4); 2501CREATE TABLE t4 (c int); 2502SET @tmp_optimizer_switch=@@optimizer_switch; 2503SET @@optimizer_switch='semijoin=on'; 2504SET @@optimizer_switch='materialization=on'; 2505SET @@optimizer_switch='firstmatch=on'; 2506SET optimizer_switch='semijoin_with_cache=on'; 2507SET optimizer_prune_level=0; 2508EXPLAIN 2509SELECT * FROM t1, t2 2510WHERE t1.a = t2.a AND t2.a IN (SELECT b FROM t3 STRAIGHT_JOIN t4); 2511id select_type table type possible_keys key key_len ref rows Extra 25121 PRIMARY t3 system NULL NULL NULL NULL 1 25131 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 1 25141 PRIMARY t2 ref a a 5 const 1 Using index 25151 PRIMARY t1 ref a a 5 func 1 Using index 25162 MATERIALIZED t4 ALL NULL NULL NULL NULL 0 2517SELECT * FROM t1, t2 2518WHERE t1.a = t2.a AND t2.a IN (SELECT b FROM t3 STRAIGHT_JOIN t4); 2519a a 2520SET optimizer_prune_level=DEFAULT; 2521SET optimizer_switch=@tmp_optimizer_switch; 2522DROP TABLE t1,t2,t3,t4; 2523# 2524# BUG#901399: Wrong result (extra row) with semijoin=ON, materialization=OFF, optimizer_prune_level=0 2525# 2526set @opl_901399= @@optimizer_prune_level; 2527set @os_091399= @@optimizer_switch; 2528SET optimizer_prune_level=0; 2529SET optimizer_switch = 'materialization=off'; 2530CREATE TABLE t1 ( c INT ) ENGINE=MyISAM; 2531INSERT INTO t1 VALUES 2532(0),(1),(2),(3),(4),(5), 2533(6),(7),(8),(9),(10),(11),(12); 2534CREATE TABLE t2 ( a INT, b INT, KEY(a)) ENGINE=MyISAM; 2535INSERT INTO t2 VALUES (3,20),(2,21),(3,22); 2536SELECT * 2537FROM t1 AS alias1, t1 AS alias2 2538WHERE ( alias1.c, alias2.c ) 2539IN ( 2540SELECT alias3.a, alias3.a 2541FROM t2 AS alias3, t2 alias4 2542WHERE alias3.b = alias4.b 2543); 2544c c 25452 2 25463 3 2547set optimizer_prune_level= @opl_901399; 2548set optimizer_switch= @os_091399; 2549DROP TABLE t1,t2; 2550# 2551# BUG#912510: Crash in do_copy_not_null with semijoin=ON, firstmatch=ON, aggregate ... 2552# 2553CREATE TABLE t1 ( a VARCHAR(1) NOT NULL ); 2554INSERT INTO t1 VALUES ('k'),('l'); 2555CREATE TABLE t2 ( b VARCHAR(1) NOT NULL, KEY(b) ); 2556INSERT INTO t2 VALUES ('k'),('l'); 2557CREATE TABLE t3 ( c VARCHAR(1) NOT NULL, KEY(c) ); 2558INSERT INTO t3 VALUES ('m'),('n'); 2559SELECT a, COUNT(*) FROM t1 2560WHERE a IN ( 2561SELECT b FROM t2 force index(b), t3 force index(c) 2562WHERE c = b AND b = a 2563); 2564a COUNT(*) 2565NULL 0 2566DROP TABLE t1, t2, t3; 2567# 2568# BUG#920255: Wrong result (extra rows) with loosescan and IN subquery 2569# 2570CREATE TABLE t1 ( a INT PRIMARY KEY, b INT, KEY(b) ); 2571INSERT INTO t1 VALUES 2572(1,2),(2,1),(3,3),(4,2),(5,5), 2573(6,3),(7,1),(8,4),(9,3),(10,2); 2574CREATE TABLE t2 ( c INT, d INT, KEY(c) ); 2575INSERT INTO t2 VALUES 2576(1,2),(2,1),(3,3),(4,2),(5,5),(6,3),(7,1); 2577analyze table t1,t2; 2578Table Op Msg_type Msg_text 2579test.t1 analyze status Engine-independent statistics collected 2580test.t1 analyze status OK 2581test.t2 analyze status Engine-independent statistics collected 2582test.t2 analyze status OK 2583explain 2584SELECT a, b, d FROM t1, t2 2585WHERE ( b, d ) IN 2586( SELECT b, d FROM t1, t2 WHERE b = c ); 2587id select_type table type possible_keys key key_len ref rows Extra 25881 PRIMARY t2 ALL NULL NULL NULL NULL 7 25891 PRIMARY t1 index b b 5 NULL 10 Using where; Using index; LooseScan 25901 PRIMARY t2 ref c c 5 test.t1.b 1 Using where; FirstMatch(t1) 25911 PRIMARY t1 ref b b 5 test.t1.b 2 Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 2592SELECT a, b, d FROM t1, t2 2593WHERE ( b, d ) IN 2594( SELECT b, d FROM t1, t2 WHERE b = c ); 2595a b d 25961 2 1 25971 2 1 25982 1 2 25992 1 2 26003 3 3 26013 3 3 26024 2 1 26034 2 1 26045 5 5 26056 3 3 26066 3 3 26077 1 2 26087 1 2 26098 4 2 26108 4 2 26119 3 3 26129 3 3 261310 2 1 261410 2 1 2615DROP TABLE t1, t2; 2616# Another testcase for the above that still uses LooseScan: 2617create table t0(a int primary key); 2618insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 2619create table t10(a int primary key); 2620insert into t10 select A.a + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C; 2621create table t1 ( 2622pk int primary key auto_increment, 2623kp1 int, 2624kp2 int, 2625filler char(100), 2626key (kp1, kp2) 2627); 2628insert into t1 (kp1, kp2, filler) 2629select 2630A.a, B.a, 'filler-data' 2631from t0 A, t0 B; 2632create table t2 (a int, filler char(100), key(a)); 2633create table t3 (a int); 2634insert into t3 values (1),(2); 2635insert into t2 2636select (A.a+1000*B.a)/20, 'filler_data' from t10 A, t0 B; 2637analyze table t1,t2,t3; 2638Table Op Msg_type Msg_text 2639test.t1 analyze status Engine-independent statistics collected 2640test.t1 analyze status Table is already up to date 2641test.t2 analyze status Engine-independent statistics collected 2642test.t2 analyze status Table is already up to date 2643test.t3 analyze status Engine-independent statistics collected 2644test.t3 analyze status OK 2645delete from t1 where kp2 in (1,3); 2646# Ref + LooseScan on t1: 2647explain select sum(t2.a) 2648from t2,t3 2649where (t3.a,t2.a) in (select kp1,kp2 from t1,t0 where t0.a=2); 2650id select_type table type possible_keys key key_len ref rows Extra 26511 PRIMARY t0 const PRIMARY PRIMARY 4 const 1 Using index 26521 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where 26531 PRIMARY t1 ref kp1 kp1 5 test.t3.a 10 Using where; Using index; LooseScan 26541 PRIMARY t2 ref a a 5 test.t1.kp2 19 Using index 2655select sum(t2.a) 2656from t2,t3 2657where (t3.a,t2.a) in (select kp1,kp2 from t1,t0 where t0.a=2); 2658sum(t2.a) 26591640 2660drop table t0,t10; 2661drop table t1,t2,t3; 2662# 2663# BUG#920713: Wrong result (missing rows) with firstmatch+BNL, IN subquery, ... 2664# 2665CREATE TABLE t1 ( a VARCHAR(1) ) ENGINE=MyISAM; 2666INSERT INTO t1 VALUES ('e'),('w'),('a'),('h'),('x'),('k'),('g'); 2667CREATE TABLE t2 ( b INT, c VARCHAR(1) ); 2668INSERT INTO t2 VALUES (0,'j'),(8,'v'); 2669SELECT * FROM t1 alias1, t2 alias2 2670WHERE alias2.c IN ( 2671SELECT alias4.c FROM t1 alias3, t2 alias4 2672); 2673a b c 2674e 0 j 2675e 8 v 2676w 0 j 2677w 8 v 2678a 0 j 2679a 8 v 2680h 0 j 2681h 8 v 2682x 0 j 2683x 8 v 2684k 0 j 2685k 8 v 2686g 0 j 2687g 8 v 2688DROP TABLE t1, t2; 2689# 2690# BUG#923246: Loosescan reports different result than other semijoin methods 2691# 2692set @tmp_923246= @@optimizer_switch; 2693set optimizer_switch='mrr=on,materialization=off'; 2694create table t0 (a int); 2695insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 2696create table t1 (kp1 int, kp2 int, c int, filler char(100), key(kp1, kp2)); 2697insert into t1 select A.a+10*(B.a+10*C.a), 0, 0, 'filler' from t0 A, t0 B, t0 C; 2698insert into t1 select * from t1 where kp1 < 20; 2699create table t3 (a int); 2700insert into t3 select A.a + 10*B.a from t0 A, t0 B; 2701select * from t3 where a in (select kp1 from t1 where kp1<20); 2702a 27030 27041 27052 27063 27074 27085 27096 27107 27118 27129 271310 271411 271512 271613 271714 271815 271916 272017 272118 272219 2723explain select * from t3 where a in (select kp1 from t1 where kp1<20); 2724id select_type table type possible_keys key key_len ref rows Extra 27251 PRIMARY t3 ALL NULL NULL NULL NULL 100 Using where 27261 PRIMARY t1 ref kp1 kp1 5 test.t3.a 1 Using index; FirstMatch(t3) 2727drop table t0,t1,t3; 2728set optimizer_switch= @tmp_923246; 2729# 2730# BUG#952583: Server crashes in Item_field::fix_after_pullout on INSERT .. SELECT 2731# 2732CREATE TABLE t1 ( a INT ); 2733INSERT INTO t1 VALUES (1),(2); 2734CREATE TABLE t2 ( b INT ); 2735INSERT INTO t2 VALUES (3),(4); 2736INSERT INTO t1 2737SELECT * FROM ( SELECT * FROM t1 ) AS alias 2738WHERE a IN ( SELECT b FROM t2 ); 2739DROP TABLE t1, t2; 2740# 2741# BUG#952372: Server crashes on 2nd execution of PS in find_field_in_tables with semijoin+materialization 2742# 2743CREATE TABLE t1 ( a INT ); 2744INSERT INTO t1 VALUES (2),(3); 2745CREATE VIEW v1 AS SELECT * FROM t1; 2746CREATE TABLE t2 ( b VARCHAR(1) ); 2747INSERT INTO t2 VALUES ('v'),('v'); 2748PREPARE pstmt FROM 2749'SELECT DISTINCT a FROM v1, t2 2750 WHERE b IN ( SELECT MIN(b) FROM t2 )'; 2751EXECUTE pstmt; 2752a 27532 27543 2755EXECUTE pstmt; 2756a 27572 27583 2759DEALLOCATE PREPARE pstmt; 2760DROP VIEW v1; 2761DROP TABLE t1, t2; 2762# 2763# BUG#978479: Wrong result (extra rows) with derived_with_keys+loosescan+semijoin=ON, materialization=OFF 2764# 2765set @tmp_jcl_978479= @@join_cache_level; 2766set join_cache_level=0; 2767set @tmp_os_978479= @@optimizer_switch; 2768set optimizer_switch = 'derived_with_keys=on,loosescan=on,semijoin=on,materialization=off'; 2769# Part#1: make sure EXPLAIN is using LooseScan: 2770CREATE TABLE t1 ( a INT, b INT ); 2771INSERT INTO t1 VALUES 2772(4,0),(6,8),(3,1),(5,8),(3,9),(2,4), 2773(2,6),(9,1),(5,4),(7,7),(5,4); 2774CREATE ALGORITHM=TEMPTABLE 2775VIEW v1 AS SELECT * FROM t1; 2776# This will use LooseScan: 2777EXPLAIN 2778SELECT * FROM t1 AS t1_1, t1 AS t1_2 2779WHERE (t1_1.a, t1_2.a) IN ( SELECT a, b FROM v1 ); 2780id select_type table type possible_keys key key_len ref rows Extra 27811 PRIMARY t1_1 ALL NULL NULL NULL NULL 11 Using where 27821 PRIMARY t1_2 ALL NULL NULL NULL NULL 11 27831 PRIMARY <derived3> ref key0 key0 5 test.t1_1.a 2 Using where; FirstMatch(t1_2) 27843 DERIVED t1 ALL NULL NULL NULL NULL 11 2785SELECT * FROM t1 AS t1_1, t1 AS t1_2 2786WHERE (t1_1.a, t1_2.a) IN ( SELECT a, b FROM v1 ); 2787a b a b 27883 1 9 1 27895 8 4 0 27903 9 9 1 27912 4 4 0 27922 4 6 8 27932 6 4 0 27942 6 6 8 27955 4 4 0 27967 7 7 7 27975 4 4 0 2798DROP VIEW v1; 2799DROP TABLE t1; 2800set @@join_cache_level= @tmp_jcl_978479; 2801set @@optimizer_switch= @tmp_os_978479; 2802# 2803# BUG#998236: Assertion failure or valgrind errors at best_access_path ... 2804# 2805CREATE TABLE t1 (a1 VARCHAR(3), a2 VARCHAR(35), KEY(a1,a2)) ENGINE=MyISAM; 2806INSERT INTO t1 VALUES 2807('USA','Arvada'),('USA','Athens'),('USA','Atlanta'),('USA','Augusta'), 2808('USA','Aurora'),('USA','Aurora'),('USA','Austin'),('USA','Bakersfield'), 2809('USA','Baltimore'),('USA','Baton'),('USA','Beaumont'),('USA','Bellevue'), 2810('USA','Berkeley'),('USA','Billings'),('USA','Birmingham'),('USA','Boise'), 2811('USA','Boston'),('USA','Boulder'),('USA','Bridgeport'),('USA','Brockton'), 2812('USA','Brownsville'),('USA','Buffalo'),('USA','Burbank'),('USA','Cambridge'), 2813('USA','Cape'),('USA','Carrollton'),('USA','Carson'),('USA','Cary'), 2814('USA','Cedar'),('USA','Chandler'),('USA','Charleston'),('USA','Charlotte'), 2815('USA','Chattanooga'),('USA','Chesapeake'),('USA','Chicago'),('USA','Chula'), 2816('USA','Cincinnati'),('USA','Citrus'),('USA','Clarksville'),('USA','Clearwater'), 2817('USA','Cleveland'),('USA','Colorado'),('USA','Columbia'),('USA','Columbus'), 2818('USA','Columbus'),('USA','Compton'),('USA','Concord'),('USA','Coral'), 2819('USA','Corona'),('USA','Corpus'),('USA','Costa'),('USA','Dallas'),('USA','Daly'), 2820('USA','Davenport'),('USA','Dayton'),('USA','Denver'),('USA','DesMoines'), 2821('USA','Detroit'),('USA','Downey'),('USA','Durham'),('USA','East'),('USA','ElCajon'), 2822('USA','ElMonte'),('USA','ElPaso'),('USA','Elgin'),('USA','Elizabeth'), 2823('USA','Erie'),('USA','Escondido'),('USA','Eugene'),('USA','Evansville'), 2824('USA','Fairfield'),('USA','Fall'),('USA','Fayetteville'),('USA','Flint'), 2825('USA','Fontana'),('USA','FortCollins'),('USA','FortLauderdale'),('USA','FortWayne'), 2826('USA','FortWorth'),('USA','Fremont'),('USA','Fresno'),('USA','Fullerton'), 2827('USA','Gainesville'),('USA','GardenGrove'),('USA','Garland'),('USA','Gary'), 2828('USA','Gilbert'),('USA','Glendale'),('USA','Glendale'),('USA','GrandPrairie'), 2829('USA','GrandRapids'),('USA','Green Bay'),('USA','Greensboro'),('USA','Hampton'), 2830('USA','Hartford'),('USA','Hayward'),('USA','Henderson'),('USA','Hialeah'), 2831('USA','Inglewood'),('USA','Livonia'); 2832CREATE TABLE t3 (c1 VARCHAR(3), c2 VARCHAR(16), PRIMARY KEY (c1,c2)) ENGINE=MyISAM; 2833INSERT INTO t3 VALUES ('USA','Chinese'), ('USA','English'); 2834CREATE TABLE t2 (b1 INT) ENGINE=MyISAM; 2835INSERT INTO t2 VALUES (1); 2836SELECT alias1.a1 AS field1 2837FROM t1 AS alias1, t1 AS alias2 2838WHERE alias1.a2 IN ( SELECT a1 FROM t1, t2 ) 2839AND alias1.a1 IS NULL 2840AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 ) 2841GROUP BY field1; 2842field1 2843DROP TABLE t1,t3,t2; 2844# 2845# BUG#1002630: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with SELECT 2846# 2847CREATE TABLE t1 (a INT); 2848INSERT INTO t1 VALUES (1),(7); 2849CREATE TABLE t2 (b INT); 2850INSERT INTO t2 VALUES (4),(6); 2851SELECT ( SELECT SUM(a) FROM t1 ) AS t1sum, b 2852FROM t2 2853WHERE (1,1) IN ( SELECT MAX(a), MIN(a) FROM t1 ) 2854GROUP BY b 2855HAVING t1sum <> 1; 2856t1sum b 2857DROP TABLE t1, t2; 2858# 2859# MDEV-3911: Assertion `fixed == 0' failed in Item_field::fix_fields 2860# on 2nd execution of PS with semijoin=on and IN subquery 2861# 2862CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM; 2863INSERT INTO t1 VALUES (0,4),(8,6); 2864CREATE TABLE t2 (c INT, d INT) ENGINE=MyISAM; 2865INSERT INTO t2 VALUES (7,1),(0,7); 2866PREPARE stmt FROM ' SELECT * FROM t1 WHERE ( a, b ) IN ( SELECT c, d FROM t2 ) '; 2867execute stmt; 2868a b 2869execute stmt; 2870a b 2871deallocate prepare stmt; 2872drop table t1,t2; 2873# 2874# MDEV-4335: Unexpected results when selecting on information_schema 2875# 2876CREATE TABLE t1 (db VARCHAR(64) DEFAULT NULL); 2877INSERT INTO t1 VALUES ('mysql'),('information_schema'); 2878SELECT * FROM t1 WHERE db IN (SELECT `SCHEMA_NAME` FROM information_schema.SCHEMATA); 2879db 2880information_schema 2881mysql 2882DROP TABLE t1; 2883# 2884# MDEV-5581: Server crashes in in JOIN::prepare on 2nd execution of PS with materialization+semijoin 2885# 2886CREATE TABLE t1 (a INT); 2887INSERT INTO t1 VALUES (2),(3); 2888CREATE TABLE t2 (b INT); 2889INSERT INTO t2 VALUES (8),(9); 2890CREATE TABLE t3 (c INT, INDEX(c)); 2891INSERT INTO t2 VALUES (5),(6); 2892PREPARE stmt FROM 2893"SELECT * FROM t1 WHERE ( 9, 5 ) IN ( SELECT b, COUNT(*) FROM t2 WHERE 1 IN ( SELECT MIN(c) FROM t3 ) )"; 2894EXECUTE stmt; 2895a 2896EXECUTE stmt; 2897a 2898DROP TABLE t1,t2,t3; 2899# 2900# MySQL Bug#13340270: assertion table->sort.record_pointers == __null 2901# 2902CREATE TABLE t1 ( 2903pk int NOT NULL, 2904col_int_key int DEFAULT NULL, 2905col_varchar_key varchar(1) DEFAULT NULL, 2906col_varchar_nokey varchar(1) DEFAULT NULL, 2907PRIMARY KEY (pk), 2908KEY col_int_key (col_int_key), 2909KEY col_varchar_key (col_varchar_key, col_int_key) 2910); 2911INSERT INTO t1 VALUES 2912(10,8,'x','x'), 2913(11,7,'d','d'), 2914(12,1,'r','r'), 2915(13,7,'f','f'), 2916(14,9,'y','y'), 2917(15,NULL,'u','u'), 2918(16,1,'m','m'), 2919(17,9,NULL,NULL), 2920(18,2,'o','o'), 2921(19,9,'w','w'), 2922(20,2,'m','m'), 2923(21,4,'q','q'); 2924CREATE TABLE t2 2925SELECT alias1.col_varchar_nokey AS field1 2926FROM t1 AS alias1 JOIN t1 AS alias2 2927ON alias2.col_int_key = alias1.pk OR 2928alias2.col_int_key = alias1.col_int_key 2929WHERE alias1.pk = 58 OR alias1.col_varchar_key = 'o' 2930 2931; 2932EXPLAIN SELECT * 2933FROM t2 2934WHERE (field1) IN (SELECT alias1.col_varchar_nokey AS field1 2935FROM t1 AS alias1 JOIN t1 AS alias2 2936ON alias2.col_int_key = alias1.pk OR 2937alias2.col_int_key = alias1.col_int_key 2938WHERE alias1.pk = 58 OR alias1.col_varchar_key = 'o' 2939); 2940id select_type table type possible_keys key key_len ref rows Extra 29411 PRIMARY alias1 index_merge PRIMARY,col_int_key,col_varchar_key PRIMARY,col_varchar_key 4,4 NULL 2 Using sort_union(PRIMARY,col_varchar_key); Using where; Start temporary 29421 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 29431 PRIMARY alias2 ALL col_int_key NULL NULL NULL 12 Range checked for each record (index map: 0x2); End temporary 2944SELECT * 2945FROM t2 2946WHERE (field1) IN (SELECT alias1.col_varchar_nokey AS field1 2947FROM t1 AS alias1 JOIN t1 AS alias2 2948ON alias2.col_int_key = alias1.pk OR 2949alias2.col_int_key = alias1.col_int_key 2950WHERE alias1.pk = 58 OR alias1.col_varchar_key = 'o' 2951); 2952field1 2953o 2954o 2955DROP TABLE t1, t2; 2956# 2957# MDEV-389: Wrong result (missing row) with semijoin, join_cache_level>4, LEFT JOIN... 2958# (testcase only) 2959# 2960connect con1,localhost,root,,; 2961connection con1; 2962SET join_cache_level = 5; 2963SET optimizer_switch = 'semijoin=on'; 2964CREATE TABLE t1 (a INT NOT NULL, b CHAR(1), KEY(a)) ENGINE=MyISAM; 2965INSERT INTO t1 VALUES (4,'p'),(1,'q'),(8,'e'); 2966CREATE TABLE t2 (c INT, d CHAR(1), KEY(c), KEY(d)) ENGINE=MyISAM; 2967INSERT INTO t2 VALUES (4,'f'),(2,'i'),(5,'h'),(3,'q'),(1,'g'); 2968SELECT a, COUNT(*) AS cnt 2969FROM t1 LEFT JOIN t2 ON (d = b) 2970WHERE a IN ( SELECT c FROM t2 WHERE b > 'k' ) 2971GROUP BY a ORDER BY a, cnt LIMIT 2; 2972a cnt 29731 1 29744 1 2975drop table t1, t2; 2976connection default; 2977disconnect con1; 2978# 2979# MDEV-4071: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with ... 2980# 2981CREATE TABLE t1 (b INT, c VARCHAR(1)) ENGINE=MyISAM; 2982INSERT INTO t1 VALUES (7,'v'),(0,'s'); 2983CREATE TABLE t2 (a INT) ENGINE=MyISAM; 2984INSERT INTO t2 VALUES (0),(8); 2985SELECT c, SUM( DISTINCT b ) AS sm FROM t1 2986WHERE ( 5, 108 ) IN ( SELECT MIN(a), MAX(a) FROM t2 ) 2987GROUP BY b 2988HAVING c <> ( SELECT MAX( c ) FROM t1 ) 2989ORDER BY sm; 2990c sm 2991DROP TABLE t1,t2; 2992# 2993# mdev-4173 ignored duplicate value when converting heap to temp table 2994# could lead to extra rows in semijoin queries or missing row in union queries 2995# 2996CREATE TABLE t1 (i1 INT) engine=myisam; 2997INSERT INTO t1 VALUES 2998(4),(8),(0),(0),(0),(7),(7),(5),(3),(4),(9),(6),(1),(5),(6),(2),(4),(4); 2999CREATE TABLE t2 (i2 INT, j2 INT) engine=myisam; 3000INSERT INTO t2 VALUES 3001(7,1),(0,7),(9,4),(3,7),(4,0),(2,2),(5,9),(3,4), 3002(1,0),(3,9),(5,8),(1,8),(204,18),(224,84),(9,6); 3003CREATE TABLE t3 (i3 INT, KEY(i3)) engine=myisam; 3004INSERT INTO t3 VALUES 3005(0),(8),(1),(8),(9),(24),(6),(1),(6),(2),(4),(2),(1); 3006select @@max_heap_table_size into @tmp_max_heap_table_size; 3007select @@join_buffer_size into @tmp_join_buffer_size; 3008set max_heap_table_size=16*1024; 3009COUNT(*) 30102834 3011COUNT(*) 30122834 3013COUNT(*) 30142834 3015COUNT(*) 30162834 3017COUNT(*) 30182834 3019COUNT(*) 30202834 3021COUNT(*) 30222834 3023COUNT(*) 30242834 3025DROP TABLE t1, t2, t3; 3026set join_buffer_size = @tmp_join_buffer_size; 3027set max_heap_table_size = @tmp_max_heap_table_size; 3028# 3029# MDEV-5161: Wrong result (missing rows) with semijoin, LEFT JOIN, ORDER BY, constant table 3030# 3031CREATE TABLE t1 (pk INT PRIMARY KEY, c1 VARCHAR(1)) ENGINE=MyISAM; 3032INSERT INTO t1 VALUES (1,'v'),(2,'v'),(3,'c'),(4,NULL),(5,'x'); 3033CREATE TABLE t2 (c2 VARCHAR(1)) ENGINE=MyISAM; 3034INSERT INTO t2 VALUES ('x'); 3035CREATE TABLE t3 (c3 VARCHAR(1)) ENGINE=MyISAM; 3036INSERT INTO t3 VALUES ('x'),('d'); 3037SELECT * FROM t1, t2 WHERE pk IN ( SELECT pk FROM t1 LEFT JOIN t3 ON (c1 = c3 ) ) ORDER BY c2, c1; 3038pk c1 c2 30391 v x 30402 v x 30413 c x 30424 NULL x 30435 x x 3044# This should show that "t1 left join t3" is still in the semi-join nest: 3045EXPLAIN EXTENDED 3046SELECT * FROM t1, t2 WHERE pk IN ( SELECT pk FROM t1 LEFT JOIN t3 ON (c1 = c3 ) ) ORDER BY c2, c1; 3047id select_type table type possible_keys key key_len ref rows filtered Extra 30481 PRIMARY t2 system NULL NULL NULL NULL 1 100.00 Using temporary; Using filesort 30491 PRIMARY t1 ALL PRIMARY NULL NULL NULL 5 100.00 Start temporary 30501 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 100.00 Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 30511 PRIMARY t3 ALL NULL NULL NULL NULL 2 100.00 Using where; End temporary; Using join buffer (incremental, BNL join) 3052Warnings: 3053Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`c1` AS `c1`,'x' AS `c2` from `test`.`t1` semi join (`test`.`t1` left join `test`.`t3` on(`test`.`t1`.`c1` = `test`.`t3`.`c3`)) where `test`.`t1`.`pk` = `test`.`t1`.`pk` order by 'x',`test`.`t1`.`c1` 3054DROP TABLE t1,t2,t3; 3055# 3056# MDEV-5059: Wrong result (missing row) wih semijoin, join_cache_level > 2, LEFT JOIN, ORDER BY 3057# 3058SET @tmp_mdev5059=@@join_cache_level; 3059SET join_cache_level = 3; 3060set @tmp_os_mdev5059= @@optimizer_switch; 3061set optimizer_switch=@local_optimizer_switch; 3062CREATE TABLE t1 (pk1 INT PRIMARY KEY, i1 INT) ENGINE=MyISAM; 3063INSERT INTO t1 VALUES (1,4),(2,5); 3064CREATE TABLE t2 (i2 INT, c2 CHAR(1)) ENGINE=MyISAM; 3065INSERT INTO t2 VALUES (1,'v'),(7,'s'); 3066CREATE TABLE t3 (pk3 INT PRIMARY KEY, i3 INT, c3 CHAR(1), INDEX(i3), INDEX(c3,i3)) ENGINE=MyISAM; 3067INSERT INTO t3 VALUES (1,7,'g'),(2,4,'p'),(3,1,'q'); 3068CREATE TABLE t4 (i4 INT) ENGINE=MyISAM; 3069INSERT INTO t4 VALUES (1); 3070SELECT * FROM t1, t2 LEFT JOIN t3 ON (c3 = c2) 3071WHERE ( i2, pk1 ) IN ( SELECT i3, 1 FROM t3, t4 ) ORDER BY i1, c2; 3072pk1 i1 i2 c2 pk3 i3 c3 30731 4 7 s NULL NULL NULL 30741 4 1 v NULL NULL NULL 3075SET join_cache_level=@tmp_mdev5059; 3076set optimizer_switch=@tmp_os_mdev5059; 3077DROP TABLE t1,t2,t3,t4; 3078# 3079# MDEV-7911: crash in Item_cond::eval_not_null_tables 3080# 3081create table t1(a int); 3082insert into t1 values(1),(2),(3),(null); 3083explain 3084select 1 from t1 where _cp932 "1" in (select '1' from t1); 3085ERROR HY000: Illegal mix of collations (cp932_japanese_ci,COERCIBLE) and (latin1_swedish_ci,COERCIBLE) for operation '=' 3086drop table t1; 3087# 3088# MDEV-7823: Server crashes in next_depth_first_tab on nested IN clauses with SQ inside 3089# 3090set @tmp_mdev7823=@@optimizer_switch; 3091set optimizer_switch=default; 3092CREATE TABLE t1 (f1 INT); 3093INSERT INTO t1 VALUES (1); 3094CREATE TABLE t2 (f2 INT, KEY(f2)); 3095INSERT INTO t2 VALUES (8),(0); 3096CREATE TABLE t3 (f3 INT); 3097INSERT INTO t3 VALUES (1),(2); 3098CREATE TABLE t4 (f4 INT); 3099INSERT INTO t4 VALUES (0),(5); 3100explain 3101SELECT * FROM t1, t2, t3 WHERE f2 IN ( f1 IN ( SELECT f4 FROM t4 ) ); 3102id select_type table type possible_keys key key_len ref rows Extra 31031 PRIMARY t1 system NULL NULL NULL NULL 1 31041 PRIMARY t2 ref f2 f2 5 const 0 Using where; Using index 31051 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 31062 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 2 Using where 3107SELECT * FROM t1, t2, t3 WHERE f2 IN ( f1 IN ( SELECT f4 FROM t4 ) ); 3108f1 f2 f3 31091 0 1 31101 0 2 3111drop table t1,t2,t3,t4; 3112set optimizer_switch= @tmp_mdev7823; 3113# 3114# MDEV-6859: scalar subqueries in a comparison produced unexpected result 3115# 3116set @tmp_mdev6859=@@optimizer_switch; 3117set optimizer_switch=default; 3118CREATE TABLE t1 ( 3119project_number varchar(50) NOT NULL, 3120PRIMARY KEY (project_number) 3121) ENGINE=MyISAM; 3122INSERT INTO t1 (project_number) VALUES ('aaa'),('bbb'); 3123CREATE TABLE t2 ( 3124id int(10) unsigned NOT NULL AUTO_INCREMENT, 3125project_number varchar(50) NOT NULL, 3126history_date date NOT NULL, 3127country varchar(50) NOT NULL, 3128PRIMARY KEY (id) 3129) ENGINE=MyISAM; 3130INSERT INTO t2 (id, project_number, history_date, country) VALUES 3131(1, 'aaa', '2014-08-09', 'france'),(2, 'aaa', '2014-09-09', 'singapore'); 3132CREATE TABLE t3 ( 3133region varchar(50) NOT NULL, 3134country varchar(50) NOT NULL 3135) ENGINE=MyISAM; 3136INSERT INTO t3 (region, country) VALUES ('apac', 'singapore'),('eame', 'france'); 3137SELECT SQL_NO_CACHE a.project_number 3138FROM t1 a 3139WHERE ( SELECT z.country 3140FROM t2 z 3141WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01' 3142ORDER BY z.id DESC LIMIT 1 3143) IN ( 3144SELECT r.country 3145FROM t3 r 3146WHERE r.region = 'eame' 3147 ); 3148project_number 3149aaa 3150drop table t1, t2, t3; 3151set optimizer_switch= @tmp_mdev6859; 3152# 3153# MDEV-12675: subquery subject to semi-join optimizations 3154# in ON expression of INNER JOIN 3155# 3156set @tmp_mdev12675=@@optimizer_switch; 3157set optimizer_switch=default; 3158create table t1 (a int) engine=myisam; 3159insert into t1 values (3),(2),(7),(2),(1); 3160create table t2 (b int, index idx(b)) engine=myisam; 3161insert into t2 values (2),(3),(2),(1),(3),(4),(1),(2),(1),(2); 3162insert into t2 select b+10 from t2; 3163insert into t2 select b+10 from t2; 3164insert into t2 select b+10 from t2; 3165insert into t2 select b+10 from t2; 3166insert into t2 select b+10 from t2; 3167insert into t2 select b+10 from t2; 3168insert into t2 select b+10 from t2; 3169insert into t2 select b+10 from t2; 3170insert into t2 select b+10 from t2; 3171insert into t2 select b+10 from t2; 3172insert into t2 select b+10 from t2; 3173insert into t2 select b+10 from t2; 3174insert into t2 select b+10 from t2; 3175analyze table t1,t2; 3176Table Op Msg_type Msg_text 3177test.t1 analyze status Engine-independent statistics collected 3178test.t1 analyze status OK 3179test.t2 analyze status Engine-independent statistics collected 3180test.t2 analyze status OK 3181explain 3182select a from t1, t2 where b between 1 and 2 and a in (select b from t2); 3183id select_type table type possible_keys key key_len ref rows Extra 31841 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where 31851 PRIMARY t2 ref idx idx 5 test.t1.a 1462 Using index; FirstMatch(t1) 31861 PRIMARY t2 range idx idx 5 NULL 5 Using where; Using index; Using join buffer (flat, BNL join) 3187explain 3188select a from t1 join t2 on b between 1 and 2 and a in (select b from t2); 3189id select_type table type possible_keys key key_len ref rows Extra 31901 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where 31911 PRIMARY t2 ref idx idx 5 test.t1.a 1462 Using index; FirstMatch(t1) 31921 PRIMARY t2 range idx idx 5 NULL 5 Using where; Using index; Using join buffer (flat, BNL join) 3193drop table t1,t2; 3194set optimizer_switch= @tmp_mdev12675; 3195# 3196# MDEV-12817: subquery NOT subject to semi-join optimizations 3197# in ON expression of INNER JOIN 3198# 3199CREATE TABLE t1 (c1 int) ENGINE=MyISAM; 3200INSERT INTO t1 VALUES (1),(2); 3201CREATE TABLE t2 (c2 int) ENGINE=MyISAM; 3202INSERT INTO t2 VALUES (3),(4); 3203CREATE TABLE t3 (c3 int) ENGINE=MyISAM; 3204INSERT INTO t3 VALUES (5),(6); 3205CREATE TABLE t4 (c4 int) ENGINE=MyISAM; 3206INSERT INTO t4 VALUES (7),(8); 3207SELECT c1 3208FROM t1 3209LEFT JOIN 3210( t2 INNER JOIN t3 ON ( 1 IN ( SELECT c4 FROM t4 ) ) ) 3211ON (c1 = c3); 3212c1 32131 32142 3215EXPLAIN EXTENDED SELECT c1 3216FROM t1 3217LEFT JOIN 3218( t2 INNER JOIN t3 ON ( 1 IN ( SELECT c4 FROM t4 ) ) ) 3219ON (c1 = c3); 3220id select_type table type possible_keys key key_len ref rows filtered Extra 32211 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 32221 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join) 32231 PRIMARY t3 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (incremental, BNL join) 32242 SUBQUERY t4 ALL NULL NULL NULL NULL 2 100.00 Using where 3225Warnings: 3226Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` left join (`test`.`t2` join `test`.`t3`) on(`test`.`t3`.`c3` = `test`.`t1`.`c1` and <cache>(<in_optimizer>(1,<exists>(/* select#2 */ select `test`.`t4`.`c4` from `test`.`t4` where 1 = `test`.`t4`.`c4`)))) where 1 3227# mdev-12820 3228SELECT * 3229FROM t1 3230LEFT JOIN 3231( ( SELECT * FROM t2 WHERE c2 IN ( SELECT c3 FROM t3 ) ) AS sq INNER JOIN t4 ) 3232ON (c1 = c2); 3233c1 c2 c4 32341 NULL NULL 32352 NULL NULL 3236EXPLAIN EXTENDED SELECT * 3237FROM t1 3238LEFT JOIN 3239( ( SELECT * FROM t2 WHERE c2 IN ( SELECT c3 FROM t3 ) ) AS sq INNER JOIN t4 ) 3240ON (c1 = c2); 3241id select_type table type possible_keys key key_len ref rows filtered Extra 32421 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 32431 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join) 32441 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using join buffer (incremental, BNL join) 32453 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where 3246Warnings: 3247Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1`,`test`.`t2`.`c2` AS `c2`,`test`.`t4`.`c4` AS `c4` from `test`.`t1` left join (`test`.`t2` join `test`.`t4`) on(`test`.`t2`.`c2` = `test`.`t1`.`c1` and <in_optimizer>(`test`.`t1`.`c1`,<exists>(/* select#3 */ select `test`.`t3`.`c3` from `test`.`t3` where <cache>(`test`.`t2`.`c2`) = `test`.`t3`.`c3`))) where 1 3248DROP TABLE t1,t2,t3,t4; 3249# 3250# MDEV-13699: Assertion `!new_field->field_name.str || 3251# strlen(new_field->field_name.str) == new_field->field_name.length' 3252# failed in create_tmp_table on 2nd execution of PS with semijoin 3253# 3254CREATE TABLE t1 (a INT); 3255INSERT INTO t1 VALUES (1),(2); 3256CREATE TABLE t2 (b INT); 3257INSERT INTO t2 VALUES (3),(4); 3258CREATE TABLE t3 (c INT); 3259CREATE ALGORITHM=MERGE VIEW v3 AS SELECT * FROM t3; 3260INSERT INTO t3 VALUES (5),(6); 3261PREPARE stmt FROM 3262"SELECT * FROM t1 3263 WHERE EXISTS ( 3264 SELECT * FROM t2 WHERE t1.a IN ( SELECT c AS fld FROM v3 ) 3265 )"; 3266EXECUTE stmt; 3267a 3268EXECUTE stmt; 3269a 3270EXECUTE stmt; 3271a 3272drop view v3; 3273drop table t1,t2,t3; 3274# 3275# MDEV-18896: IN subquery in WHERE of a table-less query used for INSERT 3276# 3277set @@optimizer_switch= @subselect_sj_tmp; 3278create table t1 (a1 varchar(25)); 3279create table t2 (a2 varchar(25)) ; 3280insert into t1 select 'xxx' from dual where 'xxx' in (select a2 from t2); 3281drop table t1,t2; 3282# 3283# MDEV-13172: Wrong result / SELECT ... WHERE EXISTS ... (with UNIQUE Key) 3284# 3285CREATE TABLE `t1` ( 3286`Id` int(11) NOT NULL, 3287PRIMARY KEY (`Id`) 3288); 3289INSERT INTO `t1` (`Id`) VALUES (1); 3290CREATE TABLE `t2` ( 3291`t1_Id` int(11) NOT NULL DEFAULT 0, 3292`col1` int(11) DEFAULT NULL, 3293UNIQUE KEY `col1` (`col1`) 3294); 3295INSERT INTO `t2` (`t1_Id`, `col1`) VALUES (1, NULL), (1, NULL); 3296SELECT Id FROM t1 WHERE Id in (SELECT t1_Id FROM t2 WHERE t2.col1 IS NULL); 3297Id 32981 3299explain extended 3300SELECT Id FROM t1 WHERE Id in (SELECT t1_Id FROM t2 WHERE t2.col1 IS NULL); 3301id select_type table type possible_keys key key_len ref rows filtered Extra 33021 PRIMARY t1 system PRIMARY NULL NULL NULL 1 100.00 33031 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 33042 MATERIALIZED t2 ref col1 col1 5 const 2 100.00 Using index condition; Using where 3305Warnings: 3306Note 1003 select 1 AS `Id` from (`test`.`t2`) where `test`.`t2`.`t1_Id` = 1 and `test`.`t2`.`col1` is null 3307DROP TABLE t1, t2; 3308# End of 5.5 test 3309# 3310# MDEV-20109: Optimizer ignores distinct key created for materialized 3311# semi-join subquery when searching for best execution plan 3312# 3313create table t1(a int); 3314insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 3315create table t2(a int); 3316insert into t2 select A.a + B.a* 10 + C.a * 100 from t1 A, t1 B, t1 C; 3317create table t3 (a int); 3318create table t4 (a int); 3319insert into t3 select A.a +1000*B.a from t2 A, t1 B; 3320insert into t4 select floor(rand()*1000) from t2 limit 500; 3321# The following must not use this query plan that does a cross join: 3322# | 1 | PRIMARY | <subquery2> | ALL | distinct_key | ... | 500 | | 3323# | 1 | PRIMARY | t3 | ALL | NULL | ... | 10000 | Using where; Using join buffer (flat, BNL join) | 3324# 3325# Instead, it should use eq_ref on the materialized table. 3326explain select * from t3 where a in (select a from t4); 3327id select_type table type possible_keys key key_len ref rows Extra 33281 PRIMARY t3 ALL NULL NULL NULL NULL 10000 33291 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 33302 MATERIALIZED t4 ALL NULL NULL NULL NULL 500 3331drop table t1, t2, t3, t4; 3332# 3333# MDEV-20770: Server crashes in JOIN::transform_in_predicates_into_in_subq 3334# upon 2nd execution of PS/SP comparing GEOMETRY with other types 3335# 3336CREATE TABLE t1 (a GEOMETRY); 3337CREATE TABLE t2 (b INT); 3338INSERT INTO t1 VALUES (GeomFromText('POINT(0 0)')),(GeomFromText('POINT(1 1)')); 3339INSERT INTO t2 VALUES (1),(2); 3340PREPARE stmt FROM "SELECT * from t1 WHERE a IN (SELECT b FROM t2)"; 3341EXECUTE stmt; 3342ERROR HY000: Illegal parameter data types geometry and int for operation '=' 3343EXECUTE stmt; 3344ERROR HY000: Illegal parameter data types geometry and int for operation '=' 3345EXECUTE stmt; 3346ERROR HY000: Illegal parameter data types geometry and int for operation '=' 3347EXECUTE stmt; 3348ERROR HY000: Illegal parameter data types geometry and int for operation '=' 3349DROP TABLE t1, t2; 3350set optimizer_switch=@subselect_sj_tmp; 3351# 3352# BUG#49129: Wrong result with IN-subquery with join_cache_level=6 and firstmatch=off 3353# 3354CREATE TABLE t0 (a INT); 3355INSERT INTO t0 VALUES (0),(1),(2),(3),(4); 3356CREATE TABLE t1 (a INT, b INT, KEY(a)); 3357INSERT INTO t1 SELECT a, a from t0; 3358INSERT INTO t1 SELECT a+5, a from t0; 3359INSERT INTO t1 SELECT a+10, a from t0; 3360CREATE TABLE t2 (a INT, b INT, PRIMARY KEY(a)); 3361INSERT INTO t2 SELECT * FROM t1; 3362UPDATE t1 SET a=3, b=11 WHERE a=4; 3363UPDATE t2 SET b=11 WHERE a=3; 3364set @save_optimizer_switch=@@optimizer_switch; 3365set optimizer_switch='firstmatch=off'; 3366The following should use a join order of t0,t1,t2, with DuplicateElimination: 3367explain 3368SELECT * FROM t0 WHERE t0.a IN 3369(SELECT t1.a FROM t1, t2 WHERE t2.a=t0.a AND t1.b=t2.b); 3370id select_type table type possible_keys key key_len ref rows Extra 33711 PRIMARY t0 ALL NULL NULL NULL NULL 5 Using where 33721 PRIMARY t1 ref a a 5 test.t0.a 1 Start temporary; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 33731 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t0.a 1 Using where; End temporary; Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan 3374SELECT * FROM t0 WHERE t0.a IN 3375(SELECT t1.a FROM t1, t2 WHERE t2.a=t0.a AND t1.b=t2.b); 3376a 33770 33781 33792 33803 3381set optimizer_switch=@save_optimizer_switch; 3382drop table t0, t1, t2; 3383# 3384# Bug #891995: IN subquery with join_cache_level >= 3 3385# 3386CREATE TABLE t1 (a varchar(1)); 3387INSERT INTO t1 VALUES ('w'),('q'); 3388CREATE TABLE t2 (a int NOT NULL, b time, PRIMARY KEY (a)); 3389INSERT INTO t2 VALUES 3390(2,'18:56:33'), (5,'19:11:10'), (3,'18:56:33'), (7,'19:11:10'); 3391CREATE TABLE t3 ( 3392a int NOT NULL, b int, c int, d varchar(1), PRIMARY KEY (a), KEY (d,c) 3393); 3394INSERT INTO t3 VALUES 3395(25,158,10,'f'), (26,5,2,'v'), (27,163,103,'f'), (28,2,3,'q'), (29,8,6,'y'); 3396set @save_optimizer_switch=@@optimizer_switch; 3397SET SESSION optimizer_switch='semijoin=on'; 3398SET SESSION optimizer_switch='join_cache_hashed=on'; 3399SET SESSION join_cache_level=3; 3400EXPLAIN 3401SELECT * FROM t1, t2 3402WHERE t2.a IN (SELECT b FROM t3 WHERE t3.d <= t1.a); 3403id select_type table type possible_keys key key_len ref rows Extra 34041 PRIMARY t1 ALL NULL NULL NULL NULL 2 34051 PRIMARY t2 ALL PRIMARY NULL NULL NULL 4 Using join buffer (flat, BNL join) 34061 PRIMARY t3 ALL d NULL NULL NULL 5 Range checked for each record (index map: 0x2); FirstMatch(t2) 3407SELECT * FROM t1, t2 3408WHERE t2.a IN (SELECT b FROM t3 WHERE t3.d <= t1.a); 3409a a b 3410w 2 18:56:33 3411q 2 18:56:33 3412w 5 19:11:10 3413SET SESSION optimizer_switch='mrr=on'; 3414SET SESSION join_cache_level=6; 3415EXPLAIN 3416SELECT * FROM t1, t2 3417WHERE t2.a IN (SELECT b FROM t3 WHERE t3.d <= t1.a); 3418id select_type table type possible_keys key key_len ref rows Extra 34191 PRIMARY t1 ALL NULL NULL NULL NULL 2 34201 PRIMARY t2 ALL PRIMARY NULL NULL NULL 4 Using join buffer (flat, BNL join) 34211 PRIMARY t3 ALL d NULL NULL NULL 5 Range checked for each record (index map: 0x2); FirstMatch(t2) 3422SELECT * FROM t1, t2 3423WHERE t2.a IN (SELECT b FROM t3 WHERE t3.d <= t1.a); 3424a a b 3425w 2 18:56:33 3426q 2 18:56:33 3427w 5 19:11:10 3428set optimizer_switch=@save_optimizer_switch; 3429set join_cache_level=default; 3430DROP TABLE t1,t2,t3; 3431# 3432# BUG#912513: Wrong result (missing rows) with join_cache_hashed+materialization+semijoin=on 3433# 3434set @os_912513= @@optimizer_switch; 3435set @jcl_912513= @@join_cache_level; 3436SET optimizer_switch = 'semijoin=on,materialization=on,join_cache_hashed=on'; 3437SET join_cache_level = 3; 3438CREATE TABLE t1 ( a INT, b INT, KEY(a) ); 3439INSERT INTO t1 VALUES 3440(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7); 3441CREATE TABLE t2 ( c INT ); 3442INSERT INTO t2 VALUES (1),(2),(3),(4),(5),(6),(7); 3443SELECT alias1.* FROM 3444t1 AS alias1 INNER JOIN t1 AS alias2 3445ON alias2.a = alias1.b 3446WHERE alias1.b IN ( 3447SELECT a FROM t1, t2 3448); 3449a b 34501 1 34512 2 34523 3 34534 4 34545 5 34556 6 34567 7 3457DROP table t1, t2; 3458set @@optimizer_switch= @os_912513; 3459set @@join_cache_level= @jcl_912513; 3460# End 3461# 3462# BUG#934342: outer join + semijoin materialization 3463# + join_cache_level > 2 3464# 3465CREATE TABLE t1 (a varchar(1), b varchar(1), INDEX idx_a(a) ); 3466INSERT INTO t1 VALUES ('v','v'), ('w','w'), ('t','t'); 3467CREATE TABLE t2 (c varchar(1), INDEX idx_c(c) ); 3468INSERT INTO t2 VALUES ('v'), ('v'), ('s'), ('j'); 3469CREATE TABLE t3 (c varchar(1), d varchar(1), INDEX idx_c(c) ); 3470INSERT INTO t3 VALUES ('v','v'), ('v','v'), ('s','s'), ('j','j'); 3471INSERT INTO t3 VALUES ('m','m'), ('d','d'), ('k','k'), ('m','m'); 3472set @tmp_otimizer_switch= @@optimizer_switch; 3473set @tmp_join_cache_level=@@join_cache_level; 3474set optimizer_switch = 'materialization=on,semijoin=on,join_cache_hashed=on'; 3475set join_cache_level=0; 3476EXPLAIN 3477SELECT * FROM t1 LEFT JOIN t2 ON (c = b) 3478WHERE (a, b) IN (SELECT a, b FROM t1 t); 3479id select_type table type possible_keys key key_len ref rows Extra 34801 PRIMARY t1 ALL idx_a NULL NULL NULL 3 34811 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 34821 PRIMARY t2 ref idx_c idx_c 4 test.t1.b 2 Using where; Using index 34832 MATERIALIZED t ALL idx_a NULL NULL NULL 3 3484SELECT * FROM t1 LEFT JOIN t2 ON (c = b) 3485WHERE (a, b) IN (SELECT a, b FROM t1 t); 3486a b c 3487v v v 3488v v v 3489w w NULL 3490t t NULL 3491EXPLAIN 3492SELECT * FROM t1 LEFT JOIN t3 ON (c = b) 3493WHERE (a, b) IN (SELECT a, b FROM t1 t); 3494id select_type table type possible_keys key key_len ref rows Extra 34951 PRIMARY t1 ALL idx_a NULL NULL NULL 3 34961 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 34971 PRIMARY t3 ref idx_c idx_c 4 test.t1.b 2 Using where 34982 MATERIALIZED t ALL idx_a NULL NULL NULL 3 3499SELECT * FROM t1 LEFT JOIN t3 ON (c = b) 3500WHERE (a, b) IN (SELECT a, b FROM t1 t); 3501a b c d 3502v v v v 3503v v v v 3504w w NULL NULL 3505t t NULL NULL 3506set join_cache_level=6; 3507EXPLAIN 3508SELECT * FROM t1 LEFT JOIN t2 ON (c = b) 3509WHERE (a, b) IN (SELECT a, b FROM t1 t); 3510id select_type table type possible_keys key key_len ref rows Extra 35111 PRIMARY t1 ALL idx_a NULL NULL NULL 3 35121 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 35131 PRIMARY t2 ref idx_c idx_c 4 test.t1.b 2 Using where; Using index 35142 MATERIALIZED t ALL idx_a NULL NULL NULL 3 3515SELECT * FROM t1 LEFT JOIN t2 ON (c = b) 3516WHERE (a, b) IN (SELECT a, b FROM t1 t); 3517a b c 3518v v v 3519v v v 3520w w NULL 3521t t NULL 3522EXPLAIN 3523SELECT * FROM t1 LEFT JOIN t3 ON (c = b) 3524WHERE (a, b) IN (SELECT a, b FROM t1 t); 3525id select_type table type possible_keys key key_len ref rows Extra 35261 PRIMARY t1 ALL idx_a NULL NULL NULL 3 35271 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 35281 PRIMARY t3 ref idx_c idx_c 4 test.t1.b 2 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan 35292 MATERIALIZED t ALL idx_a NULL NULL NULL 3 3530SELECT * FROM t1 LEFT JOIN t3 ON (c = b) 3531WHERE (a, b) IN (SELECT a, b FROM t1 t); 3532a b c d 3533v v v v 3534v v v v 3535w w NULL NULL 3536t t NULL NULL 3537set optimizer_switch=@tmp_optimizer_switch; 3538set join_cache_level=@tmp_join_cache_level; 3539DROP TABLE t1,t2,t3; 3540# End 3541# 3542# BUG#934348: GROUP BY with HAVING + semijoin materialization 3543# + join_cache_level > 2 3544# 3545CREATE TABLE t1 (a varchar(1), INDEX idx_a(a)); 3546INSERT INTO t1 VALUES ('c'), ('v'), ('c'); 3547CREATE TABLE t2 (b varchar(1)); 3548INSERT INTO t2 VALUES ('v'), ('c'); 3549set @tmp_otimizer_switch= @@optimizer_switch; 3550set @tmp_join_cache_level=@@join_cache_level; 3551set optimizer_switch = 'materialization=on,semijoin=on,join_cache_hashed=on'; 3552set join_cache_level=0; 3553EXPLAIN 3554SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) 3555GROUP BY a HAVING a != 'z'; 3556id select_type table type possible_keys key key_len ref rows Extra 35571 PRIMARY t range idx_a idx_a 4 NULL 3 Using where; Using index 35581 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 35592 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where 35602 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index 3561SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) 3562GROUP BY a HAVING a != 'z'; 3563a 3564c 3565v 3566set join_cache_level=6; 3567EXPLAIN 3568SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) 3569GROUP BY a HAVING a != 'z'; 3570id select_type table type possible_keys key key_len ref rows Extra 35711 PRIMARY t range idx_a idx_a 4 NULL 3 Using where; Using index 35721 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 35732 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where 35742 MATERIALIZED t1 ref idx_a idx_a 4 test.t2.b 2 Using index 3575SELECT a FROM t1 t WHERE a IN (SELECT b FROM t1, t2 WHERE b = a) 3576GROUP BY a HAVING a != 'z'; 3577a 3578c 3579v 3580set optimizer_switch=@tmp_optimizer_switch; 3581set join_cache_level=@tmp_join_cache_level; 3582DROP TABLE t1,t2; 3583# End 3584set join_cache_level=@save_join_cache_level; 3585set @@optimizer_switch=@save_optimizer_switch_jcl6; 3586set @optimizer_switch_for_subselect_sj_test=NULL; 3587set @join_cache_level_subselect_sj_test=NULL; 3588