[MDEV-25957] Order is not kept when using DISTINCT + ORDER BY in GROUP_CONCAT Created: 2021-06-18  Updated: 2021-06-18

Status: Open
Project: MariaDB Server
Component/s: Data Definition - Procedure
Affects Version/s: 10.5.8
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Wataru Maeda Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Environment:

CentOS 7.9, Docker



 Description   

The ORDER BY clause in the GROUP_CONCAT function allows you to specify the order, but if you add DISTINCT, the order will not be as expected.

MariaDB [relief]> select version();
+-------------------------------------+
| version()                           |
+-------------------------------------+
| 10.5.8-MariaDB-1:10.5.8+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)
 
MariaDB [relief]> CREATE OR REPLACE TABLE table1 ( id int auto_increment primary key, kind text, year int, ranking int );
Query OK, 0 rows affected (0.014 sec)
 
MariaDB [relief]> INSERT INTO table1 (kind, year, ranking) VALUES ('a', 2019, 1), ('a', 2018, 2), ('a', 2017, 1);
Query OK, 3 rows affected (0.007 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
MariaDB [relief]> INSERT INTO table1 (kind, year, ranking) VALUES ('b', 2019, 6)
    -> ;
Query OK, 1 row affected (0.001 sec)
 
MariaDB [relief]> SELECT * FROM table1;
+----+------+------+---------+
| id | kind | year | ranking |
+----+------+------+---------+
|  1 | a    | 2019 |       1 |
|  2 | a    | 2018 |       2 |
|  3 | a    | 2017 |       1 |
|  4 | b    | 2019 |       6 |
+----+------+------+---------+
4 rows in set (0.000 sec)

Here, I concatted the `ranking` in the order of `year`.

MariaDB [relief]> SELECT GROUP_CONCAT(ranking ORDER BY year) FROM table1 GROUP BY kind;
+-------------------------------------+
| GROUP_CONCAT(ranking ORDER BY year) |
+-------------------------------------+
| 1,2,1                               |
| 6                                   |
+-------------------------------------+
2 rows in set (0.001 sec)
 
MariaDB [relief]>

Then I added DISTINCT.
The expected result is '1,2'.

MariaDB [relief]> SELECT GROUP_CONCAT(DISTINCT ranking ORDER BY year) FROM table1 GROUP BY kind;
+----------------------------------------------+
| GROUP_CONCAT(DISTINCT ranking ORDER BY year) |
+----------------------------------------------+
| 2,1                                          |
| 6                                            |
+----------------------------------------------+
2 rows in set (0.001 sec)
 
MariaDB [relief]>

I tried the same thing with MySQL 5.7, and got the same result.

Is it possible that the order is not kept when DISTINCT is added to the GROUP_CONCAT function?



 Comments   
Comment by Daniel Black [ 2021-06-18 ]

I think this might be duplicate of MDEV-22089

Comment by Wataru Maeda [ 2021-06-18 ]

Thank you for your comment.
I have checked MDEV-22089.
That's when `set tmp_table_size=1024`, so I don't think it's the same problem.

In MDEV-22089, if I try `set tmp_table_size=102400;` then I got the correct result, but in my problem case I did not get the correct result.

Generated at Thu Feb 08 09:41:40 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.