1
#! /bin/sh /usr/share/dpatch/dpatch-run
2
## 92_fix_order_by32202.dpatch by Jamie Strandboge <jamie@ubuntu.com>
4
## All lines beginning with `## DP:' are a description of the patch.
8
diff -urNad mysql-dfsg-5.0-5.0.51a~/mysql-test/r/group_by.result mysql-dfsg-5.0-5.0.51a/mysql-test/r/group_by.result
9
--- mysql-dfsg-5.0-5.0.51a~/mysql-test/r/group_by.result 2008-01-11 10:23:35.000000000 -0500
10
+++ mysql-dfsg-5.0-5.0.51a/mysql-test/r/group_by.result 2008-03-17 07:41:43.000000000 -0400
11
@@ -1113,3 +1113,68 @@
16
+id INT AUTO_INCREMENT PRIMARY KEY,
20
+INSERT INTO t1(c1,c2) VALUES (5,1), (4,1), (3,5), (2,3), (1,3);
21
+SELECT * FROM t1 ORDER BY c1;
28
+SELECT * FROM t1 GROUP BY id ORDER BY c1;
35
+SELECT * FROM t1 GROUP BY id ORDER BY id DESC;
42
+SELECT * FROM t1 GROUP BY c2 ,c1, id ORDER BY c2, c1;
49
+SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1;
56
+SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1 DESC;
63
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2, c1;
68
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1;
73
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1 DESC;
80
diff -urNad mysql-dfsg-5.0-5.0.51a~/mysql-test/t/group_by.test mysql-dfsg-5.0-5.0.51a/mysql-test/t/group_by.test
81
--- mysql-dfsg-5.0-5.0.51a~/mysql-test/t/group_by.test 2008-01-11 10:23:11.000000000 -0500
82
+++ mysql-dfsg-5.0-5.0.51a/mysql-test/t/group_by.test 2008-03-17 07:41:43.000000000 -0400
84
SELECT c,b FROM t1 GROUP BY c,b;
89
+# Bug #32202: ORDER BY not working with GROUP BY
93
+ id INT AUTO_INCREMENT PRIMARY KEY,
96
+ UNIQUE KEY (c2,c1));
98
+INSERT INTO t1(c1,c2) VALUES (5,1), (4,1), (3,5), (2,3), (1,3);
100
+# Show that the test cases from the bug report pass
101
+SELECT * FROM t1 ORDER BY c1;
102
+SELECT * FROM t1 GROUP BY id ORDER BY c1;
104
+# Show that DESC is handled correctly
105
+SELECT * FROM t1 GROUP BY id ORDER BY id DESC;
107
+# Show that results are correctly ordered when ORDER BY fields
108
+# are a subset of GROUP BY ones
109
+SELECT * FROM t1 GROUP BY c2 ,c1, id ORDER BY c2, c1;
110
+SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1;
111
+SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1 DESC;
113
+# Show that results are correctly ordered when GROUP BY fields
114
+# are a subset of ORDER BY ones
115
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2, c1;
116
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1;
117
+SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1 DESC;
121
+--echo End of 5.0 tests
122
diff -urNad mysql-dfsg-5.0-5.0.51a~/sql/sql_select.cc mysql-dfsg-5.0-5.0.51a/sql/sql_select.cc
123
--- mysql-dfsg-5.0-5.0.51a~/sql/sql_select.cc 2008-03-17 07:41:25.000000000 -0400
124
+++ mysql-dfsg-5.0-5.0.51a/sql/sql_select.cc 2008-03-17 07:41:43.000000000 -0400
125
@@ -1065,10 +1065,19 @@
126
We have found that grouping can be removed since groups correspond to
127
only one row anyway, but we still have to guarantee correct result
128
order. The line below effectively rewrites the query from GROUP BY
129
- <fields> to ORDER BY <fields>. One exception is if skip_sort_order is
130
- set (see above), then we can simply skip GROUP BY.
131
+ <fields> to ORDER BY <fields>. There are two exceptions:
132
+ - if skip_sort_order is set (see above), then we can simply skip
134
+ - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
135
+ with the GROUP BY ones, i.e. either one is a prefix of another.
136
+ We only check if the ORDER BY is a prefix of GROUP BY. In this case
137
+ test_if_subpart() copies the ASC/DESC attributes from the original
139
+ If GROUP BY is a prefix of ORDER BY, then it is safe to leave
142
- order= skip_sort_order ? 0 : group_list;
143
+ if (!order || test_if_subpart(group_list, order))
144
+ order= skip_sort_order ? 0 : group_list;