From 813aaac51d2aaf0c582cbcd869fd48b948b90d66 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Jul 2011 23:37:40 +0300 Subject: [PATCH] Fix for LP BUG#806071 In case of two views with subqueries it is dificult to decide about order of injected ORDER BY clauses. A simple solution is just prohibit ORDER BY injection if there is other order by. mysql-test/r/view.result: New test added, old test changed. mysql-test/t/view.test: New test aded. sql/share/errmsg.txt: new warning added. sql/sql_view.cc: Inject ORDER BY only if there is no other one. Warning about ignoring ORDER BY in this case for EXPLAIN EXTENDED. --- mysql-test/r/view.result | 31 ++++++++++++++++++++++++++++--- mysql-test/t/view.test | 16 ++++++++++++++++ sql/share/errmsg.txt | 2 ++ sql/sql_view.cc | 16 +++++++++++++++- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 241d4f392f8..d511b55dc10 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -3135,17 +3135,18 @@ Warnings: Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` order by `test`.`t1`.`f2` select * from v1 order by f1; f1 f2 -1 1 1 2 1 3 +1 1 +2 3 2 1 2 2 -2 3 explain extended select * from v1 order by f1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 100.00 Using filesort Warnings: -Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` order by `test`.`t1`.`f1`,`test`.`t1`.`f2` +Note 1926 View 'test'.'v1' ORDER BY clause ignored because there is other ORDER BY clause already. +Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` order by `test`.`t1`.`f1` drop view v1; drop table t1; CREATE TABLE t1 ( @@ -4310,5 +4311,29 @@ f1 f2 f3 f4 EXECUTE stmt1; f1 f2 f3 f4 DEALLOCATE PREPARE stmt1; +# +# LP BUG#806071 (2 views with ORDER BY) +# DROP VIEW v1; DROP TABLE t1; +CREATE TABLE t1 (f1 int); +INSERT INTO t1 VALUES (1),(1); +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT f1 FROM t1; +CREATE ALGORITHM=MERGE VIEW v2 AS SELECT f1 FROM v1 ORDER BY f1; +SELECT * FROM v2 AS a1, v2 AS a2; +f1 f1 +1 1 +1 1 +1 1 +1 1 +EXPLAIN EXTENDED SELECT * FROM v2 AS a1, v2 AS a2; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort +1 SIMPLE ALL NULL NULL NULL NULL 2 100.00 Using join buffer (flat, BNL join) +5 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 +3 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1926 View 'test'.'v2' ORDER BY clause ignored because there is other ORDER BY clause already. +Note 1003 select `v1`.`f1` AS `f1`,`v1`.`f1` AS `f1` from `test`.`v1` join `test`.`v1` order by `v1`.`f1` +drop view v1, v2; +drop table t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index fd7ef02353e..2856138da3e 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -4253,5 +4253,21 @@ EXECUTE stmt1; DEALLOCATE PREPARE stmt1; +--echo # +--echo # LP BUG#806071 (2 views with ORDER BY) +--echo # + DROP VIEW v1; DROP TABLE t1; + +CREATE TABLE t1 (f1 int); +INSERT INTO t1 VALUES (1),(1); + +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT f1 FROM t1; +CREATE ALGORITHM=MERGE VIEW v2 AS SELECT f1 FROM v1 ORDER BY f1; + +SELECT * FROM v2 AS a1, v2 AS a2; +EXPLAIN EXTENDED SELECT * FROM v2 AS a1, v2 AS a2; + +drop view v1, v2; +drop table t1; diff --git a/sql/share/errmsg.txt b/sql/share/errmsg.txt index b0989388028..928a28aba24 100644 --- a/sql/share/errmsg.txt +++ b/sql/share/errmsg.txt @@ -6288,3 +6288,5 @@ ER_QUERY_CACHE_IS_DISABLED eng "Query cache is disabled (resize or similar command in progress); repeat this command later" ER_QUERY_CACHE_IS_GLOBALY_DISABLED eng "Query cache is globally disabled and you can't enable it only for this session" +ER_VIEW_ORDERBY_IGNORED + eng "View '%-.192s'.'%-.192s' ORDER BY clause ignored because there is other ORDER BY clause already." diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 89ccfd10a45..535cc30e3e8 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -1483,8 +1483,22 @@ bool mysql_make_view(THD *thd, File_parser *parser, TABLE_LIST *table, We can safely ignore the VIEW's ORDER BY if we merge into union branch, as order is not important there. */ - if (!table->select_lex->master_unit()->is_union()) + if (!table->select_lex->master_unit()->is_union() && + table->select_lex->order_list.elements == 0) table->select_lex->order_list.push_back(&lex->select_lex.order_list); + else + { + if (old_lex->sql_command == SQLCOM_SELECT && + (old_lex->describe & DESCRIBE_EXTENDED) && + lex->select_lex.order_list.elements && + !table->select_lex->master_unit()->is_union()) + { + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, + ER_VIEW_ORDERBY_IGNORED, + ER(ER_VIEW_ORDERBY_IGNORED), + table->db, table->table_name); + } + } /* This SELECT_LEX will be linked in global SELECT_LEX list to make it processed by mysql_handle_derived(),