[MDEV-15381] ORDER BY datediff() reversed with unnamed column in mariaDB Created: 2018-02-21  Updated: 2018-02-22  Resolved: 2018-02-22

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Subquery
Affects Version/s: 10.1.18, 10.2.12
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Francesca Ferrari Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Environment:

macOS High Sierra version 10.13.2
MacBook Air (13-inch, Early 2015)


Issue Links:
Duplicate
duplicates MDEV-15092 Ordering by aggregate expression some... Confirmed

 Description   

ORDER BY datediff() reversed with unnamed column in mariaDB.

The queries below return a different result, but there's only one difference between the two: in the second query, the datediff in the select clause is named and re-used in the ORDER BY, while in the first one it is not named. Specifically, the order is reversed. I was expecting the order to be the same.

FIRST QUERY

select Start_Date, min(End_Date), datediff(min(End_Date), Start_Date)
from (
 select Start_Date 
 from Projects 
 where Start_Date 
 not in (select End_Date from Projects)
) a,
(select End_Date 
 from Projects 
 where End_Date 
 not in (select Start_Date from Projects)
) b
where Start_Date < End_Date
group by Start_Date
order by datediff(min(End_Date), Start_Date)
;
 
+------------+---------------+-------------------------------------+
| Start_Date | min(End_Date) | datediff(min(End_Date), Start_Date) |
+------------+---------------+-------------------------------------+
| 2015-10-01 | 2015-10-04    |                                   3 |
| 2015-10-13 | 2015-10-15    |                                   2 |
| 2015-10-28 | 2015-10-29    |                                   1 |
| 2015-10-30 | 2015-10-31    |                                   1 |
+------------+---------------+-------------------------------------+

SECOND QUERY

select Start_Date, min(End_Date), datediff(min(End_Date), Start_Date) as 'test_diff'
from (
 select Start_Date 
 from Projects 
 where Start_Date 
 not in (select End_Date from Projects)
) a,
(select End_Date 
 from Projects 
 where End_Date 
 not in (select Start_Date from Projects)
) b
where Start_Date < End_Date
group by Start_Date
order by test_diff
;
 
+------------+---------------+-----------+
| Start_Date | min(End_Date) | test_diff |
+------------+---------------+-----------+
| 2015-10-28 | 2015-10-29    |         1 |
| 2015-10-30 | 2015-10-31    |         1 |
| 2015-10-13 | 2015-10-15    |         2 |
| 2015-10-01 | 2015-10-04    |         3 |
+------------+---------------+-----------+

CREATE TABLE AND INSERT STATEMENTS

create table Projects (
  Task_ID int unsigned not null
, Start_Date date not null
, End_Date date not null 
) engine=InnoDB;
 
insert into Projects values 
  (1, '2015-10-01', '2015-10-02')
, (2, '2015-10-02', '2015-10-03')
, (2, '2015-10-03', '2015-10-04')
, (2, '2015-10-13', '2015-10-14')
, (2, '2015-10-14', '2015-10-15')
, (2, '2015-10-28', '2015-10-29')
, (2, '2015-10-30', '2015-10-31')
;



 Comments   
Comment by Alice Sherepa [ 2018-02-22 ]

Thanks for the report!
It looks like this is the same problem as MDEV-15092.
I will close it for now with a note for the engineering to check this case also after fixing.

Generated at Thu Feb 08 08:20:52 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.