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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
|
create table t2 (sal int(10));
create aggregate function f1(x INT) returns int
begin
declare continue handler for not found return 0;
loop
fetch group next row;
insert into t2 (sal) values (x);
end loop;
end|
create table t1 (sal int(10),id int(10));
INSERT INTO t1 (sal,id) VALUES (5000,1);
INSERT INTO t1 (sal,id) VALUES (2000,1);
INSERT INTO t1 (sal,id) VALUES (1000,1);
select f1(sal) from t1 where id>= 1;
f1(sal)
0
Warnings:
Note 4094 At line 5 in test.f1
Note 4094 At line 5 in test.f1
Note 4094 At line 5 in test.f1
select * from t2;
sal
5000
2000
1000
drop table t2;
drop function f1;
create aggregate function f1(x INT) returns INT
begin
insert into t1(sal) values (x);
return x;
end|
ERROR HY000: Aggregate specific instruction(FETCH GROUP NEXT ROW) missing from the aggregate function
create function f1(x INT) returns INT
begin
set x=5;
fetch group next row;
return x+1;
end |
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
create aggregate function f1(x INT) returns INT
begin
declare continue handler for not found return x;
loop
fetch group next row;
end loop;
end |
select f1(1);
f1(1)
1
show create function f1;
Function sql_mode Create Function character_set_client collation_connection Database Collation
f1 STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION CREATE DEFINER=`root`@`localhost` AGGREGATE FUNCTION `f1`(x INT) RETURNS int(11)
begin
declare continue handler for not found return x;
loop
fetch group next row;
end loop;
end latin1 latin1_swedish_ci latin1_swedish_ci
alter function f1 aggregate none;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'aggregate none' at line 1
show create function f1;
Function sql_mode Create Function character_set_client collation_connection Database Collation
f1 STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION CREATE DEFINER=`root`@`localhost` AGGREGATE FUNCTION `f1`(x INT) RETURNS int(11)
begin
declare continue handler for not found return x;
loop
fetch group next row;
end loop;
end latin1 latin1_swedish_ci latin1_swedish_ci
select f1(1);
f1(1)
1
drop function f1;
create aggregate function f2(i int) returns int
begin
FEtCH GROUP NEXT ROW;
if i <= 0 then
return 0;
elseif i = 1 then
return (select count(*) from t1 where id = i);
else
return (select count(*) + f2( i - 1) from t1 where id = i);
end if;
end|
select f2(1)|
f2(1)
3
select f2(2)|
ERROR HY000: Recursive stored functions and triggers are not allowed
select f2(3)|
ERROR HY000: Recursive stored functions and triggers are not allowed
drop function f2|
create aggregate function f1(x int) returns int
begin
declare mini int default 0;
declare continue handler for not found return mini;
loop
fetch group next row;
set mini= mini+x;
fetch group next row;
end loop;
end|
select f1(10);
f1(10)
10
select f1(sal) from t1;
f1(sal)
6000
select f1(sal) from t1 where 1=0;
f1(sal)
NULL
drop function f1;
create aggregate function f1(x int) returns int
begin
declare mini int default 0;
LOOP
FETCH GROUP NEXT ROW;
set mini = mini + x;
END LOOP;
end|
ERROR 42000: No RETURN found in FUNCTION test.f1
create aggregate function f1(x int) returns int
begin
declare mini int default 0;
LOOP
FETCH GROUP NEXT ROW;
set mini = mini + x;
END LOOP;
return -1;
end|
select f1(sal) from t1|
ERROR 02000: No data - zero rows fetched, selected, or processed
drop function f1|
create aggregate function f1(x int) returns int
begin
declare mini int default 0;
declare continue handler for not found return mini;
FETCH GROUP NEXT ROW;
set mini = mini + x;
end|
select f1(sal) from t1|
ERROR 2F005: FUNCTION f1 ended without RETURN
drop function f1|
create aggregate function f1(x int) returns int
begin
declare mini int default 0;
declare continue handler for not found set mini=-1;
LOOP
FETCH GROUP NEXT ROW;
set mini = mini + x;
END LOOP;
return 0;
end|
select f1(sal) from t1|
ERROR 2F005: FUNCTION f1 ended without RETURN
drop function f1|
drop table t1|
create table t1 (sal int, id int, val int, counter int, primary key(id));
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 1, 10, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (2000, 2, 16, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 3, 18, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 4, 15, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (3000, 5, 11, 5);
create aggregate function f1(x INT) returns double
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
select id, f1(sal) from t1 where id>= 1 group by counter order by val;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1 group by counter;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by counter;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by val;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by id;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by counter;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by val;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
drop table t1;
create table t1 (sal int, id int, val int, counter int, primary key(id), unique key(val));
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 1, 10, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (2000, 2, NULL, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 3, 18, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 4, 15, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (3000, 5, 11, 5);
select id, f1(sal) from t1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1 group by counter;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by counter;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by val;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by id;
id f1(sal)
1 3000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by counter;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by val;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
drop table t1;
create table t1 (sal int, id int, val int, counter int, primary key(id), INDEX name (val,counter));
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 1, 10, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (2000, 2, 10, 4);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 3, 18, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 4, 11, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (3000, 5, 11, 5);
select id, f1(sal) from t1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1;
id f1(sal)
1 17000
select id, f1(sal) from t1 where id>= 1 group by counter;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val;
id f1(sal)
1 3000
3 6000
4 8000
select id, f1(sal) from t1 where id>= 1 group by counter order by counter;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by val;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by counter order by id;
id f1(sal)
1 1000
2 2000
3 6000
4 5000
5 3000
select id, f1(sal) from t1 where id>= 1 group by val order by counter;
id f1(sal)
1 3000
3 6000
4 8000
select id, f1(sal) from t1 where id>= 1 group by val order by id;
id f1(sal)
1 3000
3 6000
4 8000
select id, f1(sal) from t1 where id>= 1 group by val order by val;
id f1(sal)
1 3000
3 6000
4 8000
drop table t1;
drop function f1;
create aggregate function f1(x INT) returns double
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
create aggregate function f2() returns double
begin
declare z int default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z = z+1;
end loop;
end|
create table t1 (sal int, id int, val int, counter int);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 2, 10, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (2000, 1, 16, 5);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 2, 18, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 3, 15, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (3000, 4, 11, 4);
prepare test from "select f2() from t1 where id>= ?";
set @param= 2;
execute test using @param;
f2()
4
execute test using @param;
f2()
4
execute test using @param;
f2()
4
execute test using @param;
f2()
4
set @param= 1;
execute test using @param;
f2()
5
set @param= 3;
execute test using @param;
f2()
2
set @param= 4;
execute test using @param;
f2()
1
deallocate prepare test;
prepare test from "select f1(sal) from t1 where id>= ?";
set @param= 2;
execute test using @param;
f1(sal)
15000
execute test using @param;
f1(sal)
15000
execute test using @param;
f1(sal)
15000
execute test using @param;
f1(sal)
15000
set @param= 1;
execute test using @param;
f1(sal)
17000
set @param= 3;
execute test using @param;
f1(sal)
8000
set @param= 4;
execute test using @param;
f1(sal)
3000
set @param= 5;
execute test using @param;
f1(sal)
NULL
deallocate prepare test;
drop function f2;
prepare test from "select f1(sal) from t1 where id>= ?";
set @param= 2;
execute test using @param;
f1(sal)
15000
drop function f1;
create function f1(x int) returns int
return -1;
execute test using @param;
f1(sal)
-1
-1
-1
-1
drop function f1;
create aggregate function f1(x INT) returns double
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
execute test using @param;
f1(sal)
15000
deallocate prepare test;
drop table t1;
drop function f1;
create table t1 (sal int, id int, val varchar(10), counter int);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 2, 'ab', 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 1, 'cd', 5);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 2, 'ef', 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 3, 'gh', 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 4, 'ij', 4);
create table t2 (sal int, id int, val int, counter int);
INSERT INTO t2 (sal, id, val, counter) VALUES (1000, 2, 10, 2);
INSERT INTO t2 (sal, id, val, counter) VALUES (2000, 1, 16, 5);
INSERT INTO t2 (sal, id, val, counter) VALUES (6000, 2, 18, 1);
INSERT INTO t2 (sal, id, val, counter) VALUES (5000, 3, 15, 3);
INSERT INTO t2 (sal, id, val, counter) VALUES (3000, 4, 11, 4);
create aggregate function f1(x double) returns double
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
create aggregate function f2(x INT) returns CHAR(10)
begin
declare mini INT default 0;
declare continue handler for not found return mini;
loop
fetch group next row;
set mini= mini + x;
end loop;
end|
create aggregate function f3(x INT) returns CHAR(10)
begin
declare mini INT default 0;
declare continue handler for not found return mini;
loop
fetch group next row;
set mini= mini + x;
fetch group next row;
set mini= mini - x;
end loop;
end|
create aggregate function f4(x INT, y varchar(10)) returns varchar(1000)
begin
declare str varchar(1000) default '';
declare continue handler for not found return str;
loop
fetch group next row;
set str= concat(str,y);
end loop;
end|
create aggregate function f5(x INT) returns varchar(1000)
begin
declare z int default 0;
DECLARE cur1 CURSOR FOR SELECT sal FROM test.t2;
declare continue handler for not found return 0;
loop
fetch group next row;
set z = z+x;
end loop;
end|
create function f6(x int) returns int
return (select f1(sal) from t1)|
select f1(sal) from t1;
f1(sal)
5000
select f1(sal) from t1 where id>= 1 group by counter;
f1(sal)
1000
1000
1000
1000
1000
select f3(sal) from t1;
f3(sal)
1000
select f2(val) from t1;
ERROR 22007: Incorrect integer value: 'ab' for column ``.``.`x` at row 1
select val, id, c from (select f1(sal) as c from t2) as t1, t2;
val id c
10 2 17000
11 4 17000
15 3 17000
16 1 17000
18 2 17000
select f1(sal),f1(val), f1(id), f1(sal) from t2;
f1(sal) f1(val) f1(id) f1(sal)
17000 70 12 17000
select f4(sal, val) from t1;
f4(sal, val)
abcdefghij
select c from (select f1(sal) as c from t2) as t1;
c
17000
select f1((select val from t2 where 0 > 1)) from t1;
f1((select val from t2 where 0 > 1))
NULL
select f1((select val from t2 where id= 1)) from t1;
f1((select val from t2 where id= 1))
80
select f5(sal) from t1;
f5(sal)
0
SELECT f1(sal)*f1(sal) FROM t1;
f1(sal)*f1(sal)
25000000
SELECT (SELECT f1(sal) FROM t1) FROM t2;
(SELECT f1(sal) FROM t1)
5000
5000
5000
5000
5000
select id, f1(sal) from t1;
id f1(sal)
2 5000
select id, f1(sal) from t1 where id>= 1;
id f1(sal)
2 5000
select f1(sal), f1(sal) from t1 where id>= 1 group by counter;
f1(sal) f1(sal)
1000 1000
1000 1000
1000 1000
1000 1000
1000 1000
select f1(sal), f1(sal) from t1 where id>= 1 group by id ;
f1(sal) f1(sal)
1000 1000
1000 1000
1000 1000
2000 2000
select f1(sal) from t1 where id>= 1 group by id ;
f1(sal)
1000
1000
1000
2000
select f1(sal) from t1 where id>= 1 order by counter;
f1(sal)
5000
select f1(sal) from t1 where id>= 1 group by id order by counter;
f1(sal)
2000
1000
1000
1000
select counter, id, f1(sal) from t1 where id>= 1 group by id order by counter;
counter id f1(sal)
2 2 2000
3 3 1000
4 4 1000
5 1 1000
select id, f1(sal) from t1 where id>= 1 group by id order by counter;
id f1(sal)
2 2000
3 1000
4 1000
1 1000
drop table t1;
drop table t2;
drop function f1;
drop function f2;
drop function f3;
drop function f4;
drop function f5;
drop function f6;
create aggregate function f1(x INT) returns INT
begin
declare z double default 1000;
declare continue handler for not found return z;
loop
fetch group next row;
set z= (z&x);
end loop;
end|
create table t1 (sal int, id int, val int, counter int);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 2, 10, 2);
INSERT INTO t1 (sal, id, val, counter) VALUES (7000, 1, 16, 5);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 2, 18, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 3, 15, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (3000, 4, 11, 4);
INSERT INTO t1 (sal, id, val, counter) VALUES (2000, 5, 10, 7);
INSERT INTO t1 (sal, id, val, counter) VALUES (5000, 7, 13, 8);
INSERT INTO t1 (sal, id, val, counter) VALUES (6000, 6, 19, 9);
INSERT INTO t1 (sal, id, val, counter) VALUES (7000, 7, 12, 0);
INSERT INTO t1 (sal, id, val, counter) VALUES (4000, 6, 14, 1);
INSERT INTO t1 (sal, id, val, counter) VALUES (8000, 5, 19, 3);
INSERT INTO t1 (sal, id, val, counter) VALUES (9000, 4, 11, 4);
INSERT INTO t1 (sal, id, val, counter) VALUES (1000, 3, 11, 2);
select f1(sal) from t1 where id>= 1;
f1(sal)
768
drop function f1;
create aggregate function f1(x INT) returns double
begin
declare z double default 0;
declare count double default 0;
declare continue handler for not found return z/count;
loop
fetch group next row;
set z= z+x;
set count= count+1;
end loop;
end|
select f1(sal) from t1 where id>= 1;
f1(sal)
4923.076923076923
drop function f1;
create aggregate function f1(x INT) returns INT
begin
declare maxi INT default -1;
declare continue handler for not found return maxi;
loop
fetch group next row;
if maxi < x then
set maxi= x;
end if;
end loop;
end|
select f1(sal) from t1 where id>= 1;
f1(sal)
9000
drop function f1;
create aggregate function f1(x INT) returns double
begin
declare mini INT default 100000;
declare continue handler for not found return mini;
loop
fetch group next row;
if mini > x then
set mini = x;
end if;
end loop;
end|
select f1(sal) from t1 where id>= 1;
f1(sal)
1000
drop function f1;
create aggregate function f1(x INT) returns double
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z^x;
end loop;
end|
select f1(sal) from t1 where id>= 1;
f1(sal)
16288
drop function f1;
create aggregate function f1(x INT) returns INT
begin
declare z int default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
select f1(sal) from t1 where id>= 1;
f1(sal)
64000
create aggregate function f2() returns INT
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+1;
end loop;
end|
select f2() from t1;
f2()
13
create table t2 (sal int, id int);
INSERT INTO t2 (sal, id) VALUES (NULL, 1);
INSERT INTO t2 (sal, id) VALUES (2000, 1);
INSERT INTO t2 (sal, id) VALUES (3000, 1);
select f1(sal) from t2;
f1(sal)
NULL
select f1(1);
f1(1)
1
create function f3() returns int
return (select f1(sal) from t1);
select f3();
f3()
64000
create function f4() returns INT
return 1;
create aggregate function f5() returns INT
begin
declare z double default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+f3();
end loop;
end|
select f5() from t2;
f5()
192000
Warnings:
Note 4094 At line 6 in test.f5
Note 4094 At line 6 in test.f5
Note 4094 At line 6 in test.f5
create aggregate function f6(x INT) returns INT
begin
declare z int default 0;
declare continue handler for not found return z;
loop
fetch group next row;
if x then
set z= z+(select f1(sal) from t1);
end if;
end loop;
end|
select f6(sal) from t2;
f6(sal)
128000
Warnings:
Note 4094 At line 6 in test.f6
Note 4094 At line 6 in test.f6
select id, f1(sal) from t1 where id>= 1 group by id;
id f1(sal)
1 7000
2 7000
3 6000
4 12000
5 10000
6 10000
7 12000
select counter, f1(sal) from t1 where id>= 1 group by counter;
counter f1(sal)
0 7000
1 10000
2 2000
3 13000
4 12000
5 7000
7 2000
8 5000
9 6000
select val, f1(sal) from t1 where id>= 1 group by val;
val f1(sal)
10 3000
11 13000
12 7000
13 5000
14 4000
15 5000
16 7000
18 6000
19 14000
select counter, f1(sal) from t1 where id>= 1 group by id order by counter;
counter f1(sal)
0 12000
2 6000
2 7000
4 12000
5 7000
7 10000
9 10000
select counter, id, f1(sal), f1(sal) from t1 where id>= 1 group by id order by counter;
counter id f1(sal) f1(sal)
0 7 12000 12000
2 2 7000 7000
2 3 6000 6000
4 4 12000 12000
5 1 7000 7000
7 5 10000 10000
9 6 10000 10000
select counter, id, f1(sal), sum(distinct sal) from t1 where id>= 1 group by id order by counter desc;
counter id f1(sal) sum(distinct sal)
0 7 12000 12000
2 2 7000 7000
2 3 6000 6000
4 4 12000 12000
5 1 7000 7000
7 5 10000 10000
9 6 10000 10000
create table t3 (i int);
INSERT INTO t3 (i) select f1(sal) from t1;
select * from t3;
i
64000
create aggregate function f7(x INT) returns INT
begin
declare z int default 0;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE a,b,c INT;
DECLARE cur1 CURSOR FOR SELECT id FROM test.t2;
declare continue handler for not found return z;
outer_loop: LOOP
FETCH GROUP NEXT ROW;
set z= z+x;
inner_block: begin
DECLARE cur2 CURSOR FOR SELECT id FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur2;
read_loop: LOOP
FETCH cur2 INTO a;
IF done THEN
CLOSE cur2;
LEAVE read_loop;
END IF;
END LOOP read_loop;
end inner_block;
END LOOP outer_loop;
end|
select f7(sal) from t1;
f7(sal)
64000
Warnings:
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
Note 4094 At line 9 in test.f7
drop table t1;
drop table t2;
drop table t3;
drop function f1;
drop function f2;
drop function f3;
drop function f4;
drop function f5;
drop function f6;
drop function f7;
create aggregate function f1(x date) returns date
begin
declare continue handler for not found return x;
loop
fetch group next row;
end loop;
end|
select f1('2001-01-01'),cast(f1('2001-01-01') as time);
f1('2001-01-01') cast(f1('2001-01-01') as time)
2001-01-01 00:00:00
drop function f1;
#
# MDEV-15957 Unexpected "Data too long" when doing CREATE..SELECT with stored functions
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
CREATE AGGREGATE FUNCTION f1(x INT) RETURNS INT(3)
BEGIN
DECLARE res INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN res-200;
LOOP
FETCH GROUP NEXT ROW;
SET res= res + x;
END LOOP;
RETURN res;
END;
$$
CREATE TABLE t2 AS SELECT CONCAT(f1(a)) FROM t1;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`CONCAT(f1(a))` varchar(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1,t2;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET latin1
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` tinytext DEFAULT NULL,
`c2` tinytext DEFAULT NULL,
`c3` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS TEXT CHARACTER SET latin1
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` text DEFAULT NULL,
`c2` text DEFAULT NULL,
`c3` text DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET latin1
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` mediumtext DEFAULT NULL,
`c2` mediumtext DEFAULT NULL,
`c3` mediumtext DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET latin1
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` longtext DEFAULT NULL,
`c2` longtext DEFAULT NULL,
`c3` longtext DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET utf8
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` tinytext CHARACTER SET utf8 DEFAULT NULL,
`c2` text CHARACTER SET utf8 DEFAULT NULL,
`c3` varchar(255) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS TEXT CHARACTER SET utf8
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` text CHARACTER SET utf8 DEFAULT NULL,
`c2` mediumtext CHARACTER SET utf8 DEFAULT NULL,
`c3` mediumtext CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET utf8
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` mediumtext CHARACTER SET utf8 DEFAULT NULL,
`c2` longtext CHARACTER SET utf8 DEFAULT NULL,
`c3` longtext CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
CREATE AGGREGATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET utf8
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN '';
LOOP
FETCH GROUP NEXT ROW;
END LOOP;
RETURN '';
END;
$$
CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` longtext CHARACTER SET utf8 DEFAULT NULL,
`c2` longtext CHARACTER SET utf8 DEFAULT NULL,
`c3` longtext CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP FUNCTION f1;
#
# MDEV-14520: Custom aggregate functions work incorrectly with WITH ROLLUP clause
#
create aggregate function agg_sum(x INT) returns INT
begin
declare z int default 0;
declare continue handler for not found return z;
loop
fetch group next row;
set z= z+x;
end loop;
end|
create table t1 (i int);
insert into t1 values (1),(2),(2),(3);
select i, agg_sum(i) from t1 group by i with rollup;
i agg_sum(i)
1 1
2 4
3 3
NULL 8
#
# Compare with
select i, sum(i) from t1 group by i with rollup;
i sum(i)
1 1
2 4
3 3
NULL 8
drop function agg_sum;
drop table t1;
#
# User defined aggregate functions not working correctly when the schema is changed
#
CREATE SCHEMA IF NOT EXISTS common_schema;
CREATE SCHEMA IF NOT EXISTS another_schema;
DROP FUNCTION IF EXISTS common_schema.add_ints |
Warnings:
Note 1305 FUNCTION common_schema.add_ints does not exist
CREATE FUNCTION common_schema.add_ints(int_1 INT, int_2 INT) RETURNS INT NO SQL
BEGIN
RETURN int_1 + int_2;
END |
DROP FUNCTION IF EXISTS common_schema.sum_ints |
Warnings:
Note 1305 FUNCTION common_schema.sum_ints does not exist
CREATE AGGREGATE FUNCTION common_schema.sum_ints(int_val INT) RETURNS INT
BEGIN
DECLARE result INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN result;
LOOP FETCH GROUP NEXT ROW;
SET result = common_schema.add_ints(result, int_val);
END LOOP;
END |
use common_schema;
SELECT common_schema.sum_ints(seq) FROM (SELECT 1 seq UNION ALL SELECT 2) t;
common_schema.sum_ints(seq)
3
USE another_schema;
SELECT common_schema.sum_ints(seq) FROM (SELECT 1 seq UNION ALL SELECT 2) t;
common_schema.sum_ints(seq)
3
drop database common_schema;
drop database another_schema;
USE test;
# End of 10.3 tests
#
# MDEV-18813 PROCEDURE and anonymous blocks silently ignore FETCH GROUP NEXT ROW
#
CREATE PROCEDURE p1()
BEGIN
FETCH GROUP NEXT ROW;
END;
$$
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
BEGIN NOT ATOMIC
FETCH GROUP NEXT ROW;
END;
$$
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
CREATE DEFINER=root@localhost FUNCTION f1() RETURNS INT
BEGIN
FETCH GROUP NEXT ROW;
RETURN 0;
END;
$$
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1
AFTER INSERT ON t1 FOR EACH ROW
FETCH GROUP NEXT ROW;
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
DROP TABLE t1;
CREATE EVENT ev1
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH + INTERVAL 1 WEEK
DO FETCH GROUP NEXT ROW;
ERROR HY000: Aggregate specific instruction (FETCH GROUP NEXT ROW) used in a wrong context
# End of 10.4 tests
|