forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_test.go
1512 lines (1383 loc) · 77.9 KB
/
aggregate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"context"
"fmt"
"math"
"math/rand"
"sort"
"strconv"
"strings"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/parser/terror"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
type testSuiteAgg struct {
*baseTestSuite
testData testutil.TestData
}
func (s *testSuiteAgg) SetUpSuite(c *C) {
s.baseTestSuite.SetUpSuite(c)
var err error
s.testData, err = testutil.LoadTestSuiteData("testdata", "agg_suite")
c.Assert(err, IsNil)
}
func (s *testSuiteAgg) TearDownSuite(c *C) {
s.baseTestSuite.TearDownSuite(c)
c.Assert(s.testData.GenerateOutputIfNeeded(), IsNil)
}
func (s *testSuiteAgg) TestAggregation(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_hash_join_concurrency=1")
tk.MustExec("use test")
tk.MustExec("set sql_mode='STRICT_TRANS_TABLES'") // disable only-full-group-by
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c int, d int)")
tk.MustExec("insert t values (NULL, 1)")
tk.MustExec("insert t values (1, 1)")
tk.MustExec("insert t values (1, 2)")
tk.MustExec("insert t values (1, 3)")
tk.MustExec("insert t values (1, 1)")
tk.MustExec("insert t values (3, 2)")
tk.MustExec("insert t values (4, 3)")
tk.MustQuery("select bit_and(c) from t where NULL").Check(testkit.Rows("18446744073709551615"))
tk.MustQuery("select bit_or(c) from t where NULL").Check(testkit.Rows("0"))
tk.MustQuery("select bit_xor(c) from t where NULL").Check(testkit.Rows("0"))
tk.MustQuery("select approx_count_distinct(c) from t where NULL").Check(testkit.Rows("0"))
result := tk.MustQuery("select count(*) from t")
result.Check(testkit.Rows("7"))
result = tk.MustQuery("select count(*) from t group by d order by c")
result.Check(testkit.Rows("3", "2", "2"))
result = tk.MustQuery("select distinct 99 from t group by d having d > 0")
result.Check(testkit.Rows("99"))
result = tk.MustQuery("select count(*) from t having 1 = 0")
result.Check(testkit.Rows())
result = tk.MustQuery("select c,d from t group by d order by d")
result.Check(testkit.Rows("<nil> 1", "1 2", "1 3"))
result = tk.MustQuery("select - c, c as d from t group by c having null not between c and avg(distinct d) - d")
result.Check(testkit.Rows())
result = tk.MustQuery("select - c as c from t group by c having t.c > 5")
result.Check(testkit.Rows())
result = tk.MustQuery("select t1.c from t t1, t t2 group by c having c > 5")
result.Check(testkit.Rows())
result = tk.MustQuery("select count(*) from (select d, c from t) k where d != 0 group by d order by c")
result.Check(testkit.Rows("3", "2", "2"))
result = tk.MustQuery("select c as a from t group by d having a < 0")
result.Check(testkit.Rows())
result = tk.MustQuery("select c as a from t group by d having sum(a) = 2")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select count(distinct c) from t group by d order by c")
result.Check(testkit.Rows("1", "2", "2"))
result = tk.MustQuery("select approx_count_distinct(c) from t group by d order by c")
result.Check(testkit.Rows("1", "2", "2"))
result = tk.MustQuery("select sum(c) as a from t group by d order by a")
result.Check(testkit.Rows("2", "4", "5"))
result = tk.MustQuery("select sum(c) as a, sum(c+1), sum(c), sum(c+1) from t group by d order by a")
result.Check(testkit.Rows("2 4 2 4", "4 6 4 6", "5 7 5 7"))
result = tk.MustQuery("select count(distinct c,d) from t")
result.Check(testkit.Rows("5"))
result = tk.MustQuery("select approx_count_distinct(c,d) from t")
result.Check(testkit.Rows("5"))
err := tk.ExecToErr("select count(c,d) from t")
c.Assert(err, NotNil)
result = tk.MustQuery("select d*2 as ee, sum(c) from t group by ee order by ee")
result.Check(testkit.Rows("2 2", "4 4", "6 5"))
result = tk.MustQuery("select sum(distinct c) as a from t group by d order by a")
result.Check(testkit.Rows("1", "4", "5"))
result = tk.MustQuery("select min(c) as a from t group by d order by a")
result.Check(testkit.Rows("1", "1", "1"))
result = tk.MustQuery("select max(c) as a from t group by d order by a")
result.Check(testkit.Rows("1", "3", "4"))
result = tk.MustQuery("select avg(c) as a from t group by d order by a")
result.Check(testkit.Rows("1.0000", "2.0000", "2.5000"))
result = tk.MustQuery("select c, approx_count_distinct(d) as a from t group by c order by a, c")
result.Check(testkit.Rows("<nil> 1", "3 1", "4 1", "1 3"))
result = tk.MustQuery("select d, d + 1 from t group by d order by d")
result.Check(testkit.Rows("1 2", "2 3", "3 4"))
result = tk.MustQuery("select count(*) from t")
result.Check(testkit.Rows("7"))
result = tk.MustQuery("select count(distinct d) from t")
result.Check(testkit.Rows("3"))
result = tk.MustQuery("select approx_count_distinct(d) from t")
result.Check(testkit.Rows("3"))
result = tk.MustQuery("select count(*) as a from t group by d having sum(c) > 3 order by a")
result.Check(testkit.Rows("2", "2"))
result = tk.MustQuery("select max(c) from t group by d having sum(c) > 3 order by avg(c) desc")
result.Check(testkit.Rows("4", "3"))
result = tk.MustQuery("select sum(-1) from t a left outer join t b on not null is null")
result.Check(testkit.Rows("-7"))
result = tk.MustQuery("select count(*), b.d from t a left join t b on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("2 <nil>", "12 1", "2 3"))
result = tk.MustQuery("select count(b.d), b.d from t a left join t b on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("0 <nil>", "12 1", "2 3"))
result = tk.MustQuery("select count(b.d), b.d from t b right join t a on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("0 <nil>", "12 1", "2 3"))
result = tk.MustQuery("select count(*), b.d from t b right join t a on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("2 <nil>", "12 1", "2 3"))
result = tk.MustQuery("select max(case when b.d is null then 10 else b.c end), b.d from t b right join t a on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("10 <nil>", "1 1", "4 3"))
result = tk.MustQuery("select count(*) from t a , t b")
result.Check(testkit.Rows("49"))
result = tk.MustQuery("select count(*) from t a , t b, t c")
result.Check(testkit.Rows("343"))
result = tk.MustQuery("select count(*) from t a , t b where a.c = b.d")
result.Check(testkit.Rows("14"))
result = tk.MustQuery("select count(a.d), sum(b.c) from t a , t b where a.c = b.d order by a.d")
result.Check(testkit.Rows("14 13"))
result = tk.MustQuery("select count(*) from t a , t b, t c where a.c = b.d and b.d = c.d")
result.Check(testkit.Rows("40"))
result = tk.MustQuery("select count(*), a.c from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by a.c")
result.Check(testkit.Rows("36 1", "4 3"))
result = tk.MustQuery("select count(a.c), c.d from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by c.d")
result.Check(testkit.Rows("36 1", "4 3"))
result = tk.MustQuery("select count(*) from t a , t b where a.c = b.d and a.c + b.d = 2")
result.Check(testkit.Rows("12"))
result = tk.MustQuery("select count(*) from t a join t b having sum(a.c) < 0")
result.Check(testkit.Rows())
result = tk.MustQuery("select count(*) from t a join t b where a.c < 0")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("select sum(b.c), count(b.d), a.c from t a left join t b on a.c = b.d group by b.d order by b.d")
result.Check(testkit.Rows("<nil> 0 <nil>", "8 12 1", "5 2 3"))
// This two cases prove that having always resolve name from field list firstly.
result = tk.MustQuery("select 1-d as d from t having d < 0 order by d desc")
result.Check(testkit.Rows("-1", "-1", "-2", "-2"))
result = tk.MustQuery("select 1-d as d from t having d + 1 < 0 order by d + 1")
result.Check(testkit.Rows("-2", "-2"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (keywords varchar(20), type int)")
tk.MustExec("insert into t values('测试', 1), ('test', 2)")
result = tk.MustQuery("select group_concat(keywords) from t group by type order by type")
result.Check(testkit.Rows("测试", "test"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c int, d int)")
tk.MustExec("insert t values (1, -1)")
tk.MustExec("insert t values (1, 0)")
tk.MustExec("insert t values (1, 1)")
result = tk.MustQuery("select d, d*d as d from t having d = -1")
result.Check(testkit.Rows())
result = tk.MustQuery("select d*d as d from t group by d having d = -1")
result.Check(testkit.Rows("1"))
result = tk.MustQuery("select d, 1-d as d, c as d from t order by d")
result.Check(testkit.Rows("1 0 1", "0 1 1", "-1 2 1"))
result = tk.MustQuery("select d, 1-d as d, c as d from t order by d+1")
result.Check(testkit.Rows("-1 2 1", "0 1 1", "1 0 1"))
result = tk.MustQuery("select d, 1-d as d, c as d from t group by d order by d")
result.Check(testkit.Rows("1 0 1", "0 1 1", "-1 2 1"))
result = tk.MustQuery("select d as d1, t.d as d1, 1-d as d1, c as d1 from t having d1 < 10 order by d")
result.Check(testkit.Rows("-1 -1 2 1", "0 0 1 1", "1 1 0 1"))
result = tk.MustQuery("select d*d as d1, c as d1 from t group by d1 order by d1")
result.Check(testkit.Rows("0 1", "1 1"))
result = tk.MustQuery("select d*d as d1, c as d1 from t group by 2")
result.Check(testkit.Rows("1 1"))
result = tk.MustQuery("select * from t group by 2 order by d")
result.Check(testkit.Rows("1 -1", "1 0", "1 1"))
result = tk.MustQuery("select * , sum(d) from t group by 1 order by d")
result.Check(testkit.Rows("1 -1 0"))
result = tk.MustQuery("select sum(d), t.* from t group by 2 order by d")
result.Check(testkit.Rows("0 1 -1"))
result = tk.MustQuery("select d as d, c as d from t group by d + 1 order by t.d")
result.Check(testkit.Rows("-1 1", "0 1", "1 1"))
result = tk.MustQuery("select c as d, c as d from t group by d order by d")
result.Check(testkit.Rows("1 1", "1 1", "1 1"))
err = tk.ExecToErr("select d as d, c as d from t group by d")
c.Assert(err, NotNil)
err = tk.ExecToErr("select t.d, c as d from t group by d")
c.Assert(err, NotNil)
result = tk.MustQuery("select *, c+1 as d from t group by 3 order by d")
result.Check(testkit.Rows("1 -1 2"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a float, b int default 3)")
tk.MustExec("insert into t1 (a) values (2), (11), (8)")
result = tk.MustQuery("select min(a), min(case when 1=1 then a else NULL end), min(case when 1!=1 then NULL else a end) from t1 where b=3 group by b")
result.Check(testkit.Rows("2 2 2"))
// The following cases use streamed aggregation.
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, index(a))")
tk.MustExec("insert into t1 (a) values (1),(2),(3),(4),(5)")
result = tk.MustQuery("select count(a) from t1 where a < 3")
result.Check(testkit.Rows("2"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index(a))")
result = tk.MustQuery("select sum(b) from (select * from t1) t group by a")
result.Check(testkit.Rows())
result = tk.MustQuery("select sum(b) from (select * from t1) t")
result.Check(testkit.Rows("<nil>"))
tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4),(3, 5)")
result = tk.MustQuery("select avg(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2.5000", "2.0000", "4.0000"))
result = tk.MustQuery("select sum(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("5", "2", "8"))
result = tk.MustQuery("select count(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2", "1", "2"))
result = tk.MustQuery("select max(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("4", "2", "5"))
result = tk.MustQuery("select min(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index(a,b))")
tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3)")
result = tk.MustQuery("select avg(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2.5000", "2.0000", "4.0000"))
result = tk.MustQuery("select sum(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("5", "2", "8"))
result = tk.MustQuery("select count(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2", "1", "2"))
result = tk.MustQuery("select approx_count_distinct(b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2", "1", "2"))
result = tk.MustQuery("select max(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("4", "2", "5"))
result = tk.MustQuery("select min(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index(b, a))")
tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3)")
result = tk.MustQuery("select avg(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2.5000", "2.0000", "4.0000"))
result = tk.MustQuery("select sum(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("5", "2", "8"))
result = tk.MustQuery("select count(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("2", "1", "2"))
result = tk.MustQuery("select max(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("4", "2", "5"))
result = tk.MustQuery("select min(distinct b) from (select * from t1) t group by a order by a")
result.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int primary key, ds date)")
tk.MustExec("insert into t (id, ds) values (1, \"1991-09-05\"),(2,\"1991-09-05\"), (3, \"1991-09-06\"),(0,\"1991-09-06\")")
result = tk.MustQuery("select sum(id), ds from t group by ds order by id")
result.Check(testkit.Rows("3 1991-09-06", "3 1991-09-05"))
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (col0 int, col1 int)")
tk.MustExec("create table t2 (col0 int, col1 int)")
tk.MustExec("insert into t1 values(83, 0), (26, 0), (43, 81)")
tk.MustExec("insert into t2 values(22, 2), (3, 12), (38, 98)")
result = tk.MustQuery("SELECT COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL ) FROM t2, t1 AS cor0, t2 AS cor1 GROUP BY cor0.col1")
result.Check(testkit.Rows("<nil>", "<nil>"))
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (c1 int)")
tk.MustExec("create table t2 (c1 int)")
tk.MustExec("insert into t1 values(3), (2)")
tk.MustExec("insert into t2 values(1), (2)")
tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 0")
result = tk.MustQuery("select sum(c1 in (select * from t2)) from t1")
result.Check(testkit.Rows("1"))
tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 1")
result = tk.MustQuery("select sum(c1 in (select * from t2)) from t1")
result.Check(testkit.Rows("1"))
result = tk.MustQuery("select sum(c1) k from (select * from t1 union all select * from t2)t group by c1 * 2 order by k")
result.Check(testkit.Rows("1", "3", "4"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int)")
tk.MustExec("insert into t values(1, 2, 3), (1, 2, 4)")
result = tk.MustQuery("select count(distinct c), count(distinct a,b) from t")
result.Check(testkit.Rows("2 1"))
result = tk.MustQuery("select approx_count_distinct( c), approx_count_distinct( a,b) from t")
result.Check(testkit.Rows("2 1"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a float)")
tk.MustExec("insert into t values(966.36), (363.97), (569.99), (453.33), (376.45), (321.93), (12.12), (45.77), (9.66), (612.17)")
result = tk.MustQuery("select distinct count(distinct a) from t")
result.Check(testkit.Rows("10"))
result = tk.MustQuery("select distinct approx_count_distinct( a) from t")
result.Check(testkit.Rows("10"))
tk.MustExec("create table idx_agg (a int, b int, index (b))")
tk.MustExec("insert idx_agg values (1, 1), (1, 2), (2, 2)")
tk.MustQuery("select sum(a), sum(b) from idx_agg where b > 0 and b < 10").Check(testkit.Rows("4 5"))
// test without any aggregate function
tk.MustQuery("select 10 from idx_agg group by b").Check(testkit.Rows("10", "10"))
tk.MustQuery("select 11 from idx_agg group by a").Check(testkit.Rows("11", "11"))
tk.MustExec("set @@tidb_init_chunk_size=1;")
tk.MustQuery("select group_concat(b) from idx_agg group by b;").Sort().Check(testkit.Rows("1", "2,2"))
tk.MustExec("set @@tidb_init_chunk_size=2;")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int(11), b decimal(15,2))")
tk.MustExec("insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97)")
tk.MustQuery("select a, max(b) from t group by a order by a limit 2").Check(testkit.Rows("1 771.64", "2 378.49"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int(11), b char(15))")
tk.MustExec("insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97)")
tk.MustQuery("select a, max(b) from t group by a order by a limit 2").Check(testkit.Rows("1 771.64", "2 378.49"))
// for issue #6014
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int(11) NOT NULL, tags json DEFAULT NULL)")
tk.MustExec(`insert into t values (1, '{"i": 1, "n": "n1"}')`)
tk.MustExec(`insert into t values (2, '{"i": 2, "n": "n2"}')`)
tk.MustExec(`insert into t values (3, '{"i": 3, "n": "n3"}')`)
tk.MustExec(`insert into t values (4, '{"i": 4, "n": "n4"}')`)
tk.MustExec(`insert into t values (5, '{"i": 5, "n": "n5"}')`)
tk.MustExec(`insert into t values (6, '{"i": 0, "n": "n6"}')`)
tk.MustExec(`insert into t values (7, '{"i": -1, "n": "n7"}')`)
tk.MustQuery("select sum(tags->'$.i') from t").Check(testkit.Rows("14"))
// test agg with empty input
result = tk.MustQuery("select id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95) from t where null")
result.Check(testkit.Rows("<nil> 0 <nil> <nil> 0 18446744073709551615 0 <nil> <nil> <nil>"))
tk.MustExec("truncate table t")
tk.MustExec("create table s(id int)")
result = tk.MustQuery("select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id")
result.Check(testkit.Rows("<nil> 0 <nil> <nil> 0 18446744073709551615 0 <nil> <nil> <nil> 0"))
tk.MustExec(`insert into t values (1, '{"i": 1, "n": "n1"}')`)
result = tk.MustQuery("select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id")
result.Check(testkit.Rows("1 1 95 95.0000 95 95 95 95 95 95 1"))
tk.MustExec("set @@tidb_hash_join_concurrency=5")
// test agg bit col
tk.MustExec("drop table t")
tk.MustExec("CREATE TABLE `t` (`a` bit(1) NOT NULL, PRIMARY KEY (`a`))")
tk.MustExec("insert into t value(1), (0)")
tk.MustQuery("select a from t group by 1")
// This result is compatible with MySQL, the readable result is shown in the next case.
result = tk.MustQuery("select max(a) from t group by a order by a")
result.Check(testkit.Rows(string([]byte{0x0}), string([]byte{0x1})))
result = tk.MustQuery("select cast(a as signed) as idx, cast(max(a) as signed), cast(min(a) as signed) from t group by 1 order by idx")
result.Check(testkit.Rows("0 0 0", "1 1 1"))
tk.MustExec("drop table t")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("insert into t value(null, null)")
tk.MustQuery("select group_concat(a), group_concat(distinct a) from t").Check(testkit.Rows("<nil> <nil>"))
tk.MustExec("insert into t value(1, null), (null, 1), (1, 2), (3, 4)")
tk.MustQuery("select group_concat(a, b), group_concat(distinct a,b) from t").Check(testkit.Rows("12,34 12,34"))
tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 0")
tk.MustQuery("select count(distinct a) from t;").Check(testkit.Rows("2"))
tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 1")
tk.MustQuery("select count(distinct a) from t;").Check(testkit.Rows("2"))
tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 0")
tk.MustQuery("select approx_count_distinct( a) from t;").Check(testkit.Rows("2"))
tk.MustExec("drop table t")
tk.MustExec("create table t(a decimal(10, 4))")
tk.MustQuery("select 10 from t group by a").Check(testkit.Rows())
tk.MustExec("insert into t value(0), (-0.9871), (-0.9871)")
tk.MustQuery("select 10 from t group by a").Check(testkit.Rows("10", "10"))
tk.MustQuery("select sum(a) from (select a from t union all select a from t) tmp").Check(testkit.Rows("-3.9484"))
tk.MustExec("drop table t")
tk.MustExec("create table t(a tinyint, b smallint, c mediumint, d int, e bigint, f float, g double, h decimal)")
tk.MustExec("insert into t values(1, 2, 3, 4, 5, 6.1, 7.2, 8.3), (1, 3, 4, 5, 6, 7.1, 8.2, 9.3)")
result = tk.MustQuery("select var_pop(b), var_pop(c), var_pop(d), var_pop(e), var_pop(f), var_pop(g), var_pop(h) from t group by a")
result.Check(testkit.Rows("0.25 0.25 0.25 0.25 0.25 0.25 0.25"))
tk.MustExec("insert into t values(2, 3, 4, 5, 6, 7.2, 8.3, 9)")
result = tk.MustQuery("select a, var_pop(b) over w, var_pop(c) over w from t window w as (partition by a)").Sort()
result.Check(testkit.Rows("1 0.25 0.25", "1 0.25 0.25", "2 0 0"))
tk.MustExec("delete from t where t.a = 2")
tk.MustExec("insert into t values(1, 2, 4, 5, 6, 6.1, 7.2, 9)")
result = tk.MustQuery("select a, var_pop(distinct b), var_pop(distinct c), var_pop(distinct d), var_pop(distinct e), var_pop(distinct f), var_pop(distinct g), var_pop(distinct h) from t group by a")
result.Check(testkit.Rows("1 0.25 0.25 0.25 0.25 0.25 0.25 0.25"))
tk.MustExec("drop table t")
tk.MustExec("create table t(a int, b bigint, c float, d double, e decimal)")
tk.MustExec("insert into t values(1, 1000, 6.8, 3.45, 8.3), (1, 3998, -3.4, 5.12, 9.3),(1, 288, 9.2, 6.08, 1)")
result = tk.MustQuery("select variance(b), variance(c), variance(d), variance(e) from t group by a")
result.Check(testkit.Rows("2584338.6666666665 29.840000178019228 1.1808222222222229 12.666666666666666"))
tk.MustExec("insert into t values(1, 255, 6.8, 6.08, 1)")
result = tk.MustQuery("select variance(distinct b), variance(distinct c), variance(distinct d), variance(distinct e) from t group by a")
result.Check(testkit.Rows("2364075.6875 29.840000178019228 1.1808222222222229 12.666666666666666"))
tk.MustExec("insert into t values(2, 322, 0.8, 2.22, 6)")
result = tk.MustQuery("select a, variance(b) over w from t window w as (partition by a)").Sort()
result.Check(testkit.Rows("1 2364075.6875", "1 2364075.6875", "1 2364075.6875", "1 2364075.6875", "2 0"))
_, err = tk.Exec("select std_samp(a) from t")
// TODO: Fix this error message.
c.Assert(errors.Cause(err).Error(), Equals, "[expression:1305]FUNCTION test.std_samp does not exist")
// For issue #14072: wrong result when using generated column with aggregate statement
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1 (a int, b int generated always as (-a) virtual, c int generated always as (-a) stored);")
tk.MustExec("insert into t1 (a) values (2), (1), (1), (3), (NULL);")
tk.MustQuery("select sum(a) from t1 group by b order by b;").Check(testkit.Rows("<nil>", "3", "2", "2"))
tk.MustQuery("select sum(a) from t1 group by c order by c;").Check(testkit.Rows("<nil>", "3", "2", "2"))
tk.MustQuery("select sum(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "-2", "-2", "-3"))
tk.MustQuery("select sum(b) from t1 group by c order by c;").Check(testkit.Rows("<nil>", "-3", "-2", "-2"))
tk.MustQuery("select sum(c) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "-2", "-2", "-3"))
tk.MustQuery("select sum(c) from t1 group by b order by b;").Check(testkit.Rows("<nil>", "-3", "-2", "-2"))
// For stddev_pop()/std()/stddev() function
tk.MustExec("drop table if exists t1;")
tk.MustExec(`create table t1 (grp int, a bigint unsigned, c char(10) not null);`)
tk.MustExec(`insert into t1 values (1,1,"a");`)
tk.MustExec(`insert into t1 values (2,2,"b");`)
tk.MustExec(`insert into t1 values (2,3,"c");`)
tk.MustExec(`insert into t1 values (3,4,"E");`)
tk.MustExec(`insert into t1 values (3,5,"C");`)
tk.MustExec(`insert into t1 values (3,6,"D");`)
tk.MustQuery(`select stddev_pop(all a) from t1;`).Check(testkit.Rows("1.707825127659933"))
tk.MustQuery(`select stddev_pop(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726"))
tk.MustQuery(`select sum(a)+count(a)+avg(a)+stddev_pop(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726"))
tk.MustQuery(`select std(all a) from t1;`).Check(testkit.Rows("1.707825127659933"))
tk.MustQuery(`select std(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726"))
tk.MustQuery(`select sum(a)+count(a)+avg(a)+std(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726"))
tk.MustQuery(`select stddev(all a) from t1;`).Check(testkit.Rows("1.707825127659933"))
tk.MustQuery(`select stddev(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726"))
tk.MustQuery(`select sum(a)+count(a)+avg(a)+stddev(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726"))
// test null
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (a int, b int);")
tk.MustQuery("select stddev_pop(b) from t1;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select std(b) from t1;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select stddev(b) from t1;").Check(testkit.Rows("<nil>"))
tk.MustExec("insert into t1 values (1,null);")
tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>"))
tk.MustExec("insert into t1 values (1,null);")
tk.MustExec("insert into t1 values (2,null);")
tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "<nil>"))
tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "<nil>"))
tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "<nil>"))
tk.MustExec("insert into t1 values (2,1);")
tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0"))
tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0"))
tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0"))
tk.MustExec("insert into t1 values (3,1);")
tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("<nil>", "0", "0"))
// For var_samp()/stddev_samp()
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (id int(11),value1 float(10,2));")
tk.MustExec("INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);")
result = tk.MustQuery("select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id order by id;")
result.Check(testkit.Rows("1 0.816496580927726 0.6666666666666666 1 1", "2 1.118033988749895 1.25 1.2909944487358056 1.6666666666666667"))
// For issue #19676 The result of stddev_pop(distinct xxx) is wrong
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (id int);")
tk.MustExec("insert into t1 values (1),(2);")
tk.MustQuery("select stddev_pop(id) from t1;").Check(testkit.Rows("0.5"))
tk.MustExec("insert into t1 values (1);")
tk.MustQuery("select stddev_pop(distinct id) from t1;").Check(testkit.Rows("0.5"))
}
func (s *testSuiteAgg) TestAggPrune(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, b varchar(50), c int)")
tk.MustExec("insert into t values(1, '1ff', NULL), (2, '234.02', 1)")
tk.MustQuery("select id, sum(b) from t group by id").Check(testkit.Rows("1 1", "2 234.02"))
tk.MustQuery("select sum(b) from t").Check(testkit.Rows("235.02"))
tk.MustQuery("select id, count(c) from t group by id").Check(testkit.Rows("1 0", "2 1"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, b float, c float)")
tk.MustExec("insert into t values(1, 1, 3), (2, 1, 6)")
tk.MustQuery("select sum(b/c) from t group by id").Check(testkit.Rows("0.3333333333333333", "0.16666666666666666"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, b float, c float, d float)")
tk.MustExec("insert into t values(1, 1, 3, NULL), (2, 1, NULL, 6), (3, NULL, 1, 2), (4, NULL, NULL, 1), (5, NULL, 2, NULL), (6, 3, NULL, NULL), (7, NULL, NULL, NULL), (8, 1, 2 ,3)")
tk.MustQuery("select count(distinct b, c, d) from t group by id").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "0", "1"))
tk.MustQuery("select approx_count_distinct( b, c, d) from t group by id order by id").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "0", "1"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b varchar(10))")
tk.MustExec("insert into t value(1, 11),(3, NULL)")
tk.MustQuery("SELECT a, MIN(b), MAX(b) FROM t GROUP BY a").Check(testkit.Rows("1 11 11", "3 <nil> <nil>"))
}
func (s *testSuiteAgg) TestGroupConcatAggr(c *C) {
var err error
// issue #5411
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists test;")
tk.MustExec("create table test(id int, name int)")
tk.MustExec("insert into test values(1, 10);")
tk.MustExec("insert into test values(1, 20);")
tk.MustExec("insert into test values(1, 30);")
tk.MustExec("insert into test values(2, 20);")
tk.MustExec("insert into test values(3, 200);")
tk.MustExec("insert into test values(3, 500);")
result := tk.MustQuery("select id, group_concat(name) from test group by id order by id")
result.Check(testkit.Rows("1 10,20,30", "2 20", "3 200,500"))
result = tk.MustQuery("select id, group_concat(name SEPARATOR ';') from test group by id order by id")
result.Check(testkit.Rows("1 10;20;30", "2 20", "3 200;500"))
result = tk.MustQuery("select id, group_concat(name SEPARATOR ',') from test group by id order by id")
result.Check(testkit.Rows("1 10,20,30", "2 20", "3 200,500"))
result = tk.MustQuery(`select id, group_concat(name SEPARATOR '%') from test group by id order by id`)
result.Check(testkit.Rows("1 10%20%30", "2 20", `3 200%500`))
result = tk.MustQuery("select id, group_concat(name SEPARATOR '') from test group by id order by id")
result.Check(testkit.Rows("1 102030", "2 20", "3 200500"))
result = tk.MustQuery("select id, group_concat(name SEPARATOR '123') from test group by id order by id")
result.Check(testkit.Rows("1 101232012330", "2 20", "3 200123500"))
tk.MustQuery("select group_concat(id ORDER BY name) from (select * from test order by id, name limit 2,2) t").Check(testkit.Rows("2,1"))
tk.MustQuery("select group_concat(id ORDER BY name desc) from (select * from test order by id, name limit 2,2) t").Check(testkit.Rows("1,2"))
tk.MustQuery("select group_concat(name ORDER BY id) from (select * from test order by id, name limit 2,2) t").Check(testkit.Rows("30,20"))
tk.MustQuery("select group_concat(name ORDER BY id desc) from (select * from test order by id, name limit 2,2) t").Check(testkit.Rows("20,30"))
result = tk.MustQuery("select group_concat(name ORDER BY name desc SEPARATOR '++') from test;")
result.Check(testkit.Rows("500++200++30++20++20++10"))
result = tk.MustQuery("select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;")
result.Check(testkit.Rows("3--3--1--1--2--1"))
result = tk.MustQuery("select group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;")
result.Check(testkit.Rows("500++200++30++20++20++10 3--3--1--1--2--1"))
result = tk.MustQuery("select group_concat(distinct name order by name desc) from test;")
result.Check(testkit.Rows("500,200,30,20,10"))
expected := "3--3--1--1--2--1"
for maxLen := 4; maxLen < len(expected); maxLen++ {
tk.MustExec(fmt.Sprintf("set session group_concat_max_len=%v", maxLen))
result = tk.MustQuery("select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;")
result.Check(testkit.Rows(expected[:maxLen]))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
}
expected = "1--2--1--1--3--3"
for maxLen := 4; maxLen < len(expected); maxLen++ {
tk.MustExec(fmt.Sprintf("set session group_concat_max_len=%v", maxLen))
result = tk.MustQuery("select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;")
result.Check(testkit.Rows(expected[:maxLen]))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
}
expected = "500,200,30,20,10"
for maxLen := 4; maxLen < len(expected); maxLen++ {
tk.MustExec(fmt.Sprintf("set session group_concat_max_len=%v", maxLen))
result = tk.MustQuery("select group_concat(distinct name order by name desc) from test;")
result.Check(testkit.Rows(expected[:maxLen]))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
}
tk.MustExec(fmt.Sprintf("set session group_concat_max_len=%v", 1024))
// test varchar table
tk.MustExec("drop table if exists test2;")
tk.MustExec("create table test2(id varchar(20), name varchar(20));")
tk.MustExec("insert into test2 select * from test;")
tk.MustQuery("select group_concat(id ORDER BY name) from (select * from test2 order by id, name limit 2,2) t").Check(testkit.Rows("2,1"))
tk.MustQuery("select group_concat(id ORDER BY name desc) from (select * from test2 order by id, name limit 2,2) t").Check(testkit.Rows("1,2"))
tk.MustQuery("select group_concat(name ORDER BY id) from (select * from test2 order by id, name limit 2,2) t").Check(testkit.Rows("30,20"))
tk.MustQuery("select group_concat(name ORDER BY id desc) from (select * from test2 order by id, name limit 2,2) t").Check(testkit.Rows("20,30"))
result = tk.MustQuery("select group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test2;")
result.Check(testkit.Rows("500++30++200++20++20++10 3--1--3--1--2--1"))
// test Position Expr
tk.MustQuery("select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 1 desc, id SEPARATOR '++') from test;").Check(testkit.Rows("1 2 3 4 5 5003++2003++301++201++202++101"))
tk.MustQuery("select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 2 desc, name SEPARATOR '++') from test;").Check(testkit.Rows("1 2 3 4 5 2003++5003++202++101++201++301"))
err = tk.ExecToErr("select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 3 desc, name SEPARATOR '++') from test;")
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column '3' in 'order clause'")
// test Param Marker
tk.MustExec(`prepare s1 from "select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY floor(id/?) desc, name SEPARATOR '++') from test";`)
tk.MustExec("set @a=2;")
tk.MustQuery("execute s1 using @a;").Check(testkit.Rows("1 2 3 4 5 202++2003++5003++101++201++301"))
tk.MustExec(`prepare s1 from "select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY ? desc, name SEPARATOR '++') from test";`)
tk.MustExec("set @a=2;")
tk.MustQuery("execute s1 using @a;").Check(testkit.Rows("1 2 3 4 5 2003++5003++202++101++201++301"))
tk.MustExec("set @a=3;")
err = tk.ExecToErr("execute s1 using @a;")
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column '?' in 'order clause'")
tk.MustExec("set @a=3.0;")
tk.MustQuery("execute s1 using @a;").Check(testkit.Rows("1 2 3 4 5 101++202++201++301++2003++5003"))
// test partition table
tk.MustExec("drop table if exists ptest;")
tk.MustExec("CREATE TABLE ptest (id int,name int) PARTITION BY RANGE ( id ) " +
"(PARTITION `p0` VALUES LESS THAN (2), PARTITION `p1` VALUES LESS THAN (11))")
tk.MustExec("insert into ptest select * from test;")
for i := 0; i <= 1; i++ {
for j := 0; j <= 1; j++ {
tk.MustExec(fmt.Sprintf("set session tidb_opt_distinct_agg_push_down = %v", i))
tk.MustExec(fmt.Sprintf("set session tidb_opt_agg_push_down = %v", j))
result = tk.MustQuery("select /*+ agg_to_cop */ group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from ptest;")
result.Check(testkit.Rows("500++200++30++20++20++10 3--3--1--1--2--1"))
result = tk.MustQuery("select /*+ agg_to_cop */ group_concat(distinct name order by name desc) from ptest;")
result.Check(testkit.Rows("500,200,30,20,10"))
}
}
// issue #9920
tk.MustQuery("select group_concat(123, null)").Check(testkit.Rows("<nil>"))
// issue #23129
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(cid int, sname varchar(100));")
tk.MustExec("insert into t1 values(1, 'Bob'), (1, 'Alice');")
tk.MustExec("insert into t1 values(3, 'Ace');")
tk.MustExec("set @@group_concat_max_len=5;")
rows := tk.MustQuery("select group_concat(sname order by sname) from t1 group by cid;")
rows.Check(testkit.Rows("Alice", "Ace"))
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(c1 varchar(10));")
tk.MustExec("insert into t1 values('0123456789');")
tk.MustExec("insert into t1 values('12345');")
tk.MustExec("set @@group_concat_max_len=8;")
rows = tk.MustQuery("select group_concat(c1 order by c1) from t1 group by c1;")
rows.Check(testkit.Rows("01234567", "12345"))
}
func (s *testSuiteAgg) TestSelectDistinct(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
s.fillData(tk, "select_distinct_test")
tk.MustExec("begin")
r := tk.MustQuery("select distinct name from select_distinct_test;")
r.Check(testkit.Rows("hello"))
tk.MustExec("commit")
}
func (s *testSuiteAgg) TestAggPushDown(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int)")
tk.MustExec("alter table t add index idx(a, b, c)")
// test for empty table
tk.MustQuery("select count(a) from t group by a;").Check(testkit.Rows())
tk.MustQuery("select count(a) from t;").Check(testkit.Rows("0"))
// test for one row
tk.MustExec("insert t values(0,0,0)")
tk.MustQuery("select distinct b from t").Check(testkit.Rows("0"))
tk.MustQuery("select count(b) from t group by a;").Check(testkit.Rows("1"))
// test for rows
tk.MustExec("insert t values(1,1,1),(3,3,6),(3,2,5),(2,1,4),(1,1,3),(1,1,2);")
tk.MustQuery("select count(a) from t where b>0 group by a, b;").Sort().Check(testkit.Rows("1", "1", "1", "3"))
tk.MustQuery("select count(a) from t where b>0 group by a, b order by a;").Check(testkit.Rows("3", "1", "1", "1"))
tk.MustQuery("select count(a) from t where b>0 group by a, b order by a limit 1;").Check(testkit.Rows("3"))
tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a int primary key, b int, c int)")
tk.MustExec("create table tt(a int primary key, b int, c int)")
tk.MustExec("insert into t values(1, 1, 1), (2, 1, 1)")
tk.MustExec("insert into tt values(1, 2, 1)")
tk.MustQuery("select max(a.b), max(b.b) from t a join tt b on a.a = b.a group by a.c").Check(testkit.Rows("1 2"))
tk.MustQuery("select a, count(b) from (select * from t union all select * from tt) k group by a order by a").Check(testkit.Rows("1 2", "2 1"))
}
func (s *testSuiteAgg) TestOnlyFullGroupBy(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int not null primary key, b int not null, c int default null, d int not null, unique key I_b_c (b,c), unique key I_b_d (b,d))")
tk.MustExec("create table x(a int not null primary key, b int not null, c int default null, d int not null, unique key I_b_c (b,c), unique key I_b_d (b,d))")
// test AggregateFunc
tk.MustQuery("select max(a) from t group by d")
// for issue #8161: enable `any_value` in aggregation if `ONLY_FULL_GROUP_BY` is set
tk.MustQuery("select max(a), any_value(c) from t group by d;")
// test incompatible with sql_mode = ONLY_FULL_GROUP_BY
err := tk.ExecToErr("select * from t group by d")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select b-c from t group by b+c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select (b-c)*(b+c), min(a) from t group by b+c, b-c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select b between c and d from t group by b,c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select case b when 1 then c when 2 then d else d end from t group by b,c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select c > (select b from t) from t group by b")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select c is null from t group by b")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select c is true from t group by b")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select (c+b)*d from t group by c,d")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select b in (c,d) from t group by b,c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select b like '%a' from t group by c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select c REGEXP '1.*' from t group by b")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select -b from t group by c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select a, max(b) from t")
c.Assert(terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select sum(a)+b from t")
c.Assert(terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select count(b), c from t")
c.Assert(terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select distinct a, b, count(a) from t")
c.Assert(terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), IsTrue, Commentf("err %v", err))
// test compatible with sql_mode = ONLY_FULL_GROUP_BY
tk.MustQuery("select a from t group by a,b,c")
tk.MustQuery("select b from t group by b")
tk.MustQuery("select b as e from t group by b")
tk.MustQuery("select b+c from t group by b+c")
tk.MustQuery("select b+c, min(a) from t group by b+c, b-c")
tk.MustQuery("select b+c, min(a) from t group by b, c")
tk.MustQuery("select b+c from t group by b,c")
tk.MustQuery("select b between c and d from t group by b,c,d")
tk.MustQuery("select case b when 1 then c when 2 then d else d end from t group by b,c,d")
tk.MustQuery("select c > (select b from t) from t group by c")
tk.MustQuery("select exists (select * from t) from t group by d;")
tk.MustQuery("select c is null from t group by c")
tk.MustQuery("select c is true from t group by c")
tk.MustQuery("select (c+b)*d from t group by c,b,d")
tk.MustQuery("select b in (c,d) from t group by b,c,d")
tk.MustQuery("select b like '%a' from t group by b")
tk.MustQuery("select c REGEXP '1.*' from t group by c")
tk.MustQuery("select -b from t group by b")
tk.MustQuery("select max(a+b) from t")
tk.MustQuery("select avg(a)+1 from t")
tk.MustQuery("select count(c), 5 from t")
// test functional depend on primary key
tk.MustQuery("select * from t group by a")
// test functional depend on unique not null columns
tk.MustQuery("select * from t group by b,d")
// test functional depend on a unique null column
err = tk.ExecToErr("select * from t group by b,c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
// test functional dependency derived from keys in where condition
tk.MustQuery("select * from t where c = d group by b, c")
tk.MustQuery("select t.*, x.* from t, x where t.a = x.a group by t.a")
tk.MustQuery("select t.*, x.* from t, x where t.b = x.b and t.d = x.d group by t.b, t.d")
tk.MustQuery("select t.*, x.* from t, x where t.b = x.a group by t.b, t.d")
tk.MustQuery("select t.b, x.* from t, x where t.b = x.a group by t.b")
err = tk.ExecToErr("select t.*, x.* from t, x where t.c = x.a group by t.b, t.c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
// test functional dependency derived from keys in join
tk.MustQuery("select t.*, x.* from t inner join x on t.a = x.a group by t.a")
tk.MustQuery("select t.*, x.* from t inner join x on (t.b = x.b and t.d = x.d) group by t.b, x.d")
tk.MustQuery("select t.b, x.* from t inner join x on t.b = x.b group by t.b, x.d")
tk.MustQuery("select t.b, x.* from t left join x on t.b = x.b group by t.b, x.d")
tk.MustQuery("select t.b, x.* from t left join x on x.b = t.b group by t.b, x.d")
tk.MustQuery("select x.b, t.* from t right join x on x.b = t.b group by x.b, t.d")
tk.MustQuery("select x.b, t.* from t right join x on t.b = x.b group by x.b, t.d")
err = tk.ExecToErr("select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
err = tk.ExecToErr("select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
// FixMe: test functional dependency of derived table
// tk.MustQuery("select * from (select * from t) as e group by a")
// tk.MustQuery("select * from (select * from t) as e group by b,d")
// err = tk.ExecToErr("select * from (select * from t) as e group by b,c")
// c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue)
// test order by
tk.MustQuery("select c from t group by c,d order by d")
err = tk.ExecToErr("select c from t group by c order by d")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
// test ambiguous column
err = tk.ExecToErr("select c from t,x group by t.c")
c.Assert(terror.ErrorEqual(err, plannercore.ErrAmbiguous), IsTrue, Commentf("err %v", err))
}
func (s *testSuiteAgg) TestIssue16279(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists s")
tk.MustExec("create table s(a int);")
tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') from s group by date_format(a, '%Y-%m-%d');")
tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by date_format(a, '%Y-%m-%d');")
tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by xx")
}
func (s *testSuiteAgg) TestIssue24676(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists t1")
tk.MustExec(`create table t1(
id int(11) NOT NULL PRIMARY KEY,
c1 int(11) NOT NULL DEFAULT '0'
)`)
tk.MustQuery("SELECT c1 FROM t1 GROUP BY c1 ORDER BY c1 ASC;")
tk.MustQuery("SELECT ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) AS `c1` FROM `t1` GROUP BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ORDER BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ASC;")
err := tk.ExecToErr("SELECT ((floor(((`c1` - 10) / 300)) * 50000) + 0.0) AS `c1` FROM `t1` GROUP BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ORDER BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ASC;")
c.Assert(terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), IsTrue, Commentf("err %v", err))
}
func (s *testSuiteAgg) TestAggPushDownPartitionTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec(`CREATE TABLE t1 (
a int(11) DEFAULT NULL,
b tinyint(4) NOT NULL,
PRIMARY KEY (b)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE ( b ) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (30),
PARTITION p3 VALUES LESS THAN (40),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
)`)
tk.MustExec("insert into t1 values (0, 0), (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6), (3, 7), (3, 10), (3, 11), (12, 12), (12, 13), (14, 14), (14, 15), (20, 20), (20, 21), (20, 22), (23, 23), (23, 24), (23, 25), (31, 30), (31, 31), (31, 32), (33, 33), (33, 34), (33, 35), (36, 36), (80, 80), (90, 90), (100, 100)")
tk.MustExec("set @@tidb_opt_agg_push_down = 1")
tk.MustQuery("select /*+ AGG_TO_COP() */ sum(a), sum(b) from t1 where a < 40 group by a").Sort().Check(testkit.Rows(
"0 0",
"24 25",
"28 29",
"3 6",
"36 36",
"6 15",
"60 63",
"69 72",
"9 28",
"93 93",
"99 102"))
}
func (s *testSuiteAgg) TestIssue13652(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a real)")
tk.MustQuery("select a from t group by (a)")
tk.MustQuery("select a from t group by ((a))")
tk.MustQuery("select a from t group by +a")
tk.MustQuery("select a from t group by ((+a))")
_, err := tk.Exec("select a from t group by (-a)")
c.Assert(err.Error(), Equals, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")
}
func (s *testSuiteAgg) TestIssue14947(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustQuery("select ((+a+1)) as tmp from t group by tmp")
}
func (s *testSuiteAgg) TestHaving(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c1 int, c2 int, c3 int)")
tk.MustExec("insert into t values (1,2,3), (2, 3, 1), (3, 1, 2)")
tk.MustQuery("select c1 as c2, c3 from t having c2 = 2").Check(testkit.Rows("2 1"))
tk.MustQuery("select c1 as c2, c3 from t group by c2 having c2 = 2;").Check(testkit.Rows("1 3"))
tk.MustQuery("select c1 as c2, c3 from t group by c2 having sum(c2) = 2;").Check(testkit.Rows("1 3"))
tk.MustQuery("select c1 as c2, c3 from t group by c3 having sum(c2) = 2;").Check(testkit.Rows("1 3"))
tk.MustQuery("select c1 as c2, c3 from t group by c3 having sum(0) + c2 = 2;").Check(testkit.Rows("2 1"))
tk.MustQuery("select c1 as a from t having c1 = 1;").Check(testkit.Rows("1"))
tk.MustQuery("select t.c1 from t having c1 = 1;").Check(testkit.Rows("1"))
tk.MustQuery("select a.c1 from t as a having c1 = 1;").Check(testkit.Rows("1"))
tk.MustQuery("select c1 as a from t group by c3 having sum(a) = 1;").Check(testkit.Rows("1"))
tk.MustQuery("select c1 as a from t group by c3 having sum(a) + a = 2;").Check(testkit.Rows("1"))
tk.MustQuery("select a.c1 as c, a.c1 as d from t as a, t as b having c1 = 1 limit 1;").Check(testkit.Rows("1 1"))
tk.MustQuery("select sum(c1) as s from t group by c1 having sum(c1) order by s").Check(testkit.Rows("1", "2", "3"))
tk.MustQuery("select sum(c1) - 1 as s from t group by c1 having sum(c1) - 1 order by s").Check(testkit.Rows("1", "2"))
tk.MustQuery("select 1 from t group by c1 having sum(abs(c2 + c3)) = c1").Check(testkit.Rows("1"))
}
func (s *testSuiteAgg) TestIssue26496(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists UK_NSPRE_19416")
tk.MustExec("CREATE TABLE `UK_NSPRE_19416` ( `COL1` binary(20) DEFAULT NULL, `COL2` varchar(20) DEFAULT NULL, `COL4` datetime DEFAULT NULL, `COL3` bigint(20) DEFAULT NULL, `COL5` float DEFAULT NULL, UNIQUE KEY `UK_COL1` (`COL1`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("insert into `UK_NSPRE_19416`(col1) values (0xc5b428e2ebc1b78f0b183899a8df55c88a333f86), (0x004dad637b37cc4a9742484ab93f97ede2ab8bd5), (0x550c4a4390ba14fd6d382dd29063e10210c99381)")
tk.MustQuery("select t1.col1, count(t2.col1) from UK_NSPRE_19416 as t1 left join UK_NSPRE_19416 as t2 on t1.col1 = t2.col1 where t1.col1 in (0x550C4A4390BA14FD6D382DD29063E10210C99381, 0x004DAD637B37CC4A9742484AB93F97EDE2AB8BD5, 0xC5B428E2EBC1B78F0B183899A8DF55C88A333F86) group by t1.col1, t2.col1 having t1.col1 in (0x9B4B48FEBA9225BACF8F9ADEAEE810AEC26DC7A2, 0x25A6C4FAD832F8E0267AAA504CFAE767565C8B84, 0xE26E5B0080EC5A8156DACE67D13B239500E540E6)").Check(nil)
}
func (s *testSuiteAgg) TestAggEliminator(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t(a int primary key, b int)")
tk.MustQuery("select min(a), min(a) from t").Check(testkit.Rows("<nil> <nil>"))
tk.MustExec("insert into t values(1, -1), (2, -2), (3, 1), (4, NULL)")
tk.MustQuery("select max(a) from t").Check(testkit.Rows("4"))
tk.MustQuery("select min(b) from t").Check(testkit.Rows("-2"))
tk.MustQuery("select max(b*b) from t").Check(testkit.Rows("4"))
tk.MustQuery("select min(b*b) from t").Check(testkit.Rows("1"))
tk.MustQuery("select group_concat(b, b) from t group by a").Sort().Check(testkit.Rows("-1-1", "-2-2", "11", "<nil>"))
}
func (s *testSuiteAgg) TestClusterIndexMaxMinEliminator(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t;")
tk.Se.GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeOn
tk.MustExec("create table t (a int, b int, c int, primary key(a, b));")
for i := 0; i < 10+1; i++ {
tk.MustExec("insert into t values (?, ?, ?)", i, i, i)
}
tk.MustQuery("select max(a), min(a+b) from t;").Check(testkit.Rows("10 0"))
tk.MustQuery("select max(a+b), min(a+b) from t;").Check(testkit.Rows("20 0"))
tk.MustQuery("select min(a), max(a), min(b), max(b) from t;").Check(testkit.Rows("0 10 0 10"))
}
func (s *testSuiteAgg) TestMaxMinFloatScalaFunc(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec(`DROP TABLE IF EXISTS T;`)
tk.MustExec(`CREATE TABLE T(A VARCHAR(10), B VARCHAR(10), C FLOAT);`)
tk.MustExec(`INSERT INTO T VALUES('0', "val_b", 12.191);`)
tk.MustQuery(`SELECT MAX(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;`).Check(testkit.Rows("12.190999984741211"))
tk.MustQuery(`SELECT MIN(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;`).Check(testkit.Rows("12.190999984741211"))
}
func (s *testSuiteAgg) TestBuildProjBelowAgg(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (i int);")
tk.MustExec("insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);")
rs := tk.MustQuery("select i+1 as a, count(i+2), sum(i+3), group_concat(i+4), bit_or(i+5) from t group by i, hex(i+6) order by a")
rs.Check(testkit.Rows(
"2 3 12 5,5,5 6",
"3 3 15 6,6,6 7",
"4 3 18 7,7,7 8"))
}
func (s *testSuiteAgg) TestInjectProjBelowTopN(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (i int);")
tk.MustExec("insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);")
var (
input []string
output [][]string
)
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i] = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Rows())
})
tk.MustQuery(tt).Check(testkit.Rows(output[i]...))
}
}
func (s *testSuiteAgg) TestFirstRowEnum(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec(`use test;`)