[MDEV-22089] Group concat returns incorrect result when using DISTINCT and ORDER BY Created: 2020-03-31  Updated: 2023-11-10

Status: Stalled
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.4, 10.5

Type: Bug Priority: Major
Reporter: Varun Gupta (Inactive) Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-11563 GROUP_CONCAT(DISTINCT ...) may produ... Closed

 Description   

Data set:

create table t1 (a varchar(1000), b varchar(1000), c varchar(1000));
insert t1 select seq,seq, seq from seq_1_to_9;
insert t1 select seq,seq, seq from seq_1_to_9;

set tmp_table_size=1024; // set so that unique tree is dumped to the disk

Now lets run the query without DISTINCT in GROUP_CONCAT

MariaDB [test]> select group_concat(c order by a,b) from t1;
+-------------------------------------+
| group_concat(c order by a,b)        |
+-------------------------------------+
| 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9 |
+-------------------------------------+
1 row in set (0.005 sec)

Now running the query with DISTINCT in GROUP_CONCAT

MariaDB [test]> select group_concat(distinct c order by a,b) from t1;
+---------------------------------------+
| group_concat(distinct c order by a,b) |
+---------------------------------------+
| 1                                     |
+---------------------------------------+
1 row in set (0.006 sec)

This is observed on top of the branch 10.5-mdev11563 (also expected to fail on all other versions)

So the issue here is when Unique does not fit in memory for DISTINCT we flush the unique tree to the disk but in such cases we get incorrect result



 Comments   
Comment by Varun Gupta (Inactive) [ 2020-04-01 ]

Pushed a patch for this to the branch 10.5-mdev11563

Comment by Sergei Petrunia [ 2020-06-07 ]

The patch says:
https://github.com/MariaDB/server/commit/300e28f1cfc835fd301414dac78315de8c854605#diff-fc18518f3f16df7a264a08d03cb2e9d9R3790

// Currently JSON_ARRAYAGG does not support ORDER BY clause

but the parser accepts this?

Comment by Sergei Petrunia [ 2020-06-07 ]

(JSON_ARRAYAGG is already broken by the patch for MDEV-22011, see that MDEV for details)

Comment by Varun Gupta (Inactive) [ 2020-06-12 ]

New patch based on 10.5

https://github.com/MariaDB/server/commit/5a6da66e6f081877b78c81656db9fd2a7e34598a

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