diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 1fb460535ef..8d52dcc22fe 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -2829,3 +2829,34 @@ Warnings: Warning 1292 Truncated incorrect DOUBLE value: 'x' drop table t1; drop view v1; +create table t1(a1 int); +create table t2(a2 int); +insert into t1 values(1),(2); +insert into t2 values(1),(2); +create view v2 (c) as select a1 from t1; +select * from t1 natural left join t2; +a1 a2 +1 1 +1 2 +2 1 +2 2 +select * from t1 natural right join t2; +a2 a1 +1 1 +1 2 +2 1 +2 2 +select * from v2 natural left join t2; +c a2 +1 1 +1 2 +2 1 +2 2 +select * from v2 natural right join t2; +a2 c +1 1 +1 2 +2 1 +2 2 +drop table t1, t2; +drop view v2; diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 250d1972bf7..414af3f9131 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -2406,3 +2406,23 @@ insert into t1 values (1,'x',5); select * from t1 natural join v1; drop table t1; drop view v1; + +# +# Bug #6276 A SELECT that does a NATURAL OUTER JOIN without common +# columns crashes server because of empty ON condition +# + +create table t1(a1 int); +create table t2(a2 int); +insert into t1 values(1),(2); +insert into t2 values(1),(2); +create view v2 (c) as select a1 from t1; + +select * from t1 natural left join t2; +select * from t1 natural right join t2; + +select * from v2 natural left join t2; +select * from v2 natural right join t2; + +drop table t1, t2; +drop view v2; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 763fbc6c336..a7f5fa3ce03 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -3851,6 +3851,11 @@ store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref, */ table_ref_1->natural_join= table_ref_2->natural_join= NULL; + /* Add a TRUE condition to outer joins that have no common columns. */ + if (table_ref_2->outer_join && + !table_ref_1->on_expr && !table_ref_2->on_expr) + table_ref_2->on_expr= new Item_int((longlong) 1,1); /* Always true. */ + /* Change this table reference to become a leaf for name resolution. */ if (left_neighbor) {