Commit 584954ab authored by Igor Babaev's avatar Igor Babaev

Fixed LP bug #803851.

The function generate_derived_keys_for_table should set the value of
the number of keys for the derived table to 0 before it starts 
generating key definitions for the table. It's important as the
function can be called twice by the optimizer for a derived table
if the query contains a subquery to which the IN-EXIST transformation
is applicable.

Fixed a valgrind complain.
parent 55165f51
...@@ -735,3 +735,33 @@ INSERT INTO t1 VALUES (7), (1), (3); ...@@ -735,3 +735,33 @@ INSERT INTO t1 VALUES (7), (1), (3);
SELECT * FROM (SELECT * FROM t1 LIMIT 0) t; SELECT * FROM (SELECT * FROM t1 LIMIT 0) t;
a a
DROP TABLE t1; 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 <derived3> 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 <expr_cache><`test`.`t3`.`a`>(<in_optimizer>(`test`.`t3`.`a`,<exists>(select `v1`.`a` from `test`.`v1` join `test`.`t2` where ((`test`.`t2`.`a` = `v1`.`b`) and (<cache>(`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;
...@@ -358,4 +358,30 @@ SELECT * FROM (SELECT * FROM t1 LIMIT 0) t; ...@@ -358,4 +358,30 @@ SELECT * FROM (SELECT * FROM t1 LIMIT 0) t;
DROP TABLE t1; 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;
...@@ -5232,8 +5232,8 @@ void st_table::mark_virtual_columns_for_write(bool insert_fl) ...@@ -5232,8 +5232,8 @@ void st_table::mark_virtual_columns_for_write(bool insert_fl)
bool TABLE::alloc_keys(uint key_count) 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); key_info= s->key_info= (KEY*) alloc_root(&mem_root, sizeof(KEY)*key_count);
s->keys= 0;
max_keys= key_count; max_keys= key_count;
return !(key_info); return !(key_info);
} }
...@@ -5374,7 +5374,7 @@ void TABLE::use_index(int key_to_save) ...@@ -5374,7 +5374,7 @@ void TABLE::use_index(int key_to_save)
DBUG_ASSERT(!created && key_to_save < (int)s->keys); DBUG_ASSERT(!created && key_to_save < (int)s->keys);
if (key_to_save >= 0) if (key_to_save >= 0)
/* Save the given key. */ /* 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 else
/* Drop all keys; */ /* Drop all keys; */
i= 0; i= 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment