~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to mysql-test/t/func_gconcat.test

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# simple test of group_concat function
 
3
#
 
4
--disable_warnings
 
5
drop table if exists t1, t2;
 
6
--enable_warnings
 
7
 
 
8
create table t1 (grp int, a bigint unsigned, c char(10) not null, d char(10) not null);
 
9
insert into t1 values (1,1,"a","a");
 
10
insert into t1 values (2,2,"b","a");
 
11
insert into t1 values (2,3,"c","b");
 
12
insert into t1 values (3,4,"E","a");
 
13
insert into t1 values (3,5,"C","b");
 
14
insert into t1 values (3,6,"D","b");
 
15
insert into t1 values (3,7,"d","d");
 
16
insert into t1 values (3,8,"d","d");
 
17
insert into t1 values (3,9,"D","c");
 
18
 
 
19
# Test of MySQL simple request
 
20
select grp,group_concat(c) from t1 group by grp;
 
21
explain extended select grp,group_concat(c) from t1 group by grp;
 
22
select grp,group_concat(a,c) from t1 group by grp;
 
23
select grp,group_concat("(",a,":",c,")") from t1 group by grp;
 
24
 
 
25
# Test of MySQL with options
 
26
select grp,group_concat(c separator ",") from t1 group by grp;
 
27
select grp,group_concat(c separator "---->") from t1 group by grp;
 
28
select grp,group_concat(c order by c) from t1 group by grp;
 
29
select grp,group_concat(c order by c desc) from t1 group by grp;
 
30
select grp,group_concat(d order by a) from t1 group by grp;
 
31
select grp,group_concat(d order by a desc) from t1 group by grp;
 
32
--disable_warnings
 
33
select grp,group_concat(a order by a,d+c-ascii(c)-a) from t1 group by grp;
 
34
select grp,group_concat(a order by d+c-ascii(c),a) from t1 group by grp;
 
35
--enable_warnings
 
36
select grp,group_concat(c order by 1) from t1 group by grp;
 
37
select grp,group_concat(distinct c order by c) from t1 group by grp;
 
38
select grp,group_concat(distinct c order by c desc) from t1 group by grp;
 
39
explain extended select grp,group_concat(distinct c order by c desc) from t1 group by grp;
 
40
select grp,group_concat(c order by c separator ",") from t1 group by grp;
 
41
select grp,group_concat(c order by c desc separator ",") from t1 group by grp;
 
42
select grp,group_concat(distinct c order by c separator ",") from t1 group by grp;
 
43
explain extended select grp,group_concat(distinct c order by c separator ",") from t1 group by grp;
 
44
select grp,group_concat(distinct c order by c desc separator ",") from t1 group by grp;
 
45
 
 
46
# Test of SQL_LIST objects
 
47
select grp,group_concat(c order by grp desc) from t1 group by grp order by grp;
 
48
 
 
49
 
 
50
# Test transfer to real values
 
51
 
 
52
select grp, group_concat(a separator "")+0 from t1 group by grp;
 
53
select grp, group_concat(a separator "")+0.0 from t1 group by grp;
 
54
select grp, ROUND(group_concat(a separator "")) from t1 group by grp;
 
55
drop table t1;
 
56
 
 
57
# Test NULL values
 
58
 
 
59
create table t1 (grp int, c char(10));
 
60
insert into t1 values (1,NULL),(2,"b"),(2,NULL),(3,"E"),(3,NULL),(3,"D"),(3,NULL),(3,NULL),(3,"D"),(4,""),(5,NULL);
 
61
select grp,group_concat(c order by c) from t1 group by grp;
 
62
 
 
63
# Test warnings
 
64
 
 
65
set group_concat_max_len = 4;
 
66
select grp,group_concat(c) from t1 group by grp;
 
67
show warnings;
 
68
set group_concat_max_len = 1024;
 
69
 
 
70
# Test errors
 
71
 
 
72
--error 1111
 
73
select group_concat(sum(c)) from t1 group by grp;
 
74
--error 1054
 
75
select grp,group_concat(c order by 2) from t1 group by grp;
 
76
 
 
77
drop table t1;
 
78
 
 
79
# Test variable length
 
80
 
 
81
create table t1 ( URL_ID int(11), URL varchar(80));
 
82
create table t2 ( REQ_ID int(11), URL_ID int(11));
 
83
insert into t1 values (4,'www.host.com'), (5,'www.google.com'),(5,'www.help.com');
 
84
insert into t2 values (1,4), (5,4), (5,5);
 
85
# Make this order independent
 
86
--replace_result www.help.com X www.host.com X www.google.com X
 
87
select REQ_ID, Group_Concat(URL) as URL from t1, t2 where
 
88
t2.URL_ID = t1.URL_ID group by REQ_ID;
 
89
# check min/max function
 
90
--replace_result www.help.com X www.host.com X www.google.com X
 
91
select REQ_ID, Group_Concat(URL) as URL, Min(t1.URL_ID) urll,
 
92
Max(t1.URL_ID) urlg from t1, t2 where t2.URL_ID = t1.URL_ID group by REQ_ID;
 
93
 
 
94
drop table t1;
 
95
drop table t2;
 
96
 
 
97
create table t1 (id int, name varchar(16));
 
98
insert into t1 values (1,'longername'),(1,'evenlongername');
 
99
select ifnull(group_concat(concat(t1.id, ':', t1.name)), 'shortname') as 'without distinct: how it should be' from t1;
 
100
select distinct ifnull(group_concat(concat(t1.id, ':', t1.name)), 'shortname') as 'with distinct: cutoff at length of shortname' from t1;
 
101
drop table t1;
 
102
 
 
103
# check zero rows (bug#836)
 
104
create table t1(id int);
 
105
create table t2(id int);
 
106
insert into t1 values(0),(1);
 
107
select group_concat(t1.id) FROM t1,t2;
 
108
drop table t1;
 
109
drop table t2;
 
110
 
 
111
# check having
 
112
create table t1 (bar varchar(32));
 
113
insert into t1 values('test1'),('test2');
 
114
select group_concat(bar order by concat(bar,bar)) from t1;
 
115
select group_concat(bar order by concat(bar,bar) desc) from t1;
 
116
select bar from t1 having group_concat(bar)='';
 
117
select bar from t1 having instr(group_concat(bar), "test") > 0;
 
118
select bar from t1 having instr(group_concat(bar order by concat(bar,bar) desc), "test2,test1") > 0;
 
119
drop table t1;
 
120
 
 
121
# ORDER BY fix_fields()
 
122
create table t1 (a int, a1 varchar(10));
 
123
create table t2 (a0 int);
 
124
insert into t1 values (0,"a"),(0,"b"),(1,"c");
 
125
insert into t2 values (1),(2),(3);
 
126
select  group_concat(a1 order by (t1.a IN (select a0 from t2))) from t1;
 
127
select  group_concat(a1 order by (t1.a)) from t1;
 
128
drop table t1, t2;
 
129
 
 
130
#
 
131
# Problem with GROUP BY (Bug #2695)
 
132
#
 
133
 
 
134
CREATE TABLE t1 (id1 tinyint(4) NOT NULL, id2 tinyint(4) NOT NULL);
 
135
INSERT INTO t1 VALUES (1, 1),(1, 2),(1, 3),(1, 4),(1, 5),(2, 1),(2, 2),(2, 3);
 
136
CREATE TABLE t2 (id1 tinyint(4) NOT NULL);
 
137
INSERT INTO t2 VALUES (1),(2),(3),(4),(5);
 
138
SELECT t1.id1, GROUP_CONCAT(t1.id2 ORDER BY t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 AND t1.id1=1 GROUP BY t1.id1;
 
139
SELECT t1.id1, GROUP_CONCAT(t1.id2 ORDER BY t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
140
SELECT t1.id1, GROUP_CONCAT(t1.id2 ORDER BY t1.id2 DESC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
141
SELECT t1.id1, GROUP_CONCAT(t1.id2 ORDER BY 6-t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
142
 
 
143
# The following failed when it was run twice:
 
144
SELECT t1.id1, GROUP_CONCAT(t1.id2,6-t1.id2 ORDER BY 6-t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
145
SELECT t1.id1, GROUP_CONCAT(t1.id2,6-t1.id2 ORDER BY 6-t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
146
 
 
147
SELECT t1.id1, GROUP_CONCAT(t1.id2,"/",6-t1.id2 ORDER BY 1+0,6-t1.id2,t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 GROUP BY t1.id1;
 
148
drop table t1,t2;
 
149
 
 
150
#
 
151
# Problem with distinct (Bug #3381)
 
152
#
 
153
 
 
154
create table t1 (s1 char(10), s2 int not null);
 
155
insert into t1 values ('a',2),('b',2),('c',1),('a',3),('b',4),('c',4);
 
156
select distinct s1 from t1 order by s2,s1;
 
157
select group_concat(distinct s1) from t1;
 
158
select group_concat(distinct s1 order by s2) from t1 where s2 < 4;
 
159
# The following is wrong and needs to be fixed ASAP
 
160
select group_concat(distinct s1 order by s2) from t1;
 
161
drop table t1;
 
162
 
 
163
#
 
164
# Test with subqueries (Bug #3319)
 
165
#
 
166
 
 
167
create table t1 (a int, c int);
 
168
insert into t1 values (1, 2), (2, 3), (2, 4), (3, 5);
 
169
create table t2 (a int, c int);
 
170
insert into t2 values (1, 5), (2, 4), (3, 3), (3,3);
 
171
select group_concat(c) from t1;
 
172
select group_concat(c order by (select c from t2 where t2.a=t1.a limit 1)) as grp from t1;
 
173
select group_concat(c order by (select mid(group_concat(c order by a),1,5) from t2 where t2.a=t1.a)) as grp from t1;
 
174
select group_concat(c order by (select mid(group_concat(c order by a),1,5) from t2 where t2.a=t1.a) desc) as grp from t1;
 
175
select t1.a, group_concat(c order by (select c from t2 where t2.a=t1.a limit 1)) as grp from t1 group by 1;
 
176
select t1.a, group_concat(c order by (select mid(group_concat(c order by a),1,5) from t2 where t2.a=t1.a)) as grp from t1 group by 1;
 
177
select t1.a, group_concat(c order by (select mid(group_concat(c order by a),1,5) from t2 where t2.a=t1.a) desc) as grp from t1 group by 1;
 
178
 
 
179
# The following returns random results as we are sorting on blob addresses
 
180
select group_concat(c order by (select concat(5-t1.c,group_concat(c order by a)) from t2 where t2.a=t1.a)) as grp from t1;
 
181
select group_concat(c order by (select concat(t1.c,group_concat(c)) from t2 where a=t1.a)) as grp from t1;
 
182
 
 
183
select a,c,(select group_concat(c order by a) from t2 where a=t1.a) as grp from t1 order by grp;
 
184
drop table t1,t2;
 
185
 
 
186
#
 
187
# group_concat of expression with GROUP BY and external GROUP BY
 
188
#
 
189
CREATE TABLE t1 ( a int );
 
190
CREATE TABLE t2 ( a int );
 
191
INSERT INTO t1 VALUES (1), (2);
 
192
INSERT INTO t2 VALUES (1), (2);
 
193
SELECT GROUP_CONCAT(t1.a*t2.a ORDER BY t2.a) FROM t1, t2 GROUP BY t1.a;
 
194
DROP TABLE t1, t2;
 
195
 
 
196
#
 
197
# Bug #4035: group_concat() and HAVING
 
198
#
 
199
 
 
200
CREATE TABLE t1 (a char(4));
 
201
INSERT INTO t1 VALUES ('John'), ('Anna'), ('Bill');
 
202
SELECT GROUP_CONCAT(a SEPARATOR '||') AS names FROM t1 
 
203
  HAVING names LIKE '%An%';
 
204
SELECT GROUP_CONCAT(a SEPARATOR '###') AS names FROM t1 
 
205
  HAVING LEFT(names, 1) ='J';
 
206
DROP TABLE t1;
 
207
 
 
208
#
 
209
# check blobs
 
210
#
 
211
 
 
212
CREATE TABLE t1 ( a int, b TEXT );
 
213
INSERT INTO t1 VALUES (1,'First Row'), (2,'Second Row');
 
214
SELECT GROUP_CONCAT(b ORDER BY b) FROM t1 GROUP BY a;
 
215
DROP TABLE t1;
 
216
 
 
217
#
 
218
# check null values #2
 
219
#
 
220
 
 
221
CREATE TABLE t1 (A_ID INT NOT NULL,A_DESC CHAR(3) NOT NULL,PRIMARY KEY (A_ID));
 
222
INSERT INTO t1 VALUES (1,'ABC'), (2,'EFG'), (3,'HIJ');
 
223
CREATE TABLE t2 (A_ID INT NOT NULL,B_DESC CHAR(3) NOT NULL,PRIMARY KEY (A_ID,B_DESC));
 
224
INSERT INTO t2 VALUES (1,'A'),(1,'B'),(3,'F');
 
225
SELECT t1.A_ID, GROUP_CONCAT(t2.B_DESC) AS B_DESC FROM t1 LEFT JOIN t2 ON t1.A_ID=t2.A_ID GROUP BY t1.A_ID ORDER BY t1.A_DESC;
 
226
DROP TABLE t1;
 
227
DROP TABLE t2;
 
228
 
 
229
#
 
230
# blobs
 
231
#
 
232
 
 
233
create table t1 (a int, b text);
 
234
insert into t1 values (1, 'bb'), (1, 'ccc'), (1, 'a'), (1, 'bb'), (1, 'ccc');
 
235
insert into t1 values (2, 'BB'), (2, 'CCC'), (2, 'A'), (2, 'BB'), (2, 'CCC');
 
236
select group_concat(b) from t1 group by a;
 
237
select group_concat(distinct b) from t1 group by a;
 
238
select group_concat(b order by b) from t1 group by a;
 
239
select group_concat(distinct b order by b) from t1 group by a;
 
240
set local group_concat_max_len=4;
 
241
select group_concat(b) from t1 group by a;
 
242
select group_concat(distinct b) from t1 group by a;
 
243
select group_concat(b order by b) from t1 group by a;
 
244
select group_concat(distinct b order by b) from t1 group by a;
 
245
 
 
246
#
 
247
# long blobs
 
248
#
 
249
 
 
250
insert into t1 values (1, concat(repeat('1', 300), '2')), 
 
251
(1, concat(repeat('1', 300), '2')), (1, concat(repeat('0', 300), '1')), 
 
252
(2, concat(repeat('1', 300), '2')), (2, concat(repeat('1', 300), '2')), 
 
253
(2, concat(repeat('0', 300), '1'));
 
254
set local group_concat_max_len=1024;
 
255
select group_concat(b) from t1 group by a;
 
256
select group_concat(distinct b) from t1 group by a;
 
257
select group_concat(b order by b) from t1 group by a;
 
258
select group_concat(distinct b order by b) from t1 group by a;
 
259
set local group_concat_max_len=400;
 
260
select group_concat(b) from t1 group by a;
 
261
select group_concat(distinct b) from t1 group by a;
 
262
select group_concat(b order by b) from t1 group by a;
 
263
select group_concat(distinct b order by b) from t1 group by a;
 
264
 
 
265
drop table t1;
 
266
 
 
267
#
 
268
# Bug#10201
 
269
#
 
270
create table t1 (a varchar(255) character set cp1250 collate cp1250_general_ci,
 
271
                 b varchar(255) character set koi8r);
 
272
insert into t1 values ('xxx','yyy');
 
273
select collation(a) from t1;
 
274
select collation(group_concat(a)) from t1;
 
275
create table t2 select group_concat(a) as a from t1;
 
276
show create table t2;
 
277
select collation(group_concat(a,_koi8r'test')) from t1;
 
278
--error 1267
 
279
select collation(group_concat(a,_koi8r 0xC1C2)) from t1;
 
280
--error 1267
 
281
select collation(group_concat(a,b)) from t1;
 
282
drop table t1;
 
283
drop table t2;
 
284
 
 
285
#
 
286
# Bug #12829
 
287
# Cannot convert the charset of a GROUP_CONCAT result
 
288
#
 
289
CREATE TABLE t1 (a CHAR(10) CHARACTER SET cp850);
 
290
INSERT INTO t1 VALUES ('�');
 
291
SELECT a FROM t1;
 
292
SELECT GROUP_CONCAT(a) FROM t1;
 
293
DROP TABLE t1;
 
294
 
 
295
#
 
296
# bug #7769: group_concat returning null is checked in having
 
297
#
 
298
CREATE TABLE t1 (id int);
 
299
SELECT GROUP_CONCAT(id) AS gc FROM t1 HAVING gc IS NULL;
 
300
DROP TABLE t1;
 
301
 
 
302
#
 
303
# Bug #8656: Crash with group_concat on alias in outer table
 
304
#
 
305
create table t2 (a int, b int);
 
306
insert into t2 values (1,1), (2,2);
 
307
select  b x, (select group_concat(x) from t2) from  t2;
 
308
drop table t2;
 
309
 
 
310
#
 
311
# Bug #7405: problems with rollup
 
312
#
 
313
 
 
314
create table t1 (d int not null auto_increment,primary key(d), a int, b int, c int);
 
315
insert into t1(a,b) values (1,3), (1,4), (1,2), (2,7), (1,1), (1,2), (2,3), (2,3);
 
316
select d,a,b from t1 order by a;
 
317
explain select a, group_concat(b) from t1 group by a with rollup;
 
318
select a, group_concat(b) from t1 group by a with rollup;
 
319
select a, group_concat(distinct b) from t1 group by a with rollup;
 
320
select a, group_concat(b order by b) from t1 group by a with rollup;
 
321
select a, group_concat(distinct b order by b) from t1 group by a with rollup;
 
322
drop table t1;
 
323
 
 
324
#
 
325
# Bug #6475
 
326
#
 
327
create table t1 (a char(3), b char(20), primary key (a, b));
 
328
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
 
329
select group_concat(a) from t1 group by b;
 
330
drop table t1;
 
331
#
 
332
# Bug #12095: GROUP_CONCAT for one row table 
 
333
#
 
334
 
 
335
CREATE TABLE t1 (
 
336
  aID smallint(5) unsigned NOT NULL auto_increment,
 
337
  sometitle varchar(255) NOT NULL default '',
 
338
  bID smallint(5) unsigned NOT NULL,
 
339
  PRIMARY KEY  (aID),
 
340
  UNIQUE KEY sometitle (sometitle)
 
341
);
 
342
INSERT INTO t1 SET sometitle = 'title1', bID = 1;
 
343
INSERT INTO t1 SET sometitle = 'title2', bID = 1;
 
344
 
 
345
CREATE TABLE t2 (
 
346
  bID smallint(5) unsigned NOT NULL auto_increment,
 
347
  somename varchar(255) NOT NULL default '',
 
348
  PRIMARY KEY  (bID),
 
349
  UNIQUE KEY somename (somename)
 
350
);
 
351
INSERT INTO t2 SET somename = 'test';
 
352
 
 
353
SELECT COUNT(*), GROUP_CONCAT(DISTINCT t2.somename SEPARATOR ' |')
 
354
  FROM t1 JOIN t2 ON t1.bID = t2.bID;
 
355
INSERT INTO t2 SET somename = 'test2';
 
356
SELECT COUNT(*), GROUP_CONCAT(DISTINCT t2.somename SEPARATOR ' |')
 
357
  FROM t1 JOIN t2 ON t1.bID = t2.bID;
 
358
DELETE FROM t2 WHERE somename = 'test2';
 
359
SELECT COUNT(*), GROUP_CONCAT(DISTINCT t2.somename SEPARATOR ' |')
 
360
  FROM t1 JOIN t2 ON t1.bID = t2.bID;
 
361
 
 
362
DROP TABLE t1,t2;
 
363
 
 
364
#
 
365
# Bug #12859 group_concat in subquery cause incorrect not null
 
366
#
 
367
create table t1 ( a int not null default 0);
 
368
select * from (select group_concat(a) from t1) t2;
 
369
select group_concat('x') UNION ALL select 1;
 
370
drop table t1;
 
371
 
 
372
#
 
373
# Bug #12863 : missing separators after first empty cancatanated elements
 
374
#
 
375
 
 
376
CREATE TABLE t1 (id int, a varchar(9));
 
377
INSERT INTO t1 VALUES
 
378
  (2, ''), (1, ''), (2, 'x'), (1, 'y'), (3, 'z'), (3, '');
 
379
 
 
380
SELECT GROUP_CONCAT(a) FROM t1;
 
381
SELECT GROUP_CONCAT(a ORDER BY a) FROM t1;
 
382
 
 
383
SELECT GROUP_CONCAT(a) FROM t1 GROUP BY id;
 
384
SELECT GROUP_CONCAT(a ORDER BY a) FROM t1 GROUP BY id;
 
385
 
 
386
DROP TABLE t1;
 
387
 
 
388
#
 
389
# Bug #15560: GROUP_CONCAT wasn't ready for WITH ROLLUP queries
 
390
#
 
391
create table t1(f1 int);
 
392
insert into t1 values(1),(2),(3);
 
393
select f1, group_concat(f1+1) from t1 group by f1 with rollup;
 
394
select count(distinct (f1+1)) from t1 group by f1 with rollup;
 
395
drop table t1;
 
396
 
 
397
#
 
398
# Bug#14169 type of group_concat() result changed to blob if tmp_table was used
 
399
#
 
400
create table t1 (f1 int unsigned, f2 varchar(255));
 
401
insert into t1 values (1,repeat('a',255)),(2,repeat('b',255));
 
402
--enable_metadata
 
403
select f2,group_concat(f1) from t1 group by f2;
 
404
--disable_metadata
 
405
drop table t1;
 
406
 
 
407
# End of 4.1 tests
 
408
 
 
409
#
 
410
# Bug#8568 "GROUP_CONCAT returns string, unless in a UNION in which case
 
411
# returns BLOB": add a test case, the bug can not be repeated any more.
 
412
#
 
413
 
 
414
set names latin1;
 
415
create table t1 (a char, b char);
 
416
insert into t1 values ('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b');
 
417
create table t2 select group_concat(b) as a from t1 where a = 'a';
 
418
create table t3 (select group_concat(a) as a from t1 where a = 'a') union
 
419
                (select group_concat(b) as a from t1 where a = 'b');
 
420
select charset(a) from t2;
 
421
select charset(a) from t3;
 
422
drop table t1, t2, t3;
 
423
set names default;
 
424
 
 
425
#
 
426
# Bug#18281 group_concat changes charset to binary
 
427
#
 
428
create table t1 (c1 varchar(10), c2 int);
 
429
select charset(group_concat(c1 order by c2)) from t1;
 
430
drop table t1;
 
431
 
 
432
#
 
433
# Bug #16712: group_concat returns odd string instead of intended result
 
434
#
 
435
CREATE TABLE t1 (a INT(10), b LONGTEXT, PRIMARY KEY (a));
 
436
 
 
437
SET GROUP_CONCAT_MAX_LEN = 20000000;
 
438
 
 
439
INSERT INTO t1 VALUES (1,REPEAT(CONCAT('A',CAST(CHAR(0) AS BINARY),'B'), 40000));
 
440
INSERT INTO t1 SELECT a + 1, b FROM t1;
 
441
 
 
442
SELECT a, CHAR_LENGTH(b) FROM t1;
 
443
SELECT CHAR_LENGTH( GROUP_CONCAT(b) ) FROM t1;
 
444
SET GROUP_CONCAT_MAX_LEN = 1024;
 
445
DROP TABLE t1;
 
446
 
 
447
#
 
448
# Bug #22015: crash with GROUP_CONCAT over a derived table that 
 
449
#             returns the results of aggregation by GROUP_CONCAT  
 
450
#
 
451
 
 
452
CREATE TABLE t1 (a int, b int);
 
453
 
 
454
INSERT INTO t1 VALUES (2,1), (1,2), (2,2), (1,3);
 
455
 
 
456
SELECT GROUP_CONCAT(a), x 
 
457
  FROM (SELECT a, GROUP_CONCAT(b) x FROM t1 GROUP BY a) AS s
 
458
    GROUP BY x;
 
459
 
 
460
DROP TABLE t1;
 
461
#
 
462
# Bug#23451 GROUP_CONCAT truncates a multibyte utf8 character
 
463
#
 
464
set names utf8;
 
465
create table t1
 
466
(
 
467
  x text character set utf8 not null,
 
468
  y integer not null
 
469
);
 
470
insert into t1 values (repeat('a', 1022), 0), (repeat(_utf8 0xc3b7, 4), 0);
 
471
let $1= 10;
 
472
while ($1)
 
473
{
 
474
  eval set group_concat_max_len= 1022 + $1;
 
475
  --disable_result_log
 
476
  select @x:=group_concat(x) from t1 group by y;
 
477
  --enable_result_log
 
478
  select @@group_concat_max_len, length(@x), char_length(@x), right(@x,12), right(HEX(@x),12);
 
479
  dec $1;
 
480
}
 
481
drop table t1;
 
482
set group_concat_max_len=1024;
 
483
set names latin1;
 
484
 
 
485
#
 
486
# Bug#14169 type of group_concat() result changed to blob if tmp_table was used
 
487
#
 
488
create table t1 (f1 int unsigned, f2 varchar(255));
 
489
insert into t1 values (1,repeat('a',255)),(2,repeat('b',255));
 
490
--enable_metadata
 
491
select f2,group_concat(f1) from t1 group by f2;
 
492
--disable_metadata
 
493
drop table t1;
 
494
 
 
495
#
 
496
# Bug #26815: Unexpected built-in function behavior: group_concat(distinct
 
497
# substring_index())
 
498
 
499
CREATE TABLE t1(a TEXT, b CHAR(20));
 
500
INSERT INTO t1 VALUES ("one.1","one.1"),("two.2","two.2"),("one.3","one.3");
 
501
SELECT GROUP_CONCAT(DISTINCT UCASE(a)) FROM t1;
 
502
SELECT GROUP_CONCAT(DISTINCT UCASE(b)) FROM t1;
 
503
DROP TABLE t1;
 
504
 
 
505
#
 
506
# Bug #28273: GROUP_CONCAT and ORDER BY: No warning when result gets truncated.
 
507
#
 
508
CREATE TABLE t1( a VARCHAR( 10 ), b INT );
 
509
INSERT INTO t1 VALUES ( repeat( 'a', 10 ), 1), 
 
510
                      ( repeat( 'b', 10 ), 2);
 
511
SET group_concat_max_len = 20;
 
512
SELECT GROUP_CONCAT( a ) FROM t1;
 
513
SELECT GROUP_CONCAT( DISTINCT a ) FROM t1;  
 
514
SELECT GROUP_CONCAT( a ORDER BY b ) FROM t1;          
 
515
SELECT GROUP_CONCAT( DISTINCT a ORDER BY b ) FROM t1; 
 
516
SET group_concat_max_len = DEFAULT;
 
517
DROP TABLE t1;
 
518
# Bug #23856:GROUP_CONCAT and ORDER BY: junk from previous rows for query on I_S
 
519
#
 
520
SET group_concat_max_len= 65535;
 
521
CREATE TABLE t1( a TEXT, b INTEGER );
 
522
INSERT INTO t1 VALUES ( 'a', 0 ), ( 'b', 1 );
 
523
SELECT GROUP_CONCAT( a ORDER BY b ) FROM t1;
 
524
SELECT GROUP_CONCAT(DISTINCT a ORDER BY b) FROM t1;
 
525
SELECT GROUP_CONCAT(DISTINCT a) FROM t1;
 
526
SET group_concat_max_len= 10;
 
527
SELECT GROUP_CONCAT(a ORDER BY b) FROM t1;
 
528
SELECT GROUP_CONCAT(DISTINCT a ORDER BY b) FROM t1;
 
529
SELECT GROUP_CONCAT(DISTINCT a) FROM t1;
 
530
 
 
531
SET group_concat_max_len= 65535;
 
532
CREATE TABLE t2( a TEXT );
 
533
INSERT INTO t2 VALUES( REPEAT( 'a', 5000 ) );
 
534
INSERT INTO t2 VALUES( REPEAT( 'b', 5000 ) );
 
535
INSERT INTO t2 VALUES( REPEAT( 'a', 5000 ) );
 
536
SELECT LENGTH( GROUP_CONCAT( DISTINCT a ) ) FROM t2;
 
537
 
 
538
CREATE TABLE t3( a TEXT, b INT  );
 
539
INSERT INTO t3 VALUES( REPEAT( 'a', 65534 ), 1 );
 
540
INSERT INTO t3 VALUES( REPEAT( 'a', 65535 ), 2 );
 
541
INSERT INTO t3 VALUES( REPEAT( 'a', 65533 ), 3 );
 
542
SELECT LENGTH( GROUP_CONCAT( a ) ) FROM t3 WHERE b = 1;
 
543
SELECT LENGTH( GROUP_CONCAT( a ) ) FROM t3 WHERE b = 2;
 
544
SELECT LENGTH( GROUP_CONCAT( a ) ) FROM t3 WHERE b = 3;
 
545
 
 
546
SET group_concat_max_len= DEFAULT;
 
547
DROP TABLE t1, t2, t3;
 
548
 
 
549
#
 
550
# Bug#29850: Wrong charset of the GROUP_CONCAT result when the select employs
 
551
#            a temporary table.
 
552
#
 
553
set names latin1;
 
554
create table t1 (id int, name varchar(20)) DEFAULT CHARSET=utf8;
 
555
insert into t1 (id, name) values (1, "�ra");
 
556
insert into t1 (id, name) values (2, "�ra");
 
557
select b.id, group_concat(b.name) from t1 a, t1 b group by b.id;
 
558
drop table t1;
 
559
 
 
560
#
 
561
# Bug #31154: group_concat() and bit fields;
 
562
#
 
563
create table t1(a bit not null default 0);
 
564
insert into t1 values (), (), ();
 
565
select group_concat(distinct a) from t1;
 
566
select group_concat(distinct a order by a) from t1;
 
567
drop table t1;
 
568
 
 
569
create table t1(a bit(2) not null);
 
570
insert into t1 values (1), (0), (0), (3), (1);
 
571
select group_concat(distinct a) from t1;
 
572
select group_concat(distinct a order by a) from t1;
 
573
select group_concat(distinct a order by a desc) from t1;
 
574
drop table t1;
 
575
 
 
576
create table t1(a bit(2), b varchar(10), c bit);
 
577
insert into t1 values (1, 'a', 0), (0, 'b', 1), (0, 'c', 0), (3, 'd', 1),
 
578
(1, 'e', 1), (3, 'f', 1), (0, 'g', 1);
 
579
select group_concat(distinct a, c) from t1;
 
580
select group_concat(distinct a, c order by a) from t1;
 
581
select group_concat(distinct a, c) from t1;
 
582
select group_concat(distinct a, c order by a, c) from t1;
 
583
select group_concat(distinct a, c order by a desc, c desc) from t1;
 
584
 
 
585
drop table t1;
 
586
 
 
587
 
 
588
#
 
589
# Bug#30897 GROUP_CONCAT returns extra comma on empty fields
 
590
#
 
591
create table t1 (f1 char(20));
 
592
insert into t1 values (''),('');
 
593
select group_concat(distinct f1) from t1;
 
594
select group_concat(f1) from t1;
 
595
drop table t1;
 
596
# Bug#32798: DISTINCT in GROUP_CONCAT clause fails when ordering by a column
 
597
# with null values
 
598
#'
 
599
CREATE TABLE t1 (a INT, b INT);
 
600
 
 
601
INSERT INTO t1 VALUES (1, 1), (2, 2), (2, 3);
 
602
 
 
603
SELECT GROUP_CONCAT(DISTINCT a ORDER BY b) FROM t1;
 
604
SELECT GROUP_CONCAT(DISTINCT a ORDER BY b DESC) FROM t1;
 
605
SELECT GROUP_CONCAT(DISTINCT a) FROM t1;
 
606
 
 
607
SELECT GROUP_CONCAT(DISTINCT a + 1 ORDER BY 3 - b) FROM t1;
 
608
SELECT GROUP_CONCAT(DISTINCT a + 1 ORDER BY b) FROM t1;
 
609
SELECT GROUP_CONCAT(a ORDER BY 3 - b) FROM t1;
 
610
 
 
611
CREATE TABLE t2 (a INT, b INT, c INT, d INT);
 
612
 
 
613
# There is one duplicate in the expression list: 1,10
 
614
# There is one duplicate in ORDER BY list, but that shouldnt matter: 1,10
 
615
INSERT INTO t2 VALUES (1,1, 1,1), (1,1, 2,2), (1,2, 2,1), (2,1, 1,2);
 
616
 
 
617
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY c, d) FROM t2;
 
618
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY d, c) FROM t2;
 
619
 
 
620
CREATE TABLE t3 (a INT, b INT, c INT);
 
621
 
 
622
INSERT INTO t3 VALUES (1, 1, 1), (2, 1, 2), (3, 2, 1);
 
623
 
 
624
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY b, c) FROM t3;
 
625
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY c, b) FROM t3;
 
626
 
 
627
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY a, b) FROM t1;
 
628
SELECT GROUP_CONCAT(DISTINCT b, a ORDER BY a, b) FROM t1;
 
629
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY b, a) FROM t1;
 
630
SELECT GROUP_CONCAT(DISTINCT b, a ORDER BY a, b) FROM t1;
 
631
SELECT GROUP_CONCAT(DISTINCT a ORDER BY a, b) FROM t1;
 
632
SELECT GROUP_CONCAT(DISTINCT b ORDER BY b, a) FROM t1;
 
633
SELECT GROUP_CONCAT(DISTINCT a, b ORDER BY a) FROM t1;
 
634
SELECT GROUP_CONCAT(DISTINCT b, a ORDER BY b) FROM t1;
 
635
 
 
636
DROP TABLE t1, t2, t3;
 
637
 
 
638
#
 
639
# Bug #34747: crash in debug assertion check after derived table
 
640
#
 
641
CREATE TABLE t1(a INT);
 
642
INSERT INTO t1 VALUES (),();
 
643
SELECT s1.d1 FROM
 
644
(
 
645
 SELECT
 
646
  t1.a as d1,
 
647
  GROUP_CONCAT(DISTINCT t1.a) AS d2
 
648
 FROM
 
649
  t1 AS t1,
 
650
  t1 AS t2
 
651
 GROUP BY 1
 
652
) AS s1;
 
653
DROP TABLE t1;
 
654
 
 
655
--echo End of 5.0 tests