From dc25de58e58bf64c82e290b5c625fb68caa82953 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 4 Oct 2004 22:26:36 -0700 Subject: [PATCH] join_outer.result, join_outer.test: Added a test case for bug #5896. sql_select.cc: Fixed the problem of ignoring on expressions depending only on outer table when outer table either contains 1 row or is guaranteed to return only 1 row (bug #5896). sql/sql_select.cc: Fixed the problem of ignoring on expressions depending only on outer tables when outer tables either contained 1 row or is guaranteed to return not more than 1 row. (bug #5896). mysql-test/t/join_outer.test: Added a test case for bug #5896. mysql-test/r/join_outer.result: Added a test case for bug #5896. --- mysql-test/r/join_outer.result | 46 +++++++++++++++++++++++++++++++++- mysql-test/t/join_outer.test | 27 +++++++++++++++++++- sql/sql_select.cc | 22 ++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/join_outer.result b/mysql-test/r/join_outer.result index 6bafe533fac..35226c56dab 100644 --- a/mysql-test/r/join_outer.result +++ b/mysql-test/r/join_outer.result @@ -1,4 +1,4 @@ -drop table if exists t1,t2,t3,t4,t5; +drop table if exists t0,t1,t2,t3,t4,t5; CREATE TABLE t1 ( grp int(11) default NULL, a bigint(20) unsigned default NULL, @@ -745,3 +745,47 @@ player_id match_1_h * match_id home UUX 7 4 * 1 2 2 3 3 * 1 2 1 drop table t1, t2; +CREATE TABLE t0 (a0 int PRIMARY KEY); +CREATE TABLE t1 (a1 int PRIMARY KEY); +CREATE TABLE t2 (a2 int); +CREATE TABLE t3 (a3 int); +INSERT INTO t0 VALUES (1); +INSERT INTO t1 VALUES (1); +INSERT INTO t2 VALUES (1), (2); +INSERT INTO t3 VALUES (1), (2); +SELECT * FROM t1 LEFT JOIN t2 ON a1=0; +a1 a2 +1 NULL +EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON a1=0; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 system NULL NULL NULL NULL 1 +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 +SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0; +a1 a2 a3 +1 NULL NULL +EXPLAIN SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 system NULL NULL NULL NULL 1 +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 +1 SIMPLE t3 ALL NULL NULL NULL NULL 2 +SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1; +a0 a1 a2 a3 +1 1 NULL NULL +EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 system PRIMARY NULL NULL NULL 1 +1 SIMPLE t1 system PRIMARY NULL NULL NULL 1 +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 +1 SIMPLE t3 ALL NULL NULL NULL NULL 2 +INSERT INTO t0 VALUES (0); +INSERT INTO t1 VALUES (0); +SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1; +a0 a1 a2 a3 +1 1 NULL NULL +EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 +1 SIMPLE t3 ALL NULL NULL NULL NULL 2 +DROP TABLE t0,t1,t2,t3; diff --git a/mysql-test/t/join_outer.test b/mysql-test/t/join_outer.test index 0c4c9614d88..9b5fdb924e6 100644 --- a/mysql-test/t/join_outer.test +++ b/mysql-test/t/join_outer.test @@ -3,7 +3,7 @@ # --disable_warnings -drop table if exists t1,t2,t3,t4,t5; +drop table if exists t0,t1,t2,t3,t4,t5; --enable_warnings CREATE TABLE t1 ( @@ -501,3 +501,28 @@ select s.*, '*', m.*, (s.match_1_h - m.home) UUX from order by UUX desc; drop table t1, t2; + +# Test for bug #5896 + +CREATE TABLE t0 (a0 int PRIMARY KEY); +CREATE TABLE t1 (a1 int PRIMARY KEY); +CREATE TABLE t2 (a2 int); +CREATE TABLE t3 (a3 int); +INSERT INTO t0 VALUES (1); +INSERT INTO t1 VALUES (1); +INSERT INTO t2 VALUES (1), (2); +INSERT INTO t3 VALUES (1), (2); + +SELECT * FROM t1 LEFT JOIN t2 ON a1=0; +EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON a1=0; +SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0; +EXPLAIN SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0; +SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1; +EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1; + +INSERT INTO t0 VALUES (0); +INSERT INTO t1 VALUES (0); +SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1; +EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1; + +DROP TABLE t0,t1,t2,t3; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5d5cb0794f0..e104ac1ca38 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -4895,6 +4895,28 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) COND *const_cond= make_cond_for_table(cond,join->const_table_map,(table_map) 0); DBUG_EXECUTE("where",print_where(const_cond,"constants");); + for (JOIN_TAB *tab= join->join_tab+join->const_tables; + tab < join->join_tab+join->tables ; tab++) + { + if (tab->on_expr) + { + JOIN_TAB *cond_tab= tab->first_inner; + COND *tmp= make_cond_for_table(tab->on_expr, + join->const_table_map, + (table_map) 0); + if (!tmp) + continue; + tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl); + if (!tmp) + DBUG_RETURN(1); + tmp->quick_fix_field(); + cond_tab->select_cond= !cond_tab->select_cond ? tmp : + new Item_cond_and(cond_tab->select_cond,tmp); + if (!cond_tab->select_cond) + DBUG_RETURN(1); + cond_tab->select_cond->quick_fix_field(); + } + } if (const_cond && !const_cond->val_int()) { DBUG_PRINT("info",("Found impossible WHERE condition"));