From b8785595b9ce92e7d555bf6eb27371c9b7e541a6 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 24 Aug 2004 22:51:23 +0300 Subject: [PATCH] new method to detect commands where all VIEWs should be temporary tables (BUG#4803) mysql-test/r/view.result: Showing VIEW with VIEWs in subquery mysql-test/t/view.test: Showing VIEW with VIEWs in subquery sql/sql_lex.cc: new method to detect commands where all VIEWs should be temporary tables sql/sql_lex.h: new method to detect commands where all VIEWs should be temporary tables sql/sql_view.cc: new method to detect commands where all VIEWs should be temporary tables debug output added --- mysql-test/r/view.result | 9 +++++++++ mysql-test/t/view.test | 11 +++++++++++ sql/sql_lex.cc | 25 ++++++++++++++++++++++++- sql/sql_lex.h | 1 + sql/sql_view.cc | 5 ++++- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 87d64a69e49..d99743c9a03 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -1083,3 +1083,12 @@ count(*) 2 drop view v1; drop table t1; +create table t1 (a int); +create table t2 (a int); +create view v1 as select a from t1; +create view v2 as select a from t2 where a in (select a from v1); +show create view v2; +Table Create Table +v2 CREATE VIEW test.v2 AS select `test`.`t2`.`a` AS `a` from `test`.`t2` where `a` in (select `v1`.`a` AS `a` from `test`.`v1`) +drop view v2, v1; +drop table t1, t2; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 0cc403c5dfd..82f6f46d80c 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -1027,3 +1027,14 @@ insert into t1 values (null); select * from v1; drop view v1; drop table t1; + +# +# Showing VIEW with VIEWs in subquery +# +create table t1 (a int); +create table t2 (a int); +create view v1 as select a from t1; +create view v2 as select a from t2 where a in (select a from v1); +show create view v2; +drop view v2, v1; +drop table t1, t2; diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 0fe0cf51041..f806bc0f123 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1546,7 +1546,7 @@ bool st_lex::can_be_merged() } /* - check if command can use VIEW with MERGE algorithm + check if command can use VIEW with MERGE algorithm (for top VIEWs) SYNOPSIS st_lex::can_use_merged() @@ -1576,6 +1576,29 @@ bool st_lex::can_use_merged() } } +/* + check if command can't use merged views in any part of command + + SYNOPSIS + st_lex::can_not_use_merged() + + RETURN + FALSE - command can't use merged VIEWs + TRUE - VIEWs with MERGE algorithms can be used +*/ + +bool st_lex::can_not_use_merged() +{ + switch (sql_command) + { + case SQLCOM_CREATE_VIEW: + case SQLCOM_SHOW_CREATE: + return TRUE; + default: + return FALSE; + } +} + /* Detect that we need only table structure of derived table/view diff --git a/sql/sql_lex.h b/sql/sql_lex.h index d25381072a5..84b5cf3454b 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -749,6 +749,7 @@ typedef struct st_lex bool can_be_merged(); bool can_use_merged(); + bool can_not_use_merged(); bool only_view_structure(); } LEX; diff --git a/sql/sql_view.cc b/sql/sql_view.cc index f531f2c94f5..3f0e0db1724 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -659,7 +659,8 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table) if (table->algorithm != VIEW_ALGORITHM_TMEPTABLE && lex->can_be_merged() && (table->select_lex->master_unit() != &old_lex->unit || - old_lex->can_use_merged())) + old_lex->can_use_merged()) && + !old_lex->can_not_use_merged()) { /* TODO: support multi tables substitutions @@ -672,6 +673,7 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table) DBUG_ASSERT(view_table != 0); table->effective_algorithm= VIEW_ALGORITHM_MERGE; + DBUG_PRINT("info", ("algorithm: MERGE")); table->updatable= (table->updatable_view != 0); if (old_next) @@ -701,6 +703,7 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table) } table->effective_algorithm= VIEW_ALGORITHM_TMEPTABLE; + DBUG_PRINT("info", ("algorithm: TEMPORARY TABLE")); lex->select_lex.linkage= DERIVED_TABLE_TYPE; table->updatable= 0;