1# For explain 2set @@join_buffer_size=256*1024; 3set @save_optimizer_switch=@@optimizer_switch; 4 5create table t1 (a int, b int); 6create table t2 (c int, d int); 7insert into t1 values (1,1),(2,2); 8insert into t2 values (2,2),(3,3); 9 10(select a,b from t1) intersect (select c,d from t2); 11 12drop tables t1,t2; 13 14 15create table t1 (a int, b int) engine=MyISAM; 16create table t2 (c int, d int) engine=MyISAM; 17create table t3 (e int, f int) engine=MyISAM; 18insert into t1 values (1,1),(2,2),(3,3); 19insert into t2 values (2,2),(3,3),(4,4); 20insert into t3 values (1,1),(2,2),(5,5); 21 22(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 23EXPLAIN (select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 24EXPLAIN extended (select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 25EXPLAIN extended select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a; 26EXPLAIN format=json (select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 27--replace_regex /"r_total_time_ms": [0-9e\.\-+]*,/"r_total_time_ms": "REPLACED",/ 28ANALYZE format=json (select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 29--replace_regex /"r_total_time_ms": [0-9e\.\-+]*,/"r_total_time_ms": "REPLACED",/ 30ANALYZE format=json select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a; 31select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a; 32 33prepare stmt from "(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3);"; 34execute stmt; 35execute stmt; 36 37prepare stmt from "select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a"; 38execute stmt; 39execute stmt; 40 41(select a,b from t1) intersect (select c,e from t2,t3); 42EXPLAIN (select a,b from t1) intersect (select c,e from t2,t3); 43EXPLAIN extended (select a,b from t1) intersect (select c,e from t2,t3); 44EXPLAIN extended select * from ((select a,b from t1) intersect (select c,e from t2,t3)) a; 45set @@optimizer_switch='optimize_join_buffer_size=off'; 46EXPLAIN format=json (select a,b from t1) intersect (select c,e from t2,t3); 47--replace_regex /"r_total_time_ms": [0-9e\.\-+]*,/"r_total_time_ms": "REPLACED",/ 48ANALYZE format=json (select a,b from t1) intersect (select c,e from t2,t3); 49--replace_regex /"r_total_time_ms": [0-9e\.\-+]*,/"r_total_time_ms": "REPLACED",/ 50ANALYZE format=json select * from ((select a,b from t1) intersect (select c,e from t2,t3)) a; 51set @@optimizer_switch=@save_optimizer_switch; 52select * from ((select a,b from t1) intersect (select c,e from t2,t3)) a; 53 54prepare stmt from "(select a,b from t1) intersect (select c,e from t2,t3);"; 55execute stmt; 56execute stmt; 57 58prepare stmt from "select * from ((select a,b from t1) intersect (select c,e from t2,t3)) a"; 59execute stmt; 60execute stmt; 61 62 63drop tables t1,t2,t3; 64 65 66select 1 as a from dual intersect select 1 from dual; 67(select 1 from dual) intersect (select 1 from dual); 68--error ER_WRONG_USAGE 69(select 1 from dual into @v) intersect (select 1 from dual); 70--error ER_PARSE_ERROR 71select 1 from dual ORDER BY 1 intersect select 1 from dual; 72 73select 1 as a from dual union all select 1 from dual; 74--error ER_WRONG_USAGE 75select 1 from dual intersect all select 1 from dual; 76 77 78 79create table t1 (a int, b blob, a1 int, b1 blob); 80create table t2 (c int, d blob, c1 int, d1 blob); 81insert into t1 values (1,"ddd", 1, "sdfrrwwww"),(2, "fgh", 2, "dffggtt"); 82insert into t2 values (2, "fgh", 2, "dffggtt"),(3, "ffggddd", 3, "dfgg"); 83 84(select a,b,b1 from t1) intersect (select c,d,d1 from t2); 85 86drop tables t1,t2; 87 88create table t1 (a int, b blob) engine=MyISAM; 89create table t2 (c int, d blob) engine=MyISAM; 90create table t3 (e int, f blob) engine=MyISAM; 91insert into t1 values (1,1),(2,2),(3,3); 92insert into t2 values (2,2),(3,3),(4,4); 93insert into t3 values (1,1),(2,2),(5,5); 94 95(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 96select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a; 97 98prepare stmt from "(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3);"; 99execute stmt; 100execute stmt; 101 102prepare stmt from "select * from ((select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3)) a"; 103execute stmt; 104execute stmt; 105 106# make sure that blob is used 107create table t4 (select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3); 108show create table t4; 109 110drop tables t1,t2,t3,t4; 111 112create table t1 (a int, b blob) engine=MyISAM; 113create table t2 (c int, d blob) engine=MyISAM; 114create table t3 (e int, f blob) engine=MyISAM; 115insert into t1 values (1,1),(2,2),(3,3); 116insert into t2 values (2,2),(3,3); 117insert into t3 values (1,1),(2,2),(3,3); 118 119 120--sorted_result 121(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 122--sorted_result 123(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3) union (select 4,4) except (select 2,2); 124 125drop tables t1,t2,t3; 126 127create table t1 (a int, b int); 128create table t2 (c int, d int); 129create table t3 (e int, f int); 130insert into t1 values (1,1),(2,2),(3,3); 131insert into t2 values (2,2),(3,3); 132insert into t3 values (1,1),(2,2),(3,3); 133 134--sorted_result 135(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 136--sorted_result 137(select a,b from t1) intersect (select c,d from t2) intersect (select e,f from t3) union (select 4,4) except (select 2,2); 138 139drop tables t1,t2,t3; 140 141 142--echo # 143--echo # INTERSECT precedence 144--echo # 145create table t1 (a int, b blob) engine=MyISAM; 146create table t2 (c int, d blob) engine=MyISAM; 147create table t3 (e int, f blob) engine=MyISAM; 148insert into t1 values (5,5),(6,6); 149insert into t2 values (2,2),(3,3); 150insert into t3 values (1,1),(3,3); 151 152--sorted_result 153(select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 154explain extended 155(select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 156set SQL_MODE=ORACLE; 157--sorted_result 158(select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 159explain extended 160(select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 161set SQL_MODE=default; 162 163 164# test result of linear mix operation 165--sorted_result 166(select e,f from t3) intersect (select c,d from t2) union (select a,b from t1) union (select 4,4); 167explain extended 168(select e,f from t3) intersect (select c,d from t2) union (select a,b from t1) union (select 4,4); 169set SQL_MODE=ORACLE; 170--sorted_result 171(select e,f from t3) intersect (select c,d from t2) union (select a,b from t1) union (select 4,4); 172explain extended 173(select e,f from t3) intersect (select c,d from t2) union (select a,b from t1) union (select 4,4); 174set SQL_MODE=default; 175 176--sorted_result 177(/* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1`) union /* select#3 */ select `__3`.`c` AS `c`,`__3`.`d` AS `d` from ((/* select#2 */ select `test`.`t2`.`c` AS `c`,`test`.`t2`.`d` AS `d` from `test`.`t2`) intersect (/* select#4 */ select `test`.`t3`.`e` AS `e`,`test`.`t3`.`f` AS `f` from `test`.`t3`)) `__3` union (/* select#5 */ select 4 AS `4`,4 AS `4`); 178 179--sorted_result 180prepare stmt from "(select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4)"; 181execute stmt; 182execute stmt; 183 184create view v1 as (select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 185 186--sorted_result 187select b,a,b+1 from v1; 188--sorted_result 189select b,a,b+1 from v1 where a > 3; 190 191create procedure p1() 192 select * from v1; 193--sorted_result 194call p1(); 195--sorted_result 196call p1(); 197drop procedure p1; 198 199create procedure p1() 200 (select a,b from t1) union (select c,d from t2) intersect (select e,f from t3) union (select 4,4); 201--sorted_result 202call p1(); 203--sorted_result 204call p1(); 205drop procedure p1; 206 207show create view v1; 208 209drop view v1; 210drop tables t1,t2,t3; 211 212--echo # 213--echo # MDEV-14346:incorrect result of intersect with ANY/ALL/IN subquery 214--echo # 215CREATE TABLE t (i INT); 216INSERT INTO t VALUES (1),(2); 217SELECT * FROM t WHERE i != ANY ( SELECT 6 INTERSECT SELECT 3 ); 218 219select i from t where 220 exists ((select 6 as r from dual having t.i <> 6) 221 intersect 222 (select 3 from dual having t.i <> 3)); 223 224drop table t; 225 226 227--echo # 228--echo # MDEV-13749: Server crashes in _ma_unique_hash / 229--echo # JOIN_CACHE::generate_full_extensions on INTERSECT 230--echo # 231 232CREATE TABLE t1 (a varchar(32)) ENGINE=MyISAM; 233INSERT INTO t1 VALUES 234('Jakarta'),('Lisbon'),('Honolulu'),('Lusaka'),('Barcelona'),('Taipei'), 235('Brussels'),('Orlando'),('Osaka'),('Quito'),('Lima'),('Tunis'), 236('Unalaska'),('Rotterdam'),('Zagreb'),('Ufa'),('Ryazan'),('Xiamen'), 237('London'),('Izmir'),('Samara'),('Bern'),('Zhengzhou'),('Vladivostok'), 238('Yangon'),('Victoria'),('Warsaw'),('Luanda'),('Leon'),('Bangkok'), 239('Wellington'),('Zibo'),('Qiqihar'),('Delhi'),('Hamburg'),('Ottawa'), 240('Vaduz'); 241 242CREATE TABLE t2 (b varchar(32)) ENGINE=MyISAM; 243INSERT INTO t2 VALUES 244('Gaza'),('Jeddah'),('Beirut'),('Incheon'),('Tbilisi'),('Izmir'), 245('Quito'),('Riga'),('Freetown'),('Zagreb'),('Caracas'),('Orlando'), 246('Kingston'),('Turin'),('Xinyang'),('Osaka'),('Albany'),('Geneva'), 247('Omsk'),('Kazan'),('Quezon'),('Indore'),('Odessa'),('Xiamen'), 248('Winnipeg'),('Yakutsk'),('Nairobi'),('Ufa'),('Helsinki'),('Vilnius'), 249('Aden'),('Liverpool'),('Honolulu'),('Frankfurt'),('Glasgow'), 250('Vienna'),('Jackson'),('Jakarta'),('Sydney'),('Oslo'),('Novgorod'), 251('Norilsk'),('Izhevsk'),('Istanbul'),('Nice'); 252 253CREATE TABLE t3 (c varchar(32)) ENGINE=MyISAM; 254INSERT INTO t3 VALUES 255('Nicosia'),('Istanbul'),('Richmond'),('Stockholm'),('Dublin'), 256('Wichita'),('Warsaw'),('Glasgow'),('Winnipeg'),('Irkutsk'),('Quito'), 257('Xiamen'),('Berlin'),('Rome'),('Denver'),('Dallas'),('Kabul'), 258('Prague'),('Izhevsk'),('Tirana'),('Sofia'),('Detroit'),('Sorbonne'); 259 260select count(*) from ( 261 SELECT * FROM t1 LEFT OUTER JOIN t2 LEFT OUTER JOIN t3 ON b < c ON a > b 262 INTERSECT 263 SELECT * FROM t1 LEFT OUTER JOIN t2 LEFT OUTER JOIN t3 ON b < c ON a > b 264) a; 265 266drop table t1,t2,t3; 267 268CREATE TABLE t1 (a varchar(32) not null) ENGINE=MyISAM; 269INSERT INTO t1 VALUES 270('Jakarta'),('Lisbon'),('Honolulu'),('Lusaka'),('Barcelona'),('Taipei'), 271('Brussels'),('Orlando'),('Osaka'),('Quito'),('Lima'),('Tunis'), 272('Unalaska'),('Rotterdam'),('Zagreb'),('Ufa'),('Ryazan'),('Xiamen'), 273('London'),('Izmir'),('Samara'),('Bern'),('Zhengzhou'),('Vladivostok'), 274('Yangon'),('Victoria'),('Warsaw'),('Luanda'),('Leon'),('Bangkok'), 275('Wellington'),('Zibo'),('Qiqihar'),('Delhi'),('Hamburg'),('Ottawa'), 276('Vaduz'); 277 278CREATE TABLE t2 (b varchar(32) not null) ENGINE=MyISAM; 279INSERT INTO t2 VALUES 280('Gaza'),('Jeddah'),('Beirut'),('Incheon'),('Tbilisi'),('Izmir'), 281('Quito'),('Riga'),('Freetown'),('Zagreb'),('Caracas'),('Orlando'), 282('Kingston'),('Turin'),('Xinyang'),('Osaka'),('Albany'),('Geneva'), 283('Omsk'),('Kazan'),('Quezon'),('Indore'),('Odessa'),('Xiamen'), 284('Winnipeg'),('Yakutsk'),('Nairobi'),('Ufa'),('Helsinki'),('Vilnius'), 285('Aden'),('Liverpool'),('Honolulu'),('Frankfurt'),('Glasgow'), 286('Vienna'),('Jackson'),('Jakarta'),('Sydney'),('Oslo'),('Novgorod'), 287('Norilsk'),('Izhevsk'),('Istanbul'),('Nice'); 288 289CREATE TABLE t3 (c varchar(32) not null) ENGINE=MyISAM; 290INSERT INTO t3 VALUES 291('Nicosia'),('Istanbul'),('Richmond'),('Stockholm'),('Dublin'), 292('Wichita'),('Warsaw'),('Glasgow'),('Winnipeg'),('Irkutsk'),('Quito'), 293('Xiamen'),('Berlin'),('Rome'),('Denver'),('Dallas'),('Kabul'), 294('Prague'),('Izhevsk'),('Tirana'),('Sofia'),('Detroit'),('Sorbonne'); 295 296select count(*) from ( 297 SELECT * FROM t1 LEFT OUTER JOIN t2 LEFT OUTER JOIN t3 ON b < c ON a > b 298 INTERSECT 299 SELECT * FROM t1 LEFT OUTER JOIN t2 LEFT OUTER JOIN t3 ON b < c ON a > b 300) a; 301 302drop table t1,t2,t3; 303 304--echo # 305--echo # MDEV-13695: INTERSECT precedence is not in line with Oracle even 306--echo # in SQL_MODE=Oracle 307--echo # 308 309create table t12(c1 int); 310insert into t12 values(1); 311insert into t12 values(2); 312create table t13(c1 int); 313insert into t13 values(1); 314insert into t13 values(3); 315create table t234(c1 int); 316insert into t234 values(2); 317insert into t234 values(3); 318insert into t234 values(4); 319 320set SQL_MODE=oracle; 321--sorted_result 322select * from t13 union select * from t234 intersect select * from t12; 323set SQL_MODE=default; 324--sorted_result 325select * from t13 union select * from t234 intersect select * from t12; 326 327drop table t12,t13,t234; 328 329--echo # End of 10.3 tests 330