1--source include/long_test.inc
2
3#
4# test of left outer join
5#
6
7--disable_warnings
8drop table if exists t0,t1,t2,t3,t4,t5;
9--enable_warnings
10
11SET @org_optimizer_switch=@@optimizer_switch;
12SET optimizer_switch=ifnull(@optimizer_switch_for_join_outer_test,'outer_join_with_cache=off');
13if (`select @join_cache_level_for_join_outer_test is null`)
14{
15  set join_cache_level=1;
16}
17if (`select @join_cache_level_for_join_outer_test is not null`)
18{
19  set join_cache_level=@join_cache_level_for_join_outer_test;
20}
21
22CREATE TABLE t1 (
23  grp int(11) default NULL,
24  a bigint(20) unsigned default NULL,
25  c char(10) NOT NULL default ''
26) ENGINE=MyISAM;
27INSERT INTO t1 VALUES (1,1,'a'),(2,2,'b'),(2,3,'c'),(3,4,'E'),(3,5,'C'),(3,6,'D'),(NULL,NULL,'');
28create table t2 (id int, a bigint unsigned not null, c char(10), d int, primary key (a));
29insert into t2 values (1,1,"a",1),(3,4,"A",4),(3,5,"B",5),(3,6,"C",6),(4,7,"D",7);
30
31select t1.*,t2.* from t1 JOIN t2 where t1.a=t2.a;
32select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) order by t1.grp,t1.a,t2.c;
33select t1.*,t2.* from { oj t2 left outer join t1 on (t1.a=t2.a) };
34select t1.*,t2.* from t1 as t0,{ oj t2 left outer join t1 on (t1.a=t2.a) } WHERE t0.a=2;
35--sorted_result
36select t1.*,t2.* from t1 left join t2 using (a);
37select t1.*,t2.* from t1 left join t2 using (a) where t1.a=t2.a;
38select t1.*,t2.* from t1 left join t2 using (a,c);
39--sorted_result
40select t1.*,t2.* from t1 left join t2 using (c);
41select t1.*,t2.* from t1 natural left outer join t2;
42
43select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id=3;
44select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id is null;
45
46explain select t1.*,t2.* from t1,t2 where t1.a=t2.a and isnull(t2.a)=1;
47explain select t1.*,t2.* from t1 left join t2 on t1.a=t2.a where isnull(t2.a)=1;
48
49--sorted_result
50select t1.*,t2.*,t3.a from t1 left join t2 on (t1.a=t2.a) left join t1 as t3 on (t2.a=t3.a);
51
52# The next query should rearange the left joins to get this to work
53--error 1054
54explain select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a);
55--error 1054
56select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a);
57
58# The next query should give an error in MySQL
59--error 1054
60select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t2.a=t3.a);
61
62# Test of inner join
63select t1.*,t2.* from t1 inner join t2 using (a);
64select t1.*,t2.* from t1 inner join t2 on (t1.a=t2.a);
65select t1.*,t2.* from t1 natural join t2;
66
67drop table t1,t2;
68
69#
70# Test of left join bug
71#
72
73CREATE TABLE t1 (
74 usr_id INT unsigned NOT NULL,
75 uniq_id INT unsigned NOT NULL AUTO_INCREMENT,
76        start_num INT unsigned NOT NULL DEFAULT 1,
77        increment INT unsigned NOT NULL DEFAULT 1,
78 PRIMARY KEY (uniq_id),
79 INDEX usr_uniq_idx (usr_id, uniq_id),
80 INDEX uniq_usr_idx (uniq_id, usr_id)
81);
82CREATE TABLE t2 (
83 id INT unsigned NOT NULL DEFAULT 0,
84 usr2_id INT unsigned NOT NULL DEFAULT 0,
85 max INT unsigned NOT NULL DEFAULT 0,
86 c_amount INT unsigned NOT NULL DEFAULT 0,
87 d_max INT unsigned NOT NULL DEFAULT 0,
88 d_num INT unsigned NOT NULL DEFAULT 0,
89 orig_time INT unsigned NOT NULL DEFAULT 0,
90 c_time INT unsigned NOT NULL DEFAULT 0,
91 active ENUM ("no","yes") NOT NULL,
92 PRIMARY KEY (id,usr2_id),
93 INDEX id_idx (id),
94 INDEX usr2_idx (usr2_id)
95);
96INSERT INTO t1 VALUES (3,NULL,0,50),(3,NULL,0,200),(3,NULL,0,25),(3,NULL,0,84676),(3,NULL,0,235),(3,NULL,0,10),(3,NULL,0,3098),(3,NULL,0,2947),(3,NULL,0,8987),(3,NULL,0,8347654),(3,NULL,0,20398),(3,NULL,0,8976),(3,NULL,0,500),(3,NULL,0,198);
97
98#1st select shows that one record is returned with null entries for the right
99#table, when selecting on an id that does not exist in the right table t2
100SELECT t1.usr_id,t1.uniq_id,t1.increment,
101t2.usr2_id,t2.c_amount,t2.max
102FROM t1
103LEFT JOIN t2 ON t2.id = t1.uniq_id
104WHERE t1.uniq_id = 4
105ORDER BY t2.c_amount;
106
107# The same with RIGHT JOIN
108SELECT t1.usr_id,t1.uniq_id,t1.increment,
109t2.usr2_id,t2.c_amount,t2.max
110FROM t2
111RIGHT JOIN t1 ON t2.id = t1.uniq_id
112WHERE t1.uniq_id = 4
113ORDER BY t2.c_amount;
114
115INSERT INTO t2 VALUES (2,3,3000,6000,0,0,746584,837484,'yes');
116--error ER_DUP_ENTRY
117INSERT INTO t2 VALUES (2,3,3000,6000,0,0,746584,837484,'yes');
118INSERT INTO t2 VALUES (7,3,1000,2000,0,0,746294,937484,'yes');
119
120#3rd select should show that one record is returned with null entries for the
121# right table, when selecting on an id that does not exist in the right table
122# t2 but this select returns an empty set!!!!
123SELECT t1.usr_id,t1.uniq_id,t1.increment,t2.usr2_id,t2.c_amount,t2.max FROM t1 LEFT JOIN t2 ON t2.id = t1.uniq_id WHERE t1.uniq_id = 4 ORDER BY t2.c_amount;
124SELECT t1.usr_id,t1.uniq_id,t1.increment,t2.usr2_id,t2.c_amount,t2.max FROM t1 LEFT JOIN t2 ON t2.id = t1.uniq_id WHERE t1.uniq_id = 4 GROUP BY t2.c_amount;
125# Removing the ORDER BY works:
126SELECT t1.usr_id,t1.uniq_id,t1.increment,t2.usr2_id,t2.c_amount,t2.max FROM t1 LEFT JOIN t2 ON t2.id = t1.uniq_id WHERE t1.uniq_id = 4;
127
128drop table t1,t2;
129
130#
131# Test of LEFT JOIN with const tables (failed for frankie@etsetb.upc.es)
132#
133
134CREATE TABLE t1 (
135  cod_asig int(11) DEFAULT '0' NOT NULL,
136  desc_larga_cat varchar(80) DEFAULT '' NOT NULL,
137  desc_larga_cas varchar(80) DEFAULT '' NOT NULL,
138  desc_corta_cat varchar(40) DEFAULT '' NOT NULL,
139  desc_corta_cas varchar(40) DEFAULT '' NOT NULL,
140  cred_total double(3,1) DEFAULT '0.0' NOT NULL,
141  pre_requisit int(11),
142  co_requisit int(11),
143  preco_requisit int(11),
144  PRIMARY KEY (cod_asig)
145);
146
147INSERT INTO t1 VALUES (10360,'asdfggfg','Introduccion a los  Ordenadores I','asdfggfg','Introduccio Ordinadors I',6.0,NULL,NULL,NULL);
148INSERT INTO t1 VALUES (10361,'Components i Circuits Electronics I','Componentes y Circuitos Electronicos I','Components i Circuits Electronics I','Comp. i Circ. Electr. I',6.0,NULL,NULL,NULL);
149INSERT INTO t1 VALUES (10362,'Laboratori d`Ordinadors','Laboratorio de Ordenadores','Laboratori d`Ordinadors','Laboratori Ordinadors',4.5,NULL,NULL,NULL);
150INSERT INTO t1 VALUES (10363,'Tecniques de Comunicacio Oral i Escrita','Tecnicas de Comunicacion Oral y Escrita','Tecniques de Comunicacio Oral i Escrita','Tec. Com. Oral i Escrita',4.5,NULL,NULL,NULL);
151INSERT INTO t1 VALUES (11403,'Projecte Fi de Carrera','Proyecto Fin de Carrera','Projecte Fi de Carrera','PFC',9.0,NULL,NULL,NULL);
152INSERT INTO t1 VALUES (11404,'+lgebra lineal','Algebra lineal','+lgebra lineal','+lgebra lineal',15.0,NULL,NULL,NULL);
153INSERT INTO t1 VALUES (11405,'+lgebra lineal','Algebra lineal','+lgebra lineal','+lgebra lineal',18.0,NULL,NULL,NULL);
154INSERT INTO t1 VALUES (11406,'Calcul Infinitesimal','Cßlculo Infinitesimal','Calcul Infinitesimal','Calcul Infinitesimal',15.0,NULL,NULL,NULL);
155
156CREATE TABLE t2 (
157  idAssignatura int(11) DEFAULT '0' NOT NULL,
158  Grup int(11) DEFAULT '0' NOT NULL,
159  Places smallint(6) DEFAULT '0' NOT NULL,
160  PlacesOcupades int(11) DEFAULT '0',
161  PRIMARY KEY (idAssignatura,Grup)
162);
163
164
165INSERT INTO t2 VALUES (10360,12,333,0);
166INSERT INTO t2 VALUES (10361,30,2,0);
167INSERT INTO t2 VALUES (10361,40,3,0);
168INSERT INTO t2 VALUES (10360,45,10,0);
169INSERT INTO t2 VALUES (10362,10,12,0);
170INSERT INTO t2 VALUES (10360,55,2,0);
171INSERT INTO t2 VALUES (10360,70,0,0);
172INSERT INTO t2 VALUES (10360,565656,0,0);
173INSERT INTO t2 VALUES (10360,32767,7,0);
174INSERT INTO t2 VALUES (10360,33,8,0);
175INSERT INTO t2 VALUES (10360,7887,85,0);
176INSERT INTO t2 VALUES (11405,88,8,0);
177INSERT INTO t2 VALUES (10360,0,55,0);
178INSERT INTO t2 VALUES (10360,99,0,0);
179INSERT INTO t2 VALUES (11411,30,10,0);
180INSERT INTO t2 VALUES (11404,0,0,0);
181INSERT INTO t2 VALUES (10362,11,111,0);
182INSERT INTO t2 VALUES (10363,33,333,0);
183INSERT INTO t2 VALUES (11412,55,0,0);
184INSERT INTO t2 VALUES (50003,66,6,0);
185INSERT INTO t2 VALUES (11403,5,0,0);
186INSERT INTO t2 VALUES (11406,11,11,0);
187INSERT INTO t2 VALUES (11410,11410,131,0);
188INSERT INTO t2 VALUES (11416,11416,32767,0);
189INSERT INTO t2 VALUES (11409,0,0,0);
190
191CREATE TABLE t3 (
192  id int(11) NOT NULL auto_increment,
193  dni_pasaporte char(16) DEFAULT '' NOT NULL,
194  idPla int(11) DEFAULT '0' NOT NULL,
195  cod_asig int(11) DEFAULT '0' NOT NULL,
196  any smallint(6) DEFAULT '0' NOT NULL,
197  quatrimestre smallint(6) DEFAULT '0' NOT NULL,
198  estat char(1) DEFAULT 'M' NOT NULL,
199  PRIMARY KEY (id),
200  UNIQUE dni_pasaporte (dni_pasaporte,idPla),
201  UNIQUE dni_pasaporte_2 (dni_pasaporte,idPla,cod_asig,any,quatrimestre)
202);
203
204INSERT INTO t3 VALUES (1,'11111111',1,10362,98,1,'M');
205
206CREATE TABLE t4 (
207  id int(11) NOT NULL auto_increment,
208  papa int(11) DEFAULT '0' NOT NULL,
209  fill int(11) DEFAULT '0' NOT NULL,
210  idPla int(11) DEFAULT '0' NOT NULL,
211  PRIMARY KEY (id),
212  KEY papa (idPla,papa),
213  UNIQUE papa_2 (idPla,papa,fill)
214);
215
216INSERT INTO t4 VALUES (1,-1,10360,1);
217INSERT INTO t4 VALUES (2,-1,10361,1);
218INSERT INTO t4 VALUES (3,-1,10362,1);
219
220--sorted_result
221SELECT DISTINCT fill,desc_larga_cat,cred_total,Grup,Places,PlacesOcupades FROM t4 LEFT JOIN t3 ON t3.cod_asig=fill AND estat='S'   AND dni_pasaporte='11111111'   AND t3.idPla=1 , t2,t1 WHERE fill=t1.cod_asig   AND Places>PlacesOcupades   AND fill=idAssignatura   AND t4.idPla=1   AND papa=-1;
222
223SELECT DISTINCT fill,t3.idPla FROM t4 LEFT JOIN t3 ON t3.cod_asig=t4.fill AND t3.estat='S' AND t3.dni_pasaporte='1234' AND t3.idPla=1 ;
224
225INSERT INTO t3 VALUES (3,'1234',1,10360,98,1,'S');
226SELECT DISTINCT fill,t3.idPla FROM t4 LEFT JOIN t3 ON t3.cod_asig=t4.fill AND t3.estat='S' AND t3.dni_pasaporte='1234' AND t3.idPla=1 ;
227
228drop table t1,t2,t3,test.t4;
229
230#
231# Test of IS NULL on AUTO_INCREMENT with LEFT JOIN
232#
233
234CREATE TABLE t1 (
235  id smallint(5) unsigned NOT NULL auto_increment,
236  name char(60) DEFAULT '' NOT NULL,
237  PRIMARY KEY (id)
238);
239INSERT INTO t1 VALUES (1,'Antonio Paz');
240INSERT INTO t1 VALUES (2,'Lilliana Angelovska');
241INSERT INTO t1 VALUES (3,'Thimble Smith');
242
243CREATE TABLE t2 (
244  id smallint(5) unsigned NOT NULL auto_increment,
245  owner smallint(5) unsigned DEFAULT '0' NOT NULL,
246  name char(60),
247  PRIMARY KEY (id)
248);
249INSERT INTO t2 VALUES (1,1,'El Gato');
250INSERT INTO t2 VALUES (2,1,'Perrito');
251INSERT INTO t2 VALUES (3,3,'Happy');
252
253--sorted_result
254select t1.name, t2.name, t2.id from t1 left join t2 on (t1.id = t2.owner);
255select t1.name, t2.name, t2.id from t1 left join t2 on (t1.id = t2.owner) where t2.id is null;
256explain select t1.name, t2.name, t2.id from t1 left join t2 on (t1.id = t2.owner) where t2.id is null;
257explain select t1.name, t2.name, t2.id from t1 left join t2 on (t1.id = t2.owner) where t2.name is null;
258select count(*) from t1 left join t2 on (t1.id = t2.owner);
259
260--sorted_result
261select t1.name, t2.name, t2.id from t2 right join t1 on (t1.id = t2.owner);
262select t1.name, t2.name, t2.id from t2 right join t1 on (t1.id = t2.owner) where t2.id is null;
263explain select t1.name, t2.name, t2.id from t2 right join t1 on (t1.id = t2.owner) where t2.id is null;
264explain select t1.name, t2.name, t2.id from t2 right join t1 on (t1.id = t2.owner) where t2.name is null;
265select count(*) from t2 right join t1 on (t1.id = t2.owner);
266
267--sorted_result
268select t1.name, t2.name, t2.id,t3.id from t2 right join t1 on (t1.id = t2.owner) left join t1 as t3 on t3.id=t2.owner;
269--sorted_result
270select t1.name, t2.name, t2.id,t3.id from t1 right join t2 on (t1.id = t2.owner) right join t1 as t3 on t3.id=t2.owner;
271--sorted_result
272select t1.name, t2.name, t2.id, t2.owner, t3.id from t1 left join t2 on (t1.id = t2.owner) right join t1 as t3 on t3.id=t2.owner;
273
274drop table t1,t2;
275
276create table t1 (id int not null, str char(10), index(str));
277insert into t1 values (1, null), (2, null), (3, "foo"), (4, "bar");
278select * from t1 where str is not null order by id;
279select * from t1 where str is null;
280drop table t1;
281
282#
283# Test wrong LEFT JOIN query
284#
285
286CREATE TABLE t1 (
287  t1_id bigint(21) NOT NULL auto_increment,
288  PRIMARY KEY (t1_id)
289);
290CREATE TABLE t2 (
291  t2_id bigint(21) NOT NULL auto_increment,
292  PRIMARY KEY (t2_id)
293);
294CREATE TABLE t3 (
295  t3_id bigint(21) NOT NULL auto_increment,
296  PRIMARY KEY (t3_id)
297);
298CREATE TABLE t4 (
299  seq_0_id bigint(21) DEFAULT '0' NOT NULL,
300  seq_1_id bigint(21) DEFAULT '0' NOT NULL,
301  KEY seq_0_id (seq_0_id),
302  KEY seq_1_id (seq_1_id)
303);
304CREATE TABLE t5 (
305  seq_0_id bigint(21) DEFAULT '0' NOT NULL,
306  seq_1_id bigint(21) DEFAULT '0' NOT NULL,
307  KEY seq_1_id (seq_1_id),
308  KEY seq_0_id (seq_0_id)
309);
310
311insert into t1 values (1);
312insert into t2 values (1);
313insert into t3 values (1);
314insert into t4 values (1,1);
315insert into t5 values (1,1);
316
317--error 1054
318explain select * from t3 left join t4 on t4.seq_1_id = t2.t2_id left join t1 on t1.t1_id = t4.seq_0_id left join t5 on t5.seq_0_id = t1.t1_id left join t2 on t2.t2_id = t5.seq_1_id where t3.t3_id = 23;
319
320drop table t1,t2,t3,t4,t5;
321
322#
323# Another LEFT JOIN problem
324# (The problem was that the result changed when we added ORDER BY)
325#
326
327create table t1 (n int, m int, o int, key(n));
328create table t2 (n int not null, m int, o int, primary key(n));
329insert into t1 values (1, 2, 11), (1, 2, 7), (2, 2, 8), (1,2,9),(1,3,9);
330insert into t2 values (1, 2, 3),(2, 2, 8), (4,3,9),(3,2,10);
331select t1.*, t2.* from t1 left join t2 on t1.n = t2.n and
332t1.m = t2.m where t1.n = 1;
333--sorted_result
334select t1.*, t2.* from t1 left join t2 on t1.n = t2.n and
335t1.m = t2.m where t1.n = 1 order by t1.o;
336drop table t1,t2;
337
338# Test bug with NATURAL join:
339
340CREATE TABLE t1 (id1 INT NOT NULL PRIMARY KEY, dat1 CHAR(1), id2 INT);
341INSERT INTO t1 VALUES (1,'a',1);
342INSERT INTO t1 VALUES (2,'b',1);
343INSERT INTO t1 VALUES (3,'c',2);
344
345CREATE TABLE t2 (id2 INT NOT NULL PRIMARY KEY, dat2 CHAR(1));
346INSERT INTO t2 VALUES (1,'x');
347INSERT INTO t2 VALUES (2,'y');
348INSERT INTO t2 VALUES (3,'z');
349
350SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL;
351SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;
352
353drop table t1,t2;
354
355create table t1 ( color varchar(20), name varchar(20) );
356insert into t1 values ( 'red', 'apple' );
357insert into t1 values ( 'yellow', 'banana' );
358insert into t1 values ( 'green', 'lime' );
359insert into t1 values ( 'black', 'grape' );
360insert into t1 values ( 'blue', 'blueberry' );
361create table t2 ( count int, color varchar(20) );
362insert into t2 values (10, 'green');
363insert into t2 values (5, 'black');
364insert into t2 values (15, 'white');
365insert into t2 values (7, 'green');
366select * from t1;
367select * from t2;
368select * from t2 natural join t1;
369select t2.count, t1.name from t2 natural join t1;
370select t2.count, t1.name from t2 inner join t1 using (color);
371drop table t1;
372drop table t2;
373
374#
375# Test of LEFT JOIN + GROUP FUNCTIONS within functions:
376#
377
378CREATE TABLE t1 (
379  pcode varchar(8) DEFAULT '' NOT NULL
380);
381INSERT INTO t1 VALUES ('kvw2000'),('kvw2001'),('kvw3000'),('kvw3001'),('kvw3002'),('kvw3500'),('kvw3501'),('kvw3502'),('kvw3800'),('kvw3801'),('kvw3802'),('kvw3900'),('kvw3901'),('kvw3902'),('kvw4000'),('kvw4001'),('kvw4002'),('kvw4200'),('kvw4500'),('kvw5000'),('kvw5001'),('kvw5500'),('kvw5510'),('kvw5600'),('kvw5601'),('kvw6000'),('klw1000'),('klw1020'),('klw1500'),('klw2000'),('klw2001'),('klw2002'),('kld2000'),('klw2500'),('kmw1000'),('kmw1500'),('kmw2000'),('kmw2001'),('kmw2100'),('kmw3000'),('kmw3200');
382CREATE TABLE t2 (
383  pcode varchar(8) DEFAULT '' NOT NULL,
384  KEY pcode (pcode)
385);
386INSERT INTO t2 VALUES ('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw6000'),('kvw6000'),('kld2000');
387
388SELECT t1.pcode, IF(ISNULL(t2.pcode), 0, COUNT(*)) AS count FROM t1
389LEFT JOIN t2 ON t1.pcode = t2.pcode GROUP BY t1.pcode;
390SELECT SQL_BIG_RESULT t1.pcode, IF(ISNULL(t2.pcode), 0, COUNT(*)) AS count FROM t1 LEFT JOIN t2 ON t1.pcode = t2.pcode GROUP BY t1.pcode;
391drop table t1,t2;
392
393#
394# Another left join problem
395#
396
397CREATE TABLE t1 (
398  id int(11),
399  pid int(11),
400  rep_del tinyint(4),
401  KEY id (id),
402  KEY pid (pid)
403);
404INSERT INTO t1 VALUES (1,NULL,NULL);
405INSERT INTO t1 VALUES (2,1,NULL);
406select * from t1 LEFT JOIN t1 t2 ON (t1.id=t2.pid) AND t2.rep_del IS NULL;
407create index rep_del ON t1(rep_del);
408select * from t1 LEFT JOIN t1 t2 ON (t1.id=t2.pid) AND t2.rep_del IS NULL;
409drop table t1;
410
411CREATE TABLE t1 (
412  id int(11) DEFAULT '0' NOT NULL,
413  name tinytext DEFAULT '' NOT NULL,
414  UNIQUE id (id)
415);
416INSERT INTO t1 VALUES (1,'yes'),(2,'no');
417CREATE TABLE t2 (
418  id int(11) DEFAULT '0' NOT NULL,
419  idx int(11) DEFAULT '0' NOT NULL,
420  UNIQUE id (id,idx)
421);
422INSERT INTO t2 VALUES (1,1);
423explain SELECT * from t1 left join t2 on t1.id=t2.id where t2.id IS NULL;
424SELECT * from t1 left join t2 on t1.id=t2.id where t2.id IS NULL;
425drop table t1,t2;
426
427#
428# Test problem with using key_column= constant in ON and WHERE
429#
430create table t1 (bug_id mediumint, reporter mediumint);
431create table t2 (bug_id mediumint, who mediumint, index(who));
432insert into t2 values (1,1),(1,2);
433insert into t1 values (1,1),(2,1);
434SELECT * FROM t1 LEFT JOIN t2 ON (t1.bug_id =  t2.bug_id AND  t2.who = 2) WHERE  (t1.reporter = 2 OR t2.who = 2);
435drop table t1,t2;
436
437#
438# Test problem with LEFT JOIN
439
440create table t1 (fooID smallint unsigned auto_increment, primary key (fooID));
441create table t2 (fooID smallint unsigned not null, barID smallint unsigned not null, primary key (fooID,barID));
442insert into t1 (fooID) values (10),(20),(30);
443insert into t2 values (10,1),(20,2),(30,3);
444explain select * from t2 left join t1 on t1.fooID = t2.fooID and t1.fooID = 30;
445select * from t2 left join t1 on t1.fooID = t2.fooID and t1.fooID = 30;
446--sorted_result
447select * from t2 left join t1 ignore index(primary) on t1.fooID = t2.fooID and t1.fooID = 30;
448drop table t1,t2;
449
450create table t1 (i int);
451create table t2 (i int);
452create table t3 (i int);
453insert into t1 values(1),(2);
454insert into t2 values(2),(3);
455insert into t3 values(2),(4);
456--sorted_result
457select * from t1 natural left join t2 natural left join t3;
458select * from t1 natural left join t2 where (t2.i is not null)=0;
459--sorted_result
460select * from t1 natural left join t2 where (t2.i is not null) is not null;
461select * from t1 natural left join t2 where (i is not null)=0;
462--sorted_result
463select * from t1 natural left join t2 where (i is not null) is not null;
464drop table t1,t2,t3;
465
466#
467# Test of USING
468#
469create table t1 (f1 integer,f2 integer,f3 integer);
470create table t2 (f2 integer,f4 integer);
471create table t3 (f3 integer,f5 integer);
472select * from t1
473         left outer join t2 using (f2)
474         left outer join t3 using (f3);
475drop table t1,t2,t3;
476
477create table t1 (a1 int, a2 int);
478create table t2 (b1 int not null, b2 int);
479create table t3 (c1 int, c2 int);
480
481insert into t1 values (1,2), (2,2), (3,2);
482insert into t2 values (1,3), (2,3);
483insert into t3 values (2,4),        (3,4);
484
485select * from t1 left join t2  on  b1 = a1 left join t3  on  c1 = a1  and  b1 is null;
486explain select * from t1 left join t2  on  b1 = a1 left join t3  on  c1 = a1  and  b1 is null;
487
488drop table t1, t2, t3;
489
490# Test for BUG#8711 '<=>' was considered to be a NULL-rejecting predicate.
491create table t1 (
492  a int(11),
493  b char(10),
494  key (a)
495);
496insert into t1 (a) values (1),(2),(3),(4);
497create table t2 (a int);
498
499select * from t1 left join t2 on t1.a=t2.a where not (t2.a <=> t1.a);
500select * from t1 left join t2 on t1.a=t2.a having not (t2.a <=> t1.a);
501drop table t1,t2;
502
503# Test for BUG#5088
504
505create table t1 (
506  match_id tinyint(3) unsigned not null auto_increment,
507  home tinyint(3) unsigned default '0',
508  unique key match_id (match_id),
509  key match_id_2 (match_id)
510);
511
512insert into t1 values("1", "2");
513
514create table t2 (
515  player_id tinyint(3) unsigned default '0',
516  match_1_h tinyint(3) unsigned default '0',
517  key player_id (player_id)
518);
519
520insert into t2 values("1", "5");
521insert into t2 values("2", "9");
522insert into t2 values("3", "3");
523insert into t2 values("4", "7");
524insert into t2 values("5", "6");
525insert into t2 values("6", "8");
526insert into t2 values("7", "4");
527insert into t2 values("8", "12");
528insert into t2 values("9", "11");
529insert into t2 values("10", "10");
530
531explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from
532  (t2 s left join t1 m on m.match_id = 1)
533  order by m.match_id desc;
534
535explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from
536  (t2 s left join t1 m on m.match_id = 1)
537  order by UUX desc;
538
539select s.*, '*', m.*, (s.match_1_h - m.home) UUX from
540  (t2 s left join t1 m on m.match_id = 1)
541  order by UUX desc;
542
543explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from
544  t2 s straight_join t1 m where m.match_id = 1
545  order by UUX desc;
546
547select s.*, '*', m.*, (s.match_1_h - m.home) UUX from
548  t2 s straight_join t1 m where m.match_id = 1
549  order by UUX desc;
550
551drop table t1, t2;
552
553# Tests for bugs #6307 and 6460
554
555create table t1 (a int, b int, unique index idx (a, b));
556create table t2 (a int, b int, c int, unique index idx (a, b));
557
558insert into t1 values (1, 10), (1,11), (2,10), (2,11);
559insert into t2 values (1,10,3);
560
561select t1.a, t1.b, t2.c from t1 left join t2
562                                on t1.a=t2.a and t1.b=t2.b and t2.c=3
563   where t1.a=1 and t2.c is null;
564
565drop table t1, t2;
566
567CREATE TABLE t1 (
568  ts_id bigint(20) default NULL,
569  inst_id tinyint(4) default NULL,
570  flag_name varchar(64) default NULL,
571  flag_value text,
572  UNIQUE KEY ts_id (ts_id,inst_id,flag_name)
573) ENGINE=MyISAM DEFAULT CHARSET=utf8;
574
575CREATE TABLE t2 (
576  ts_id bigint(20) default NULL,
577  inst_id tinyint(4) default NULL,
578  flag_name varchar(64) default NULL,
579  flag_value text,
580  UNIQUE KEY ts_id (ts_id,inst_id,flag_name)
581) ENGINE=MyISAM DEFAULT CHARSET=utf8;
582
583INSERT INTO t1 VALUES
584  (111056548820001, 0, 'flag1', NULL),
585  (111056548820001, 0, 'flag2', NULL),
586  (2, 0, 'other_flag', NULL);
587
588INSERT INTO t2 VALUES
589  (111056548820001, 3, 'flag1', 'sss');
590
591SELECT t1.flag_name,t2.flag_value
592  FROM t1 LEFT JOIN t2
593          ON (t1.ts_id = t2.ts_id AND t1.flag_name = t2.flag_name AND
594              t2.inst_id = 3)
595  WHERE t1.inst_id = 0 AND t1.ts_id=111056548820001 AND
596        t2.flag_value IS  NULL;
597
598DROP TABLE t1,t2;
599
600CREATE TABLE t1 (
601  id int(11) unsigned NOT NULL auto_increment,
602  text_id int(10) unsigned default NULL,
603  PRIMARY KEY  (id)
604);
605
606INSERT INTO t1 VALUES("1", "0");
607INSERT INTO t1 VALUES("2", "10");
608
609CREATE TABLE t2 (
610  text_id char(3) NOT NULL default '',
611  language_id char(3) NOT NULL default '',
612  text_data text,
613  PRIMARY KEY  (text_id,language_id)
614);
615
616INSERT INTO t2 VALUES("0", "EN", "0-EN");
617INSERT INTO t2 VALUES("0", "SV", "0-SV");
618INSERT INTO t2 VALUES("10", "EN", "10-EN");
619INSERT INTO t2 VALUES("10", "SV", "10-SV");
620SELECT t1.id, t1.text_id, t2.text_data
621  FROM t1 LEFT JOIN t2
622               ON t1.text_id = t2.text_id
623                  AND t2.language_id = 'SV'
624  WHERE (t1.id LIKE '%' OR t2.text_data LIKE '%');
625
626DROP TABLE t1, t2;
627
628# Test for bug #5896
629
630CREATE TABLE t0 (a0 int PRIMARY KEY);
631CREATE TABLE t1 (a1 int PRIMARY KEY);
632CREATE TABLE t2 (a2 int);
633CREATE TABLE t3 (a3 int);
634INSERT INTO t0 VALUES (1);
635INSERT INTO t1 VALUES (1);
636INSERT INTO t2 VALUES (1), (2);
637INSERT INTO t3 VALUES (1), (2);
638
639SELECT * FROM t1 LEFT JOIN t2 ON a1=0;
640EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON a1=0;
641SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0;
642EXPLAIN SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0;
643SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1;
644EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1;
645
646INSERT INTO t0 VALUES (0);
647INSERT INTO t1 VALUES (0);
648SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1;
649EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1;
650
651# Test for BUG#4480
652drop table t1,t2;
653create table t1 (a int, b int);
654insert into t1 values (1,1),(2,2),(3,3);
655create table t2 (a int, b int);
656insert into t2 values (1,1), (2,2);
657
658select * from t2 right join t1 on t2.a=t1.a;
659select straight_join * from t2 right join t1 on t2.a=t1.a;
660
661DROP TABLE t0,t1,t2,t3;
662
663#
664# Test for bug #9017: left join mistakingly converted to inner join
665#
666
667CREATE TABLE t1 (a int PRIMARY KEY, b int);
668CREATE TABLE t2 (a int PRIMARY KEY, b int);
669
670INSERT INTO t1 VALUES (1,1), (2,1), (3,1), (4,2);
671INSERT INTO t2 VALUES (1,2), (2,2);
672
673SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a;
674SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE t1.b=1;
675SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
676  WHERE t1.b=1 XOR (NOT ISNULL(t2.a) AND t2.b=1);
677SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE not(0+(t1.a=30 and t2.b=1));
678
679DROP TABLE t1,t2;
680
681# Bug #8681: Bad warning message when group_concat() exceeds max length
682set group_concat_max_len=5;
683create table t1 (a int, b varchar(20));
684create table t2 (a int, c varchar(20));
685insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb");
686insert into t2 values (1,"cccccccccc"),(2,"dddddddddd");
687select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a;
688select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a;
689select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a;
690select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by a;
691drop table t1, t2;
692set group_concat_max_len=default;
693
694# End of 4.1 tests
695
696#
697# BUG#10162 - ON is merged with WHERE, left join is convered to a regular join
698#
699create table t1 (gid smallint(5) unsigned not null, x int(11) not null, y int(11) not null, art int(11) not null, primary key  (gid,x,y));
700insert t1 values (1, -5, -8, 2), (1, 2, 2, 1), (1, 1, 1, 1);
701create table t2 (gid smallint(5) unsigned not null, x int(11) not null, y int(11) not null, id int(11) not null, primary key  (gid,id,x,y), key id (id));
702insert t2 values (1, -5, -8, 1), (1, 1, 1, 1), (1, 2, 2, 1);
703create table t3 ( set_id smallint(5) unsigned not null, id tinyint(4) unsigned not null, name char(12) not null, primary key  (id,set_id));
704insert t3 values (0, 1, 'a'), (1, 1, 'b'), (0, 2, 'c'), (1, 2, 'd'), (1, 3, 'e'), (1, 4, 'f'), (1, 5, 'g'), (1, 6, 'h');
705explain select name from t1 left join t2 on t1.x = t2.x and t1.y = t2.y
706left join t3 on t1.art = t3.id where t2.id =1 and t2.x = -5 and t2.y =-8
707and t1.gid =1 and t2.gid =1 and t3.set_id =1;
708drop tables t1,t2,t3;
709
710#
711# Test for bug #9938: invalid conversion from outer join to inner join
712# for queries containing indirect reference in WHERE clause
713#
714
715CREATE TABLE t1 (EMPNUM INT, GRP INT);
716INSERT INTO t1 VALUES (0, 10);
717INSERT INTO t1 VALUES (2, 30);
718
719CREATE TABLE t2 (EMPNUM INT, NAME CHAR(5));
720INSERT INTO t2 VALUES (0, 'KERI');
721INSERT INTO t2 VALUES (9, 'BARRY');
722
723CREATE VIEW v1 AS
724SELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS EMPNUM, NAME, GRP
725  FROM t2 LEFT OUTER JOIN t1 ON t2.EMPNUM=t1.EMPNUM;
726
727SELECT * FROM v1;
728SELECT * FROM v1 WHERE EMPNUM < 10;
729
730DROP VIEW v1;
731DROP TABLE t1,t2;
732
733#
734# Test for bug #11285: false Item_equal on expression in outer join
735#
736
737CREATE TABLE t1 (c11 int);
738CREATE TABLE t2 (c21 int);
739INSERT INTO t1 VALUES (30), (40), (50);
740INSERT INTO t2 VALUES (300), (400), (500);
741SELECT * FROM t1 LEFT JOIN t2 ON (c11=c21 AND c21=30) WHERE c11=40;
742DROP TABLE t1, t2;
743#
744# Test for bugs
745# #12101: erroneously applied outer join elimination in case of WHERE NOT BETWEEN
746# #12102: erroneously missing outer join elimination in case of WHERE IN/IF
747#
748
749CREATE TABLE t1 (a int PRIMARY KEY, b int);
750CREATE TABLE t2 (a int PRIMARY KEY, b int);
751
752INSERT INTO t1 VALUES (1,2), (2,1), (3,2), (4,3), (5,6), (6,5), (7,8), (8,7), (9,10);
753INSERT INTO t2 VALUES (3,0), (4,1), (6,4), (7,5);
754
755SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.b <= t1.a AND t1.a <= t1.b;
756SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a BETWEEN t2.b AND t1.b;
757SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a NOT BETWEEN t2.b AND t1.b);
758
759--sorted_result
760SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.b > t1.a OR t1.a > t1.b;
761--sorted_result
762SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a NOT BETWEEN t2.b AND t1.b;
763--sorted_result
764SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a BETWEEN t2.b AND t1.b);
765
766--sorted_result
767SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t2.b > t1.a OR t1.a > t1.b;
768--sorted_result
769SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a != t2.a AND t1.a BETWEEN t2.b AND t1.b);
770
771SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a AND (t2.b > t1.a OR t1.a > t1.b);
772SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a != t2.a OR t1.a BETWEEN t2.b AND t1.b);
773
774SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t1.a = t2.b;
775SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a IN(t2.a, t2.b);
776SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a NOT IN(t2.a, t2.b));
777
778SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a != t1.b AND t1.a != t2.b;
779SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a NOT IN(t1.b, t2.b);
780SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a IN(t1.b, t2.b));
781
782SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.a != t2.b OR (t1.a != t2.a AND t1.a != t2.b);
783SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t2.a = t2.b AND t1.a IN(t2.a, t2.b));
784
785SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.a != t2.b AND t1.a != t1.b AND t1.a != t2.b;
786SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t2.a = t2.b OR t1.a IN(t1.b, t2.b));
787
788EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t1.a = t2.b;
789EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a IN(t2.a, t2.b);
790EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a > IF(t1.a = t2.b-2, t2.b, t2.b-1);
791
792DROP TABLE t1,t2;
793
794#
795# Test for bug #17164: ORed FALSE blocked conversion of outer join into join
796#
797
798# Test case moved to join_outer_innodb
799
800#
801# Bug 19396: LEFT OUTER JOIN over views in curly braces
802#
803--disable_warnings
804DROP VIEW IF EXISTS v1,v2;
805DROP TABLE IF EXISTS t1,t2;
806--enable_warnings
807
808CREATE TABLE t1 (a int);
809CREATE table t2 (b int);
810INSERT INTO t1 VALUES (1), (2), (3), (4), (1), (1), (3);
811INSERT INTO t2 VALUES (2), (3);
812
813CREATE VIEW v1 AS SELECT a FROM t1 JOIN t2 ON t1.a=t2.b;
814CREATE VIEW v2 AS SELECT b FROM t2 JOIN t1 ON t2.b=t1.a;
815
816SELECT v1.a, v2. b
817  FROM v1 LEFT OUTER JOIN v2 ON (v1.a=v2.b) AND (v1.a >= 3)
818    GROUP BY v1.a;
819SELECT v1.a, v2. b
820  FROM { OJ v1 LEFT OUTER JOIN v2 ON (v1.a=v2.b) AND (v1.a >= 3) }
821    GROUP BY v1.a;
822
823DROP VIEW v1,v2;
824DROP TABLE t1,t2;
825
826#
827# Bug 19816: LEFT OUTER JOIN with constant ORed predicates in WHERE clause
828#
829
830CREATE TABLE t1 (a int);
831CREATE TABLE t2 (b int);
832INSERT INTO t1 VALUES (1), (2), (3), (4);
833INSERT INTO t2 VALUES (2), (3);
834
835--sorted_result
836SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE (1=1);
837
838--sorted_result
839SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE (1 OR 1);
840--sorted_result
841SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE (0 OR 1);
842--sorted_result
843SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE (1=1 OR 2=2);
844--sorted_result
845SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE (1=1 OR 1=0);
846
847DROP TABLE t1,t2;
848
849#
850# Bug 26017: LEFT OUTER JOIN over two constant tables and
851#            a case-insensitive comparison predicate field=const
852#
853
854CREATE TABLE t1 (
855  f1 varchar(16) collate latin1_swedish_ci PRIMARY KEY,
856  f2 varchar(16) collate latin1_swedish_ci
857);
858CREATE TABLE t2 (
859  f1 varchar(16) collate latin1_swedish_ci PRIMARY KEY,
860  f3 varchar(16) collate latin1_swedish_ci
861);
862
863INSERT INTO t1 VALUES ('bla','blah');
864INSERT INTO t2 VALUES ('bla','sheep');
865
866SELECT * FROM t1 JOIN t2 USING(f1) WHERE f1='Bla';
867SELECT * FROM t1 LEFT JOIN t2 USING(f1) WHERE f1='bla';
868SELECT * FROM t1 LEFT JOIN t2 USING(f1) WHERE f1='Bla';
869
870DROP TABLE t1,t2;
871
872#
873# Bug 28188: 'not exists' optimization for outer joins
874#
875
876CREATE TABLE t1 (id int PRIMARY KEY, a varchar(8));
877CREATE TABLE t2 (id int NOT NULL, b int NOT NULL, INDEX idx(id));
878INSERT INTO t1 VALUES
879  (1,'aaaaaaa'), (5,'eeeeeee'), (4,'ddddddd'), (2,'bbbbbbb'), (3,'ccccccc');
880INSERT INTO t2 VALUES
881  (3,10), (2,20), (5,30), (3,20), (5,10), (3,40), (3,30), (2,10), (2,40);
882
883EXPLAIN
884SELECT t1.id, a FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.b IS NULL;
885
886flush status;
887SELECT t1.id, a FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.b IS NULL;
888show status like 'Handler_read%';
889
890DROP TABLE t1,t2;
891
892#
893# Bug 28571: outer join with false on condition over constant tables
894#
895
896CREATE TABLE t1 (c int  PRIMARY KEY, e int NOT NULL);
897INSERT INTO t1 VALUES (1,0), (2,1);
898CREATE TABLE t2 (d int PRIMARY KEY);
899INSERT INTO t2 VALUES (1), (2), (3);
900
901EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON e<>0 WHERE c=1 AND d IS NULL;
902SELECT * FROM t1 LEFT JOIN t2 ON e<>0 WHERE c=1 AND d IS NULL;
903SELECT * FROM t1 LEFT JOIN t2 ON e<>0 WHERE c=1 AND d<=>NULL;
904
905DROP TABLE t1,t2;
906
907--echo #
908--echo # Bug#47650: using group by with rollup without indexes returns incorrect
909--echo # results with where
910--echo #
911CREATE TABLE t1 ( a INT );
912INSERT INTO t1 VALUES (1);
913
914CREATE TABLE t2 ( a INT, b INT );
915INSERT INTO t2 VALUES (1, 1),(1, 2),(1, 3),(2, 4),(2, 5);
916
917EXPLAIN
918SELECT t1.a, COUNT( t2.b ), SUM( t2.b ), MAX( t2.b )
919FROM t1 LEFT JOIN t2 USING( a )
920GROUP BY t1.a WITH ROLLUP;
921
922SELECT t1.a, COUNT( t2.b ), SUM( t2.b ), MAX( t2.b )
923FROM t1 LEFT JOIN t2 USING( a )
924GROUP BY t1.a WITH ROLLUP;
925
926EXPLAIN
927SELECT t1.a, COUNT( t2.b ), SUM( t2.b ), MAX( t2.b )
928FROM t1 JOIN t2 USING( a )
929GROUP BY t1.a WITH ROLLUP;
930
931SELECT t1.a, COUNT( t2.b ), SUM( t2.b ), MAX( t2.b )
932FROM t1 JOIN t2 USING( a )
933GROUP BY t1.a WITH ROLLUP;
934
935DROP TABLE t1, t2;
936
937--echo #
938--echo # Bug#51598 Inconsistent behaviour with a COALESCE statement inside an IN comparison
939--echo #
940CREATE TABLE t1(f1 INT, f2 INT, f3 INT);
941INSERT INTO t1 VALUES (1, NULL, 3);
942CREATE TABLE t2(f1 INT, f2 INT);
943INSERT INTO t2 VALUES (2, 1);
944
945EXPLAIN EXTENDED SELECT * FROM t1 LEFT JOIN t2 ON t1.f2 = t2.f2
946WHERE (COALESCE(t1.f1, t2.f1), f3) IN ((1, 3), (2, 2));
947
948SELECT * FROM t1 LEFT JOIN t2 ON t1.f2 = t2.f2
949WHERE (COALESCE(t1.f1, t2.f1), f3) IN ((1, 3), (2, 2));
950
951DROP TABLE t1, t2;
952
953--echo #
954--echo # Bug#52357: Assertion failed: join->best_read in greedy_search
955--echo # optimizer_search_depth=0
956--echo #
957CREATE TABLE t1( a INT );
958
959INSERT INTO t1 VALUES (1),(2);
960SET optimizer_search_depth = 0;
961
962--echo # Should not core dump on query preparation
963EXPLAIN
964SELECT 1
965FROM t1 tt3 LEFT  OUTER JOIN t1 tt4 ON 1
966            LEFT  OUTER JOIN t1 tt5 ON 1
967            LEFT  OUTER JOIN t1 tt6 ON 1
968            LEFT  OUTER JOIN t1 tt7 ON 1
969            LEFT  OUTER JOIN t1 tt8 ON 1
970            RIGHT OUTER JOIN t1 tt2 ON 1
971            RIGHT OUTER JOIN t1 tt1 ON 1
972            STRAIGHT_JOIN    t1 tt9 ON 1;
973
974SET optimizer_search_depth = DEFAULT;
975DROP TABLE t1;
976
977--echo #
978--echo # Bug#46091 STRAIGHT_JOIN + RIGHT JOIN returns different result
979--echo #
980CREATE TABLE t1 (f1 INT NOT NULL);
981INSERT INTO t1 VALUES (9),(0);
982
983CREATE TABLE t2 (f1 INT NOT NULL);
984INSERT INTO t2 VALUES
985(5),(3),(0),(3),(1),(0),(1),(7),(1),(0),(0),(8),(4),(9),(0),(2),(0),(8),(5),(1);
986
987SELECT STRAIGHT_JOIN COUNT(*) FROM t1 TA1
988RIGHT JOIN t2 TA2 JOIN t2 TA3 ON TA2.f1 ON TA3.f1;
989
990EXPLAIN SELECT STRAIGHT_JOIN COUNT(*) FROM t1 TA1
991RIGHT JOIN t2 TA2 JOIN t2 TA3 ON TA2.f1 ON TA3.f1;
992
993DROP TABLE t1, t2;
994
995--echo #
996--echo # Bug#48971 Segfault in add_found_match_trig_cond () at sql_select.cc:5990
997--echo #
998CREATE TABLE t1(f1 INT, PRIMARY KEY (f1));
999INSERT INTO t1 VALUES (1),(2);
1000
1001EXPLAIN EXTENDED SELECT STRAIGHT_JOIN jt1.f1 FROM t1 AS jt1
1002 LEFT JOIN t1 AS jt2
1003  RIGHT JOIN t1 AS jt3
1004    JOIN t1 AS jt4 ON 1
1005   LEFT JOIN t1 AS jt5 ON 1
1006  ON 1
1007  RIGHT JOIN t1 AS jt6 ON jt6.f1
1008 ON 1;
1009
1010EXPLAIN EXTENDED SELECT STRAIGHT_JOIN jt1.f1 FROM t1 AS jt1
1011 RIGHT JOIN t1 AS jt2
1012  RIGHT JOIN t1 AS jt3
1013    JOIN t1 AS jt4 ON 1
1014   LEFT JOIN t1 AS jt5 ON 1
1015  ON 1
1016  RIGHT JOIN t1 AS jt6 ON jt6.f1
1017 ON 1;
1018
1019DROP TABLE t1;
1020
1021--echo #
1022--echo # Bug#57688 Assertion `!table || (!table->write_set || bitmap_is_set(table->write_set, field
1023--echo #
1024
1025CREATE TABLE t1 (f1 INT NOT NULL, PRIMARY KEY (f1));
1026CREATE TABLE t2 (f1 INT NOT NULL, f2 INT NOT NULL, PRIMARY KEY (f1, f2));
1027
1028INSERT INTO t1 VALUES (4);
1029INSERT INTO t2 VALUES (3, 3);
1030INSERT INTO t2 VALUES (7, 7);
1031
1032EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1033WHERE t1.f1 = 4
1034GROUP BY t2.f1, t2.f2;
1035
1036SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1037WHERE t1.f1 = 4
1038GROUP BY t2.f1, t2.f2;
1039
1040EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1041WHERE t1.f1 = 4 AND t2.f1 IS NOT NULL AND t2.f2 IS NOT NULL
1042GROUP BY t2.f1, t2.f2;
1043
1044SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1045WHERE t1.f1 = 4 AND t2.f1 IS NOT NULL AND t2.f2 IS NOT NULL
1046GROUP BY t2.f1, t2.f2;
1047
1048DROP TABLE t1,t2;
1049
1050--echo #
1051--echo # Bug#57034 incorrect OUTER JOIN result when joined on unique key
1052--echo #
1053
1054CREATE TABLE t1 (pk INT PRIMARY KEY,
1055                 col_int INT,
1056                 col_int_unique INT UNIQUE KEY);
1057INSERT INTO t1 VALUES (1,NULL,2), (2,0,0);
1058
1059CREATE TABLE t2 (pk INT PRIMARY KEY,
1060                 col_int INT,
1061                 col_int_unique INT UNIQUE KEY);
1062INSERT INTO t2 VALUES (1,0,1), (2,0,2);
1063
1064EXPLAIN
1065SELECT * FROM t1 LEFT JOIN t2
1066  ON t1.col_int_unique = t2.col_int_unique AND t1.col_int = t2.col_int
1067  WHERE t1.pk=1;
1068
1069SELECT * FROM t1 LEFT JOIN t2
1070  ON t1.col_int_unique = t2.col_int_unique AND t1.col_int = t2.col_int
1071  WHERE t1.pk=1;
1072
1073DROP TABLE t1,t2;
1074
1075--echo #
1076--echo # Bug#48046 Server incorrectly processing JOINs on NULL values
1077--echo #
1078
1079# bug#48046 is a duplicate of bug#57034
1080
1081CREATE TABLE `BB` (
1082  `pk` int(11) NOT NULL AUTO_INCREMENT,
1083  `time_key` time DEFAULT NULL,
1084  `varchar_key` varchar(1) DEFAULT NULL,
1085  `varchar_nokey` varchar(1) DEFAULT NULL,
1086  PRIMARY KEY (`pk`),
1087  KEY `time_key` (`time_key`),
1088  KEY `varchar_key` (`varchar_key`)
1089) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
1090
1091INSERT INTO `BB` VALUES (10,'18:27:58',NULL,NULL);
1092
1093SELECT table1.time_key AS field1, table2.pk
1094FROM BB table1  LEFT JOIN BB table2
1095 ON table2.varchar_nokey = table1.varchar_key
1096 HAVING field1;
1097
1098DROP TABLE BB;
1099
1100--echo #
1101--echo # Bug#49600 Server incorrectly processing RIGHT JOIN with
1102--echo #           constant WHERE clause and no index
1103--echo #
1104
1105# bug#49600 is a duplicate of bug#57034
1106
1107CREATE TABLE `BB` (
1108  `col_datetime_key` datetime DEFAULT NULL,
1109  `col_varchar_key` varchar(1) DEFAULT NULL,
1110  `col_varchar_nokey` varchar(1) DEFAULT NULL,
1111  KEY `col_datetime_key` (`col_datetime_key`),
1112  KEY `col_varchar_key` (`col_varchar_key`)
1113) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1114
1115INSERT INTO `BB` VALUES ('1900-01-01 00:00:00',NULL,NULL);
1116
1117SELECT table1.col_datetime_key
1118FROM BB table1 RIGHT JOIN BB table2
1119 ON table2 .col_varchar_nokey = table1.col_varchar_key
1120 WHERE 7;
1121
1122# Disable keys, and we get incorrect result for the same query
1123ALTER TABLE BB DISABLE KEYS;
1124
1125SELECT table1.col_datetime_key
1126FROM BB table1 RIGHT JOIN BB table2
1127 ON table2 .col_varchar_nokey = table1.col_varchar_key
1128 WHERE 7;
1129
1130DROP TABLE BB;
1131
1132
1133--echo #
1134--echo # Bug#58490: Incorrect result in multi level OUTER JOIN
1135--echo # in combination with IS NULL
1136--echo #
1137
1138CREATE TABLE t1 (i INT NOT NULL);
1139INSERT INTO t1 VALUES (0),    (2),(3),(4);
1140CREATE TABLE t2 (i INT NOT NULL);
1141INSERT INTO t2 VALUES (0),(1),    (3),(4);
1142CREATE TABLE t3 (i INT NOT NULL);
1143INSERT INTO t3 VALUES (0),(1),(2),    (4);
1144CREATE TABLE t4 (i INT NOT NULL);
1145INSERT INTO t4 VALUES (0),(1),(2),(3)   ;
1146
1147--sorted_result
1148SELECT * FROM
1149 t1 LEFT JOIN
1150 ( t2 LEFT JOIN
1151   ( t3 LEFT JOIN
1152     t4
1153     ON t4.i = t3.i
1154   )
1155   ON t3.i = t2.i
1156 )
1157 ON t2.i = t1.i
1158 ;
1159
1160--sorted_result
1161SELECT * FROM
1162 t1 LEFT JOIN
1163 ( t2 LEFT JOIN
1164   ( t3 LEFT JOIN
1165     t4
1166     ON t4.i = t3.i
1167   )
1168   ON t3.i = t2.i
1169 )
1170 ON t2.i = t1.i
1171 WHERE t4.i IS NULL;
1172
1173
1174# Most simplified testcase to reproduce the bug.
1175# (Has to be at least a two level nested outer join)
1176--sorted_result
1177SELECT * FROM
1178 t1 LEFT JOIN
1179 ( ( t2 LEFT JOIN
1180     t3
1181     ON t3.i = t2.i
1182   )
1183 )
1184 ON t2.i = t1.i
1185 WHERE t3.i IS NULL;
1186
1187
1188# Extended testing:
1189# We then add some equi-join inside the query above:
1190# (There Used to be some problems here with first
1191#  proposed patch for this bug)
1192--sorted_result
1193SELECT * FROM
1194 t1 LEFT JOIN
1195 ( ( t2 LEFT JOIN
1196     t3
1197     ON t3.i = t2.i
1198   )
1199   JOIN t4
1200   ON t4.i=t2.i
1201 )
1202 ON t2.i = t1.i
1203 WHERE t3.i IS NULL;
1204
1205--sorted_result
1206SELECT * FROM
1207 t1 LEFT JOIN
1208 ( ( t2 LEFT JOIN
1209     t3
1210     ON t3.i = t2.i
1211   )
1212   JOIN (t4 AS t4a JOIN t4 AS t4b ON t4a.i=t4b.i)
1213   ON t4a.i=t2.i
1214 )
1215 ON t2.i = t1.i
1216 WHERE t3.i IS NULL;
1217
1218--sorted_result
1219SELECT * FROM
1220 t1 LEFT JOIN
1221 ( ( t2 LEFT JOIN
1222     t3
1223     ON t3.i = t2.i
1224   )
1225   JOIN (t4 AS t4a, t4 AS t4b)
1226   ON t4a.i=t2.i
1227 )
1228 ON t2.i = t1.i
1229 WHERE t3.i IS NULL;
1230
1231
1232DROP TABLE t1,t2,t3,t4;
1233
1234## Bug#49322 & bug#58490 are duplicates. However, we include testcases
1235## for both.
1236--echo #
1237--echo # Bug#49322(Duplicate): Server is adding extra NULL row
1238--echo # on processing a WHERE clause
1239--echo #
1240
1241CREATE TABLE h (pk INT NOT NULL, col_int_key INT);
1242INSERT INTO h VALUES (1,NULL),(4,2),(5,2),(3,4),(2,8);
1243
1244CREATE TABLE m (pk INT NOT NULL, col_int_key INT);
1245INSERT INTO m VALUES (1,2),(2,7),(3,5),(4,7),(5,5),(6,NULL),(7,NULL),(8,9);
1246CREATE TABLE k (pk INT NOT NULL, col_int_key INT);
1247INSERT INTO k VALUES (1,9),(2,2),(3,5),(4,2),(5,7),(6,0),(7,5);
1248
1249# Baseline query wo/ 'WHERE ... IS NULL' - was correct
1250--sorted_result
1251SELECT TABLE1.pk FROM k TABLE1
1252RIGHT JOIN h TABLE2 ON TABLE1.col_int_key=TABLE2.col_int_key
1253RIGHT JOIN m TABLE4 ON TABLE2.col_int_key=TABLE4.col_int_key;
1254
1255# Adding 'WHERE ... IS NULL' -> incorrect result
1256SELECT TABLE1.pk FROM k TABLE1
1257RIGHT JOIN h TABLE2 ON TABLE1.col_int_key=TABLE2.col_int_key
1258RIGHT JOIN m TABLE4 ON TABLE2.col_int_key=TABLE4.col_int_key
1259WHERE TABLE1.pk IS NULL;
1260
1261DROP TABLE h,m,k;
1262
1263--echo
1264--echo # BUG#12567331 - INFINITE LOOP WHEN RESOLVING AN ALIASED COLUMN
1265--echo # USED IN GROUP BY
1266--echo
1267CREATE TABLE t1 (
1268  col_varchar_1024_latin1_key varchar(1024),
1269  col_varchar_10_latin1 varchar(10),
1270  col_int int(11),
1271  pk int(11)
1272);
1273CREATE TABLE t2 (
1274  col_int_key int(11),
1275  col_int int(11),
1276  pk int(11)
1277);
1278
1279PREPARE prep_stmt_9846 FROM '
1280SELECT alias1.pk AS field1 FROM
1281t1 AS alias1
1282LEFT JOIN
1283(
1284  t2 AS alias2
1285  RIGHT  JOIN
1286  (
1287    t2 AS alias3
1288    JOIN t1 AS alias4
1289    ON 1
1290  )
1291  ON 1
1292)
1293ON 1
1294GROUP BY field1';
1295execute prep_stmt_9846;
1296execute prep_stmt_9846;
1297drop table t1,t2;
1298
1299--echo #
1300--echo # Bug #11765810	58813: SERVER THREAD HANGS WHEN JOIN + WHERE + GROUP BY
1301--echo # IS EXECUTED TWICE FROM P
1302--echo #
1303CREATE TABLE t1 ( a INT ) ENGINE = MYISAM;
1304INSERT INTO t1 VALUES (1);
1305PREPARE prep_stmt FROM '
1306 SELECT 1 AS f FROM t1
1307 LEFT JOIN t1 t2
1308  RIGHT JOIN t1 t3
1309    JOIN t1 t4
1310   ON 1
1311  ON 1
1312 ON 1
1313 GROUP BY f';
1314EXECUTE prep_stmt;
1315EXECUTE prep_stmt;
1316
1317DROP TABLE t1;
1318
1319--echo #
1320--echo # Bug#49600: outer join of two single-row tables with joining attributes
1321--echo #            evaluated to nulls
1322
1323create table t1 (a int, b int);
1324create table t2 (a int, b int);
1325insert into t1 values (1, NULL);
1326insert into t2 values (2, NULL);
1327
1328select * from t1 left join t2 on t1.b=t2.b;
1329
1330select * from t1 left join t2 on t1.b=t2.b where 1=1;
1331
1332drop table t1,t2;
1333
1334--echo #
1335--echo # Bug#53161: outer join in the derived table is erroneously converted
1336--echo #            into an inner join for a query with a group by clause
1337--echo #
1338
1339create table t1 (pk int not null primary key, a int not null);
1340create table t2 like t1;
1341create table t3 like t1;
1342create table t4 (pk int not null primary key);
1343insert into t1 values (1000, 1), (1001, 1);
1344insert into t2 values (2000, 2), (2001, 2);
1345insert into t3 values (3000, 3), (3001, 2);
1346insert into t4 values (4000), (4001);
1347
1348explain extended
1349select t2.pk,
1350      (select t3.pk+if(isnull(t4.pk),0,t4.pk)
1351         from t3 left join t4 on t4.pk=t3.pk
1352           where t3.pk=t2.pk+1000 limit 1 ) as t
1353  from t1,t2
1354    where t2.pk=t1.pk+1000 and t1.pk>1000
1355  group by t2.pk;
1356
1357select t2.pk,
1358      (select t3.pk+if(isnull(t4.pk),0,t4.pk)
1359         from t3 left join t4 on t4.pk=t3.pk
1360           where t3.pk=t2.pk+1000 limit 1 ) as t
1361  from t1,t2
1362    where t2.pk=t1.pk+1000 and t1.pk>1000
1363  group by t2.pk;
1364
1365drop table t1,t2,t3,t4;
1366
1367--echo #
1368--echo # Bug#57024: Poor performance when conjunctive condition over the outer
1369--echo #            table is used in the on condition of an outer join
1370--echo #
1371
1372create table t1 (a int);
1373insert into t1 values (NULL), (NULL), (NULL), (NULL);
1374insert into t1 select * from t1;
1375insert into t1 select * from t1;
1376insert into t1 select * from t1;
1377insert into t1 select * from t1;
1378insert into t1 select * from t1;
1379insert into t1 select * from t1;
1380insert into t1 select * from t1;
1381insert into t1 select * from t1;
1382insert into t1 select * from t1;
1383insert into t1 select * from t1;
1384insert into t1 select * from t1;
1385insert into t1 select * from t1;
1386insert into t1 select * from t1;
1387insert into t1 select * from t1;
1388insert into t1 select * from t1;
1389insert into t1 select * from t1;
1390insert into t1 select * from t1;
1391insert into t1 select * from t1;
1392insert into t1 values (4), (2), (1), (3);
1393
1394create table t2 like t1;
1395insert into t2 select if(t1.a is null, 10, t1.a) from t1;
1396
1397create table t3 (a int, b int, index idx(a));
1398insert into t3 values (1, 100), (3, 301), (4, 402), (1, 102), (1, 101);
1399insert into t3 values (11, 100), (33, 301), (44, 402), (11, 102), (11, 101);
1400insert into t3 values (22, 100), (53, 301), (64, 402), (22, 102), (22, 101);
1401
1402analyze table t1,t2,t3;
1403
1404flush status;
1405select sum(t3.b) from t1 left join t3 on t3.a=t1.a and t1.a is not null;
1406show status like "handler_read%";
1407flush status;
1408select sum(t3.b) from t2 left join t3 on t3.a=t2.a and t2.a <> 10;
1409show status like "handler_read%";
1410
1411drop table t1,t2,t3;
1412
1413--echo #
1414--echo # Bug#57688 Assertion `!table || (!table->write_set || bitmap_is_set(table->write_set, field
1415--echo #
1416
1417CREATE TABLE t1 (f1 INT NOT NULL, PRIMARY KEY (f1));
1418CREATE TABLE t2 (f1 INT NOT NULL, f2 INT NOT NULL, PRIMARY KEY (f1, f2));
1419
1420INSERT INTO t1 VALUES (4);
1421INSERT INTO t2 VALUES (3, 3);
1422INSERT INTO t2 VALUES (7, 7);
1423
1424EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1425WHERE t1.f1 = 4
1426GROUP BY t2.f1, t2.f2;
1427
1428SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1429WHERE t1.f1 = 4
1430GROUP BY t2.f1, t2.f2;
1431
1432EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1433WHERE t1.f1 = 4 AND t2.f1 IS NOT NULL AND t2.f2 IS NOT NULL
1434GROUP BY t2.f1, t2.f2;
1435
1436SELECT * FROM t1 LEFT JOIN t2 ON t2.f1 = t1.f1
1437WHERE t1.f1 = 4 AND t2.f1 IS NOT NULL AND t2.f2 IS NOT NULL
1438GROUP BY t2.f1, t2.f2;
1439
1440DROP TABLE t1,t2;
1441
1442--echo #
1443--echo # Bug#13068506 - QUERY WITH GROUP BY ON NON-AGGR COLUMN RETURNS
1444--echo #                WRONG RESULT
1445--echo #
1446
1447CREATE TABLE t1 (i1 int);
1448INSERT INTO t1 VALUES (100), (101);
1449
1450CREATE TABLE t2 (i2 int, i3 int);
1451INSERT INTO t2 VALUES (20,1),(10,2);
1452
1453CREATE TABLE t3 (i4 int(11));
1454INSERT INTO t3 VALUES (1),(2);
1455
1456let $query= SELECT (
1457  SELECT MAX( t2.i2 )
1458  FROM t3 RIGHT JOIN t2 ON ( t2.i3 = 2 )
1459  WHERE t2.i3 <> t1.i1
1460) AS field1
1461FROM t1;
1462
1463--echo
1464--eval $query;
1465--echo
1466--eval $query GROUP BY field1;
1467
1468--echo
1469drop table t1,t2,t3;
1470
1471--echo # End of test for Bug#13068506
1472
1473--echo End of 5.1 tests
1474
1475--echo #
1476--echo # LP BUG#994392: Wrong result with RIGHT/LEFT JOIN and ALL subquery
1477--echo # predicate in WHERE condition.
1478--echo #
1479
1480CREATE TABLE t1(a INT);
1481INSERT INTO t1 VALUES(9);
1482CREATE TABLE t2(b INT);
1483INSERT INTO t2 VALUES(8);
1484CREATE TABLE t3(c INT);
1485INSERT INTO t3 VALUES(3);
1486SELECT * FROM t2 RIGHT JOIN t3 ON(c = b) WHERE b < ALL(SELECT a FROM t1 WHERE a <= 7);
1487SELECT * FROM t3 LEFT JOIN t2 ON(c = b) WHERE b < ALL(SELECT a FROM t1 WHERE a <= 7);
1488SELECT * FROM t2 RIGHT JOIN t3 ON(c = b) WHERE b not in (SELECT a FROM t1 WHERE a <= 7);
1489SELECT * FROM t3 LEFT JOIN t2 ON(c = b) WHERE b not in (SELECT a FROM t1 WHERE a <= 7);
1490drop table t1,t2,t3;
1491
1492--echo End of 5.2 tests
1493
1494--echo #
1495--echo # LP bug #813447: LEFT JOIN with single-row inner table and
1496--echo #                 a subquery in ON expression
1497--echo #
1498
1499CREATE TABLE t1 (a int);
1500INSERT INTO t1 VALUES (0);
1501
1502CREATE TABLE t2 (a int);
1503INSERT INTO t2 VALUES (0);
1504
1505CREATE TABLE t3 (a int);
1506INSERT INTO t3 VALUES (0), (0);
1507
1508SELECT t2.a FROM t1 LEFT JOIN t2 ON (6) IN (SELECT a FROM t3);
1509EXPLAIN EXTENDED
1510SELECT t2.a FROM t1 LEFT JOIN t2 ON (6) IN (SELECT a FROM t3);
1511
1512DROP TABLE t1,t2,t3;
1513
1514--echo #
1515--echo # LP bug #817384 Wrong result with outer join + subquery in ON
1516--echo # clause +unique key
1517--echo #
1518
1519CREATE TABLE t1 ( c int NOT NULL , b char(1) NOT NULL ) ;
1520INSERT INTO t1 VALUES (1,'b');
1521
1522CREATE TABLE t2 ( a int NOT NULL , b char(1) NOT NULL , PRIMARY KEY (a)) ;
1523INSERT INTO t2 VALUES (1,'a');
1524
1525create table t3 (c1 char(1), c2 char(2));
1526insert into t3 values ('c','d');
1527insert into t3 values ('c','d');
1528
1529
1530EXPLAIN SELECT t2.b
1531FROM t1 LEFT JOIN t2 ON t1.c = t2.a AND ( t2.b , t1.b ) IN (SELECT * from t3);
1532SELECT t2.b
1533FROM t1 LEFT JOIN t2 ON t1.c = t2.a AND ( t2.b , t1.b ) IN (SELECT * from t3);
1534
1535EXPLAIN SELECT t2.b
1536FROM t1 LEFT JOIN t2 ON (t2.b) IN (SELECT c2 from t3) AND t2.a = 1;
1537SELECT t2.b
1538FROM t1 LEFT JOIN t2 ON (t2.b) IN (SELECT c2 from t3) AND t2.a = 1;
1539
1540DROP TABLE t1,t2,t3;
1541
1542--echo #
1543--echo # lp:825035 second execution of PS with outer join
1544--echo #
1545
1546CREATE TABLE t1 (a int);
1547INSERT INTO t1 VALUES (1),(2),(3),(4);
1548
1549CREATE TABLE t2 (a int);
1550
1551PREPARE stmt FROM
1552"SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a";
1553
1554EXECUTE stmt;
1555EXECUTE stmt;
1556
1557DEALLOCATE PREPARE stmt;
1558
1559DROP TABLE t1,t2;
1560
1561--echo #
1562--echo # lp:838633 second execution of PS with outer join
1563--echo #                 converted to inner join
1564--echo #
1565
1566CREATE TABLE t1 ( b int NOT NULL ) ;
1567INSERT INTO t1 VALUES (9),(10);
1568
1569CREATE TABLE t2 ( b int NOT NULL, PRIMARY KEY (b)) ;
1570INSERT INTO t2 VALUES
1571  (75),(76),(77),(78),(79),(80),(81),(82),(83),(84),(85),(86),(87),(88),(89),
1572  (10), (90),(91),(92),(93),(94),(95),(96),(97),(98),(99),(100);
1573
1574CREATE TABLE t3 ( a int, b int NOT NULL , PRIMARY KEY (b)) ;
1575INSERT INTO t3 VALUES
1576  (0,6),(0,7),(0,8),(2,9),(0,10),(2,21),(0,22),(2,23),(2,24),(2,25);
1577
1578set @save_join_cache_level= @@join_cache_level;
1579SET SESSION join_cache_level=4;
1580
1581EXPLAIN EXTENDED
1582SELECT * FROM (t2 LEFT JOIN t1 ON t1.b = t2.b) JOIN t3 ON t1.b = t3.b;
1583
1584PREPARE stmt FROM
1585'SELECT * FROM (t2 LEFT JOIN t1 ON t1.b = t2.b) JOIN t3 ON t1.b = t3.b';
1586
1587EXECUTE stmt;
1588EXECUTE stmt;
1589
1590DEALLOCATE PREPARE stmt;
1591
1592SET SESSION join_cache_level=@save_join_cache_level;
1593
1594DROP TABLE t1,t2,t3;
1595
1596--echo #
1597--echo # LP bug #943543: LEFT JOIN converted to JOIN with
1598--echo #                 ORed IS NULL(primary key) in WHERE clause
1599--echo #
1600
1601CREATE TABLE t1 (
1602  a int, b int NOT NULL, pk int NOT NULL,
1603  PRIMARY KEY (pk), INDEX idx(b)
1604);
1605INSERT INTO t1 VALUES
1606  (NULL,1,1), (6,2,2), (5,3,3), (NULL,4,4),
1607  (1,9,6), (8,5,7), (NULL,8,8), (8,1,5);
1608
1609CREATE TABLE t2 (pk int PRIMARY KEY);
1610INSERT INTO t2 VALUES (3), (8), (5);
1611
1612EXPLAIN EXTENDED
1613SELECT t1.pk FROM t2 JOIN t1 ON t2.pk = t1.a
1614 WHERE t1.b BETWEEN 5 AND 6 AND t1.pk IS NULL OR t1.pk = 5
1615ORDER BY t1.pk;
1616SELECT t1.pk FROM t2 JOIN t1 ON t2.pk = t1.a
1617 WHERE t1.b BETWEEN 5 AND 6 AND t1.pk IS NULL OR t1.pk = 5
1618ORDER BY t1.pk;
1619
1620EXPLAIN EXTENDED
1621SELECT t1.pk FROM t2 LEFT JOIN t1 ON t2.pk = t1.a
1622 WHERE t1.b BETWEEN 5 AND 6 AND t1.pk IS NULL OR t1.pk = 5
1623ORDER BY t1.pk;
1624SELECT t1.pk FROM t2 LEFT JOIN t1 ON t2.pk = t1.a
1625 WHERE t1.b BETWEEN 5 AND 6 AND t1.pk IS NULL OR t1.pk = 5
1626ORDER BY t1.pk;
1627
1628DROP TABLE t2;
1629
1630CREATE TABLE t2 (c int, d int, KEY (c));
1631INSERT INTO t2 VALUES
1632  (3,30), (8,88), (5,50), (8,81),
1633  (4,40), (9,90), (7,70), (9,90),
1634  (13,130), (18,188), (15,150), (18,181),
1635  (14,140), (19,190), (17,170), (19,190);
1636
1637INSERT INTO t1 VALUES (8,5,9);
1638
1639EXPLAIN EXTENDED
1640SELECT t1.b, t2.c, t2.d FROM t2 JOIN t1 ON t2.c = t1.a
1641  WHERE t1.pk BETWEEN 5 AND 6 AND t1.b IS NULL OR t1.b = 5
1642ORDER BY t1.b;
1643SELECT t1.b, t2.c, t2.d FROM t2 JOIN t1 ON t2.c = t1.a
1644  WHERE t1.pk BETWEEN 5 AND 6 AND t1.b IS NULL OR t1.b = 5
1645ORDER BY t1.b;
1646
1647EXPLAIN EXTENDED
1648SELECT t1.b, t2.c, t2.d FROM t2 LEFT JOIN t1 ON t2.c = t1.a
1649  WHERE t1.pk BETWEEN 5 AND 6 AND t1.b IS NULL OR t1.b = 5
1650ORDER BY t1.b;
1651SELECT t1.b, t2.c, t2.d FROM t2 LEFT JOIN t1 ON t2.c = t1.a
1652  WHERE t1.pk BETWEEN 5 AND 6 AND t1.b IS NULL OR t1.b = 5
1653ORDER BY t1.b;
1654
1655DROP TABLE t1,t2;
1656
1657--echo #
1658--echo # Bug mdev-4336: LEFT JOIN with disjunctive
1659--echo #                <non-nullable datetime field> IS NULL in WHERE
1660--echo #                causes a hang and eventual crash
1661--echo #
1662
1663CREATE TABLE t1 (
1664  id int(11) NOT NULL,
1665  modified datetime NOT NULL,
1666  PRIMARY KEY (id)
1667);
1668
1669SELECT a.* FROM t1 a LEFT JOIN t1 b ON a.id = b.id
1670  WHERE a.modified > b.modified or b.modified IS NULL;
1671
1672DROP TABLE t1;
1673
1674--echo #
1675--echo # MDEV-4817: Optimizer fails to optimize expression of the form 'FOO' IS NULL
1676--echo #
1677create table t0 (a int not null);
1678insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1679
1680alter table t0 add person_id varchar(255) not null;
1681create table t1 (pk int not null primary key);
1682insert into t1 select A.a + 10*B.a from t0 A, t0 B;
1683
1684explain select * from t1 left join t0 on t0.a=t1.pk where t0.person_id='fooo' or 'xyz' IS NULL;
1685explain select * from t1 left join t0 on t0.a=t1.pk where t0.person_id='fooo';
1686explain select * from t1 left join t0 on t0.a=t1.pk where t0.person_id='fooo' or t0.person_id='bar';
1687
1688drop table t0, t1;
1689
1690--echo #
1691--echo # MDEV-4836: Wrong result on <not null date column> IS NULL (old documented hack stopped working)
1692--echo #  (this is a regression after fix for MDEV-4817)
1693--echo #
1694CREATE TABLE t1 (id INT, d DATE NOT NULL);
1695INSERT INTO t1 VALUES (1,'0000-00-00'),(2,'0000-00-00');
1696CREATE TABLE t2 (i INT);
1697SELECT * FROM t1 LEFT JOIN t2 ON (id=i) WHERE NULL OR d IS NULL;
1698DROP TABLE t1,t2;
1699
1700
1701CREATE TABLE t1 (i1 INT, d1 DATE NOT NULL);
1702INSERT INTO t1 VALUES (1,'2012-12-21'),(2,'0000-00-00');
1703
1704CREATE TABLE t2 (i2 INT, j2 INT);
1705INSERT INTO t2 VALUES (1,10),(2,20);
1706
1707SELECT * FROM t1 LEFT JOIN t2 ON i1 = j2 WHERE d1 IS NULL AND 1 OR i1 = i2;
1708DROP TABLE t1,t2;
1709
1710--echo # Another testcase
1711CREATE TABLE t1 (i1 INT) ENGINE=MyISAM;
1712INSERT INTO t1 VALUES (NULL);
1713
1714CREATE TABLE t2 (i2 INT, a INT, b INT) ENGINE=MyISAM;
1715CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
1716
1717INSERT INTO t2 VALUES (NULL,1,2),(NULL,2,3);
1718SELECT * FROM t1 LEFT JOIN v2 ON i1 = i2 WHERE a < b;
1719SELECT * FROM t1 LEFT JOIN t2 ON i1 = i2 WHERE a < b;
1720
1721drop view v2;
1722drop table t1,t2;
1723
1724--echo #
1725--echo # Bug mdev-4942: LEFT JOIN with conjunctive
1726--echo #                <non-nullable datetime field> IS NULL in WHERE
1727--echo #                causes an assert failure
1728--echo #
1729
1730CREATE TABLE t1 ( i1 int, d1 date );
1731INSERT INTO t1 VALUES (1,'2001-06-26'), (2,'2000-11-16');
1732
1733CREATE TABLE t2 ( i2 int, d2 date NOT NULL );
1734INSERT INTO t2 VALUES (3,'2000-03-06'), (4,'2007-09-25');
1735
1736SELECT * FROM t1 LEFT JOIN t2 ON i1 = i2 WHERE d1 IS NULL AND d2 IS NULL;
1737
1738DROP TABLE t1,t2;
1739
1740--echo #
1741--echo # Bug mdev-4952: LEFT JOIN with disjunctive
1742--echo #                <non-nullable datetime field> IS NULL in WHERE
1743--echo #                causes an assert failure
1744--echo #
1745
1746CREATE TABLE t1 (a1 int, b1 int NOT NULL) ENGINE=MyISAM;
1747INSERT INTO t1 VALUES (1, 10), (2, 11);
1748
1749CREATE TABLE t2 (dt datetime NOT NULL, a2 int, b2 int) ENGINE=MyISAM;
1750INSERT INTO t2 VALUES
1751('2006-10-08 09:34:54', 1, 100), ('2001-01-19 01:04:43', 2, 200);
1752
1753SELECT * FROM t1 LEFT JOIN t2 ON a1 = a2
1754  WHERE ( dt IS NULL OR FALSE ) AND b2 IS NULL;
1755
1756DROP TABLE t1,t2;
1757
1758--echo #
1759--echo # Bug mdev-4962: nested outer join with
1760--echo #                <non-nullable datetime field> IS NULL in WHERE
1761--echo #                causes an assert failure
1762--echo #
1763
1764CREATE TABLE t1 (i1 int) ENGINE=MyISAM;
1765INSERT INTO t1 VALUES (1),(2);
1766
1767CREATE TABLE t2 (i2 int) ENGINE=MyISAM;
1768INSERT INTO t2 VALUES (10),(20);
1769
1770CREATE TABLE t3 (i3 int, d3 datetime NOT NULL) ENGINE=MyISAM;
1771INSERT INTO t3 VALUES (8,'2008-12-04 17:53:42'),(9,'2012-12-21 12:12:12');
1772
1773SELECT * FROM t1 LEFT JOIN t2 LEFT JOIN t3 ON i2 = i3 ON i1 = i3
1774  WHERE d3 IS NULL;
1775EXPLAIN EXTENDED
1776SELECT * FROM t1 LEFT JOIN t2 LEFT JOIN t3 ON i2 = i3 ON i1 = i3
1777  WHERE d3 IS NULL;
1778
1779DROP TABLE t1,t2,t3;
1780
1781--echo #
1782--echo # Bug mdev-6705: wrong on expression after constant row substitution
1783--echo #                that triggers a simplification of WHERE condition
1784--echo #
1785
1786CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
1787INSERT INTO t1 VALUES (10,8);
1788
1789CREATE TABLE t2 (c int) ENGINE=MyISAM;
1790INSERT INTO t2 VALUES (8),(9);
1791
1792CREATE TABLE t3 (d int) ENGINE=MyISAM;
1793INSERT INTO t3 VALUES (3),(8);
1794
1795EXPLAIN EXTENDED
1796SELECT * FROM t1 INNER JOIN t2 ON c = b LEFT JOIN t3 ON d = a
1797  WHERE b IN (1,2,3) OR b = d;
1798
1799SELECT * FROM t1 INNER JOIN t2 ON c = b LEFT JOIN t3 ON d = a
1800  WHERE b IN (1,2,3) OR b = d;
1801
1802DROP TABLE t1,t2,t3;
1803
1804--echo #
1805--echo # MDEV-6634: Wrong estimates for ref(const) and key IS NULL predicate
1806--echo #
1807create table t1(a int);
1808insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1809create table t2 (a int, b int, c int, key(b), key(c));
1810
1811insert into t2 select
1812  @a:=A.a + 10*B.a+100*C.a,
1813  IF(@a<900, NULL, @a),
1814  IF(@a<500, NULL, @a)
1815from t1 A, t1 B, t1 C;
1816
1817delete from t1 where a=0;
1818
1819--echo # Check that there are different #rows of NULLs for b and c, both !=10:
1820explain select * from t2 force index (b) where b is null;
1821explain select * from t2 force index (c) where c is null;
1822
1823explain select * from t1 left join t2 on t2.b is null;
1824explain select * from t1 left join t2 on t2.c is null;
1825
1826drop table t1,t2;
1827
1828--echo #
1829--echo # MDEV-10006: optimizer doesn't convert outer join to inner on views with WHERE clause
1830--echo #
1831
1832CREATE TABLE t1(i1 int primary key, v1 int, key(v1));
1833INSERT INTO t1 VALUES (1, 1);
1834INSERT INTO t1 VALUES (2, 2);
1835INSERT INTO t1 VALUES (3, 3);
1836INSERT INTO t1 VALUES (4, 4);
1837INSERT INTO t1 VALUES (5, 3);
1838INSERT INTO t1 VALUES (6, 6);
1839INSERT INTO t1 VALUES (7, 7);
1840INSERT INTO t1 VALUES (8, 8);
1841INSERT INTO t1 VALUES (9, 9);
1842
1843CREATE TABLE t2(i2 int primary key, v2 int, key(v2));
1844INSERT INTO t2 VALUES (1, 1);
1845INSERT INTO t2 VALUES (2, 2);
1846INSERT INTO t2 VALUES (3, 3);
1847INSERT INTO t2 VALUES (4, 4);
1848INSERT INTO t2 VALUES (5, 3);
1849INSERT INTO t2 VALUES (6, 6);
1850INSERT INTO t2 VALUES (7, 7);
1851INSERT INTO t2 VALUES (8, 8);
1852INSERT INTO t2 VALUES (9, 9);
1853
1854CREATE TABLE t3(i3 int primary key, v3 int, key(v3));
1855INSERT INTO t3 VALUES (2, 2);
1856INSERT INTO t3 VALUES (4, 4);
1857INSERT INTO t3 VALUES (6, 6);
1858INSERT INTO t3 VALUES (8, 8);
1859
1860--echo # This should have a join order of t3,t1,t2 (or t3,t2,t1, the idea is that t3 is the first one)
1861EXPLAIN EXTENDED
1862SELECT * FROM
1863 (SELECT t1.i1 as i1, t1.v1 as v1,
1864                 t2.i2 as i2, t2.v2 as v2,
1865                 t3.i3 as i3, t3.v3 as v3
1866            FROM t1 JOIN t2 on t1.i1 = t2.i2
1867       LEFT JOIN t3 on t2.i2 = t3.i3
1868 ) as w1
1869WHERE v3 = 4;
1870
1871--echo # This should have the same join order like the query above:
1872EXPLAIN EXTENDED
1873SELECT * FROM
1874 (SELECT t1.i1 as i1, t1.v1 as v1,
1875                 t2.i2 as i2, t2.v2 as v2,
1876                 t3.i3 as i3, t3.v3 as v3
1877            FROM t1 JOIN t2 on t1.i1 = t2.i2
1878       LEFT JOIN t3 on t2.i2 = t3.i3
1879           WHERE t1.i1 = t2.i2
1880             AND 1 = 1
1881 ) as w2
1882WHERE v3 = 4;
1883
1884drop table t1,t2,t3;
1885
1886--echo #
1887--echo # MDEV-11958: LEFT JOIN with stored routine produces incorrect result
1888--echo #
1889
1890CREATE TABLE t (x INT);
1891INSERT INTO t VALUES(1),(NULL);
1892CREATE FUNCTION f (val INT, ret INT) RETURNS INT DETERMINISTIC RETURN IFNULL(val, ret);
1893
1894SELECT t1.x, t2.x, IFNULL(t2.x,0), f(t2.x,0)
1895                            FROM t t1 LEFT JOIN t t2
1896                            ON t1.x = t2.x
1897                            WHERE IFNULL(t2.x,0)=0;
1898explain extended
1899SELECT t1.x, t2.x, IFNULL(t2.x,0), f(t2.x,0)
1900                            FROM t t1 LEFT JOIN t t2
1901                            ON t1.x = t2.x
1902                            WHERE IFNULL(t2.x,0)=0;
1903SELECT t1.x, t2.x, IFNULL(t2.x,0), f(t2.x,0)
1904                            FROM t t1 LEFT JOIN t t2
1905                            ON t1.x = t2.x
1906                            WHERE f(t2.x,0)=0;
1907explain extended
1908SELECT t1.x, t2.x, IFNULL(t2.x,0), f(t2.x,0)
1909                            FROM t t1 LEFT JOIN t t2
1910                            ON t1.x = t2.x
1911                            WHERE f(t2.x,0)=0;
1912
1913drop function f;
1914drop table t;
1915CREATE TABLE t1 (
1916 col1 DECIMAL(33,5) NULL DEFAULT NULL,
1917 col2 DECIMAL(33,5) NULL DEFAULT NULL
1918);
1919
1920CREATE TABLE t2 (
1921 col1 DECIMAL(33,5) NULL DEFAULT NULL,
1922 col2 DECIMAL(33,5) NULL DEFAULT NULL,
1923 col3 DECIMAL(33,5) NULL DEFAULT NULL
1924);
1925
1926INSERT INTO t1 VALUES (2, 1.1), (2, 2.1);
1927INSERT INTO t2 VALUES (3, 3.1, 4), (1, 1, NULL);
1928
1929DELIMITER |;
1930
1931CREATE FUNCTION f1 ( p_num DECIMAL(45,15), p_return DECIMAL(45,15))
1932RETURNS decimal(33,5)
1933LANGUAGE SQL
1934DETERMINISTIC
1935CONTAINS SQL
1936SQL SECURITY INVOKER
1937BEGIN
1938  IF p_num IS NULL THEN
1939    RETURN p_return;
1940  ELSE
1941    RETURN p_num;
1942  END IF;
1943END |
1944
1945DELIMITER ;|
1946
1947let $q1=
1948SELECT t1.col1, t2.col1, t2.col3
1949FROM t1 LEFT OUTER JOIN t2 ON t1.col1 = t2.col2
1950WHERE IFNULL(t2.col3,0) = 0;
1951
1952eval $q1;
1953eval EXPLAIN EXTENDED $q1;
1954
1955let $q2=
1956SELECT t1.col1, t2.col1, t2.col3
1957FROM t1 LEFT OUTER JOIN t2 ON t1.col1 = t2.col2
1958WHERE f1(t2.col3,0) = 0;
1959eval $q2;
1960eval EXPLAIN EXTENDED $q2;
1961
1962DROP FUNCTION f1;
1963
1964DROP TABLE t1,t2;
1965
1966--echo #
1967--echo # MDEV-10397: Server crashes in key_copy with join_cache_level > 2 and join on BIT fields
1968--echo #
1969
1970CREATE TABLE t1 (b1 BIT NOT NULL);
1971INSERT INTO t1 VALUES (0),(1);
1972
1973CREATE TABLE t2 (b2 BIT NOT NULL);
1974INSERT INTO t2 VALUES (0),(1);
1975
1976set @save_join_cache_level= @@join_cache_level;
1977SET  @@join_cache_level = 3;
1978SELECT t1.b1+'0' , t2.b2 + '0' FROM t1 LEFT JOIN t2 ON b1 = b2;
1979DROP TABLE t1, t2;
1980set @@join_cache_level= @save_join_cache_level;
1981
1982--echo #
1983--echo # MDEV-14779: using left join causes incorrect results with materialization and derived tables
1984--echo #
1985
1986create table t1(id  int);
1987insert into t1 values (1),(2);
1988create table t2(sid int, id int);
1989insert into t2 values (1,1),(2,2);
1990
1991select * from t1 t
1992 left join (select * from t2 where sid in (select max(sid) from t2 where 0=1 group by id)) r
1993 on t.id=r.id ;
1994drop table t1, t2;
1995
1996--echo #
1997--echo # MDEV-16726: SELECT with STRAGHT JOIN containing NESTED RIGHT JOIN
1998--echo #             converted to INNER JOIN with first constant inner table
1999--echo #
2000
2001CREATE TABLE t1 (
2002  pk int PRIMARY KEY, i1 int, v1 varchar(1), v2 varchar(1), KEY v1 (v1,i1)
2003) engine=MyISAM;
2004INSERT INTO t1 VALUES
2005  (8,3,'c','c'),(9,4,'z','z'),(10,3,'i','i'),(11,186,'x','x'),
2006  (14,226,'m','m'),(15,133,'p','p');
2007
2008CREATE TABLE t2 (
2009  pk int PRIMARY KEY, i1 int, v1 varchar(1), v2 varchar(1)
2010) engine=MyISAM;
2011INSERT INTO t2 VALUES (10,6,'p','p');
2012
2013EXPLAIN EXTENDED
2014SELECT STRAIGHT_JOIN t2.v2
2015FROM
2016  (t1 as tb1 LEFT JOIN t1 AS tb2 ON tb2.v1 = tb1.v2)
2017  RIGHT JOIN
2018  (t2,t1)
2019  ON t1.pk = t2.pk AND t2.v2 = tb1.v1
2020WHERE tb1.pk = 40
2021ORDER BY tb1.i1;
2022
2023EXPLAIN EXTENDED
2024SELECT STRAIGHT_JOIN t2.v2
2025FROM
2026    (t2,t1)
2027    LEFT JOIN
2028    (t1 as tb1 LEFT JOIN t1 AS tb2 ON tb2.v1 = tb1.v2)
2029   ON t1.pk = t2.pk AND t2.v2 = tb1.v1
2030WHERE tb1.pk = 40
2031ORDER BY tb1.i1;
2032
2033SELECT STRAIGHT_JOIN DISTINCT t2.v2
2034FROM
2035  (t1 as tb1 LEFT JOIN t1 AS tb2 ON tb2.v1 = tb1.v2)
2036  RIGHT JOIN
2037  (t2,t1)
2038  ON t1.pk = t2.pk AND t2.v2 = tb1.v1
2039WHERE tb1.pk = 40
2040ORDER BY tb1.i1;
2041
2042DROP TABLE t1,t2;
2043
2044--echo #
2045--echo # MDEV-19790 : IS NOT TRUE / IS NOT FALSE predicates over
2046--echo #              inner tables of outer joins
2047--echo #
2048
2049create table t1 (a int);
2050create table t2 (b int);
2051insert into t1 values (3), (7), (1);
2052insert into t2 values (7), (4), (3);
2053select * from t1 left join t2 on a=b;
2054
2055let $q=
2056select * from t1 left join t2 on a=b where (b > 3) is not true;
2057eval $q;
2058eval explain extended $q;
2059
2060let $q=
2061select * from t1 left join t2 on a=b where (b > 3) is not false;
2062eval $q;
2063eval explain extended $q;
2064
2065drop table t1,t2;
2066
2067--echo # end of 5.5 tests
2068
2069--echo #
2070--echo # MDEV-19258: chained right joins all converted to inner joins
2071--echo #
2072
2073 CREATE TABLE t1 (
2074  id int NOT NULL AUTO_INCREMENT,
2075  timestamp bigint NOT NULL,
2076  modifiedBy varchar(255) DEFAULT NULL,
2077  PRIMARY KEY (id)
2078);
2079
2080CREATE TABLE t2 (
2081  id int NOT NULL,
2082  REV int NOT NULL,
2083  REVTYPE tinyint DEFAULT NULL,
2084  profile_id int DEFAULT NULL,
2085  PRIMARY KEY (id,REV)
2086);
2087
2088CREATE TABLE t3 (
2089  id int NOT NULL,
2090  REV int NOT NULL,
2091  person_id int DEFAULT NULL,
2092  PRIMARY KEY (id,REV)
2093);
2094
2095CREATE TABLE t4 (
2096  id int NOT NULL,
2097  REV int NOT NULL,
2098  PRIMARY KEY (id,REV)
2099);
2100
2101INSERT INTO t1 VALUES
2102(1,1294391193890,'Cxqy$*9.kKeE'),(2,1294643906883,'rE4wqGV0gif@'),
2103(3,1294643927456,'L?3yt(%dY$Br'),(4,1294644343525,'WH&ObiZ$#2S4'),
2104(5,1294644616416,'YXnCbt?olUZ0'),(6,1294644954537,'8Npe4!(#lU@k'),
2105(7,1294645046659,'knc0GhXB1#ib'),(8,1294645183829,'w*oPpVfuS8^m'),
2106(9,1294645386701,'hwXR@3qVzrbU'),(10,1294645525982,'BeLW*Y9ndP0l'),
2107(11,1294645627723,'nTegib^)qZ$I'),(12,1294650860266,'u62C^Kzx3wH8'),
2108(13,1294657613745,'4&BkFjGa!qLg'),(14,1294660627161,')anpt312SCoh'),
2109(15,1294661023336,'LtJ2PX?*kTmx'),(16,1294662838066,'POGRr@?#ofpl'),
2110(17,1294663020989,'o.)1EOT2jnF7'),(18,1294663308065,'&TZ0F0LHE6.h'),
2111(19,1294664900039,'j)kSC%^In$9d'),(20,1294668904556,'97glN50)cAo.'),
2112(21,1294728056853,'lrKZxmw?I.Ek'),(22,1294728157174,'@P*SRg!pT.q?'),
2113(23,1294728327099,'W9gPrptF.)8n'),(24,1294728418481,'$q*c^sM&URd#'),
2114(25,1294728729620,'9*f4&bTPRtHo'),(26,1294728906014,')4VtTEnS7$oI'),
2115(27,1294732190003,'8dkNSPq2u3AQ'),(28,1294733205065,'SV2N6IoEf438'),
2116(29,1294741984927,'rBKj.0S^Ey%*'),(30,1294751748352,'j$2DvlBqk)Fw'),
2117(31,1294753902212,'C$N6OrEw8elz'),(32,1294758120598,'DCSVZw!rnxXq'),
2118(33,1294761769556,'OTS@QU8a6s5c'),(34,1294816845305,'IUE2stG0D3L5'),
2119(35,1294816966909,'Xd16yka.9nHe'),(36,1294817116302,'lOQHZpm%!8qb'),
2120(37,1294817374775,'^&pE3IhNf7ey'),(38,1294817538907,'oEn4#7C0Vhfp'),
2121(39,1294818482950,'bx54J*O0Va&?'),(40,1294819047024,'J%@a&1.qgdb?'),
2122(41,1294821826077,'C9kojr$L3Phz'),(42,1294825454458,'gG#BOnM80ZPi'),
2123(43,1294904129918,'F^!TrjM#zdvc'),(44,1294904254166,'Va&Tb)k0RvlM'),
2124(45,1294904414964,'dJjq0M6HvhR#'),(46,1294904505784,'nJmxg)ELqY(b'),
2125(47,1294904602835,'dhF#or$Vge!7'),(48,1294904684728,'?bIh5E3l!0em'),
2126(49,1294904877898,'Y*WflOdcxnk.'),(50,1294905002390,'*?H!lUgez5A.'),
2127(51,1294905096043,'wlEIY3n9uz!p'),(52,1294905404621,'T?qv3H6&hlQD'),
2128(53,1294905603922,'S@Bhys^Ti7bt'),(54,1294905788416,'KR?a5NVukz#l'),
2129(55,1294905993190,'A*&q4kWhED!o'),(56,1294906205254,'fT0%7z0DF6h*'),
2130(57,1294906319680,'LhzdW4?ivjR0'),(58,1294906424296,'h0KDlns%U*6T'),
2131(59,1294906623844,'b$CfB1noI6Ax'),(60,1294911258896,'#T1*LP!3$Oys');
2132
2133
2134INSERT INTO t2 VALUES
2135(1,1,0,10209),(1,42480,1,10209),(1,61612,1,10209),(1,257545,1,10209),
2136(1,385332,1,10209),(1,1687999,1,10209),(3,1,0,10210),(3,617411,2,10210),
2137(4,11,0,14),(4,95149,1,10211),(4,607890,2,10211),(5,1,0,10212),
2138(6,1,0,10213),(6,93344,1,10213),(6,295578,1,10213),(6,295579,1,10213),
2139(6,295644,1,10213),(7,1,0,10214),(7,12,1,7),(7,688796,1,10214),
2140(7,1140433,1,10214),(7,1715227,1,10214),(8,1,0,10215),(8,74253,1,10215),
2141(8,93345,1,10215),(8,12,2,2),(9,1,0,10216),(9,93342,1,10216),
2142(9,122354,1,10216),(9,301499,2,10216),(10,11,0,5),(10,93343,1,10217),
2143(10,122355,1,10217),(10,123050,1,10217),(10,301500,2,10217),(11,1,0,10218),
2144(11,87852,1,10218),(11,605499,2,10218),(12,1,0,10219),(12,88024,1,10219),
2145(12,605892,2,10219),(13,1,0,10220);
2146
2147INSERT INTO t3 VALUES
2148(1,1,300003),(1,117548,NULL),(2,1,300003),(2,117548,300006),
2149(3,1,300153),(3,117548,NULL),(4,1,300153),(4,117548,NULL),
2150(5,1,300153),(5,117548,NULL),(6,1,300182),(6,117548,NULL),
2151(7,1,300205),(7,117548,NULL),(8,1,300217),(8,117548,NULL),
2152(9,1,300290),(9,117548,NULL),(10,1,300290),(10,117548,NULL),
2153(11,1,300405),(11,117548,NULL),(12,1,300670),(12,117548,NULL),
2154(13,1,300670),(13,117548,NULL),(14,1,300006),(14,117548,NULL),
2155(15,1,300671),(15,117548,NULL),(16,1,300732),(16,117548,NULL);
2156
2157INSERT INTO t4 VALUES
2158(300000,1),(300001,1),(300003,1),(300004,1),
2159(300005,1),(300005,688796),(300006,1),(300006,97697),
2160(300009,1),(300010,1),(300011,1),(300012,1),(300013,1),
2161(300014,1),(300015,1),(300016,1),(300017,1),(300018,1),
2162(300019,1),(300020,1),(300021,1),(300022,1),(300023,1),
2163(300024,1),(300025,1),(300026,1),(300027,1),(300028,1);
2164
2165let $q1=
2166SELECT *
2167FROM t1 INNER JOIN t2 ON t2.REV=t1.id
2168        INNER JOIN t3 ON t3.id=t2.profile_id
2169        INNER JOIN t4 ON t4.id=t3.person_id
2170WHERE t1.timestamp < 1294664900039 AND t1.timestamp > 1294644616416  AND
2171      t2.REVTYPE=2;
2172
2173--echo # This should have join order of t2,t3,t4,t1
2174eval EXPLAIN EXTENDED $q1;
2175eval $q1;
2176
2177let $q2=
2178SELECT *
2179FROM t1 RIGHT JOIN t2 ON t2.REV=t1.id
2180        RIGHT JOIN t3 ON t3.id=t2.profile_id
2181        RIGHT JOIN t4 ON t4.id=t3.person_id
2182WHERE t1.timestamp < 1294664900039 AND t1.timestamp > 1294644616416
2183      AND t2.REVTYPE=2;
2184
2185--echo # This should have join order of t2,t3,t4,t1 with the same plan as above
2186--echo # because all RIGHT JOIN operations are converted into INNER JOIN
2187eval EXPLAIN EXTENDED $q2;
2188eval $q2;
2189
2190DROP TABLE t1,t2,t3,t4;
2191
2192--echo # end of 10.1 tests
2193
2194--echo #
2195--echo # MDEV-25362: name resolution for subqueries in ON expressions
2196--echo #
2197
2198create table t1 (a int, b int);
2199create table t2 (c int, d int);
2200create table t3 (e int, f int);
2201create table t4 (g int, h int);
2202
2203--error ER_BAD_FIELD_ERROR
2204explain
2205select *
2206from
2207  t1 left join
2208   (t2
2209    join
2210    t3 on
2211    (t3.f=t1.a)
2212   ) on (t2.c=t1.a );
2213
2214# This must produce an error:
2215--error ER_BAD_FIELD_ERROR
2216explain
2217select *
2218from
2219  t1 left join
2220   (t2
2221    join
2222    t3 on
2223    (t3.f=(select max(g) from t4 where t4.h=t1.a))
2224   ) on (t2.c=t1.a );
2225
2226drop table t1,t2,t3,t4;
2227
2228create table t1 (a int);
2229insert into t1 values (1),(2);
2230create table t2 (b int);
2231insert into t2 values (1),(2);
2232create table t3 (c int);
2233insert into t3 values (1),(2);
2234
2235--error ER_BAD_FIELD_ERROR
2236select * from ( select * from t1 left join t2
2237                              on b in (select x from t3 as sq1)
2238              ) as sq2;
2239
2240drop table t1,t2,t3;
2241
2242--echo # end of 10.2 tests
2243
2244--echo #
2245--echo # MDEV-22866: Crash in join optimizer with constant outer join nest
2246--echo #
2247
2248CREATE TABLE t1 (a INT) ENGINE=MyISAM;
2249INSERT INTO t1 VALUES (1),(2);
2250
2251CREATE TABLE t2 (b INT) ENGINE=MyISAM;
2252INSERT INTO t2 VALUES (3),(4);
2253
2254CREATE TABLE t3 (c INT, KEY(c)) ENGINE=MyISAM;
2255
2256CREATE TABLE t4 (d INT, KEY(d)) ENGINE=MyISAM;
2257INSERT INTO t4 VALUES (5),(6);
2258
2259CREATE TABLE t5 (e INT) ENGINE=MyISAM;
2260INSERT INTO t5 VALUES (7),(8);
2261
2262CREATE TABLE t6 (f INT) ENGINE=MyISAM;
2263INSERT INTO t6 VALUES (9),(10);
2264
2265SELECT *
2266FROM
2267  t1
2268  LEFT JOIN (
2269    t2 LEFT JOIN (
2270       t3 JOIN
2271       t4 ON t3.c = t4.d and t3.c >2 and t3.c<0
2272    ) ON t2.b >= t4.d
2273  ) ON t1.a <= t2.b
2274  LEFT JOIN t5 ON t2.b = t5.e
2275  LEFT JOIN t6 ON t3.c = t6.f;
2276
2277drop table t1,t2,t3,t4,t5,t6;
2278
2279--echo #
2280--echo # MDEV-17518: Range optimization doesn't use ON expressions from nested outer joins
2281--echo #
2282create table t1(a int);
2283insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
2284
2285create table t2(a int);
2286insert into t2 values (0),(1);
2287
2288create table t3 (a int, b int, key(a));
2289insert into t3 select A.a + B.a* 10 + C.a * 100, 12345 from t1 A, t1 B, t1 C;
2290
2291--echo # Uses range for table t3:
2292explain select * from t1 left join t3 on t1.a=t3.b and t3.a<5;
2293
2294--echo # This must use range for table t3, too:
2295explain select * from t1 left join (t3 join t2) on t1.a=t3.b and t3.a<5;
2296
2297--echo #
2298--echo # .. part 2: make sure condition selectivity can use the condition too.
2299--echo #
2300alter table t3 drop key a;
2301set @tmp1=@@optimizer_use_condition_selectivity;
2302set @tmp2=@@use_stat_tables;
2303set @tmp3=@@histogram_size;
2304set use_stat_tables=preferably;
2305set optimizer_use_condition_selectivity=4;
2306set histogram_size=100;
2307
2308analyze table t3 persistent for all;
2309
2310--echo # t3.filtered is less than 100%:
2311explain extended select * from t1 left join t3 on t1.a=t3.b and t3.a<5;
2312
2313--echo # t3.filtered must less than 100%, too:
2314explain extended select * from t1 left join (t3 join t2) on t1.a=t3.b and t3.a<5;
2315
2316drop table t1,t2,t3;
2317set optimizer_use_condition_selectivity= @tmp1;
2318set use_stat_tables= @tmp2;
2319set histogram_size= @tmp3;
2320
2321--echo # Another test
2322CREATE TABLE t1 (i1 int) ;
2323CREATE TABLE t2 (pk int NOT NULL PRIMARY KEY) ;
2324CREATE TABLE t3 (pk int NOT NULL, i1 int, PRIMARY KEY (pk)) ;
2325INSERT INTO t3 VALUES (2, NULL);
2326
2327CREATE TABLE t4 (pk int NOT NULL, i1 int, PRIMARY KEY (pk), KEY i1 (i1)) ;
2328CREATE VIEW v4 AS SELECT * FROM t4;
2329
2330SELECT 1
2331FROM t3 RIGHT JOIN t1 ON t3.i1 = t1.i1
2332  LEFT JOIN v4
2333  RIGHT JOIN t2 ON v4.i1 = t2.pk ON t1.i1 = t2.pk
2334WHERE t3.pk IN (2);
2335
2336drop view v4;
2337drop table t1,t2,t3,t4;
2338
2339--echo # end of 10.3 tests
2340
2341SET optimizer_switch=@org_optimizer_switch;
2342