From 2dcec449e0c0854b1d3387dc7d55e3b5969baa1a Mon Sep 17 00:00:00 2001 From: "gshchepa/uchum@host.loc" <> Date: Wed, 26 Mar 2008 22:43:12 +0400 Subject: [PATCH] Fixed bug #35193. View definition as SELECT ... FROM DUAL WHERE ... has valid syntax, but use of such view in SELECT or SHOW CREATE VIEW syntax causes unexpected syntax error. Server omits FROM DUAL clause when storing view body string in a .frm file for further evaluation. However, syntax of SELECT-witout-FROM query is more restrictive than SELECT FROM DUAL syntax, and doesn't allow the WHERE clause. NOTE: this syntax difference is not documented. View registration procedure has been modified to preserve original structure of view's body. --- mysql-test/r/view.result | 18 ++++++++++++++++++ mysql-test/t/view.test | 23 +++++++++++++++++++++++ sql/sql_select.cc | 8 ++++++++ 3 files changed, 49 insertions(+) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 520bf9426b8..eb7a89c3d12 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -3659,6 +3659,24 @@ DROP TABLE t1; # -- End of test case for Bug#34337. +# ----------------------------------------------------------------- +# -- Bug#35193: VIEW query is rewritten without "FROM DUAL", +# -- causing syntax error +# ----------------------------------------------------------------- + +CREATE VIEW v1 AS SELECT 1 FROM DUAL WHERE 1; + +SELECT * FROM v1; +1 +1 +SHOW CREATE TABLE v1; +View Create View +v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 1 AS `1` from DUAL where 1 + +DROP VIEW v1; + +# -- End of test case for Bug#35193. + # ----------------------------------------------------------------- # -- End of 5.0 tests. # ----------------------------------------------------------------- diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 23e64b0546f..9fa981ccb9a 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -3537,6 +3537,29 @@ DROP TABLE t1; ########################################################################### +--echo # ----------------------------------------------------------------- +--echo # -- Bug#35193: VIEW query is rewritten without "FROM DUAL", +--echo # -- causing syntax error +--echo # ----------------------------------------------------------------- +--echo + +CREATE VIEW v1 AS SELECT 1 FROM DUAL WHERE 1; + +--echo + +SELECT * FROM v1; +SHOW CREATE TABLE v1; + +--echo + +DROP VIEW v1; + +--echo +--echo # -- End of test case for Bug#35193. +--echo + +########################################################################### + --echo # ----------------------------------------------------------------- --echo # -- End of 5.0 tests. --echo # ----------------------------------------------------------------- diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 6392f7c4299..bb3030a721c 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -15806,6 +15806,14 @@ void st_select_lex::print(THD *thd, String *str) /* go through join tree */ print_join(thd, str, &top_join_list); } + else if (where) + { + /* + "SELECT 1 FROM DUAL WHERE 2" should not be printed as + "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not. + */ + str->append(STRING_WITH_LEN(" from DUAL ")); + } // Where Item *cur_where= where;