Bug #46518 | Removing ORDER BY from aggregate query changes result set (produces extra rows) | ||
---|---|---|---|
Submitted: | 2 Aug 2009 22:02 | Modified: | 25 Sep 2009 0:26 |
Reporter: | Patrick Crews | Email Updates: | |
Status: | Duplicate | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.0.85, 5.1, 6.0 | OS: | Any |
Assigned to: | Assigned Account | CPU Architecture: | Any |
Tags: | Aggregate Function, azalea, order by, regression |
[2 Aug 2009 22:02]
Patrick Crews
[21 Sep 2009 11:46]
Jørgen Løland
As described in BUG#46328, the correct behavior is to return 1 record when a group function is used without a group by clause. This may be a duplicate of BUG#46328
[21 Sep 2009 12:29]
Jørgen Løland
Bug is also reproducible in 5.1
[21 Sep 2009 12:47]
Jørgen Løland
SELECT count( `pk` ) field2 FROM D ORDER BY field2 , `pk` ; and SELECT count( * ) field2 ... also returns 100 records (which is wrong), while SELECT sum( 'pk' ) field2 ... returns a single record (which is correct)
[22 Sep 2009 8:44]
Jørgen Løland
Queries that contain aggregate functions but does not have a GROUP BY clause shall return one (or zero) record only (http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html) Furthermore, for non-aggregate fields "...the server is free to return any value from the group, so the results are indeterminate unless all values are the same." (http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-columns.html) Hence, an ORDER BY clause has no value if 1) The query contains one or more aggregate functions, and 2) The query does not contain a GROUP BY clause. Suggested fix for this bug: optimize away ORDER BY for queries satisfying the above conditions.
[25 Sep 2009 0:26]
Omer Barnir
Duplicate of bug#47280
[25 Sep 2009 12:47]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/84648 3133 Jorgen Loland 2009-09-25 Bug#47280 - strange results from count(*) with order by multiple columns without where/group Bug#46518 - Removing ORDER BY from aggregate query changes result set (produces extra rows) Simple SELECT with implicit grouping returns many rows if the ORDER BY clause contains the aggregate function from the SELECT list. Queries with implicit grouping should only return a single record. Function opt_sum_query is called if a query performs grouping but has no fields in the group list. The function tries to substitute count(), min() and max() aggregate functions with constants. In the queries that failed, opt_sum_query found a non-aggregate field and gave up the optimization. However, non-aggregate fields can be substituted with a const in this case: When the select list contains non-aggregate fields not in the group list, the server is free to return any value from the group, so the value in such fields is indeterminate unless all values are the same. @ mysql-test/r/func_group.result Test for BUG#47280 @ mysql-test/t/func_group.test Test for BUG#47280 @ sql/opt_sum.cc Function opt_sum_func gave up const optimization of min/max/count if a non-aggregate field was encountered. This was incorrect since this optimization is only done if the group list is empty (hence, only zero or one row is returned), and the server is free to choose any of the field values in the group. @ sql/sql_select.cc Improved comment for why min/max/count can be optimize away