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

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
#
# Test the debugging feature "show procedure/function code <name>" 
#

-- source include/is_debug_build.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#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;


--echo End of 5.0 tests.