2
# Test the debugging feature "show procedure/function code <name>"
5
-- source include/have_debug.inc
8
drop procedure if exists empty;
9
drop procedure if exists code_sample;
12
create procedure empty()
15
show procedure code empty;
18
create function almost_empty()
21
show function code almost_empty;
22
drop function almost_empty;
25
create procedure code_sample(x int, out err int, out nulls int)
27
declare count int default 0;
31
declare c cursor for select name from t1;
32
declare exit handler for not found close c;
37
declare n varchar(20);
38
declare continue handler for sqlexception set err=1;
42
set nulls = nulls + 1;
44
set count = count + 1;
45
update t2 set idx = count where name=n;
50
select t.name, t.idx from t2 t order by idx asc;
53
show procedure code code_sample;
54
drop procedure code_sample;
58
# BUG#15737: Stored procedure optimizer bug with LEAVE
60
# This is a much more extensive test case than is strictly needed,
61
# but it was kept as is for two reasons:
62
# - The bug occurs under some quite special circumstances, so it
63
# wasn't trivial to create a smaller test,
64
# - There's some value in having another more complex code sample
65
# in this test file. This might catch future code generation bugs
66
# that doesn't show in behaviour in any obvious way.
69
drop procedure if exists sudoku_solve;
73
create procedure sudoku_solve(p_naive boolean, p_all boolean)
77
drop temporary table if exists sudoku_work, sudoku_schedule;
79
create temporary table sudoku_work
81
row smallint not null,
82
col smallint not null,
83
dig smallint not null,
85
key using btree (cnt),
86
key using btree (row),
87
key using btree (col),
88
unique key using hash (row,col)
91
create temporary table sudoku_schedule
93
idx int not null auto_increment primary key,
94
row smallint not null,
101
update sudoku_work set cnt = 0 where dig = 0;
105
insert into sudoku_schedule (row,col)
106
select row,col from sudoku_work where cnt is not null order by cnt desc;
109
declare v_scounter bigint default 0;
110
declare v_i smallint default 1;
111
declare v_dig smallint;
112
declare v_schedmax smallint;
114
select count(*) into v_schedmax from sudoku_schedule;
119
declare v_tcounter bigint default 0;
122
while v_i <= v_schedmax do
124
declare v_row, v_col smallint;
126
select row,col into v_row,v_col from sudoku_schedule where v_i = idx;
128
select dig into v_dig from sudoku_work
129
where v_row = row and v_col = col;
134
update sudoku_work set dig = 1
135
where v_row = row and v_col = col;
138
update sudoku_work set dig = 0
139
where v_row = row and v_col = col;
143
select v_scounter as 'Solutions';
147
set v_dig = v_dig + 1;
148
update sudoku_work set dig = v_dig
149
where v_row = row and v_col = col;
152
set v_tcounter = v_tcounter + 1;
153
if not sudoku_digit_ok(v_row, v_col, v_dig) then
160
select dig from sudoku_work;
161
select v_tcounter as 'Tests';
162
set v_scounter = v_scounter + 1;
164
if p_all and v_i > 0 then
173
drop temporary table sudoku_work, sudoku_schedule;
177
# The interestings parts are where the code for the two "leave" are:
179
#| 26 | jump_if_not 30 (v_i@3 > 0) |
181
#| 30 | stmt 0 "select v_scounter as 'Solutions'" |
184
#| 42 | jump_if_not 45 (p_all@1 and (v_i@3 > 0)) |
185
#| 43 | set v_i@3 (v_i@3 - 1) |
187
#| 45 | stmt 9 "drop temporary table sudoku_work, sud..." |
188
#+-----+-----------------------------------------------------------------------+
189
# The bug appeared at position 42 (with the wrong destination).
190
show procedure code sudoku_solve;
192
drop procedure sudoku_solve;
195
# Bug#19194 (Right recursion in parser for CASE causes excessive stack
197
# This bug also exposed a flaw in the generated code with nested case
202
DROP PROCEDURE IF EXISTS proc_19194_simple;
203
DROP PROCEDURE IF EXISTS proc_19194_searched;
204
DROP PROCEDURE IF EXISTS proc_19194_nested_1;
205
DROP PROCEDURE IF EXISTS proc_19194_nested_2;
206
DROP PROCEDURE IF EXISTS proc_19194_nested_3;
207
DROP PROCEDURE IF EXISTS proc_19194_nested_4;
212
CREATE PROCEDURE proc_19194_simple(i int)
214
DECLARE str CHAR(10);
217
WHEN 1 THEN SET str="1";
218
WHEN 2 THEN SET str="2";
219
WHEN 3 THEN SET str="3";
220
ELSE SET str="unknown";
226
CREATE PROCEDURE proc_19194_searched(i int)
228
DECLARE str CHAR(10);
231
WHEN i=1 THEN SET str="1";
232
WHEN i=2 THEN SET str="2";
233
WHEN i=3 THEN SET str="3";
234
ELSE SET str="unknown";
240
# Outer SIMPLE case, inner SEARCHED case
241
CREATE PROCEDURE proc_19194_nested_1(i int, j int)
243
DECLARE str_i CHAR(10);
244
DECLARE str_j CHAR(10);
247
WHEN 10 THEN SET str_i="10";
252
WHEN j=1 THEN SET str_j="1";
253
WHEN j=2 THEN SET str_j="2";
254
WHEN j=3 THEN SET str_j="3";
255
ELSE SET str_j="unknown";
259
WHEN 30 THEN SET str_i="30";
260
WHEN 40 THEN SET str_i="40";
261
ELSE SET str_i="unknown";
267
# Outer SEARCHED case, inner SIMPLE case
268
CREATE PROCEDURE proc_19194_nested_2(i int, j int)
270
DECLARE str_i CHAR(10);
271
DECLARE str_j CHAR(10);
274
WHEN i=10 THEN SET str_i="10";
279
WHEN 1 THEN SET str_j="1";
280
WHEN 2 THEN SET str_j="2";
281
WHEN 3 THEN SET str_j="3";
282
ELSE SET str_j="unknown";
286
WHEN i=30 THEN SET str_i="30";
287
WHEN i=40 THEN SET str_i="40";
288
ELSE SET str_i="unknown";
294
# Outer SIMPLE case, inner SIMPLE case
295
CREATE PROCEDURE proc_19194_nested_3(i int, j int)
297
DECLARE str_i CHAR(10);
298
DECLARE str_j CHAR(10);
301
WHEN 10 THEN SET str_i="10";
306
WHEN 1 THEN SET str_j="1";
307
WHEN 2 THEN SET str_j="2";
308
WHEN 3 THEN SET str_j="3";
309
ELSE SET str_j="unknown";
313
WHEN 30 THEN SET str_i="30";
314
WHEN 40 THEN SET str_i="40";
315
ELSE SET str_i="unknown";
321
# Outer SEARCHED case, inner SEARCHED case
322
CREATE PROCEDURE proc_19194_nested_4(i int, j int)
324
DECLARE str_i CHAR(10);
325
DECLARE str_j CHAR(10);
328
WHEN i=10 THEN SET str_i="10";
333
WHEN j=1 THEN SET str_j="1";
334
WHEN j=2 THEN SET str_j="2";
335
WHEN j=3 THEN SET str_j="3";
336
ELSE SET str_j="unknown";
340
WHEN i=30 THEN SET str_i="30";
341
WHEN i=40 THEN SET str_i="40";
342
ELSE SET str_i="unknown";
350
SHOW PROCEDURE CODE proc_19194_simple;
351
SHOW PROCEDURE CODE proc_19194_searched;
352
SHOW PROCEDURE CODE proc_19194_nested_1;
353
SHOW PROCEDURE CODE proc_19194_nested_2;
354
SHOW PROCEDURE CODE proc_19194_nested_3;
355
SHOW PROCEDURE CODE proc_19194_nested_4;
357
CALL proc_19194_nested_1(10, 1);
360
# Before 19194, the generated code was:
361
# 20 jump_if_not 23(27) 30
362
# 21 set str_i@2 _latin1'30'
363
# As opposed to the expected:
364
# 20 jump_if_not 23(27) (case_expr@0 = 30)
365
# 21 set str_i@2 _latin1'30'
367
# and as a result, this call returned "30",
368
# because the expression 30 is always true,
369
# masking the case 40, case 0 and the else.
371
CALL proc_19194_nested_1(25, 1);
373
CALL proc_19194_nested_1(20, 1);
374
CALL proc_19194_nested_1(20, 2);
375
CALL proc_19194_nested_1(20, 3);
376
CALL proc_19194_nested_1(20, 4);
377
CALL proc_19194_nested_1(30, 1);
378
CALL proc_19194_nested_1(40, 1);
379
CALL proc_19194_nested_1(0, 0);
381
CALL proc_19194_nested_2(10, 1);
384
# Before 19194, the generated code was:
385
# 20 jump_if_not 23(27) (case_expr@0 = (i@0 = 30))
386
# 21 set str_i@2 _latin1'30'
387
# As opposed to the expected:
388
# 20 jump_if_not 23(27) (i@0 = 30)
389
# 21 set str_i@2 _latin1'30'
390
# and as a result, this call crashed the server, because there is no
391
# such variable as "case_expr@0".
393
CALL proc_19194_nested_2(25, 1);
395
CALL proc_19194_nested_2(20, 1);
396
CALL proc_19194_nested_2(20, 2);
397
CALL proc_19194_nested_2(20, 3);
398
CALL proc_19194_nested_2(20, 4);
399
CALL proc_19194_nested_2(30, 1);
400
CALL proc_19194_nested_2(40, 1);
401
CALL proc_19194_nested_2(0, 0);
403
CALL proc_19194_nested_3(10, 1);
404
CALL proc_19194_nested_3(25, 1);
405
CALL proc_19194_nested_3(20, 1);
406
CALL proc_19194_nested_3(20, 2);
407
CALL proc_19194_nested_3(20, 3);
408
CALL proc_19194_nested_3(20, 4);
409
CALL proc_19194_nested_3(30, 1);
410
CALL proc_19194_nested_3(40, 1);
411
CALL proc_19194_nested_3(0, 0);
413
CALL proc_19194_nested_4(10, 1);
414
CALL proc_19194_nested_4(25, 1);
415
CALL proc_19194_nested_4(20, 1);
416
CALL proc_19194_nested_4(20, 2);
417
CALL proc_19194_nested_4(20, 3);
418
CALL proc_19194_nested_4(20, 4);
419
CALL proc_19194_nested_4(30, 1);
420
CALL proc_19194_nested_4(40, 1);
421
CALL proc_19194_nested_4(0, 0);
423
DROP PROCEDURE proc_19194_simple;
424
DROP PROCEDURE proc_19194_searched;
425
DROP PROCEDURE proc_19194_nested_1;
426
DROP PROCEDURE proc_19194_nested_2;
427
DROP PROCEDURE proc_19194_nested_3;
428
DROP PROCEDURE proc_19194_nested_4;
431
# Bug#19207: Final parenthesis omitted for CREATE INDEX in Stored
434
# Wrong criteria was used to distinguish the case when there was no
435
# lookahead performed in the parser. Bug affected only statements
436
# ending in one-character token without any optional tail, like CREATE
440
DROP PROCEDURE IF EXISTS p1;
443
CREATE PROCEDURE p1() CREATE INDEX idx ON t1 (c1);
444
SHOW PROCEDURE CODE p1;
450
# Bug#26977 exception handlers never hreturn
453
drop table if exists t1;
454
drop procedure if exists proc_26977_broken;
455
drop procedure if exists proc_26977_works;
458
create table t1(a int unique);
462
create procedure proc_26977_broken(v int)
464
declare i int default 5;
466
declare continue handler for sqlexception
468
select 'caught something';
478
select 'do something';
479
insert into t1 values (v);
480
select 'do something again';
481
insert into t1 values (v);
484
create procedure proc_26977_works(v int)
486
declare i int default 5;
488
declare continue handler for sqlexception
490
select 'caught something';
498
select 'optimizer: keep hreturn';
501
select 'do something';
502
insert into t1 values (v);
503
select 'do something again';
504
insert into t1 values (v);
508
show procedure code proc_26977_broken;
510
show procedure code proc_26977_works;
512
## This caust an error because of jump short cut
514
call proc_26977_broken(1);
517
call proc_26977_works(2);
520
drop procedure proc_26977_broken;
521
drop procedure proc_26977_works;
524
# Bug#33618 Crash in sp_rcontext
528
drop procedure if exists proc_33618_h;
529
drop procedure if exists proc_33618_c;
534
create procedure proc_33618_h(num int)
536
declare count1 int default '0';
537
declare vb varchar(30);
538
declare last_row int;
543
declare cur1 cursor for select `a` from t_33618;
544
declare continue handler for not found set last_row = 1;
550
declare exit handler for 1062 begin end;
552
if (last_row = 1) then
553
## should generate a hpop instruction here
564
create procedure proc_33618_c(num int)
566
declare count1 int default '0';
567
declare vb varchar(30);
568
declare last_row int;
573
declare cur1 cursor for select `a` from t_33618;
574
declare continue handler for not found set last_row = 1;
580
declare cur2 cursor for select `b` from t_33618;
582
if (last_row = 1) then
583
## should generate a cpop instruction here
595
show procedure code proc_33618_h;
596
show procedure code proc_33618_c;
598
drop procedure proc_33618_h;
599
drop procedure proc_33618_c;
602
# Bug#20906 (Multiple assignments in SET in stored routine produce incorrect
607
drop procedure if exists p_20906_a;
608
drop procedure if exists p_20906_b;
611
create procedure p_20906_a() SET @a=@a+1, @b=@b+1;
612
show procedure code p_20906_a;
620
create procedure p_20906_b() SET @a=@a+1, @b=@b+1, @c=@c+1;
621
show procedure code p_20906_b;
630
drop procedure p_20906_a;
631
drop procedure p_20906_b;
633
--echo End of 5.0 tests.
636
# Bug #26303: reserve() not called before qs_append() may lead to buffer
640
CREATE PROCEDURE p1()
642
DECLARE dummy int default 0;
650
SHOW PROCEDURE CODE p1;