summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/sp-code.test
blob: 5bb9466907aaf8b659662e8c53fafa88e16ab80c (plain)
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
#
# Test the debugging feature "show procedure/function code <name>" 
#

-- source include/have_debug.inc

--disable_warnings
drop procedure if exists empty;
drop procedure if exists code_sample;
--enable_warnings

create procedure empty()
begin
end;
show procedure code empty;
drop procedure empty;

create function almost_empty()
    returns int
  return 0;
show function code almost_empty;
drop function almost_empty;

delimiter //;
create procedure code_sample(x int, out err int, out nulls int)
begin
  declare count int default 0;

  set nulls = 0;
  begin
    declare c cursor for select name from t1;
    declare exit handler for not found close c;

    open c;
    loop
      begin
        declare n varchar(20);
        declare continue handler for sqlexception set err=1;

        fetch c into n;
        if isnull(n) then
          set nulls = nulls + 1;
	else
          set count = count + 1;
          update t2 set idx = count where name=n;
        end if;
      end;
    end loop;
  end;
  select t.name, t.idx from t2 t order by idx asc;
end//
delimiter ;//
show procedure code code_sample;
drop procedure code_sample;


#
# BUG#15737: Stored procedure optimizer bug with LEAVE
#
# This is a much more extensive test case than is strictly needed,
# but it was kept as is for two reasons:
# - The bug occurs under some quite special circumstances, so it
#   wasn't trivial to create a smaller test,
# - There's some value in having another more complex code sample
#   in this test file. This might catch future code generation bugs
#   that doesn't show in behaviour in any obvious way.

--disable_warnings
drop procedure if exists sudoku_solve;
--enable_warnings

delimiter //;
create procedure sudoku_solve(p_naive boolean, p_all boolean)
  deterministic
  modifies sql data
begin
  drop temporary table if exists sudoku_work, sudoku_schedule;

  create temporary table sudoku_work
  (
    row smallint not null,
    col smallint not null,
    dig smallint not null,
    cnt smallint,
    key using btree (cnt),
    key using btree (row),
    key using btree (col),
    unique key using hash (row,col)
  );

  create temporary table sudoku_schedule
  (
    idx int not null auto_increment primary key,
    row smallint not null,
    col smallint not null
  );

  call sudoku_init();

  if p_naive then
    update sudoku_work set cnt = 0 where dig = 0;
  else
    call sudoku_count();
  end if;
  insert into sudoku_schedule (row,col)
    select row,col from sudoku_work where cnt is not null order by cnt desc;

  begin
    declare v_scounter bigint default 0;
    declare v_i smallint default 1;
    declare v_dig smallint;
    declare v_schedmax smallint;

    select count(*) into v_schedmax from sudoku_schedule;

   more: 
    loop
    begin
      declare v_tcounter bigint default 0;

     sched:
      while v_i <= v_schedmax do
      begin
        declare v_row, v_col smallint;

        select row,col into v_row,v_col from sudoku_schedule where v_i = idx;

        select dig into v_dig from sudoku_work
          where v_row = row and v_col = col;

        case v_dig
        when 0 then
          set v_dig = 1;
          update sudoku_work set dig = 1
            where v_row = row and v_col = col;
        when 9 then
          if v_i > 0 then
            update sudoku_work set dig = 0
              where v_row = row and v_col = col;
            set v_i = v_i - 1;
            iterate sched;
          else
            select v_scounter as 'Solutions';
            leave more;
          end if;
        else
          set v_dig = v_dig + 1;
          update sudoku_work set dig = v_dig
            where v_row = row and v_col = col;
        end case;

        set v_tcounter = v_tcounter + 1;
        if not sudoku_digit_ok(v_row, v_col, v_dig) then
          iterate sched;
        end if;
        set v_i = v_i + 1;
      end;
      end while sched;

      select dig from sudoku_work;
      select v_tcounter as 'Tests';
      set v_scounter = v_scounter + 1;

      if p_all and v_i > 0 then
        set v_i = v_i - 1;
      else
        leave more;
      end if;
    end;
    end loop more;
  end;

  drop temporary table sudoku_work, sudoku_schedule;
end//
delimiter ;//

# The interestings parts are where the code for the two "leave" are:
# ...
#|  26 | jump_if_not 30 (v_i@3 > 0)                                            |
# ...
#|  30 | stmt 0 "select v_scounter as 'Solutions'"                             |
#|  31 | jump 45                                                               |
# ...
#|  42 | jump_if_not 45 (p_all@1 and (v_i@3 > 0))                              |
#|  43 | set v_i@3 (v_i@3 - 1)                                                 |
#|  44 | jump 14                                                               |
#|  45 | stmt 9 "drop temporary table sudoku_work, sud..."                     |
#+-----+-----------------------------------------------------------------------+
# The bug appeared at position 42 (with the wrong destination).
show procedure code sudoku_solve;

drop procedure sudoku_solve;

#
# Bug#19194 (Right recursion in parser for CASE causes excessive stack
#   usage, limitation)
# This bug also exposed a flaw in the generated code with nested case
# statements
#

--disable_warnings
DROP PROCEDURE IF EXISTS proc_19194_simple;
DROP PROCEDURE IF EXISTS proc_19194_searched;
DROP PROCEDURE IF EXISTS proc_19194_nested_1;
DROP PROCEDURE IF EXISTS proc_19194_nested_2;
DROP PROCEDURE IF EXISTS proc_19194_nested_3;
DROP PROCEDURE IF EXISTS proc_19194_nested_4;
--enable_warnings

delimiter |;

CREATE PROCEDURE proc_19194_simple(i int)
BEGIN
  DECLARE str CHAR(10);

  CASE i
    WHEN 1 THEN SET str="1";
    WHEN 2 THEN SET str="2";
    WHEN 3 THEN SET str="3";
    ELSE SET str="unknown";
  END CASE;

  SELECT str;
END|

CREATE PROCEDURE proc_19194_searched(i int)
BEGIN
  DECLARE str CHAR(10);

  CASE
    WHEN i=1 THEN SET str="1";
    WHEN i=2 THEN SET str="2";
    WHEN i=3 THEN SET str="3";
    ELSE SET str="unknown";
  END CASE;

  SELECT str;
END|

# Outer SIMPLE case, inner SEARCHED case
CREATE PROCEDURE proc_19194_nested_1(i int, j int)
BEGIN
  DECLARE str_i CHAR(10);
  DECLARE str_j CHAR(10);

  CASE i
    WHEN 10 THEN SET str_i="10";
    WHEN 20 THEN
    BEGIN
      set str_i="20";
      CASE
        WHEN j=1 THEN SET str_j="1";
        WHEN j=2 THEN SET str_j="2";
        WHEN j=3 THEN SET str_j="3";
      ELSE SET str_j="unknown";
      END CASE;
      select "i was 20";
    END;
    WHEN 30 THEN SET str_i="30";
    WHEN 40 THEN SET str_i="40";
    ELSE SET str_i="unknown";
  END CASE;

  SELECT str_i, str_j;
END|

# Outer SEARCHED case, inner SIMPLE case
CREATE PROCEDURE proc_19194_nested_2(i int, j int)
BEGIN
  DECLARE str_i CHAR(10);
  DECLARE str_j CHAR(10);

  CASE
    WHEN i=10 THEN SET str_i="10";
    WHEN i=20 THEN
    BEGIN
      set str_i="20";
      CASE j
        WHEN 1 THEN SET str_j="1";
        WHEN 2 THEN SET str_j="2";
        WHEN 3 THEN SET str_j="3";
      ELSE SET str_j="unknown";
      END CASE;
      select "i was 20";
    END;
    WHEN i=30 THEN SET str_i="30";
    WHEN i=40 THEN SET str_i="40";
    ELSE SET str_i="unknown";
  END CASE;

  SELECT str_i, str_j;
END|

# Outer SIMPLE case, inner SIMPLE case
CREATE PROCEDURE proc_19194_nested_3(i int, j int)
BEGIN
  DECLARE str_i CHAR(10);
  DECLARE str_j CHAR(10);

  CASE i
    WHEN 10 THEN SET str_i="10";
    WHEN 20 THEN
    BEGIN
      set str_i="20";
      CASE j
        WHEN 1 THEN SET str_j="1";
        WHEN 2 THEN SET str_j="2";
        WHEN 3 THEN SET str_j="3";
      ELSE SET str_j="unknown";
      END CASE;
      select "i was 20";
    END;
    WHEN 30 THEN SET str_i="30";
    WHEN 40 THEN SET str_i="40";
    ELSE SET str_i="unknown";
  END CASE;

  SELECT str_i, str_j;
END|

# Outer SEARCHED case, inner SEARCHED case
CREATE PROCEDURE proc_19194_nested_4(i int, j int)
BEGIN
  DECLARE str_i CHAR(10);
  DECLARE str_j CHAR(10);

  CASE
    WHEN i=10 THEN SET str_i="10";
    WHEN i=20 THEN
    BEGIN
      set str_i="20";
      CASE
        WHEN j=1 THEN SET str_j="1";
        WHEN j=2 THEN SET str_j="2";
        WHEN j=3 THEN SET str_j="3";
      ELSE SET str_j="unknown";
      END CASE;
      select "i was 20";
    END;
    WHEN i=30 THEN SET str_i="30";
    WHEN i=40 THEN SET str_i="40";
    ELSE SET str_i="unknown";
  END CASE;

  SELECT str_i, str_j;
END|

delimiter ;|

SHOW PROCEDURE CODE proc_19194_simple;
SHOW PROCEDURE CODE proc_19194_searched;
SHOW PROCEDURE CODE proc_19194_nested_1;
SHOW PROCEDURE CODE proc_19194_nested_2;
SHOW PROCEDURE CODE proc_19194_nested_3;
SHOW PROCEDURE CODE proc_19194_nested_4;

CALL proc_19194_nested_1(10, 1);

#
# Before 19194, the generated code was:
#   20      jump_if_not 23(27) 30
#   21      set str_i@2 _latin1'30'
# As opposed to the expected:
#   20      jump_if_not 23(27) (case_expr@0 = 30)
#   21      set str_i@2 _latin1'30'
#
# and as a result, this call returned "30",
# because the expression 30 is always true,
# masking the case 40, case 0 and the else.
#
CALL proc_19194_nested_1(25, 1);

CALL proc_19194_nested_1(20, 1);
CALL proc_19194_nested_1(20, 2);
CALL proc_19194_nested_1(20, 3);
CALL proc_19194_nested_1(20, 4);
CALL proc_19194_nested_1(30, 1);
CALL proc_19194_nested_1(40, 1);
CALL proc_19194_nested_1(0, 0);

CALL proc_19194_nested_2(10, 1);

#
# Before 19194, the generated code was:
#   20      jump_if_not 23(27) (case_expr@0 = (i@0 = 30))
#   21      set str_i@2 _latin1'30'
# As opposed to the expected:
#   20      jump_if_not 23(27) (i@0 = 30)
#   21      set str_i@2 _latin1'30'
# and as a result, this call crashed the server, because there is no
# such variable as "case_expr@0".
#
CALL proc_19194_nested_2(25, 1);

CALL proc_19194_nested_2(20, 1);
CALL proc_19194_nested_2(20, 2);
CALL proc_19194_nested_2(20, 3);
CALL proc_19194_nested_2(20, 4);
CALL proc_19194_nested_2(30, 1);
CALL proc_19194_nested_2(40, 1);
CALL proc_19194_nested_2(0, 0);

CALL proc_19194_nested_3(10, 1);
CALL proc_19194_nested_3(25, 1);
CALL proc_19194_nested_3(20, 1);
CALL proc_19194_nested_3(20, 2);
CALL proc_19194_nested_3(20, 3);
CALL proc_19194_nested_3(20, 4);
CALL proc_19194_nested_3(30, 1);
CALL proc_19194_nested_3(40, 1);
CALL proc_19194_nested_3(0, 0);

CALL proc_19194_nested_4(10, 1);
CALL proc_19194_nested_4(25, 1);
CALL proc_19194_nested_4(20, 1);
CALL proc_19194_nested_4(20, 2);
CALL proc_19194_nested_4(20, 3);
CALL proc_19194_nested_4(20, 4);
CALL proc_19194_nested_4(30, 1);
CALL proc_19194_nested_4(40, 1);
CALL proc_19194_nested_4(0, 0);

DROP PROCEDURE proc_19194_simple;
DROP PROCEDURE proc_19194_searched;
DROP PROCEDURE proc_19194_nested_1;
DROP PROCEDURE proc_19194_nested_2;
DROP PROCEDURE proc_19194_nested_3;
DROP PROCEDURE proc_19194_nested_4;

#
# Bug#19207: Final parenthesis omitted for CREATE INDEX in Stored
# Procedure
#
# Wrong criteria was used to distinguish the case when there was no
# lookahead performed in the parser.  Bug affected only statements
# ending in one-character token without any optional tail, like CREATE
# INDEX and CALL.
#
--disable_warnings
DROP PROCEDURE IF EXISTS p1;
--enable_warnings

CREATE PROCEDURE p1() CREATE INDEX idx ON t1 (c1);
SHOW PROCEDURE CODE p1;

DROP PROCEDURE p1;


#
# Bug#26977 exception handlers never hreturn
#
--disable_warnings
drop table if exists t1;
drop procedure if exists proc_26977_broken;
drop procedure if exists proc_26977_works;
--enable_warnings

create table t1(a int unique);

delimiter //;

create procedure proc_26977_broken(v int)
begin
  declare i int default 5;

  declare continue handler for sqlexception
  begin
    select 'caught something';
    retry:
    while i > 0 do
      begin
        set i = i - 1;
        select 'looping', i;
      end;
    end while retry;
  end;

  select 'do something';
  insert into t1 values (v);
  select 'do something again';
  insert into t1 values (v);
end//

create procedure proc_26977_works(v int)
begin
  declare i int default 5;

  declare continue handler for sqlexception
  begin
    select 'caught something';
    retry:
    while i > 0 do
      begin
        set i = i - 1;
        select 'looping', i;
      end;
    end while retry;
    select 'optimizer: keep hreturn';
  end;

  select 'do something';
  insert into t1 values (v);
  select 'do something again';
  insert into t1 values (v);
end//
delimiter ;//

show procedure code proc_26977_broken;

show procedure code proc_26977_works;

## This caust an error because of jump short cut
## optimization.
call proc_26977_broken(1);

## This works
call proc_26977_works(2);

drop table t1;
drop procedure proc_26977_broken;
drop procedure proc_26977_works;

#
# Bug#33618 Crash in sp_rcontext
#

--disable_warnings
drop procedure if exists proc_33618_h;
drop procedure if exists proc_33618_c;
--enable_warnings

delimiter //;

create procedure proc_33618_h(num int)
begin
  declare count1 int default '0';
  declare vb varchar(30);
  declare last_row int;
        
  while(num>=1) do
    set num=num-1;
    begin
      declare cur1 cursor for select `a` from t_33618;
      declare continue handler for not found set last_row = 1;
      set last_row:=0;
      open cur1;
      rep1:
      repeat
        begin
          declare exit handler for 1062 begin end;
          fetch cur1 into vb;
          if (last_row = 1) then
            ## should generate a hpop instruction here
            leave rep1;
          end if;
        end;
        until last_row=1
      end repeat;
      close cur1;
    end;
  end while;
end//

create procedure proc_33618_c(num int)
begin
  declare count1 int default '0';
  declare vb varchar(30);
  declare last_row int;
        
  while(num>=1) do
    set num=num-1;
    begin
      declare cur1 cursor for select `a` from t_33618;
      declare continue handler for not found set last_row = 1;
      set last_row:=0;
      open cur1;
      rep1:
      repeat
        begin
          declare cur2 cursor for select `b` from t_33618;
          fetch cur1 into vb;
          if (last_row = 1) then
            ## should generate a cpop instruction here
            leave rep1;
          end if;
        end;
        until last_row=1
      end repeat;
      close cur1;
    end;
  end while;
end//
delimiter ;//

show procedure code proc_33618_h;
show procedure code proc_33618_c;

drop procedure proc_33618_h;
drop procedure proc_33618_c;

#
# Bug#20906 (Multiple assignments in SET in stored routine produce incorrect
# instructions)
#

--disable_warnings
drop procedure if exists p_20906_a;
drop procedure if exists p_20906_b;
--enable_warnings

create procedure p_20906_a() SET @a=@a+1, @b=@b+1;
show procedure code p_20906_a;

set @a=1;
set @b=1;

call p_20906_a();
select @a, @b;

create procedure p_20906_b() SET @a=@a+1, @b=@b+1, @c=@c+1;
show procedure code p_20906_b;

set @a=1;
set @b=1;
set @c=1;

call p_20906_b();
select @a, @b, @c;

drop procedure p_20906_a;
drop procedure p_20906_b;

--echo End of 5.0 tests.

#
# Bug #26303: reserve() not called before qs_append() may lead to buffer
# overflow
#
DELIMITER //;
CREATE PROCEDURE p1() 
BEGIN 
  DECLARE dummy int default 0;

  CASE 12 
    WHEN 12 
    THEN SET dummy = 0;
  END CASE;
END//
DELIMITER ;//
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;

--echo #
--echo # Bug#23032: Handlers declared in a SP do not handle warnings generated in sub-SP
--echo #

--echo
--echo # - Case 4: check that "No Data trumps Warning".
--echo

CREATE TABLE t1(a INT);
INSERT INTO t1 VALUES (1), (2), (3);

delimiter |;

CREATE PROCEDURE p1()
BEGIN
  DECLARE c CURSOR FOR SELECT a FROM t1;

  OPEN c;
  
  BEGIN
    DECLARE v INT;

    DECLARE CONTINUE HANDLER FOR SQLWARNING
    BEGIN
      SELECT "Warning found!";
      SHOW WARNINGS;
    END;

    DECLARE EXIT HANDLER FOR NOT FOUND
    BEGIN
      SELECT "End of Result Set found!";
      SHOW WARNINGS;
    END;

    WHILE TRUE DO
      FETCH c INTO v;
    END WHILE;
  END;

  CLOSE c;

  SELECT a INTO @foo FROM t1 LIMIT 1; # Clear warning stack
END|

delimiter ;|

SET @save_dbug = @@debug_dbug;
SET SESSION debug_dbug="+d,bug23032_emit_warning";
CALL p1();
SET SESSION debug_dbug=@save_dbug;

DROP PROCEDURE p1;
DROP TABLE t1;

--echo #
--echo # Bug#11763507 - 56224: FUNCTION NAME IS CASE-SENSITIVE
--echo #
SET @@SQL_MODE = '';
DELIMITER $;
CREATE FUNCTION testf_bug11763507() RETURNS INT
BEGIN
    RETURN 0;
END
$

CREATE PROCEDURE testp_bug11763507()
BEGIN
    SELECT "PROCEDURE testp_bug11763507";
END
$

DELIMITER ;$

# STORED FUNCTIONS
SHOW FUNCTION CODE testf_bug11763507;
SHOW FUNCTION CODE TESTF_bug11763507;

# STORED PROCEDURE
SHOW PROCEDURE CODE testp_bug11763507;
SHOW PROCEDURE CODE TESTP_bug11763507;

DROP PROCEDURE testp_bug11763507;
DROP FUNCTION testf_bug11763507;

--echo #END OF BUG#11763507 test.


--echo #
--echo # MDEV-23408 Wrong result upon query from I_S and further Assertion `!alias_arg || strlen(alias_arg->str) == alias_arg->length' failed with certain connection charset
--echo #

SET NAMES utf8;
SET SESSION character_set_connection=latin1;
DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  DECLARE a VARCHAR(10) CHARACTER SET utf8;
  SET a='ä';
  SELECT a, 'ä' AS b;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
CALL p1;
DROP PROCEDURE p1;

--echo #
--echo # End of 10.2 tests
--echo #


--echo #
--echo # MDEV-13581 ROW TYPE OF t1 and t1%ROWTYPE for routine parameters
--echo #

CREATE TABLE t1 (a INT, b TEXT);
DELIMITER $$;
CREATE PROCEDURE p1(a ROW TYPE OF t1, OUT b ROW TYPE OF t1)
BEGIN
  SET a.a = 100;
  SET a.b = 'aaa';
  SET b.a = 200;
  SET b.b = 'bbb';
  SET a = b;
  SET b = a;
  SET a.a = b.a;
  SET b.a = a.a;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
DROP TABLE t1;

--echo #
--echo # MDEV-14415 Add Oracle-style FOR loop to sql_mode=DEFAULT
--echo #

--echo # Integer range FOR loop

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  FOR i IN 1..3
  DO
    SELECT i;
  END FOR;
END;
$$
DELIMITER ;$$
CALL p1;
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo # Nested integer range FOR loops

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  fori:
  FOR i IN 1..3
  DO
    forj:
    FOR j IN 1..3
    DO
      IF i = 3 THEN
        LEAVE fori;
      END IF;
      IF j = 3 THEN
        LEAVE forj;
      END IF;
      SELECT i,j;
    END FOR;
  END FOR;
END;
$$
DELIMITER ;$$
CALL p1;
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo # Explicit cursor FOR loops

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  DECLARE cur0 CURSOR FOR SELECT 10 AS a, 'b0' AS b;
  DECLARE cur1 CURSOR FOR SELECT 10 AS a, 'b0' AS b;
  DECLARE cur2 CURSOR FOR SELECT 10 AS a, 'b0' AS b;
  FOR rec1 IN cur1
  DO
    SELECT rec1.a, rec1.b;
    SET rec1.a= 11;
    SET rec1.b= 'b1';
    SELECT rec1.a, rec1.b;
  END FOR;
  FOR rec0 IN cur0
  DO
    SET rec0.a= 10;
    SET rec0.b='b0';
  END FOR;
  FOR rec2 IN cur2
  DO
    SET rec2.a= 10;
    SET rec2.b='b0';
  END FOR;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo # Nested explicit cursor FOR loops

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  DECLARE cur0 CURSOR FOR SELECT 10 AS a, 'b0' AS b;
  FOR rec0 IN cur0
  DO
    BEGIN
      DECLARE cur1 CURSOR FOR SELECT 11 AS a, 'b1' AS b;
      SET rec0.a= 11;
      SET rec0.b= 'b0';
      FOR rec1 IN cur1
      DO
        SET rec1.a= 11;
        SET rec1.b= 'b1';
        BEGIN
          DECLARE cur2 CURSOR FOR SELECT 12 AS a, 'b2' AS b;
          FOR rec2 IN cur2
          DO
            SET rec2.a=12;
            SET rec2.b='b2';
          END FOR;
        END;
      END FOR;
    END;
  END FOR;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo # Implicit cursor FOR loops

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  FOR rec1 IN (SELECT 11 AS a, 'b1' AS b)
  DO
    SELECT rec1.a, rec1.b;
    SET rec1.a= 11;
    SET rec1.b= 'b1';
    SELECT rec1.a, rec1.b;
  END FOR;
  FOR rec0 IN (SELECT 10 AS a, 'b0' AS b)
  DO
    SET rec0.a= 10;
    SET rec0.b='b0';
  END FOR;
  FOR rec2 IN (SELECT 12 AS a, 'b2' AS b)
  DO
    SET rec2.a= 10;
    SET rec2.b='b0';
  END FOR;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;

--echo # Nested implicit cursor FOR loops

DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
  FOR rec0 IN (SELECT 10 AS a, 'b0' AS b)
  DO
    SET rec0.a= 11;
    SET rec0.b= 'b0';
    FOR rec1 IN (SELECT 11 AS a, 'b1' AS b)
    DO
      SET rec1.a= 11;
      SET rec1.b= 'b1';
      FOR rec2 IN (SELECT 12 AS a, 'b2' AS b)
      DO
        SET rec2.a=12;
        SET rec2.b='b2';
      END FOR;
    END FOR;
  END FOR;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;

--echo #
--echo # MDEV-14623: Output of show function code does not show FETCH GROUP NEXT ROW
--echo # for custom aggregates
--echo #

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

delimiter ;|
show function code f1;
drop function f1;

--echo #
--echo # End of 10.3 tests
--echo #

--echo #
--echo # Start of 10.4 tests
--echo #

--echo #
--echo # MDEV-32275 getting error 'Illegal parameter data types row and bigint for operation '+' ' when using ITERATE in a FOR..DO
--echo #

--echo #
--echo # Unlabeled FOR/cursor with a nested labeled LOOP inside
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
  FOR _row IN (SELECT '' AS a) DO
    SELECT 'start of outerLoop';
    innerLoop: LOOP
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _row.a = 'v1'
      THEN
        SELECT 'start of THEN block';
        ITERATE innerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END LOOP;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo #
--echo # Labeled FOR/cursor with a nested labeled LOOP
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
  FOR _row IN (SELECT '' AS a) DO
    SELECT 'start of outerLoop';
    innerLoop: LOOP
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _row.a = 'v1'
      THEN
        SELECT 'start of IF/v1/THEN block';
        ITERATE innerLoop;
      END IF;
      IF _row.a = 'v2'
      THEN
        SELECT 'start of IF/v2/THEN block';
        ITERATE outerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END LOOP;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo #
--echo # Unlabeled FOR/integer with a labeled LOOP inside
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
  FOR _index IN 1..10 DO
    SELECT 'start of outerLoop';
    innerLoop: LOOP
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _index = 1
      THEN
        SELECT 'start of THEN block';
        ITERATE innerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END LOOP;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo #
--echo # Labeled FOR/integer with a labeled LOOP inside
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
  FOR _index IN 1..10 DO
    SELECT 'start of outerLoop';
    innerLoop: LOOP
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _index = 1
      THEN
        SELECT 'start of IF/1/THEN block';
        ITERATE innerLoop;
      END IF;
      IF _index = 2
      THEN
        SELECT 'start of IF/2/THEN block';
        ITERATE outerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END LOOP;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;


--echo #
--echo # Unlabeled FOR/integer with a labeled FOR inside
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
  FOR _index_outer IN 1..10 DO
    SELECT 'start of outerLoop';
    innerLoop:
    FOR _index_inner IN 1..10 DO
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _index_inner = 1
      THEN
        SELECT 'start of IF/1/THEN block';
        ITERATE innerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END FOR;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;



--echo #
--echo # Labeled FOR/integer with a labeled FOR inside
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
  FOR _index_outer IN 1..10 DO
    SELECT 'start of outerLoop';
    innerLoop:
    FOR _index_inner IN 1..10 DO
      SELECT 'start of innerLoop';
      IF loopDone THEN
        LEAVE innerLoop;
      END IF;
      SET loopDone = TRUE;
      IF _index_inner = 1
      THEN
        SELECT 'start of IF/1/THEN block';
        ITERATE innerLoop;
      END IF;
      IF _index_inner = 2
      THEN
        SELECT 'start of IF/2/THEN block';
        ITERATE outerLoop;
      END IF;
      SELECT 'end of innerLoop';
    END FOR;
    SELECT 'end of outerLoop';
  END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;

--echo #
--echo # End of 10.4 tests
--echo #


--echo #
--echo # MDEV-19640 Wrong SHOW PROCEDURE output for SET GLOBAL sysvar1=expr, sysvar2=expr
--echo #

DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
  SET GLOBAL max_allowed_packet=16000000, max_error_count=60;
  SELECT @@GLOBAL.max_allowed_packet, @@GLOBAL.max_error_count;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;

--echo #
--echo # End of 10.5 tests
--echo #