~vlad-lesin/percona-server/mysql-5.0.33-original

« back to all changes in this revision

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

  • Committer: Vlad Lesin
  • Date: 2012-07-31 09:21:34 UTC
  • Revision ID: vladislav.lesin@percona.com-20120731092134-zfodx022b7992wsi
VirginĀ 5.0.33

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/is_debug_build.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
#
 
196
# Bug#19207: Final parenthesis omitted for CREATE INDEX in Stored
 
197
# Procedure
 
198
#
 
199
# Wrong criteria was used to distinguish the case when there was no
 
200
# lookahead performed in the parser.  Bug affected only statements
 
201
# ending in one-character token without any optional tail, like CREATE
 
202
# INDEX and CALL.
 
203
#
 
204
--disable_warnings
 
205
DROP PROCEDURE IF EXISTS p1;
 
206
--enable_warnings
 
207
 
 
208
CREATE PROCEDURE p1() CREATE INDEX idx ON t1 (c1);
 
209
SHOW PROCEDURE CODE p1;
 
210
 
 
211
DROP PROCEDURE p1;
 
212
 
 
213
 
 
214
--echo End of 5.0 tests.