Commit ee5795f2 authored by unknown's avatar unknown
Browse files

Fix bug#13327 check_equality() wasn't checking view's fields

check_equality() finds equalities among field items. It checks input items
to be Item_fields thus skipping view's fields, which are represented by
Item_direct_view_ref. Because of this index wasn't applied in all cases
it can be.

To fix this problem check_equality() now takes real item of
Item_direct_view_ref, except outer view refs (with depended_from set).


sql/sql_select.cc:
  Fix bug #13327 VIEW performs index scan
   For proper views fields handling check_equality() now takes real item from Item_direct_view_ref, with exception of outer view refs.
mysql-test/r/view.result:
  Test case for bug#13327 VIEW performs index scan
mysql-test/t/view.test:
  Test case for bug#13327 VIEW performs index scan
parent 20a6f152
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -2298,3 +2298,25 @@ a
3
DROP VIEW v1;
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, INDEX(a,b));
CREATE TABLE t2 LIKE t1;
CREATE TABLE t3 (a INT);
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
INSERT INTO t2 VALUES (1,1),(2,2),(3,3);
INSERT INTO t3 VALUES (1),(2),(3);
CREATE VIEW v1 AS SELECT t1.* FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b;
CREATE VIEW v2 AS SELECT t3.* FROM t1,t3 WHERE t1.a=t3.a;
EXPLAIN SELECT t1.* FROM t1 JOIN t2 WHERE t1.a=t2.a AND t1.b=t2.b AND t1.a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a	a	5	const	1	Using where; Using index
1	SIMPLE	t2	ref	a	a	10	const,test.t1.b	2	Using where; Using index
EXPLAIN SELECT * FROM v1 WHERE a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ref	a	a	5	const	1	Using where; Using index
1	PRIMARY	t2	ref	a	a	10	const,test.t1.b	2	Using where; Using index
EXPLAIN SELECT * FROM v2 WHERE a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ref	a	a	5	const	1	Using where; Using index
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	Using where
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;
+20 −0
Original line number Diff line number Diff line
@@ -2167,3 +2167,23 @@ SELECT v_1.a FROM v1 AS v_1 GROUP BY v_1.a HAVING v_1.a IN (1,2,3);

DROP VIEW v1;
DROP TABLE t1;

#
# Bug #13327 view wasn't using index for const condition
#

CREATE TABLE t1 (a INT, b INT, INDEX(a,b));
CREATE TABLE t2 LIKE t1;
CREATE TABLE t3 (a INT);
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
INSERT INTO t2 VALUES (1,1),(2,2),(3,3);
INSERT INTO t3 VALUES (1),(2),(3);
CREATE VIEW v1 AS SELECT t1.* FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b;
CREATE VIEW v2 AS SELECT t3.* FROM t1,t3 WHERE t1.a=t3.a;
EXPLAIN SELECT t1.* FROM t1 JOIN t2 WHERE t1.a=t2.a AND t1.b=t2.b AND t1.a=1;
EXPLAIN SELECT * FROM v1 WHERE a=1;
EXPLAIN SELECT * FROM v2 WHERE a=1;
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;

+15 −0
Original line number Diff line number Diff line
@@ -6254,6 +6254,21 @@ static bool check_equality(Item *item, COND_EQUAL *cond_equal)
  {
    Item *left_item= ((Item_func*) item)->arguments()[0];
    Item *right_item= ((Item_func*) item)->arguments()[1];

    if (left_item->type() == Item::REF_ITEM &&
        ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
    {
      if (((Item_ref*)left_item)->depended_from)
        return FALSE;
      left_item= left_item->real_item();
    }
    if (right_item->type() == Item::REF_ITEM &&
        ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
    {
      if (((Item_ref*)right_item)->depended_from)
        return FALSE;
      right_item= right_item->real_item();
    }
    if (left_item->type() == Item::FIELD_ITEM &&
        right_item->type() == Item::FIELD_ITEM &&
        !((Item_field*)left_item)->depended_from &&