diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index 94254aad671..187f86b8895 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -735,3 +735,33 @@ INSERT INTO t1 VALUES (7), (1), (3); SELECT * FROM (SELECT * FROM t1 LIMIT 0) t; a DROP TABLE t1; +# +# LP bug #803851: materialized view + IN->EXISTS +# +SET SESSION optimizer_switch='semijoin=off,derived_with_keys=on'; +CREATE TABLE t1 (a int, b int); +INSERT INTO t1 VALUES (2,2), (3,3), (1,1); +CREATE TABLE t2 (a int); +INSERT INTO t2 VALUES (1), (2), (1); +CREATE TABLE t3 (a int); +INSERT INTO t3 VALUES (3), (1), (2), (1); +CREATE VIEW v1 AS SELECT a, MAX(b) AS b FROM t1 GROUP BY a; +EXPLAIN EXTENDED +SELECT * FROM t3 +WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 4 100.00 Using where +2 DEPENDENT SUBQUERY ref key0 key0 5 func 2 100.00 +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +3 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using temporary; Using filesort +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <`test`.`t3`.`a`>((`test`.`t3`.`a`,(select `v1`.`a` from `test`.`v1` join `test`.`t2` where ((`test`.`t2`.`a` = `v1`.`b`) and ((`test`.`t3`.`a`) = `v1`.`a`))))) +SELECT * FROM t3 +WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b); +a +1 +2 +1 +SET SESSION optimizer_switch=default; +DROP VIEW v1; +DROP TABLE t1,t2,t3; diff --git a/mysql-test/t/derived_view.test b/mysql-test/t/derived_view.test index 26d8629878c..8348885c745 100644 --- a/mysql-test/t/derived_view.test +++ b/mysql-test/t/derived_view.test @@ -358,4 +358,30 @@ SELECT * FROM (SELECT * FROM t1 LIMIT 0) t; DROP TABLE t1; +--echo # +--echo # LP bug #803851: materialized view + IN->EXISTS +--echo # + +SET SESSION optimizer_switch='semijoin=off,derived_with_keys=on'; + +CREATE TABLE t1 (a int, b int); +INSERT INTO t1 VALUES (2,2), (3,3), (1,1); + +CREATE TABLE t2 (a int); +INSERT INTO t2 VALUES (1), (2), (1); + +CREATE TABLE t3 (a int); +INSERT INTO t3 VALUES (3), (1), (2), (1); +CREATE VIEW v1 AS SELECT a, MAX(b) AS b FROM t1 GROUP BY a; + +EXPLAIN EXTENDED +SELECT * FROM t3 + WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b); +SELECT * FROM t3 + WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b); + +SET SESSION optimizer_switch=default; + +DROP VIEW v1; +DROP TABLE t1,t2,t3; diff --git a/sql/table.cc b/sql/table.cc index d61ab15e957..86f2ae09450 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -5232,8 +5232,8 @@ void st_table::mark_virtual_columns_for_write(bool insert_fl) bool TABLE::alloc_keys(uint key_count) { - DBUG_ASSERT(!s->keys); key_info= s->key_info= (KEY*) alloc_root(&mem_root, sizeof(KEY)*key_count); + s->keys= 0; max_keys= key_count; return !(key_info); } @@ -5374,7 +5374,7 @@ void TABLE::use_index(int key_to_save) DBUG_ASSERT(!created && key_to_save < (int)s->keys); if (key_to_save >= 0) /* Save the given key. */ - memcpy(key_info, key_info + key_to_save, sizeof(KEY)); + memmove(key_info, key_info + key_to_save, sizeof(KEY)); else /* Drop all keys; */ i= 0;