From ab411f8f1c2e84082623c038eb024c15c58745b5 Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Thu, 9 Jun 2011 12:43:28 -0700 Subject: [PATCH] Fixed LP bug #794909. The function generate_derived_keys did not take into account the fact that the last element in the array of keyuses could be just a barrier element. In some cases it could lead to a crash of the server. Also fixed a couple of other bugs in generate_derived_keys: the inner loop in the body of if this function did not change the cycle variables properly. --- mysql-test/r/derived_view.result | 21 +++++++++++++++++++++ mysql-test/t/derived_view.test | 20 ++++++++++++++++++++ sql/sql_select.cc | 14 +++++++++----- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index 5125f91005f..5e3fc1e8cf1 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -568,3 +568,24 @@ TODO: Add test with 64 tables mergeable view to test fall back to materialization on tables > MAX_TABLES merge drop table t1,t2; drop view v1,v2,v3,v4,v6,v7; +# +# LP bug #794909: crash when defining possible keys for +# a materialized view/derived_table +# +CREATE TABLE t1 (f1 int) ; +INSERT INTO t1 VALUES (149), (150), (224), (29); +CREATE TABLE t2 (f1 int, KEY (f1)); +INSERT INTO t2 VALUES (149), (NULL), (224); +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1; +EXPLAIN +SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 index f1 f1 5 NULL 3 Using where; Using index +1 PRIMARY ref key0 key0 5 test.t2.f1 2 +2 DERIVED t1 ALL NULL NULL NULL NULL 4 +SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1; +f1 f1 +149 149 +224 224 +DROP VIEW v1; +DROP TABLE t1,t2; diff --git a/mysql-test/t/derived_view.test b/mysql-test/t/derived_view.test index a8dd703d58c..93c2eb48fcd 100644 --- a/mysql-test/t/derived_view.test +++ b/mysql-test/t/derived_view.test @@ -215,3 +215,23 @@ select * from t1 join (select * from t2 group by f2) tt on t1.f1=tt.f2 join t1 x --echo materialization on tables > MAX_TABLES merge drop table t1,t2; drop view v1,v2,v3,v4,v6,v7; + +--echo # +--echo # LP bug #794909: crash when defining possible keys for +--echo # a materialized view/derived_table +--echo # + +CREATE TABLE t1 (f1 int) ; +INSERT INTO t1 VALUES (149), (150), (224), (29); + +CREATE TABLE t2 (f1 int, KEY (f1)); +INSERT INTO t2 VALUES (149), (NULL), (224); + +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1; + +EXPLAIN +SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1; +SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1; + +DROP VIEW v1; +DROP TABLE t1,t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index e5714ab4bbc..775a0ca2cb1 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8328,6 +8328,8 @@ bool generate_derived_keys(DYNAMIC_ARRAY *keyuse_array) TABLE *prev_table= 0; for (uint i= 0; i < elements; i++, keyuse++) { + if (!keyuse->table) + break; KEYUSE *first_table_keyuse= NULL; table_map last_used_tables= 0; uint count= 0; @@ -8353,11 +8355,13 @@ bool generate_derived_keys(DYNAMIC_ARRAY *keyuse_array) } count++; keyuse++; - if (keyuse->table != prev_table && - generate_derived_keys_for_table(first_table_keyuse, count, ++keys)) - return TRUE; - if (++i == elements) - break; + if (keyuse->table != prev_table) + { + if (generate_derived_keys_for_table(first_table_keyuse, count, ++keys)) + return TRUE; + keyuse--; + derived= NULL; + } } } return FALSE;