Commit acee1edb authored by unknown's avatar unknown

BUG#24127: (a,b) IN (SELECT c,d ...) can produce wrong results if a and/or b are NULLs:

- Make the code produce correct result: use an array of triggers to turn on/off equalities for each
  compared column. Also turn on/off optimizations based on those equalities.
- Make EXPLAIN output show "Full scan on NULL key" for tables for which we switch between
  ref/unique_subquery/index_subquery and ALL access.
- index_subquery engine now has HAVING clause when it is needed, and it is
  displayed in EXPLAIN EXTENDED
- Fix incorrect presense of "Using index" for index/unique-based subqueries (BUG#22930)
// bk trigger note: this commit refers to BUG#24127


mysql-test/r/ndb_subquery.result:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Updated test results (checked)
mysql-test/r/subselect.result:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Updated test results (checked)
mysql-test/r/subselect2.result:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Updated test results (checked)
mysql-test/r/subselect3.result:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Testcases
mysql-test/t/subselect3.test:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Testcases
sql/item_cmpfunc.cc:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - For row-based IN subqueries, use one flag per each column. Set the flags appropriately before
    running the subquery.
sql/item_cmpfunc.h:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
   - Added Item_func_trig_cond::get_triv_var()
sql/item_subselect.cc:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Item_subselect::exec() and subselect_*_engine::exec() don't have parameter
    anymore - now Item_subselect owns the pushed down predicates guard flags.
  - A correct set of conditional predicates is now pushed into row-based IN 
    subquery.
  - select_indexsubquery_engine now has "HAVING clause" (needed for correct query
    results), and it is shown in EXPLAIN EXTENDED
sql/item_subselect.h:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Item_subselect::exec() and subselect_*_engine::exec() don't have parameter
    anymore - now Item_subselect owns the pushed down predicates guard flags.
  - A correct set of conditional predicates is now pushed into row-based IN 
    subquery.
  - select_indexsubquery_engine now has "HAVING clause" (needed for correct query
    results), and it is shown in EXPLAIN EXTENDED
sql/mysql_priv.h:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Added "in_having_cond" special Item name
sql/mysqld.cc:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Added "in_having_cond" special Item name
sql/sql_lex.h:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
sql/sql_select.cc:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Make "ref" analyzer be able to work with conditional equalities
  - Fix subquery optimization code to match the changes in what kinds of 
    conditions are pushed down into subqueries 
  - Fix wrong EXPLAIN output in some queries with subquery (BUG#22390)
sql/sql_select.h:
  BUG#24127: wrong result for (null,not-null) IN (SELECT a,b ...)
  - Make "ref" analyzer be able to work with conditional equalities
  - Fix wrong EXPLAIN output in some queries with subquery (BUG#22390)
parent 710c31f4
......@@ -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
......
......@@ -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;
......@@ -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
......
......@@ -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);
......
This diff is collapsed.
......@@ -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
......@@ -141,7 +142,7 @@ drop table t1, t2, t3;
#
# BUG#24085
# 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
......@@ -172,11 +173,13 @@ 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$
# select count(*) from t1 group by grp having grp=PARAM
# we'll have:
# 1 -> (2)
# 2 -> () - nothing
# PARAM subuqery result
# 1 -> {(2)}
# 2 -> {} - empty set
create table t2 (oref int, a int);
insert into t2 values
(1, NULL),
......@@ -202,3 +205,271 @@ 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;
......@@ -813,11 +813,35 @@ longlong Item_in_optimizer::val_int()
We disable the predicates we've pushed down into subselect, run the
subselect and see if it has produced any rows.
*/
((Item_in_subselect*)args[1])->enable_pushed_conds= FALSE;
Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
if (cache->cols() == 1)
{
item_subs->set_cond_guard_var(0, FALSE);
longlong tmp= args[1]->val_bool_result();
result_for_null_param= null_value= !item_subs->engine->no_rows();
item_subs->set_cond_guard_var(0, TRUE);
}
else
{
uint i;
uint ncols= cache->cols();
/*
Turn off the predicates that are based on column compares for
which the left part is currently NULL
*/
for (i= 0; i < ncols; i++)
{
if (cache->el(i)->null_value)
item_subs->set_cond_guard_var(i, FALSE);
}
longlong tmp= args[1]->val_bool_result();
result_for_null_param= null_value=
!((Item_in_subselect*)args[1])->engine->no_rows();
((Item_in_subselect*)args[1])->enable_pushed_conds= TRUE;
result_for_null_param= null_value= !item_subs->engine->no_rows();
/* Turn all predicates back on */
for (i= 0; i < ncols; i++)
item_subs->set_cond_guard_var(i, TRUE);
}
}
}
return 0;
......
......@@ -311,6 +311,7 @@ class Item_func_trig_cond: public Item_bool_func
enum Functype functype() const { return TRIG_COND_FUNC; };
const char *func_name() const { return "trigcond"; };
bool const_item() const { return FALSE; }
bool *get_trig_var() { return trig_var; }
};
class Item_func_not_all :public Item_func_not
......
This diff is collapsed.
......@@ -94,7 +94,7 @@ class Item_subselect :public Item_result_field
return null_value;
}
bool fix_fields(THD *thd, Item **ref);
virtual bool exec(bool full_scan);
virtual bool exec();
virtual void fix_length_and_dec();
table_map used_tables() const;
table_map not_null_tables() const { return 0; }
......@@ -104,6 +104,7 @@ class Item_subselect :public Item_result_field
Item *get_tmp_table_item(THD *thd);
void update_used_tables();
void print(String *str);
virtual bool have_guarded_conds() { return FALSE; }
bool change_engine(subselect_engine *eng)
{
old_engine= engine;
......@@ -249,13 +250,21 @@ class Item_in_subselect :public Item_exists_subselect
bool transformed;
public:
/* Used to trigger on/off conditions that were pushed down to subselect */
bool enable_pushed_conds;
bool *pushed_cond_guards;
bool *get_cond_guard(int i)
{
return pushed_cond_guards ? pushed_cond_guards + i : NULL;
}
void set_cond_guard_var(int i, bool v) { pushed_cond_guards[i]= v; }
bool have_guarded_conds() { return test(pushed_cond_guards); }
Item_func_not_all *upper_item; // point on NOT/NOP before ALL/SOME subquery
Item_in_subselect(Item * left_expr, st_select_lex *select_lex);
Item_in_subselect()
:Item_exists_subselect(), optimizer(0), abort_on_null(0), transformed(0),
enable_pushed_conds(TRUE), upper_item(0)
pushed_cond_guards(NULL), upper_item(0)
{}
subs_type substype() { return IN_SUBS; }
......@@ -340,23 +349,22 @@ class subselect_engine: public Sql_alloc
SYNOPSIS
exec()
full_scan TRUE - Pushed-down predicates are disabled, the engine
must disable made based on those predicates.
FALSE - Pushed-down predicates are in effect.
DESCRIPTION
Execute the engine. The result of execution is subquery value that is
either captured by previously set up select_result-based 'sink' or
stored somewhere by the exec() method itself.
A required side effect: if full_scan==TRUE, subselect_engine->no_rows()
should return correct result.
A required side effect: If at least one pushed-down predicate is
disabled, subselect_engine->no_rows() must return correct result after
the exec() call.
RETURN
0 - OK
1 - Either an execution error, or the engine was be "changed", and
1 - Either an execution error, or the engine was "changed", and the
caller should call exec() again for the new engine.
*/
virtual int exec(bool full_scan)= 0;
virtual int exec()= 0;
virtual uint cols()= 0; /* return number of columns in select */
virtual uint8 uncacheable()= 0; /* query is uncacheable */
enum Item_result type() { return res_type; }
......@@ -391,7 +399,7 @@ class subselect_single_select_engine: public subselect_engine
void cleanup();
int prepare();
void fix_length_and_dec(Item_cache** row);
int exec(bool full_scan);
int exec();
uint cols();
uint8 uncacheable();
void exclude();
......@@ -415,7 +423,7 @@ class subselect_union_engine: public subselect_engine
void cleanup();
int prepare();
void fix_length_and_dec(Item_cache** row);
int exec(bool full_scan);
int exec();
uint cols();
uint8 uncacheable();
void exclude();
......@@ -429,11 +437,30 @@ class subselect_union_engine: public subselect_engine
struct st_join_table;
/*
A subquery execution engine that evaluates the subquery by doing one index
lookup in a unique index.
This engine is used to resolve subqueries in forms
outer_expr IN (SELECT tbl.unique_key FROM tbl WHERE subq_where)
or, tuple-based:
(oe1, .. oeN) IN (SELECT uniq_key_part1, ... uniq_key_partK
FROM tbl WHERE subqwhere)
i.e. the subquery is a single table SELECT without GROUP BY, aggregate
functions, etc.
*/
class subselect_uniquesubquery_engine: public subselect_engine
{
protected:
st_join_table *tab;
Item *cond;
Item *cond; /* The WHERE condition of subselect */
/*
TRUE<=> last execution produced empty set. Valid only when left
expression is NULL.
......@@ -453,7 +480,7 @@ class subselect_uniquesubquery_engine: public subselect_engine
void cleanup();
int prepare();
void fix_length_and_dec(Item_cache** row);
int exec(bool full_scan);
int exec();
uint cols() { return 1; }
uint8 uncacheable() { return UNCACHEABLE_DEPENDENT; }
void exclude();
......@@ -471,16 +498,47 @@ class subselect_indexsubquery_engine: public subselect_uniquesubquery_engine
{
/* FALSE for 'ref', TRUE for 'ref-or-null'. */
bool check_null;
/*
The "having" clause. This clause (further reffered to as "artificial
having") was inserted by subquery transformation code. It contains
Item(s) that have a side-effect: they record whether the subquery has
produced a row with NULL certain components. We need to use it for cases
like
(oe1, oe2) IN (SELECT t.key, t.no_key FROM t1)
where we do index lookup on t.key=oe1 but need also to check if there
was a row such that t.no_key IS NULL.
NOTE: This is currently here and not in the uniquesubquery_engine. Ideally
it should have been in uniquesubquery_engine in order to allow execution of
subqueries like
(oe1, oe2) IN (SELECT primary_key, non_key_maybe_null_field FROM tbl)
We could use uniquesubquery_engine for the first component and let
Item_is_not_null_test( non_key_maybe_null_field) to handle the second.
However, subqueries like the above are currently not handled by index
lookup-based subquery engines, the engine applicability check misses
them: it doesn't switch the engine for case of artificial having and
[eq_]ref access (only for artifical having + ref_or_null or no having).
The above example subquery is handled as a full-blown SELECT with eq_ref
access to one table.
Due to this limitation, the "artificial having" currently needs to be
checked by only in indexsubquery_engine.
*/
Item *having;
public:
// constructor can assign THD because it will be called after JOIN::prepare
subselect_indexsubquery_engine(THD *thd, st_join_table *tab_arg,
Item_subselect *subs, Item *where,
bool chk_null)
Item *having_arg, bool chk_null)
:subselect_uniquesubquery_engine(thd, tab_arg, subs, where),
check_null(chk_null)
check_null(chk_null),
having(having_arg)
{}
int exec(bool full_scan);
int exec();
void print (String *str);
};
......
......@@ -1197,7 +1197,7 @@ extern const char *command_name[];
extern const char *first_keyword, *my_localhost, *delayed_user, *binary_keyword;
extern const char **errmesg; /* Error messages */
extern const char *myisam_recover_options_str;
extern const char *in_left_expr_name, *in_additional_cond;
extern const char *in_left_expr_name, *in_additional_cond, *in_having_cond;
extern const char * const triggers_file_ext;
extern const char * const trigname_file_ext;
extern Eq_creator eq_creator;
......
......@@ -449,10 +449,13 @@ char *mysqld_unix_port, *opt_mysql_tmpdir;
const char **errmesg; /* Error messages */
const char *myisam_recover_options_str="OFF";
const char *myisam_stats_method_str="nulls_unequal";
/* name of reference on left espression in rewritten IN subquery */
const char *in_left_expr_name= "<left expr>";
/* name of additional condition */
const char *in_additional_cond= "<IN COND>";
const char *in_having_cond= "<IN HAVING>";
my_decimal decimal_zero;
/* classes for comparation parsing/processing */
Eq_creator eq_creator;
......
......@@ -469,7 +469,7 @@ class st_select_lex_unit: public st_select_lex_node {
void set_thd(THD *thd_arg) { thd= thd_arg; }
friend void lex_start(THD *thd, uchar *buf, uint length);
friend int subselect_union_engine::exec(bool);
friend int subselect_union_engine::exec();
List<Item> *get_unit_column_types();
};
......
This diff is collapsed.
......@@ -35,8 +35,17 @@ typedef struct keyuse_t {
satisfied if val has NULL 'value'.
*/
bool null_rejecting;
/* TRUE<=> This ref access is an outer subquery reference access */
bool outer_ref;
/*
!NULL - This KEYUSE was created from an equality that was wrapped into
an Item_func_trig_cond. This means the equality (and validity of
this KEYUSE element) can be turned on and off. The on/off state
is indicted by the pointed value:
*cond_guard == TRUE <=> equality condition is on
*cond_guard == FALSE <=> equality condition is off
NULL - Otherwise (the source equality can't be turned off)
*/
bool *cond_guard;
} KEYUSE;
class store_key;
......@@ -51,6 +60,18 @@ typedef struct st_table_ref
byte *key_buff2; // key_buff+key_length
store_key **key_copy; //
Item **items; // val()'s for each keypart
/*
Array of pointers to trigger variables. Some/all of the pointers may be
NULL. The ref access can be used iff
for each used key part i, (!cond_guards[i] || *cond_guards[i])
This array is used by subquery code. The subquery code may inject
triggered conditions, i.e. conditions that can be 'switched off'. A ref
access created from such condition is not valid when at least one of the
underlying conditions is switched off (see subquery code for more details)
*/
bool **cond_guards;
/*
(null_rejecting & (1<<i)) means the condition is '=' and no matching
rows will be produced if items[i] IS NULL (see add_not_null_conds())
......@@ -99,6 +120,13 @@ enum enum_nested_loop_state
NESTED_LOOP_QUERY_LIMIT= 3, NESTED_LOOP_CURSOR_LIMIT= 4
};
/* Values for JOIN_TAB::packed_info */
#define TAB_INFO_HAVE_VALUE 1
#define TAB_INFO_USING_INDEX 2
#define TAB_INFO_USING_WHERE 4
#define TAB_INFO_FULL_SCAN_ON_NULL 8
typedef enum_nested_loop_state
(*Next_select_func)(JOIN *, struct st_join_table *, bool);
typedef int (*Read_record_func)(struct st_join_table *tab);
......@@ -119,7 +147,15 @@ typedef struct st_join_table {
st_join_table *last_inner; /* last table table for embedding outer join */
st_join_table *first_upper; /* first inner table for embedding outer join */
st_join_table *first_unmatched; /* used for optimization purposes only */
/* Special content for EXPLAIN 'Extra' column or NULL if none */
const char *info;
/*
Bitmap of TAB_INFO_* bits that encodes special line for EXPLAIN 'Extra'
column, or 0 if there is no info.
*/
uint packed_info;
Read_record_func read_first_record;
Next_select_func next_select;
READ_RECORD read_record;
......@@ -386,7 +422,7 @@ class JOIN :public Sql_alloc
Item_sum ***func);
int rollup_send_data(uint idx);
int rollup_write_data(uint idx, TABLE *table);
bool test_in_subselect(Item **where);
void remove_subq_pushed_predicates(Item **where);
/*
Release memory and, if possible, the open tables held by this execution
plan (and nested plans). It's used to release some tables before
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment