Bug #84537 GROUP BY DESC gives wrong result when groups on DECIMAL and sees a NULL
Submitted: 17 Jan 2017 14:11 Modified: 5 Sep 2017 2:22
Reporter: Guilhem Bichot Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.1 OS:Any
Assigned to: CPU Architecture:Any

[17 Jan 2017 14:11] Guilhem Bichot
Description:
I didn't check earlier versions.
Verified with 1890e673719e1421faae20ec373f06d830a81b5a of 8.0.

create table t(d date, i int);
insert into t values(null,1),('2017-01-14',3);

select week(d)/10, group_concat(i) from t group by week(d)/10;
week(d)/10	group_concat(i)
NULL	1
0.2000	3

select week(d)/10, group_concat(i) from t group by week(d)/10 desc;
week(d)/10	group_concat(i)
0.2000	3,1

The last result is incorrect: groups of 0.2 and NULL have been wrongly merged. The result should be that of the non-desc query with the order rows reversed.

How to repeat:
create table t(d date, i int);
insert into t values(null,1),('2017-01-14',3);
select week(d)/10, group_concat(i) from t group by week(d)/10 desc;

Suggested fix:
Cached_item_decimal::cmp(), when it transitions from old row containing NULL to new row not containing NULL (which happens as DESC makes NULL come last), wrongly returns FALSE (which means the rows are in the same group, which is wrong). To fix, please imitate Cached_item_int::cmp().
[5 Sep 2017 2:22] Paul DuBois
Posted by developer:
 
Fixed in 8.0.3.

GROUP BY DESC on DECIMAL values could incorrectly group NULL with
non-NULL values.