~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/t/sp-code.test

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Test the debugging feature "show procedure/function code <name>" 
 
3
#
 
4
 
 
5
-- source include/have_debug.inc
 
6
 
 
7
--disable_warnings
 
8
drop procedure if exists empty;
 
9
drop procedure if exists code_sample;
 
10
--enable_warnings
 
11
 
 
12
create procedure empty()
 
13
begin
 
14
end;
 
15
show procedure code empty;
 
16
drop procedure empty;
 
17
 
 
18
create function almost_empty()
 
19
    returns int
 
20
  return 0;
 
21
show function code almost_empty;
 
22
drop function almost_empty;
 
23
 
 
24
delimiter //;
 
25
create procedure code_sample(x int, out err int, out nulls int)
 
26
begin
 
27
  declare count int default 0;
 
28
 
 
29
  set nulls = 0;
 
30
  begin
 
31
    declare c cursor for select name from t1;
 
32
    declare exit handler for not found close c;
 
33
 
 
34
    open c;
 
35
    loop
 
36
      begin
 
37
        declare n varchar(20);
 
38
        declare continue handler for sqlexception set err=1;
 
39
 
 
40
        fetch c into n;
 
41
        if isnull(n) then
 
42
          set nulls = nulls + 1;
 
43
        else
 
44
          set count = count + 1;
 
45
          update t2 set idx = count where name=n;
 
46
        end if;
 
47
      end;
 
48
    end loop;
 
49
  end;
 
50
  select t.name, t.idx from t2 t order by idx asc;
 
51
end//
 
52
delimiter ;//
 
53
show procedure code code_sample;
 
54
drop procedure code_sample;
 
55
 
 
56
 
 
57
#
 
58
# BUG#15737: Stored procedure optimizer bug with LEAVE
 
59
#
 
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.
 
67
 
 
68
--disable_warnings
 
69
drop procedure if exists sudoku_solve;
 
70
--enable_warnings
 
71
 
 
72
delimiter //;
 
73
create procedure sudoku_solve(p_naive boolean, p_all boolean)
 
74
  deterministic
 
75
  modifies sql data
 
76
begin
 
77
  drop temporary table if exists sudoku_work, sudoku_schedule;
 
78
 
 
79
  create temporary table sudoku_work
 
80
  (
 
81
    row smallint not null,
 
82
    col smallint not null,
 
83
    dig smallint not null,
 
84
    cnt smallint,
 
85
    key using btree (cnt),
 
86
    key using btree (row),
 
87
    key using btree (col),
 
88
    unique key using hash (row,col)
 
89
  );
 
90
 
 
91
  create temporary table sudoku_schedule
 
92
  (
 
93
    idx int not null auto_increment primary key,
 
94
    row smallint not null,
 
95
    col smallint not null
 
96
  );
 
97
 
 
98
  call sudoku_init();
 
99
 
 
100
  if p_naive then
 
101
    update sudoku_work set cnt = 0 where dig = 0;
 
102
  else
 
103
    call sudoku_count();
 
104
  end if;
 
105
  insert into sudoku_schedule (row,col)
 
106
    select row,col from sudoku_work where cnt is not null order by cnt desc;
 
107
 
 
108
  begin
 
109
    declare v_scounter bigint default 0;
 
110
    declare v_i smallint default 1;
 
111
    declare v_dig smallint;
 
112
    declare v_schedmax smallint;
 
113
 
 
114
    select count(*) into v_schedmax from sudoku_schedule;
 
115
 
 
116
   more: 
 
117
    loop
 
118
    begin
 
119
      declare v_tcounter bigint default 0;
 
120
 
 
121
     sched:
 
122
      while v_i <= v_schedmax do
 
123
      begin
 
124
        declare v_row, v_col smallint;
 
125
 
 
126
        select row,col into v_row,v_col from sudoku_schedule where v_i = idx;
 
127
 
 
128
        select dig into v_dig from sudoku_work
 
129
          where v_row = row and v_col = col;
 
130
 
 
131
        case v_dig
 
132
        when 0 then
 
133
          set v_dig = 1;
 
134
          update sudoku_work set dig = 1
 
135
            where v_row = row and v_col = col;
 
136
        when 9 then
 
137
          if v_i > 0 then
 
138
            update sudoku_work set dig = 0
 
139
              where v_row = row and v_col = col;
 
140
            set v_i = v_i - 1;
 
141
            iterate sched;
 
142
          else
 
143
            select v_scounter as 'Solutions';
 
144
            leave more;
 
145
          end if;
 
146
        else
 
147
          set v_dig = v_dig + 1;
 
148
          update sudoku_work set dig = v_dig
 
149
            where v_row = row and v_col = col;
 
150
        end case;
 
151
 
 
152
        set v_tcounter = v_tcounter + 1;
 
153
        if not sudoku_digit_ok(v_row, v_col, v_dig) then
 
154
          iterate sched;
 
155
        end if;
 
156
        set v_i = v_i + 1;
 
157
      end;
 
158
      end while sched;
 
159
 
 
160
      select dig from sudoku_work;
 
161
      select v_tcounter as 'Tests';
 
162
      set v_scounter = v_scounter + 1;
 
163
 
 
164
      if p_all and v_i > 0 then
 
165
        set v_i = v_i - 1;
 
166
      else
 
167
        leave more;
 
168
      end if;
 
169
    end;
 
170
    end loop more;
 
171
  end;
 
172
 
 
173
  drop temporary table sudoku_work, sudoku_schedule;
 
174
end//
 
175
delimiter ;//
 
176
 
 
177
# The interestings parts are where the code for the two "leave" are:
 
178
# ...
 
179
#|  26 | jump_if_not 30 (v_i@3 > 0)                                            |
 
180
# ...
 
181
#|  30 | stmt 0 "select v_scounter as 'Solutions'"                             |
 
182
#|  31 | jump 45                                                               |
 
183
# ...
 
184
#|  42 | jump_if_not 45 (p_all@1 and (v_i@3 > 0))                              |
 
185
#|  43 | set v_i@3 (v_i@3 - 1)                                                 |
 
186
#|  44 | jump 14                                                               |
 
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;
 
191
 
 
192
drop procedure sudoku_solve;
 
193
 
 
194
#
 
195
# Bug#19194 (Right recursion in parser for CASE causes excessive stack
 
196
#   usage, limitation)
 
197
# This bug also exposed a flaw in the generated code with nested case
 
198
# statements
 
199
#
 
200
 
 
201
--disable_warnings
 
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;
 
208
--enable_warnings
 
209
 
 
210
delimiter |;
 
211
 
 
212
CREATE PROCEDURE proc_19194_simple(i int)
 
213
BEGIN
 
214
  DECLARE str CHAR(10);
 
215
 
 
216
  CASE i
 
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";
 
221
  END CASE;
 
222
 
 
223
  SELECT str;
 
224
END|
 
225
 
 
226
CREATE PROCEDURE proc_19194_searched(i int)
 
227
BEGIN
 
228
  DECLARE str CHAR(10);
 
229
 
 
230
  CASE
 
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";
 
235
  END CASE;
 
236
 
 
237
  SELECT str;
 
238
END|
 
239
 
 
240
# Outer SIMPLE case, inner SEARCHED case
 
241
CREATE PROCEDURE proc_19194_nested_1(i int, j int)
 
242
BEGIN
 
243
  DECLARE str_i CHAR(10);
 
244
  DECLARE str_j CHAR(10);
 
245
 
 
246
  CASE i
 
247
    WHEN 10 THEN SET str_i="10";
 
248
    WHEN 20 THEN
 
249
    BEGIN
 
250
      set str_i="20";
 
251
      CASE
 
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";
 
256
      END CASE;
 
257
      select "i was 20";
 
258
    END;
 
259
    WHEN 30 THEN SET str_i="30";
 
260
    WHEN 40 THEN SET str_i="40";
 
261
    ELSE SET str_i="unknown";
 
262
  END CASE;
 
263
 
 
264
  SELECT str_i, str_j;
 
265
END|
 
266
 
 
267
# Outer SEARCHED case, inner SIMPLE case
 
268
CREATE PROCEDURE proc_19194_nested_2(i int, j int)
 
269
BEGIN
 
270
  DECLARE str_i CHAR(10);
 
271
  DECLARE str_j CHAR(10);
 
272
 
 
273
  CASE
 
274
    WHEN i=10 THEN SET str_i="10";
 
275
    WHEN i=20 THEN
 
276
    BEGIN
 
277
      set str_i="20";
 
278
      CASE j
 
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";
 
283
      END CASE;
 
284
      select "i was 20";
 
285
    END;
 
286
    WHEN i=30 THEN SET str_i="30";
 
287
    WHEN i=40 THEN SET str_i="40";
 
288
    ELSE SET str_i="unknown";
 
289
  END CASE;
 
290
 
 
291
  SELECT str_i, str_j;
 
292
END|
 
293
 
 
294
# Outer SIMPLE case, inner SIMPLE case
 
295
CREATE PROCEDURE proc_19194_nested_3(i int, j int)
 
296
BEGIN
 
297
  DECLARE str_i CHAR(10);
 
298
  DECLARE str_j CHAR(10);
 
299
 
 
300
  CASE i
 
301
    WHEN 10 THEN SET str_i="10";
 
302
    WHEN 20 THEN
 
303
    BEGIN
 
304
      set str_i="20";
 
305
      CASE j
 
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";
 
310
      END CASE;
 
311
      select "i was 20";
 
312
    END;
 
313
    WHEN 30 THEN SET str_i="30";
 
314
    WHEN 40 THEN SET str_i="40";
 
315
    ELSE SET str_i="unknown";
 
316
  END CASE;
 
317
 
 
318
  SELECT str_i, str_j;
 
319
END|
 
320
 
 
321
# Outer SEARCHED case, inner SEARCHED case
 
322
CREATE PROCEDURE proc_19194_nested_4(i int, j int)
 
323
BEGIN
 
324
  DECLARE str_i CHAR(10);
 
325
  DECLARE str_j CHAR(10);
 
326
 
 
327
  CASE
 
328
    WHEN i=10 THEN SET str_i="10";
 
329
    WHEN i=20 THEN
 
330
    BEGIN
 
331
      set str_i="20";
 
332
      CASE
 
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";
 
337
      END CASE;
 
338
      select "i was 20";
 
339
    END;
 
340
    WHEN i=30 THEN SET str_i="30";
 
341
    WHEN i=40 THEN SET str_i="40";
 
342
    ELSE SET str_i="unknown";
 
343
  END CASE;
 
344
 
 
345
  SELECT str_i, str_j;
 
346
END|
 
347
 
 
348
delimiter ;|
 
349
 
 
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;
 
356
 
 
357
CALL proc_19194_nested_1(10, 1);
 
358
 
 
359
#
 
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'
 
366
#
 
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.
 
370
#
 
371
CALL proc_19194_nested_1(25, 1);
 
372
 
 
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);
 
380
 
 
381
CALL proc_19194_nested_2(10, 1);
 
382
 
 
383
#
 
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".
 
392
#
 
393
CALL proc_19194_nested_2(25, 1);
 
394
 
 
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);
 
402
 
 
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);
 
412
 
 
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);
 
422
 
 
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;
 
429
 
 
430
#
 
431
# Bug#19207: Final parenthesis omitted for CREATE INDEX in Stored
 
432
# Procedure
 
433
#
 
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
 
437
# INDEX and CALL.
 
438
#
 
439
--disable_warnings
 
440
DROP PROCEDURE IF EXISTS p1;
 
441
--enable_warnings
 
442
 
 
443
CREATE PROCEDURE p1() CREATE INDEX idx ON t1 (c1);
 
444
SHOW PROCEDURE CODE p1;
 
445
 
 
446
DROP PROCEDURE p1;
 
447
 
 
448
 
 
449
#
 
450
# Bug#26977 exception handlers never hreturn
 
451
#
 
452
--disable_warnings
 
453
drop table if exists t1;
 
454
drop procedure if exists proc_26977_broken;
 
455
drop procedure if exists proc_26977_works;
 
456
--enable_warnings
 
457
 
 
458
create table t1(a int unique);
 
459
 
 
460
delimiter //;
 
461
 
 
462
create procedure proc_26977_broken(v int)
 
463
begin
 
464
  declare i int default 5;
 
465
 
 
466
  declare continue handler for sqlexception
 
467
  begin
 
468
    select 'caught something';
 
469
    retry:
 
470
    while i > 0 do
 
471
      begin
 
472
        set i = i - 1;
 
473
        select 'looping', i;
 
474
      end;
 
475
    end while retry;
 
476
  end;
 
477
 
 
478
  select 'do something';
 
479
  insert into t1 values (v);
 
480
  select 'do something again';
 
481
  insert into t1 values (v);
 
482
end//
 
483
 
 
484
create procedure proc_26977_works(v int)
 
485
begin
 
486
  declare i int default 5;
 
487
 
 
488
  declare continue handler for sqlexception
 
489
  begin
 
490
    select 'caught something';
 
491
    retry:
 
492
    while i > 0 do
 
493
      begin
 
494
        set i = i - 1;
 
495
        select 'looping', i;
 
496
      end;
 
497
    end while retry;
 
498
    select 'optimizer: keep hreturn';
 
499
  end;
 
500
 
 
501
  select 'do something';
 
502
  insert into t1 values (v);
 
503
  select 'do something again';
 
504
  insert into t1 values (v);
 
505
end//
 
506
delimiter ;//
 
507
 
 
508
show procedure code proc_26977_broken;
 
509
 
 
510
show procedure code proc_26977_works;
 
511
 
 
512
## This caust an error because of jump short cut
 
513
## optimization.
 
514
call proc_26977_broken(1);
 
515
 
 
516
## This works
 
517
call proc_26977_works(2);
 
518
 
 
519
drop table t1;
 
520
drop procedure proc_26977_broken;
 
521
drop procedure proc_26977_works;
 
522
 
 
523
#
 
524
# Bug#33618 Crash in sp_rcontext
 
525
#
 
526
 
 
527
--disable_warnings
 
528
drop procedure if exists proc_33618_h;
 
529
drop procedure if exists proc_33618_c;
 
530
--enable_warnings
 
531
 
 
532
delimiter //;
 
533
 
 
534
create procedure proc_33618_h(num int)
 
535
begin
 
536
  declare count1 int default '0';
 
537
  declare vb varchar(30);
 
538
  declare last_row int;
 
539
        
 
540
  while(num>=1) do
 
541
    set num=num-1;
 
542
    begin
 
543
      declare cur1 cursor for select `a` from t_33618;
 
544
      declare continue handler for not found set last_row = 1;
 
545
      set last_row:=0;
 
546
      open cur1;
 
547
      rep1:
 
548
      repeat
 
549
        begin
 
550
          declare exit handler for 1062 begin end;
 
551
          fetch cur1 into vb;
 
552
          if (last_row = 1) then
 
553
            ## should generate a hpop instruction here
 
554
            leave rep1;
 
555
          end if;
 
556
        end;
 
557
        until last_row=1
 
558
      end repeat;
 
559
      close cur1;
 
560
    end;
 
561
  end while;
 
562
end//
 
563
 
 
564
create procedure proc_33618_c(num int)
 
565
begin
 
566
  declare count1 int default '0';
 
567
  declare vb varchar(30);
 
568
  declare last_row int;
 
569
        
 
570
  while(num>=1) do
 
571
    set num=num-1;
 
572
    begin
 
573
      declare cur1 cursor for select `a` from t_33618;
 
574
      declare continue handler for not found set last_row = 1;
 
575
      set last_row:=0;
 
576
      open cur1;
 
577
      rep1:
 
578
      repeat
 
579
        begin
 
580
          declare cur2 cursor for select `b` from t_33618;
 
581
          fetch cur1 into vb;
 
582
          if (last_row = 1) then
 
583
            ## should generate a cpop instruction here
 
584
            leave rep1;
 
585
          end if;
 
586
        end;
 
587
        until last_row=1
 
588
      end repeat;
 
589
      close cur1;
 
590
    end;
 
591
  end while;
 
592
end//
 
593
delimiter ;//
 
594
 
 
595
show procedure code proc_33618_h;
 
596
show procedure code proc_33618_c;
 
597
 
 
598
drop procedure proc_33618_h;
 
599
drop procedure proc_33618_c;
 
600
 
 
601
#
 
602
# Bug#20906 (Multiple assignments in SET in stored routine produce incorrect
 
603
# instructions)
 
604
#
 
605
 
 
606
--disable_warnings
 
607
drop procedure if exists p_20906_a;
 
608
drop procedure if exists p_20906_b;
 
609
--enable_warnings
 
610
 
 
611
create procedure p_20906_a() SET @a=@a+1, @b=@b+1;
 
612
show procedure code p_20906_a;
 
613
 
 
614
set @a=1;
 
615
set @b=1;
 
616
 
 
617
call p_20906_a();
 
618
select @a, @b;
 
619
 
 
620
create procedure p_20906_b() SET @a=@a+1, @b=@b+1, @c=@c+1;
 
621
show procedure code p_20906_b;
 
622
 
 
623
set @a=1;
 
624
set @b=1;
 
625
set @c=1;
 
626
 
 
627
call p_20906_b();
 
628
select @a, @b, @c;
 
629
 
 
630
drop procedure p_20906_a;
 
631
drop procedure p_20906_b;
 
632
 
 
633
--echo End of 5.0 tests.
 
634
 
 
635
#
 
636
# Bug #26303: reserve() not called before qs_append() may lead to buffer
 
637
# overflow
 
638
#
 
639
DELIMITER //;
 
640
CREATE PROCEDURE p1() 
 
641
BEGIN 
 
642
  DECLARE dummy int default 0;
 
643
 
 
644
  CASE 12 
 
645
    WHEN 12 
 
646
    THEN SET dummy = 0;
 
647
  END CASE;
 
648
END//
 
649
DELIMITER ;//
 
650
SHOW PROCEDURE CODE p1;
 
651
DROP PROCEDURE p1;