From def04705986a9abad6927c0b52aaf63c136b546b Mon Sep 17 00:00:00 2001 From: Gleb Shchepa Date: Tue, 28 Apr 2009 05:19:13 +0500 Subject: backport from 6.0: Bug #40925: Equality propagation takes non indexed attribute Query execution plans and execution time of queries like select a, b, c from t1 where a > '2008-11-21' and b = a limit 10 depended on the order of equality operator parameters: "b = a" and "a = b" are not same. An equality propagation algorithm has been fixed: the substitute_for_best_equal_field function should not substitute a field for an equal field if both fields belong to the same table. mysql-test/r/select.result: Added test case for bug #40925. mysql-test/t/select.test: Added test case for bug #40925. sql/item.cc: Bug #40925: Equality propagation takes non indexed attribute An equality propagation algorithm has been fixed: the substitute_for_best_equal_field function should not substitute a field for an equal field if both fields belong to the same table. --- mysql-test/r/select.result | 13 +++++++++++++ mysql-test/t/select.test | 14 ++++++++++++++ sql/item.cc | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 9558b0533ad..c9dd65108da 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -4388,4 +4388,17 @@ f3 f4 count 1 abc 1 1 def 2 drop table t1, t2, t3; +CREATE TABLE t1 (a INT KEY, b INT); +INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4); +EXPLAIN EXTENDED SELECT a, b FROM t1 WHERE a > 1 AND a = b LIMIT 2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 Using where +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`b` = `test`.`t1`.`a`) and (`test`.`t1`.`a` > 1)) limit 2 +EXPLAIN EXTENDED SELECT a, b FROM t1 WHERE a > 1 AND b = a LIMIT 2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 Using where +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where ((`test`.`t1`.`a` = `test`.`t1`.`b`) and (`test`.`t1`.`a` > 1)) limit 2 +DROP TABLE t1; End of 5.0 tests diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 1e8dc7ac2f6..60f2f191e0b 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -3737,4 +3737,18 @@ cr.f4 = cr2.f4 GROUP BY a.f3, cr.f4; drop table t1, t2, t3; + + +# +# Bug #40925: Equality propagation takes non indexed attribute +# + +CREATE TABLE t1 (a INT KEY, b INT); +INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4); + +EXPLAIN EXTENDED SELECT a, b FROM t1 WHERE a > 1 AND a = b LIMIT 2; +EXPLAIN EXTENDED SELECT a, b FROM t1 WHERE a > 1 AND b = a LIMIT 2; + +DROP TABLE t1; + --echo End of 5.0 tests diff --git a/sql/item.cc b/sql/item.cc index c284e8b3bf4..13f09914ec6 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4341,7 +4341,7 @@ Item *Item_field::replace_equal_field(byte *arg) return const_item; } Item_field *subst= item_equal->get_first(); - if (subst && !field->eq(subst->field)) + if (subst && field->table != subst->field->table && !field->eq(subst->field)) return subst; } return this; -- cgit v1.2.1