Commit 31805e62 authored by Igor Babaev's avatar Igor Babaev

Fixed LP bug #879860.

The MIN/MAX optimization cannot be applied to a subquery if its WHERE clause
contains a conjunctive condition depending on an outer reference.
parent c9259f16
...@@ -1805,4 +1805,27 @@ MAX(a) ...@@ -1805,4 +1805,27 @@ MAX(a)
NULL NULL
DROP TABLE t1,t2; DROP TABLE t1,t2;
# #
# Bug #879860: MIN/MAX for subquery returning empty set
#
CREATE TABLE t1 (a int PRIMARY KEY);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (a int NOT NULL);
INSERT INTO t2 VALUES (10);
CREATE TABLE t3 ( a int, b int);
INSERT INTO t3 VALUES (19,1), (20,5);
EXPLAIN EXTENDED
SELECT (SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b) FROM t3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1 100.00
Warnings:
Note 1276 Field or reference 'test.t3.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select (select min('1') from `test`.`t1` join `test`.`t2` where ('10' = `test`.`t3`.`b`)) AS `(SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b)` from `test`.`t3`
SELECT (SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b) FROM t3;
(SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b)
NULL
NULL
DROP TABLE t1,t2,t3;
#
End of 5.2 tests End of 5.2 tests
...@@ -1174,5 +1174,24 @@ SELECT MAX(a) FROM t1 WHERE RAND()*0<>0 AND a<10; ...@@ -1174,5 +1174,24 @@ SELECT MAX(a) FROM t1 WHERE RAND()*0<>0 AND a<10;
DROP TABLE t1,t2; DROP TABLE t1,t2;
--echo #
--echo # Bug #879860: MIN/MAX for subquery returning empty set
--echo #
CREATE TABLE t1 (a int PRIMARY KEY);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (a int NOT NULL);
INSERT INTO t2 VALUES (10);
CREATE TABLE t3 ( a int, b int);
INSERT INTO t3 VALUES (19,1), (20,5);
EXPLAIN EXTENDED
SELECT (SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b) FROM t3;
SELECT (SELECT MIN(t1.a) FROM t1,t2 WHERE t2.a = t3.b) FROM t3;
DROP TABLE t1,t2,t3;
--echo # --echo #
--echo End of 5.2 tests --echo End of 5.2 tests
...@@ -608,6 +608,10 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, ...@@ -608,6 +608,10 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo,
if (!cond) if (!cond)
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
Field *field= field_part->field; Field *field= field_part->field;
if (cond->used_tables() & OUTER_REF_TABLE_BIT)
{
DBUG_RETURN(FALSE);
}
if (!(cond->used_tables() & field->table->map) && if (!(cond->used_tables() & field->table->map) &&
test(cond->used_tables() & ~PSEUDO_TABLE_BITS)) test(cond->used_tables() & ~PSEUDO_TABLE_BITS))
{ {
......
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