Bug #105052 GROUP_CONCAT prematurely truncates with UNION
Submitted: 27 Sep 2021 14:46 Modified: 27 Sep 2021 14:55
Reporter: Kim Miesse Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S2 (Serious)
Version:5.7.34, 5.7.35 OS:Any
Assigned to: CPU Architecture:Any

[27 Sep 2021 14:46] Kim Miesse
Description:
GROUP_CONCAT output is truncated prematurely when used directly with UNION (ALL). This is regardless of hitting the group_concat_max_len or not. See How to repeat for details.

How to repeat:
-- Setup with group_concat_max_len = 1024
drop table if exists t1;
create table if not exists `t1`(`a` int,`b` text)engine=innodb default charset=utf8;
insert into t1(a,b) values (2,repeat('a',500)),(2,repeat('b',500));
insert into t1(a,b) values (3,repeat('c',1000)),(3,repeat('d',1000));

-- Without UNION, returns full 1001 or 1024 char for gcb as expected 
select a, group_concat(`b`) as gcb from `t1` group by a;

-- With UNION, prematurely truncates gcb from t1 at 341 char on both from t1
select a, group_concat(`b`) as gcb from `t1` group by a
union select 99, 'sdfs';

Suggested fix:
One fix is to wrap the statement(s) with the GROUP_CONCAT in another SELECT as shown below:

-- Fix: returns 1001 and 1024 char as expected for gcb
select * from (
	select a, group_concat(`b`) as gcb from `t1` group by a) a1
union select 99, 'sdfs';
[27 Sep 2021 14:55] MySQL Verification Team
Hello Kim,

Thank you for the report and test case.

regards,
Umesh