diff --git a/mysql-test/r/table_elim.result b/mysql-test/r/table_elim.result index 6c713946b27..3200ce392eb 100644 --- a/mysql-test/r/table_elim.result +++ b/mysql-test/r/table_elim.result @@ -566,3 +566,22 @@ id select_type table type possible_keys key key_len ref rows Extra # ^^ The above must not produce a QEP of t3,t5,t2,t4 # as that violates the "no interleaving of outer join nests" rule. DROP TABLE t1,t2,t3,t4,t5; +# +# BUG#884184: Wrong result with RIGHT JOIN + derived_merge +# +CREATE TABLE t1 (a int(11), b varchar(1)) ; +INSERT IGNORE INTO t1 VALUES (0,'g'); +CREATE TABLE t3 ( a varchar(1)) ; +INSERT IGNORE INTO t3 VALUES ('g'); +CREATE TABLE t2 ( a int(11) NOT NULL, PRIMARY KEY (a)) ; +create view v1 as SELECT t1.* FROM t1 LEFT JOIN t2 ON ( t1.a = t2.a ) WHERE t2.a <> 0; +SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b ); +a b +NULL NULL +EXPLAIN SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b ); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t3 system NULL NULL NULL NULL 1 +1 SIMPLE t1 ALL NULL NULL NULL NULL 1 +1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index +drop view v1; +DROP TABLE t1,t2,t3; diff --git a/mysql-test/t/table_elim.test b/mysql-test/t/table_elim.test index 5576362b396..0aeedfb2994 100644 --- a/mysql-test/t/table_elim.test +++ b/mysql-test/t/table_elim.test @@ -499,3 +499,21 @@ WHERE t3.f2 ; DROP TABLE t1,t2,t3,t4,t5; +--echo # +--echo # BUG#884184: Wrong result with RIGHT JOIN + derived_merge +--echo # +CREATE TABLE t1 (a int(11), b varchar(1)) ; +INSERT IGNORE INTO t1 VALUES (0,'g'); + +CREATE TABLE t3 ( a varchar(1)) ; +INSERT IGNORE INTO t3 VALUES ('g'); + +CREATE TABLE t2 ( a int(11) NOT NULL, PRIMARY KEY (a)) ; +create view v1 as SELECT t1.* FROM t1 LEFT JOIN t2 ON ( t1.a = t2.a ) WHERE t2.a <> 0; + +SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b ); +EXPLAIN SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b ); + +drop view v1; +DROP TABLE t1,t2,t3; + diff --git a/sql/opt_table_elimination.cc b/sql/opt_table_elimination.cc index fdf818abb8e..65a7d662425 100644 --- a/sql/opt_table_elimination.cc +++ b/sql/opt_table_elimination.cc @@ -695,6 +695,8 @@ eliminate_tables_for_list(JOIN *join, List *join_list, { table_map outside_used_tables= tables_used_elsewhere | tables_used_on_left; + if (on_expr) + outside_used_tables |= on_expr->used_tables(); if (tbl->nested_join) { /* This is "... LEFT JOIN (join_nest) ON cond" */