Commit 813aaac5 authored by unknown's avatar unknown

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.
parent c86ffc23
......@@ -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 <derived3> ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
1 SIMPLE <derived5> 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;
......@@ -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;
......@@ -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."
......@@ -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(),
......
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