Below is the list of changes that have just been committed into a local
5.0 repository of martin. When martin does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html
ChangeSet@stripped, 2008-01-12 16:06:01+01:00, mhansson@stripped +3 -0
Bug #27219: count(*) in order by
Aggregate functions were allowed in ORDER BY clauses without GROUP BY,
with unclear semantics and inconsistent results.
Fixed by rasing an error in this case if ONLY_FULL_GROUP_BY is set.
mysql-test/r/order_by.result@stripped, 2008-01-12 16:05:56+01:00, mhansson@stripped +42 -0
Bug#27219: Correct result.
mysql-test/t/order_by.test@stripped, 2008-01-12 16:05:57+01:00, mhansson@stripped +41 -0
Bug#27219: Test case.
sql/sql_select.cc@stripped, 2008-01-12 16:05:57+01:00, mhansson@stripped +9 -0
Bug#27219: The fix
diff -Nrup a/mysql-test/r/order_by.result b/mysql-test/r/order_by.result
--- a/mysql-test/r/order_by.result 2007-04-04 07:24:55 +02:00
+++ b/mysql-test/r/order_by.result 2008-01-12 16:05:56 +01:00
@@ -1064,3 +1064,45 @@ a b
10 00:00:10
0 00:00:00
DROP TABLE t1;
+CREATE TABLE t1 (a INT, b INT);
+INSERT INTO t1 VALUES (1, 3), (1, 3), (2, 2), (3, 1), (3, 1), (3, 1);
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+SELECT a FROM t1 ORDER BY COUNT(*);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY COUNT(a);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY COUNT(b);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY a, COUNT(b);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY SUM(a);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY MAX(a);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY MIN(a);
+ERROR HY000: Invalid use of group function
+SELECT a FROM t1 ORDER BY AVG(a);
+ERROR HY000: Invalid use of group function
+SELECT a, COUNT(*) FROM t1 GROUP BY a ORDER BY COUNT(*);
+a COUNT(*)
+2 1
+1 2
+3 3
+SELECT a, COUNT(*) FROM t1 GROUP BY a ORDER BY COUNT(*) DESC;
+a COUNT(*)
+3 3
+1 2
+2 1
+SELECT a, SUM(b) FROM t1 GROUP BY a ORDER BY SUM(b);
+a SUM(b)
+2 2
+3 3
+1 6
+SELECT a, SUM(b) FROM t1 GROUP BY a ORDER BY SUM(b) DESC;
+a SUM(b)
+1 6
+3 3
+2 2
+SET @@sql_mode = @old_sql_mode;
+DROP TABLE t1;
diff -Nrup a/mysql-test/t/order_by.test b/mysql-test/t/order_by.test
--- a/mysql-test/t/order_by.test 2007-06-01 15:49:00 +02:00
+++ b/mysql-test/t/order_by.test 2008-01-12 16:05:57 +01:00
@@ -726,3 +726,44 @@ SELECT a, b FROM t1 ORDER BY b DESC;
SELECT a, b FROM t1 ORDER BY SEC_TO_TIME(a) DESC;
DROP TABLE t1;
+
+#
+# Bug #27219: count(*) in order by
+#
+CREATE TABLE t1 (a INT, b INT);
+INSERT INTO t1 VALUES (1, 3), (1, 3), (2, 2), (3, 1), (3, 1), (3, 1);
+
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY COUNT(*);
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY COUNT(a);
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY COUNT(b);
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY a, COUNT(b);
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY SUM(a);
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY MAX(a);
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY MIN(a);
+
+--error ER_INVALID_GROUP_FUNC_USE
+SELECT a FROM t1 ORDER BY AVG(a);
+
+SELECT a, COUNT(*) FROM t1 GROUP BY a ORDER BY COUNT(*);
+SELECT a, COUNT(*) FROM t1 GROUP BY a ORDER BY COUNT(*) DESC;
+
+SELECT a, SUM(b) FROM t1 GROUP BY a ORDER BY SUM(b);
+SELECT a, SUM(b) FROM t1 GROUP BY a ORDER BY SUM(b) DESC;
+
+SET @@sql_mode = @old_sql_mode;
+
+DROP TABLE t1;
diff -Nrup a/sql/sql_select.cc b/sql/sql_select.cc
--- a/sql/sql_select.cc 2007-10-02 16:45:48 +02:00
+++ b/sql/sql_select.cc 2008-01-12 16:05:57 +01:00
@@ -589,6 +589,15 @@ JOIN::prepare(Item ***rref_pointer_array
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
DBUG_RETURN(-1);
}
+ /* Check that there are no aggregate functions inside ORDER BY. */
+ if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY)
+ for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
+ if ((*tmp_order->item)->with_sum_func)
+ {
+ my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
+ MYF(0));
+ DBUG_RETURN(-1);
+ }
}
}
{
| Thread |
|---|
| • bk commit into 5.0 tree (mhansson:1.2546) BUG#27219 | mhansson | 12 Jan |