[MDEV-8110] Sorting Bug Created: 2015-05-06  Updated: 2015-05-06  Resolved: 2015-05-06

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: None
Fix Version/s: N/A

Type: Bug Priority: Critical
Reporter: Alexander Pentermann Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None

Attachments: File dump.sql    

 Description   

The following initial situation:

A small selection:
A Table (see at dump.sql):

user datum ereignis
1 2007-02-13 Eintritt Maßnahme
1 2007-03-13 Eintritt
2 2007-04-04 Eintritt Maßnahme
2 2007-05-02 Eintritt
2 2008-01-31 Kündigung am
2 2008-02-29 Austritt

When I execute the following Command:

SELECT * FROM
	(SELECT * FROM `userhitory`
	ORDER BY user,datum ASC) s
GROUP BY user

With Maria DB 10.0.17 I get the following result:

user datum ereignis
1 2007-02-13 Eintritt Maßnahme
2 2007-04-04 Eintritt Maßnahme
3 2014-07-01 Eintritt Maßnahme
4 2014-08-27 Eintritt Maßnahme

With MySQL Server 5.5 I get the following result:

user datum ereignis
1 2013-08-15 Austritt
2 2008-02-29 Austritt
3 2015-01-29 Austritt
4 2015-02-26 Austritt

The expected result is the MySQL 5.5 result.

Is there a bug?



 Comments   
Comment by Elena Stepanova [ 2015-05-06 ]

Please see this article which will hopefully answer your question: https://mariadb.com/kb/en/mariadb/why-is-order-by-in-a-from-subquery-ignored/

It describes the general situation with ORDER BY in subqueries.
In your particular case, there is yet another problem – you are using partial GROUP BY, so the result of your query is indeterminate. MySQL manual describes it and specifically mentions that ORDER BY does not make a difference here:
https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.

So, the fact that what you are getting with MySQL now meets your expectations is pure luck and cannot be relied upon. This behavior can change any time, even in a minor release upgrade (and even more likely in the next major version), and it will not be considered as breaking compatibility.

Comment by Alexander Pentermann [ 2015-05-06 ]

ok thank you!

Generated at Thu Feb 08 07:24:43 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.