mysql ignores view order when selecting with group by

Bug #247727 reported by Derek Price
2
Affects Status Importance Assigned to Milestone
MySQL Server
Unknown
Unknown
mysql-dfsg-5.0 (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Binary package hint: mysql-server

I already reported this to MySQL <http://bugs.mysql.com/bug.php?id=38061&thanks=4>.

----- Begin <http://bugs.mysql.com/bug.php?id=38061&thanks=4> -----
Description:
Under 5.0.51a, SELECT w/GROUP BY ignores the order of an underlying view. Under 5.0.22,
it did not.

How to repeat:
For example, using the following simple DB in both MySQL versions:

DROP TABLE IF EXISTS `source`;
CREATE TABLE `source` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `data` varchar(45) NOT NULL,
  `subkey` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO source (`id`,`data`,`subkey`) VALUES
 (1,'a',1),
 (2,'b',1);

DROP TABLE IF EXISTS `source_view`;
DROP VIEW IF EXISTS `source_view`;
CREATE VIEW `source_view` AS SELECT * FROM source ORDER BY id DESC;

Under 5.0.22, "SELECT * FROM source_view GROUP BY subkey;" yields:

2, 'b', 1

Under 5.0.51a, the same query yields:

1, 'a', 1

Suggested fix:
A simple workaround for the problem as presented above is to skip the intermediate view
entirely and just implement it as a subselect, but there is no workaround if the breaking
select demonstrated above was implemented in a second view, since views do not allow
subselects.

I think that restoring the 5.0.22 behavior would be the right thing to do.
----- End <http://bugs.mysql.com/bug.php?id=38061&thanks=4> -----

Revision history for this message
Derek Price (derek-ximbiot) wrote :

Oh, I'm using the MySQL version from Hardy Heron (8.04LTS), with latest updates:

$ lsb_release -rd
Description: Ubuntu 8.04.1
Release: 8.04
$ dpkg --status mysql-server
Package: mysql-common
...
Source: mysql-dfsg-5.0
Version: 5.0.51a-3ubuntu5.1

Revision history for this message
Giuseppe Maxia (giuseppe-maxia) wrote :

This is expected behavior.
http://dev.mysql.com/doc/refman/5.0/en/select.html

"If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an ORDER BY for the same columns."

"If ORDER BY occurs within a subquery and also is applied in the outer query, the outermost ORDER BY takes precedence. "

The above statements mean that you are sorting by subkey, which has duplicates. In case of two rows having the same value in the sorting column, the returned row is not guaranteed to be in any specific order.

Revision history for this message
Giuseppe Maxia (giuseppe-maxia) wrote :

This is not a bug. It's expected behavior.

Changed in mysql-dfsg-5.0:
status: New → Invalid
Revision history for this message
Chad Miller (cmiller) wrote :

Agreed, not a bug.

More specifically, the row that is chosen to represent a class of rows defined by GROUP BY is undefined. In most other SQL implementations, it is even /illegal/ to SELECT columns that are neither aggregate functions nor the columns listed in the GROUP BY expression, and so you could never even know what row was chosen to represent that group. You should /never/ rely on any value you get outside the GROUP BY expressions or an aggregate function.

You almost certainly should be using a subselect to join grouped data with a particular, /well-defined/ row of the dataset that you're interested in.

Revision history for this message
Derek Price (derek-ximbiot) wrote :

Okay, I guess the previous version of our application was relying on undocumented/unspecified behavior from previous MySQL versions. Anyhow, I've worked out a work around already, so it's no skin off my back. Thanks.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.