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';