diff --git a/mysql-test/r/join.result b/mysql-test/r/join.result index db9b051a58f..d33205c418b 100644 --- a/mysql-test/r/join.result +++ b/mysql-test/r/join.result @@ -59,11 +59,9 @@ id count(t2.id) 107 1 select t1.id,t2.id from t2 left join t1 on t1.id>=74 and t1.id<=0 where t2.id=75 and t1.id is null; id id -NULL 75 explain select t1.id,t2.id from t2 left join t1 on t1.id>=74 and t1.id<=0 where t2.id=75 and t1.id is null; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 const PRIMARY NULL NULL NULL 1 Impossible ON condition -1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using where +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables explain select t1.id, t2.id from t1, t2 where t2.id = t1.id and t1.id <0 and t1.id > 0; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables diff --git a/mysql-test/r/join_nested.result b/mysql-test/r/join_nested.result index ae15ac446e5..81ca53cb727 100644 --- a/mysql-test/r/join_nested.result +++ b/mysql-test/r/join_nested.result @@ -1208,3 +1208,22 @@ SELECT * FROM t1 LEFT JOIN t2 LEFT JOIN t3 ON t2.a=t3.a ON t1.a=t3.a; a a a 1 NULL NULL DROP TABLE t1,t2,t3; +CREATE TABLE t1(a int, key (a)); +CREATE TABLE t2(b int, key (b)); +CREATE TABLE t3(c int, key (c)); +INSERT INTO t1 VALUES (NULL), (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), +(10), (11), (12), (13), (14), (15), (16), (17), (18), (19); +INSERT INTO t2 VALUES (NULL), (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), +(10), (11), (12), (13), (14), (15), (16), (17), (18), (19); +INSERT INTO t3 VALUES (0), (1), (2), (3), (4), (5); +EXPLAIN SELECT a, b, c FROM t1 LEFT JOIN (t2, t3) ON c < 3 and b = c; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t3 index c c 5 NULL 6 Using where; Using index +1 SIMPLE t2 ref b b 5 test.t3.c 2 Using where; Using index +1 SIMPLE t1 index NULL a 5 NULL 21 Using index +EXPLAIN SELECT a, b, c FROM t1 LEFT JOIN (t2, t3) ON b < 3 and b = c; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 range b b 5 NULL 3 Using where; Using index +1 SIMPLE t3 ref c c 5 test.t2.b 2 Using where; Using index +1 SIMPLE t1 index NULL a 5 NULL 21 Using index +DROP TABLE t1,t2,t3; diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 16b12a436d3..3f4cfa815d3 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -2181,10 +2181,10 @@ a a a select * from (t1 as t2 left join t1 as t3 using (a)) left outer join t1 on t1.a>1; a a a 1 1 2 -1 1 3 2 2 2 -2 2 3 3 3 2 +1 1 3 +2 2 3 3 3 3 select * from t1 left outer join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; a a a diff --git a/mysql-test/t/join_nested.test b/mysql-test/t/join_nested.test index 8fd3958b509..63623cb5f54 100644 --- a/mysql-test/t/join_nested.test +++ b/mysql-test/t/join_nested.test @@ -727,3 +727,23 @@ DELETE FROM t2; SELECT * FROM t1 LEFT JOIN t2 LEFT JOIN t3 ON t2.a=t3.a ON t1.a=t3.a; DROP TABLE t1,t2,t3; + +#on expression for a nested outer join does not depend on the outer table +#bug #4976 + +CREATE TABLE t1(a int, key (a)); +CREATE TABLE t2(b int, key (b)); +CREATE TABLE t3(c int, key (c)); + +INSERT INTO t1 VALUES (NULL), (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), +(10), (11), (12), (13), (14), (15), (16), (17), (18), (19); + +INSERT INTO t2 VALUES (NULL), (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), +(10), (11), (12), (13), (14), (15), (16), (17), (18), (19); + +INSERT INTO t3 VALUES (0), (1), (2), (3), (4), (5); + +EXPLAIN SELECT a, b, c FROM t1 LEFT JOIN (t2, t3) ON c < 3 and b = c; +EXPLAIN SELECT a, b, c FROM t1 LEFT JOIN (t2, t3) ON b < 3 and b = c; + +DROP TABLE t1,t2,t3; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 444dd5b56d1..4eeef610b0c 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -6077,8 +6077,10 @@ simplify_joins(JOIN *join, List *join_list, COND *conds, bool top) table->embedding->nested_join->used_tables|= used_tables; table->embedding->nested_join->not_null_tables|= not_null_tables; } - - if (!table->outer_join || (used_tables & not_null_tables)) + + if (!table->outer_join || (used_tables & not_null_tables) || + (table->outer_join && + !(table->on_expr->used_tables() & ~used_tables))) { /* For some of the inner tables there are conjunctive predicates