Commit 24903ed5 authored by unknown's avatar unknown
Browse files

Merge spetrunia@bk-internal.mysql.com:/home/bk/mysql-5.0-opt

into  mysql.com:/home/psergey/mysql-5.0-bug8804-r12


sql/mysqld.cc:
  Auto merged
parents e396abc9 a3416606
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ insert into t2 values (1,1,1),(2,2,2),(3,3,3), (4,4,4), (5,5,5);
explain select * from t2 where p NOT IN (select p from t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	Using where
2	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using index
2	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	
select * from t2 where p NOT IN (select p from t1) order by p;
p	u	o
4	4	4
@@ -17,7 +17,7 @@ p u o
explain select * from t2 where p NOT IN (select u from t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	Using where
2	DEPENDENT SUBQUERY	t1	unique_subquery	u	u	4	func	1	Using index
2	DEPENDENT SUBQUERY	t1	unique_subquery	u	u	4	func	1	
select * from t2 where p NOT IN (select u from t1) order by p;
p	u	o
4	4	4
@@ -25,7 +25,7 @@ p u o
explain select * from t2 where p NOT IN (select o from t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	Using where
2	DEPENDENT SUBQUERY	t1	index_subquery	o	o	4	func	1	Using index
2	DEPENDENT SUBQUERY	t1	index_subquery	o	o	4	func	1	
select * from t2 where p NOT IN (select o from t1) order by p;
p	u	o
4	4	4
+16 −16
Original line number Diff line number Diff line
@@ -744,7 +744,7 @@ id select_type table type possible_keys key key_len ref rows Extra
3	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
Warnings:
Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 AS `1` having trigcond((<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1))) union select 3 AS `3` having trigcond((<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))))
Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 AS `1` having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1)) union select 3 AS `3` having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3))))
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
id
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
@@ -892,7 +892,7 @@ id select_type table type possible_keys key key_len ref rows Extra
1	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	a	a	5	func	2	Using index
Warnings:
Note	1003	select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
Note	1003	select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having <is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
CREATE TABLE t3 (a int(11) default '0');
INSERT INTO t3 VALUES (1),(2),(3);
SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
@@ -907,7 +907,7 @@ id select_type table type possible_keys key key_len ref rows Extra
2	DEPENDENT SUBQUERY	t2	ref_or_null	a	a	5	func	2	Using where; Using index
2	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	Using where
Warnings:
Note	1003	select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(select 1 AS `Not_used` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and trigcond(((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`)))) having trigcond(<is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
Note	1003	select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(select 1 AS `Not_used` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`))) having <is_not_null_test>(`test`.`t2`.`a`))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
drop table t1,t2,t3;
create table t1 (a float);
select 10.5 IN (SELECT * from t1 LIMIT 1);
@@ -1305,7 +1305,7 @@ a
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	4	Using where; Using index
2	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using index; Using where
2	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	Using where
Warnings:
Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<primary_index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on PRIMARY where (`test`.`t1`.`b` <> 30))))
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
@@ -1462,27 +1462,27 @@ a3 1
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	index	NULL	s1	6	NULL	3	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index; Full scan on NULL key
Warnings:
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`))))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	index	NULL	s1	6	NULL	3	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index; Full scan on NULL key
Warnings:
Note	1003	select `test`.`t1`.`s1` AS `s1`,<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
Note	1003	select `test`.`t1`.`s1` AS `s1`,<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	index	NULL	s1	6	NULL	3	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index; Full scan on NULL key
Warnings:
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`))))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	index	NULL	s1	6	NULL	3	Using index
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index; Using where
2	DEPENDENT SUBQUERY	t2	index_subquery	s1	s1	6	func	2	Using index; Using where; Full scan on NULL key
Warnings:
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < _latin1'a2')))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
Note	1003	select `test`.`t1`.`s1` AS `s1`,(not(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < _latin1'a2') having trigcond(<is_not_null_test>(`test`.`t2`.`s1`))))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
drop table t1,t2;
create table t2 (a int, b int);
create table t3 (a int);
@@ -2816,19 +2816,19 @@ id select_type table type possible_keys key key_len ref rows Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
Warnings:
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where ((`test`.`t2`.`flag` = _latin1'0') and trigcond((((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)) and ((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having trigcond((<is_not_null_test>(`test`.`t2`.`one`) and <is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1`
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where ((`test`.`t2`.`flag` = _latin1'0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1`
explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where
Warnings:
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where ((`test`.`t2`.`flag` = _latin1'N') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) and (<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`))))))
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where ((`test`.`t2`.`flag` = _latin1'N') and (<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) and (<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`))))
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	
2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	Using where; Using temporary; Using filesort
Warnings:
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where (`test`.`t2`.`flag` = _latin1'0') group by `test`.`t2`.`one`,`test`.`t2`.`two` having trigcond((((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)) and ((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)) and <is_not_null_test>(`test`.`t2`.`one`) and <is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1`
Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where (`test`.`t2`.`flag` = _latin1'0') group by `test`.`t2`.`one`,`test`.`t2`.`two` having (trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))) and trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1`
DROP TABLE t1,t2;
CREATE TABLE t1 (a char(5), b char(5));
INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
@@ -3009,7 +3009,7 @@ INSERT INTO t2 VALUES (1),(2),(3);
EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3	
2	DEPENDENT SUBQUERY	t1	index_subquery	a	a	5	func	2	Using index
2	DEPENDENT SUBQUERY	t1	index_subquery	a	a	5	func	2	Using index; Full scan on NULL key
SELECT a, a IN (SELECT a FROM t1) FROM t2;
a	a IN (SELECT a FROM t1)
1	1
+5 −5
Original line number Diff line number Diff line
@@ -126,11 +126,11 @@ id select_type table type possible_keys key key_len ref rows Extra
1	PRIMARY	t2	ALL	DDOCTYPEID_IDX	NULL	NULL	NULL	9	Using where
1	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	34	test.t2.DOCID	1	
1	PRIMARY	t4	eq_ref	PRIMARY	PRIMARY	34	test.t2.DOCTYPEID	1	
2	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using index; Using where
3	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using index; Using where
4	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using index; Using where
5	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using index; Using where
6	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX	PRIMARY	34	func	1	Using index; Using where
2	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using where
3	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using where
4	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using where
5	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX	PRIMARY	34	func	1	Using where
6	DEPENDENT SUBQUERY	t3	unique_subquery	PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX	PRIMARY	34	func	1	Using where
drop table t1, t2, t3, t4;
CREATE TABLE t1 (a int(10) , PRIMARY KEY (a)) Engine=InnoDB;
INSERT INTO t1 VALUES (1),(2);
+487 −9

File changed.

Preview size limit exceeded, changes collapsed.

+345 −8
Original line number Diff line number Diff line
@@ -19,10 +19,11 @@ insert into t1 (oref, grp, ie) values
# Ok, for
#   select max(ie) from t1 where oref=PARAM group by grp
# we'll have:
#  1  ->  (1, NULL)    matching + NULL
#  2  ->  (3)          non-matching
#  3  ->  (3, NULL)    non-matching + NULL
#  4  ->  ()           nothing.
# PARAM      subquery result  
#   1   ->   {(1), (NULL)}    matching + NULL
#   2   ->   {(3)}            non-matching
#   3   ->   {(3), (NULL)}    non-matching + NULL
#   4   ->   {}               empty set

create table t2 (oref int, a int);
insert into t2 values 
@@ -34,18 +35,21 @@ insert into t2 values

# true, false, null, false, null
select a, oref, a in (select max(ie) 
  from t1 where oref=t2.oref group by grp) from t2;
  from t1 where oref=t2.oref group by grp) Z from t2;

# This must have a trigcond
explain extended
select a, oref, a in (select max(ie) 
  from t1 where oref=t2.oref group by grp) from t2;
  from t1 where oref=t2.oref group by grp) Z from t2;

# This must not have a trigcond:
explain extended
select a, oref from t2 
where a in (select max(ie) from t1 where oref=t2.oref group by grp);

select a, oref, a in (
  select max(ie) from t1 where oref=t2.oref group by grp union
  select max(ie) from t1 where oref=t2.oref group by grp
  ) Z from t2;

# Non-correlated subquery, 2 NULL evaluations
create table t3 (a int);
@@ -135,3 +139,336 @@ from t3;

drop table t1, t2, t3;


#
# BUG#24085: Wrong query result for "NULL IN (SELECT ... UNION SELECT ...)"
#

# case 1: NULL IN (SELECT not_null_val FROM ...) w/o HAVING/GROUP-BY/etc
create table t1 (a int NOT NULL, b int NOT NULL, key(a));
insert into t1 values 
  (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);

create table t2 like t1;
insert into t2 select * from t1;
update t2 set b=1;

create table t3 (a int, oref int);
insert into t3 values (1, 1), (NULL,1), (NULL,0);
select a, oref, 
       t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z 
from t3;

--echo This must show a trig_cond:
explain extended
select a, oref, 
       t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z 
from t3;
drop table t1,t2,t3;


# case 2: NULL IN (SELECT not_null_val FROM) where SELECT has GROUP BY
create table t1 (oref int, grp int);
insert into t1 (oref, grp) values
 (1, 1),
 (1, 1);

# Ok, for  
#   select count(*) from t1 group by grp having grp=PARAM
# we'll have:
#  PARAM    subuqery result
#    1  ->    {(2)}   
#    2  ->    {} - empty set
create table t2 (oref int, a int);
insert into t2 values 
  (1, NULL),
  (2, NULL);

select a, oref, 
       a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;

--echo This must show a trig_cond:
explain extended
select a, oref, 
       a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;

drop table t1, t2;

create table t1 (a int, b int, primary key (a));
insert into t1 values (1,1), (3,1),(100,1);
create table t2 (a int, b int);
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);

select a,b, a in (select a from t1 where t1.b = t2.b union select a from
t1 where t1.b = t2.b) Z from t2 ;
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
drop table t1, t2;


#
# BUG#24127: Incorrect results of row-based subqueries with NULLs on the left side.
#
create table t3 (a int);
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2 (a int, b int, oref int);
insert into t2 values (NULL,1, 100), (NULL,2, 100);

create table t1 (a int, b int, c int, key(a,b));
insert into t1 select 2*A, 2*A, 100 from t3;

# First test index subquery engine
explain extended select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;

# Then check that we do turn off 'ref' scans in the subquery
create table t4 (x int);
insert into t4 select A.a + 10*B.a from t1 A, t1 B;
explain extended 
  select a,b, oref, 
         (a,b) in (select a,b from t1,t4 where c=t2.oref) Z 
  from t2;
select a,b, oref, 
       (a,b) in (select a,b from t1,t4 where c=t2.oref) Z 
from t2;

drop table t1,t2,t3,t4;

# More tests for tricky multi-column cases, where some of pushed-down
# equalities are used for index lookups and some arent.
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values
 ('aa', 10, 2, 1),
 ('aa', 10, 1, 1),
 ('aa', 20, 2, 1),
 ('bb', 10, 3, 1),
 ('cc', 10, 4, 2),
 ('cc', 20, 3, 2),
 
 ('ee', 10, 2, 1),
 ('ee', 10, 1, 2),
 
 ('ff', 20, 2, 2),
 ('ff', 20, 1, 2);
create table t2 (oref char(4), a int, b int);
insert into t2 values 
  ('ee', NULL, 1),
  ('bb', 2, 1),
  ('ff', 2, 2),
  ('cc', 3, NULL),
  ('bb', NULL, NULL),
  ('aa', 1, 1),
  ('dd', 1, NULL);
alter table t1 add index idx(ie1,ie2);

--cc 3 NULL NULL
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=3 and b is null ;
insert into t2 values ('new1', 10,10);
insert into t1 values ('new1', 1234, 10, NULL); 
-- new1, 10, 10, NULL,
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10; 
explain extended
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10; 
drop table t1, t2;

# Now test different column types:
create table t1 (oref char(4), grp int, ie int);
insert into t1 (oref, grp, ie) values
 ('aa', 10, 2),
 ('aa', 10, 1),
 ('aa', 20, NULL),

 ('bb', 10, 3),

 ('cc', 10, 4),
 ('cc', 20, NULL),
 
 ('ee', 10, NULL),
 ('ee', 10, NULL),
 
 ('ff', 20, 2),
 ('ff', 20, 1);

create table t2 (oref char(4), a int);
insert into t2 values 
  ('ee', NULL),
  ('bb', 2),
  ('ff', 2),
  ('cc', 3),
  ('aa', 1),
  ('dd', NULL),
  ('bb', NULL);

select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); 

select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);


select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;

select oref, a from t2 where 
  a in (select min(ie) from t1 where oref=t2.oref group by grp);
  
select oref, a from t2 where 
  a not in (select min(ie) from t1 where oref=t2.oref group by grp);

#
update t1 set ie=3 where oref='ff' and ie=1;

select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
grp) Z from t2;


select oref, a from t2 where a in (select min(ie) from t1 where
oref=t2.oref group by grp);

select oref, a from t2 where a not in (select min(ie) from t1 where
oref=t2.oref group by grp);

select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
grp having min(ie) > 1) Z from t2;

select oref, a from t2 where a in (select min(ie) from t1 where
oref=t2.oref group by grp having min(ie) > 1);
  
select oref, a from t2 where a not in (select min(ie) from t1 where
oref=t2.oref group by grp having min(ie) > 1);

#
alter table t1 add index idx(ie);

explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); 

select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);


alter table t1 drop index idx;
alter table t1 add index idx(oref,ie);

explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); 

select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);

explain 
select oref, a, 
       a in (select min(ie) from t1 where oref=t2.oref 
             group by grp having min(ie) > 1) Z 
from t2;

select oref, a, 
       a in (select min(ie) from t1 where oref=t2.oref 
             group by grp having min(ie) > 1) Z 
from t2;

select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref 
                                   group by grp having min(ie) > 1);
  
select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref 
                                       group by grp having min(ie) > 1);

drop table t1,t2;

create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values
 ('aa', 10, 2, 1),
 ('aa', 10, 1, 1),
 ('aa', 20, 2, 1),

 ('bb', 10, 3, 1),

 ('cc', 10, 4, 2),
 ('cc', 20, 3, 2),
 
 ('ee', 10, 2, 1),
 ('ee', 10, 1, 2),
 
 ('ff', 20, 2, 2),
 ('ff', 20, 1, 2);

create table t2 (oref char(4), a int, b int);
insert into t2 values 
  ('ee', NULL, 1),
  ('bb', 2, 1),
  ('ff', 2, 2),
  ('cc', 3, NULL),
  ('bb', NULL, NULL),
  ('aa', 1, 1),
  ('dd', 1, NULL);

select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;

select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref); 

select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);

select oref, a, b, 
             (a,b) in (select min(ie1),max(ie2) from t1 
                       where oref=t2.oref group by grp) Z 
from t2;

select oref, a, b from t2 where 
  (a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
  
select oref, a, b from t2 where
  (a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);

alter table t1 add index idx(ie1,ie2);

explain select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;

select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;

select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref); 

select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);

explain extended 
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;

drop table t1,t2;

create table t1 (oref char(4), grp int, ie int primary key);
insert into t1 (oref, grp, ie) values
 ('aa', 10, 2),
 ('aa', 10, 1),

 ('bb', 10, 3),

 ('cc', 10, 4),
 ('cc', 20, 5),
 ('cc', 10, 6);
 
create table t2 (oref char(4), a int);
insert into t2 values 
  ('ee', NULL),
  ('bb', 2),
  ('cc', 5),
  ('cc', 2),
  ('cc', NULL),
  ('aa', 1),
  ('bb', NULL);

explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;

select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);

select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);

explain 
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;

select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;

drop table t1,t2;
Loading