Bug #27219 | count(*) in order by | ||
---|---|---|---|
Submitted: | 16 Mar 2007 15:48 | Modified: | 7 Apr 2008 17:24 |
Reporter: | Martin Friebe (Gold Quality Contributor) (OCA) | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server | Severity: | S3 (Non-critical) |
Version: | 5.0.38 | OS: | Linux (Linux) |
Assigned to: | Evgeny Potemkin | CPU Architecture: | Any |
Tags: | count, order by, qc |
[16 Mar 2007 15:48]
Martin Friebe
[16 Mar 2007 15:48]
Martin Friebe
template for test
Attachment: order_by_aggregate.test (application/octet-stream, text), 1.65 KiB.
[19 Mar 2007 10:56]
Sveta Smirnova
Thank you for the report. Verified as described.
[27 Jun 2007 15:34]
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/29733 ChangeSet@1.2498, 2007-06-27 17:35:25+03:00, mhansson@linux-st28.site +3 -0 Bug #27219: count(*) in order by Aggregate functions were allowed in ORDER BY queries even when sql mode ONLY_FULL_GROUP_BY was set. Previously this was checked only for queries with GROUP BY. Fixed by checking for aggregate functions when there is an ORDER BY also.
[4 Jan 2008 14:23]
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/40559 ChangeSet@1.2546, 2008-01-04 15:26:46+01:00, mhansson@linux-st28.site +4 -0 Bug #27219: count(*) in order by Aggregate functions were allowed in ORDER BY clauses even when sql mode ONLY_FULL_GROUP_BY was set. Previously this was checked only for GROUP BY clauses. Fixed by checking for aggregate functions in ORDER BY clause also.
[12 Jan 2008 15:06]
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/40956 ChangeSet@1.2546, 2008-01-12 16:06:01+01:00, mhansson@linux-st28.site +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.
[31 Jan 2008 18:07]
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/41519 ChangeSet@1.2590, 2008-01-31 19:07:28+01:00, mhansson@linux-st28.site +8 -0 Bug#27219: count(*) in order by The error check for using aggregate functions in ORDER BY was incomplete, especially with respect to subqueries. Fixed by extending the check to cater for nesting of grouped statements.
[18 Feb 2008 17:54]
Gleb Shchepa
There is a similar problem with subqueries in the HAVING clause: subquery without GROUP BY clause rises ER_MIX_OF_GROUP_FUNC_AND_FIELDS if its HAVING clause contains set function that was aggregated in the outer query: SELECT t1.a FROM t1 GROUP BY t1.a HAVING t1.a IN (SELECT t2.a FROM t2 HAVING SUM(t1.b) = t2.a); Actually there is no mixing of grouped columns with non-grouped: SUM(t1.b) is aggregated on the outer query level and it is constant on the subquery level, so there are no groupings in subquery at all.
[18 Feb 2008 17:58]
Gleb Shchepa
Oops, please ignore phrase: "so there are no groupings in subquery at all" at the end of previous message, of course there is a grouping without GROUP BY clause.
[18 Feb 2008 19:09]
Gleb Shchepa
Sorry for another mistake: query in the previous messages should be SELECT t1.a FROM t1 GROUP BY t1.a HAVING t1.a IN (SELECT t2.a FROM t2 HAVING SUM(t1.b)); (error message is: "1140: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause")
[22 Feb 2008 1:48]
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/42799 ChangeSet@1.2607, 2008-02-22 05:41:21+04:00, gshchepa@host.loc +7 -0 Fixed bug #27219: mixing of columns and aggregate functions (COUNT/MIN/SUM...) in SELECT and ORDER BY expression list in the query without GROUP BY clause doesn't raise the ER_MIX_OF_GROUP_FUNC_AND_FIELDS error in the ONLY_FULL_GROUP_BY mode. Examples: SELECT a FROM t ORDER BY COUNT(a); SELECT 1 FROM t ORDER BY COUNT(*), a; That has been fixed. Also recognition of parts in the expressions like "SUM(a) + b" has been implemented (queries like "SELECT SUM(a) + b FROM t" evaluated without a error in the ER_MIX_OF_GROUP_FUNC_AND_FIELDS mode).
[14 Mar 2008 14:29]
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/44004 ChangeSet@1.2604, 2008-03-14 17:28:40+03:00, evgen@moonbone.local +7 -0 Bug#27219: Aggregate functions in ORDER BY. Mixing of aggregate functions and non-grouping columns aren't allowed in the ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because of insufficient check. In order to check more thoroughly the new algorithm employs two bitmaps lex->non_agg_fields_used and lex->agg_funcs_used. Each bit in them represents a level of nesting. When it is set to 1 it means that respectively a non aggregated field or an aggregate function were used on this level of select nesting. An error is thrown when bits are set in both variables for a nesting level. Bits are set by the Item_field::fix_fields and the Item_sum::check_sum_func functions.
[24 Mar 2008 21:14]
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/44367 ChangeSet@1.2604, 2008-03-25 00:13:43+03:00, evgen@moonbone.local +8 -0 Bug#27219: Aggregate functions in ORDER BY. Mixing of aggregate functions and non-grouping columns aren't allowed in the ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because of insufficient check. In order to check more thoroughly the new algorithm employs a list of outer fields used in a sum function and a SELECT_LEX::full_group_by_flag. Each non-outer field checked to be aggregated or not by the Item_field::fix_fields function and the current select is marked accordingly. All outer fields that used under an aggregate function are added to the Item_sum::outer_fields list and later checked by the Item_sum::check_sum_func function.
[26 Mar 2008 17:31]
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/44470 ChangeSet@1.2604, 2008-03-26 20:18:10+03:00, evgen@moonbone.local +10 -0 Bug#27219: Aggregate functions in ORDER BY. Mixing aggregate functions and non-grouping columns is not allowed in the ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because of insufficient check. In order to check more thoroughly the new algorithm employs a list of outer fields used in a sum function and a SELECT_LEX::full_group_by_flag. Each non-outer field checked to find out whether it's aggregated or not by the Item_field::fix_fields function and the current select is marked accordingly. All outer fields that are used under an aggregate function are added to the Item_sum::outer_fields list and later checked by the Item_sum::check_sum_func function.
[26 Mar 2008 22:03]
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/44483 ChangeSet@1.2604, 2008-03-27 01:02:49+03:00, evgen@moonbone.local +10 -0 Bug#27219: Aggregate functions in ORDER BY. Mixing aggregate functions and non-grouping columns is not allowed in the ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because of insufficient check. In order to check more thoroughly the new algorithm employs a list of outer fields used in a sum function and a SELECT_LEX::full_group_by_flag. Each non-outer field checked to find out whether it's aggregated or not and the current select is marked accordingly. All outer fields that are used under an aggregate function are added to the Item_sum::outer_fields list and later checked by the Item_sum::check_sum_func function.
[28 Mar 2008 9:56]
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/44564 ChangeSet@1.2601, 2008-03-28 13:53:24+04:00, gshchepa@host.loc +10 -0 Bug#27219: Aggregate functions in ORDER BY. Mixing aggregate functions and non-grouping columns is not allowed in the ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because of insufficient check. In order to check more thoroughly the new algorithm employs a list of outer fields used in a sum function and a SELECT_LEX::full_group_by_flag. Each non-outer field checked to find out whether it's aggregated or not and the current select is marked accordingly. All outer fields that are used under an aggregate function are added to the Item_sum::outer_fields list and later checked by the Item_sum::check_sum_func function.
[31 Mar 2008 14:52]
Bugs System
Pushed into 5.0.60
[31 Mar 2008 14:53]
Bugs System
Pushed into 5.1.24-rc
[31 Mar 2008 18:51]
Paul DuBois
Noted in 5.0.60, 5.1.24 changelogs. In ORDER BY clauses, mixing aggregate functions and non-grouping columns is not allowed if the ONLY_FULL_GROUP_BY SQL mode is enabled. However, in some cases, no error was thrown because of insufficient checking. Resetting report to Patch queued waiting for push into 6.0.x.
[3 Apr 2008 13:01]
Bugs System
Pushed into 6.0.5-alpha
[7 Apr 2008 17:24]
Paul DuBois
Noted in 6.0.5 changelog.