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

« back to all changes in this revision

Viewing changes to mysql-test/t/sp_trans.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
# tests that require InnoDB...
 
3
#
 
4
 
 
5
-- source include/have_log_bin.inc
 
6
-- source include/have_innodb.inc
 
7
 
 
8
--disable_warnings
 
9
drop table if exists t1, t2, t3;
 
10
--enable_warnings
 
11
 
 
12
delimiter |;
 
13
 
 
14
#
 
15
# BUG#8850: Truncate table in a stored procedure locks the tables
 
16
#
 
17
--disable_warnings
 
18
drop procedure if exists bug8850|
 
19
--enable_warnings
 
20
create table t1 (a int) engine=innodb|
 
21
create procedure bug8850()
 
22
begin
 
23
  truncate table t1; insert t1 values (1); rollback;
 
24
end|
 
25
 
 
26
set autocommit=0|
 
27
insert t1 values (2)|
 
28
call bug8850()|
 
29
commit|
 
30
select * from t1|
 
31
 
 
32
call bug8850()|
 
33
set autocommit=1|
 
34
select * from t1|
 
35
drop table t1|
 
36
drop procedure bug8850|
 
37
 
 
38
 
 
39
#
 
40
# BUG#10015: Crash in InnoDB if stored routines are used
 
41
# (crash happens in auto-commit mode)
 
42
#
 
43
--disable_warnings
 
44
drop function if exists bug10015_1|
 
45
drop function if exists bug10015_2|
 
46
drop function if exists bug10015_3|
 
47
drop function if exists bug10015_4|
 
48
drop function if exists bug10015_5|
 
49
drop function if exists bug10015_6|
 
50
drop function if exists bug10015_7|
 
51
drop procedure if exists bug10015_8|
 
52
--enable_warnings
 
53
create table t1 (id int) engine=innodb|
 
54
create table t2 (id int primary key, j int) engine=innodb|
 
55
insert into t1 values (1),(2),(3)|
 
56
create function bug10015_1() returns int return (select count(*) from t1)|
 
57
select *, bug10015_1() from t1|
 
58
drop function bug10015_1|
 
59
# Test couple of a bit more complex cases
 
60
create function bug10015_2() returns int 
 
61
  begin
 
62
    declare i, s int;
 
63
    set i:= (select min(id) from t1);
 
64
    set s:= (select max(id) from t1);
 
65
    return (s - i);
 
66
  end|
 
67
select *, bug10015_2() from t1|
 
68
drop function bug10015_2|
 
69
create function bug10015_3() returns int 
 
70
  return (select max(a.id - b.id) from t1 as a, t1 as b where a.id >= b.id)|
 
71
select *, bug10015_3() from t1|
 
72
drop function bug10015_3|
 
73
create function bug10015_4(i int) returns int 
 
74
  begin
 
75
    declare m int;
 
76
    set m:= (select max(id) from t2);
 
77
    insert into t2 values (i, m);
 
78
    return m;
 
79
  end|
 
80
select *, bug10015_4(id) from t1|
 
81
select * from t2|
 
82
drop function bug10015_4|
 
83
# Now let us test how statement rollback works
 
84
# This function will cause the whole stmt to be rolled back,
 
85
# there should not be any traces left.
 
86
create function bug10015_5(i int) returns int
 
87
  begin
 
88
    if (i = 5) then
 
89
      insert into t2 values (1, 0);
 
90
    end if;
 
91
    return i;
 
92
  end|
 
93
--error ER_DUP_ENTRY
 
94
insert into t1 values (bug10015_5(4)), (bug10015_5(5))|
 
95
select * from t1|
 
96
drop function bug10015_5|
 
97
# Thanks to error-handler this function should not cause rollback
 
98
# of statement calling it. But insert statement in it should be 
 
99
# rolled back completely and don't leave any traces in t2.
 
100
# Unfortunately we can't implement such behavior in 5.0, so it
 
101
# is something to be fixed in later 5.* releases (TODO).
 
102
create function bug10015_6(i int) returns int
 
103
  begin
 
104
    declare continue handler for sqlexception set @error_in_func:= 1;
 
105
    if (i = 5) then
 
106
      insert into t2 values (4, 0), (1, 0);
 
107
    end if;
 
108
    return i;
 
109
  end|
 
110
set @error_in_func:= 0|
 
111
insert into t1 values (bug10015_6(5)), (bug10015_6(6))|
 
112
select @error_in_func|
 
113
select * from t1|
 
114
select * from t2|
 
115
drop function bug10015_6|
 
116
# Let us test that we don't allow any statements causing transaction
 
117
# commit in stored functions (we test only most interesting cases here).
 
118
# Cases which can be caught at creation time:
 
119
--error 1422
 
120
create function bug10015_7() returns int
 
121
  begin
 
122
    alter table t1 add k int;
 
123
    return 1;
 
124
  end|
 
125
--error 1422
 
126
create function bug10015_7() returns int
 
127
  begin
 
128
    start transaction;
 
129
    return 1;
 
130
  end|
 
131
--error 1422
 
132
create function bug10015_7() returns int
 
133
  begin
 
134
    drop table t1;
 
135
    return 1;
 
136
  end|
 
137
# It should be OK to drop temporary table.
 
138
create function bug10015_7() returns int
 
139
  begin
 
140
    drop temporary table t1;
 
141
    return 1;
 
142
  end|
 
143
drop function bug10015_7|
 
144
--error 1422
 
145
create function bug10015_7() returns int
 
146
  begin
 
147
    commit;
 
148
    return 1;
 
149
  end|
 
150
# Now let us test cases which we can catch only at run-time:
 
151
create function bug10015_7() returns int
 
152
  begin
 
153
    call bug10015_8();
 
154
    return 1;
 
155
  end|
 
156
create procedure bug10015_8() alter table t1 add k int|
 
157
--error 1422
 
158
select *, bug10015_7() from t1|
 
159
drop procedure bug10015_8|
 
160
create procedure bug10015_8() start transaction|
 
161
--error 1422
 
162
select *, bug10015_7() from t1|
 
163
drop procedure bug10015_8|
 
164
# Again it is OK to drop temporary table
 
165
# We are surpressing warnings since they are not essential
 
166
create procedure bug10015_8() drop temporary table if exists t1_temp|
 
167
--disable_warnings
 
168
select *, bug10015_7() from t1|
 
169
--enable_warnings
 
170
drop procedure bug10015_8|
 
171
create procedure bug10015_8() commit|
 
172
--error 1422
 
173
select *, bug10015_7() from t1|
 
174
drop procedure bug10015_8|
 
175
drop function bug10015_7|
 
176
drop table t1, t2|
 
177
 
 
178
 
 
179
#
 
180
# BUG#13825 "Triggers: crash if release savepoint".
 
181
# Also general test for handling of savepoints in stored routines.
 
182
#
 
183
# According to SQL standard we should establish new savepoint
 
184
# level before executing stored function/trigger and destroy 
 
185
# this savepoint level after execution. Stored procedures by
 
186
# default should be executed using the same savepoint level
 
187
# as their caller (to execute stored procedure using new 
 
188
# savepoint level one should explicitly specify NEW SAVEPOINT
 
189
# LEVEL clause in procedure creation statement which MySQL
 
190
# does not support yet).
 
191
--disable_warnings
 
192
drop function if exists bug13825_0|
 
193
drop function if exists bug13825_1|
 
194
drop function if exists bug13825_2|
 
195
drop function if exists bug13825_3|
 
196
drop function if exists bug13825_4|
 
197
drop function if exists bug13825_5|
 
198
drop procedure if exists bug13825_0|
 
199
drop procedure if exists bug13825_1|
 
200
drop procedure if exists bug13825_2|
 
201
drop table if exists t1|
 
202
--enable_warnings
 
203
create table t1 (i int) engine=innodb|
 
204
create table t2 (i int) engine=innodb|
 
205
create function bug13825_0() returns int
 
206
begin
 
207
  rollback to savepoint x;
 
208
  return 1;
 
209
end|
 
210
create function bug13825_1() returns int
 
211
begin
 
212
  release savepoint x;
 
213
  return 1;
 
214
end|
 
215
create function bug13825_2() returns int
 
216
begin
 
217
  insert into t1 values (2);
 
218
  savepoint x;
 
219
  insert into t1 values (3);
 
220
  rollback to savepoint x;
 
221
  insert into t1 values (4);
 
222
  return 1;
 
223
end|
 
224
create procedure bug13825_0()
 
225
begin
 
226
  rollback to savepoint x;
 
227
end|
 
228
create procedure bug13825_1()
 
229
begin
 
230
  release savepoint x;
 
231
end|
 
232
create procedure bug13825_2()
 
233
begin
 
234
  savepoint x;
 
235
end|
 
236
insert into t2 values (1)|
 
237
create trigger t2_bi before insert on t2 for each row
 
238
  rollback to savepoint x|
 
239
create trigger t2_bu before update on t2 for each row
 
240
  release savepoint x|
 
241
create trigger t2_bd before delete on t2 for each row
 
242
begin
 
243
  insert into t1 values (2);
 
244
  savepoint x;
 
245
  insert into t1 values (3);
 
246
  rollback to savepoint x;
 
247
  insert into t1 values (4);
 
248
end|
 
249
create function bug13825_3(rb int) returns int
 
250
begin
 
251
  insert into t1 values(1);
 
252
  savepoint x;
 
253
  insert into t1 values(2);
 
254
  if rb then
 
255
    rollback to savepoint x;
 
256
  end if;
 
257
  insert into t1 values(3);
 
258
  return rb;
 
259
end|
 
260
create function bug13825_4() returns int
 
261
begin
 
262
  savepoint x;
 
263
  insert into t1 values(2);
 
264
  rollback to savepoint x;
 
265
  return 0;
 
266
end|
 
267
create function bug13825_5(p int) returns int
 
268
begin
 
269
  savepoint x;
 
270
  insert into t2 values(p);
 
271
  rollback to savepoint x;
 
272
  insert into t2 values(p+1);
 
273
  return p;
 
274
end|
 
275
set autocommit= 0|
 
276
# Test of savepoint level handling for stored functions and triggers
 
277
begin |
 
278
insert into t1 values (1)|
 
279
savepoint x|
 
280
--error ER_SP_DOES_NOT_EXIST
 
281
set @a:= bug13825_0()|
 
282
--error ER_SP_DOES_NOT_EXIST
 
283
insert into t2 values (2)|
 
284
--error ER_SP_DOES_NOT_EXIST
 
285
set @a:= bug13825_1()|
 
286
--error ER_SP_DOES_NOT_EXIST
 
287
update t2 set i = 2|
 
288
set @a:= bug13825_2()|
 
289
select * from t1|
 
290
rollback to savepoint x|
 
291
select * from t1|
 
292
delete from t2|
 
293
select * from t1|
 
294
rollback to savepoint x|
 
295
select * from t1|
 
296
# Of course savepoints set in function should not be visible from its caller
 
297
release savepoint x|
 
298
set @a:= bug13825_2()|
 
299
select * from t1|
 
300
--error ER_SP_DOES_NOT_EXIST
 
301
rollback to savepoint x|
 
302
delete from t1|
 
303
commit|
 
304
# Test of savepoint level handling for stored procedures
 
305
begin|
 
306
insert into t1 values (5)|
 
307
savepoint x|
 
308
insert into t1 values (6)|
 
309
call bug13825_0()|
 
310
select * from t1|
 
311
call bug13825_1()|
 
312
--error ER_SP_DOES_NOT_EXIST
 
313
rollback to savepoint x|
 
314
savepoint x|
 
315
insert into t1 values (7)|
 
316
call bug13825_2()|
 
317
rollback to savepoint x|
 
318
select * from t1|
 
319
delete from t1|
 
320
commit|
 
321
set autocommit= 1|
 
322
# Let us test that savepoints work inside of functions
 
323
# even in auto-commit mode
 
324
select bug13825_3(0)|
 
325
select * from t1|
 
326
delete from t1|
 
327
select bug13825_3(1)|
 
328
select * from t1|
 
329
delete from t1|
 
330
# Curious case: rolling back to savepoint which is set by first
 
331
# statement in function should not rollback whole transaction.
 
332
set autocommit= 0|
 
333
begin|
 
334
insert into t1 values (1)|
 
335
set @a:= bug13825_4()|
 
336
select * from t1|
 
337
delete from t1|
 
338
commit|
 
339
set autocommit= 1|
 
340
# Other curious case: savepoint in the middle of statement
 
341
drop table t2|
 
342
create table t2 (i int) engine=innodb|
 
343
insert into t1 values (1), (bug13825_5(2)), (3)|
 
344
select * from t1|
 
345
select * from t2|
 
346
# Cleanup
 
347
drop function bug13825_0|
 
348
drop function bug13825_1|
 
349
drop function bug13825_2|
 
350
drop function bug13825_3|
 
351
drop function bug13825_4|
 
352
drop function bug13825_5|
 
353
drop procedure bug13825_0|
 
354
drop procedure bug13825_1|
 
355
drop procedure bug13825_2|
 
356
drop table t1, t2|
 
357
 
 
358
 
 
359
#
 
360
# BUG#14840: CONTINUE handler problem
 
361
#
 
362
--disable_warnings
 
363
drop table if exists t3|
 
364
drop procedure if exists bug14840_1|
 
365
drop procedure if exists bug14840_2|
 
366
--enable_warnings
 
367
 
 
368
create table t3
 
369
(
 
370
  x int,
 
371
  y int,
 
372
  primary key (x)
 
373
) engine=InnoDB|
 
374
 
 
375
# This used to hang the client since the insert returned with an
 
376
# error status (left over from the update) even though it succeeded,
 
377
# which caused the execution to end at that point.
 
378
create procedure bug14840_1()
 
379
begin
 
380
  declare err int default 0;
 
381
  declare continue handler for sqlexception
 
382
    set err = err + 1;
 
383
 
 
384
  start transaction;
 
385
  update t3 set x = 1, y = 42 where x = 2;
 
386
  insert into t3 values (3, 4711);
 
387
  if err > 0 then
 
388
    rollback;
 
389
  else
 
390
    commit;
 
391
  end if;
 
392
  select * from t3;
 
393
end|
 
394
 
 
395
# A simpler (non-transactional) case: insert at select should be done
 
396
create procedure bug14840_2()
 
397
begin
 
398
  declare err int default 0;
 
399
  declare continue handler for sqlexception
 
400
    begin
 
401
      set err = err + 1;
 
402
      select err as 'Ping';
 
403
    end;
 
404
 
 
405
  update t3 set x = 1, y = 42 where x = 2;
 
406
  update t3 set x = 1, y = 42 where x = 2;
 
407
  insert into t3 values (3, 4711);
 
408
  select * from t3;
 
409
end|
 
410
 
 
411
insert into t3 values (1, 3), (2, 5)|
 
412
call bug14840_1()|
 
413
 
 
414
delete from t3|
 
415
insert into t3 values (1, 3), (2, 5)|
 
416
call bug14840_2()|
 
417
 
 
418
drop procedure bug14840_1|
 
419
drop procedure bug14840_2|
 
420
drop table t3|
 
421
 
 
422
 
 
423
#
 
424
# BUG#10656: Stored Procedure - Create index and Truncate table command error
 
425
#
 
426
--disable_warnings
 
427
drop procedure if exists bug10656_create_index|
 
428
drop procedure if exists bug10656_myjoin|
 
429
drop procedure if exists bug10656_truncate_table|
 
430
--enable_warnings
 
431
 
 
432
CREATE TABLE t3 (
 
433
  `ID` int(11) default NULL,
 
434
  `txt` char(5) default NULL
 
435
) ENGINE=InnoDB DEFAULT CHARSET=latin1|
 
436
 
 
437
INSERT INTO t3 (`ID`,`txt`) VALUES
 
438
 (1,'a'), (2,'b'), (3,'c'), (4,'d')|
 
439
 
 
440
CREATE TABLE t4 (
 
441
  `ID` int(11) default NULL,
 
442
  `txt` char(5) default NULL
 
443
) ENGINE=InnoDB DEFAULT CHARSET=latin1|
 
444
 
 
445
INSERT INTO t4 (`ID`,`txt`) VALUES
 
446
 (1,'a'), (2,'b'), (3,'c'), (4,'d')|
 
447
 
 
448
create procedure bug10656_create_index()
 
449
begin
 
450
  create index bug10656_my_index on t3 (ID);
 
451
end|
 
452
call bug10656_create_index()|
 
453
 
 
454
create procedure bug10656_myjoin()
 
455
begin
 
456
  update t3, t4 set t3.txt = t4.txt where t3.id = t4.id;
 
457
end|
 
458
call bug10656_myjoin()|
 
459
 
 
460
create procedure bug10656_truncate_table()
 
461
begin
 
462
  truncate table t3;
 
463
end|
 
464
call bug10656_truncate_table()|
 
465
 
 
466
 
 
467
drop procedure bug10656_create_index|
 
468
drop procedure bug10656_myjoin|
 
469
drop procedure bug10656_truncate_table|
 
470
drop table t3, t4|
 
471
 
 
472
#
 
473
# BUG#3448
 
474
#
 
475
--disable_warnings
 
476
create table t3 (
 
477
  a int primary key,
 
478
  ach char(1)
 
479
) engine = innodb|
 
480
 
 
481
create table t4 (
 
482
  b int  primary key,
 
483
  bch char(1)
 
484
) engine = innodb|
 
485
--enable_warnings
 
486
 
 
487
insert into t3 values (1 , 'aCh1' ) , ('2' , 'aCh2')|
 
488
insert into t4 values (1 , 'bCh1' )|
 
489
 
 
490
--disable_warnings
 
491
drop procedure if exists bug3448|
 
492
--enable_warnings
 
493
create procedure bug3448()
 
494
  select * from t3 inner join t4 on t3.a = t4.b|
 
495
 
 
496
select * from t3 inner join t4 on t3.a = t4.b|
 
497
call bug3448()|
 
498
call bug3448()|
 
499
 
 
500
drop procedure bug3448|
 
501
drop table t3, t4|
 
502
 
 
503
#
 
504
# BUG#14210: "Simple query with > operator on large table gives server
 
505
# crash"
 
506
# Check that cursors work in case when HEAP tables are converted to
 
507
# MyISAM
 
508
#
 
509
--disable_warnings
 
510
drop procedure if exists bug14210|
 
511
--enable_warnings
 
512
set @@session.max_heap_table_size=16384|
 
513
select @@session.max_heap_table_size|
 
514
# To trigger the memory corruption the original table must be InnoDB.
 
515
# No harm if it's not, so don't warn if the suite is run with --skip-innodb
 
516
--disable_warnings
 
517
create table t3 (a char(255)) engine=InnoDB|
 
518
--enable_warnings
 
519
create procedure bug14210_fill_table()
 
520
begin
 
521
  declare table_size, max_table_size int default 0;
 
522
  select @@session.max_heap_table_size into max_table_size;
 
523
  delete from t3;
 
524
  insert into t3 (a) values (repeat('a', 255));
 
525
  repeat
 
526
    insert into t3 select a from t3;
 
527
    select count(*)*255 from t3 into table_size;
 
528
  until table_size > max_table_size*2 end repeat;
 
529
end|
 
530
call bug14210_fill_table()|
 
531
drop procedure bug14210_fill_table|
 
532
create table t4 like t3|
 
533
 
 
534
create procedure bug14210()
 
535
begin
 
536
  declare a char(255);
 
537
  declare done int default 0;
 
538
  declare c cursor for select * from t3;
 
539
  declare continue handler for sqlstate '02000' set done = 1;
 
540
  open c;
 
541
  repeat
 
542
    fetch c into a;
 
543
    if not done then
 
544
       insert into t4 values (upper(a));
 
545
    end if;
 
546
  until done end repeat;
 
547
  close c;
 
548
end|
 
549
call bug14210()|
 
550
select count(*) from t4|
 
551
 
 
552
drop table t3, t4|
 
553
drop procedure bug14210|
 
554
set @@session.max_heap_table_size=default|
 
555
 
 
556
#
 
557
# BUG#7787: Stored procedures: improper warning for "grant execute" statement
 
558
#
 
559
 
 
560
# Prepare.
 
561
 
 
562
CREATE DATABASE db_bug7787|
 
563
use db_bug7787|
 
564
 
 
565
# Test.
 
566
 
 
567
CREATE PROCEDURE p1()
 
568
  SHOW INNODB STATUS; |
 
569
 
 
570
GRANT EXECUTE ON PROCEDURE p1 TO user_bug7787@localhost|
 
571
 
 
572
# Cleanup.
 
573
 
 
574
DROP DATABASE db_bug7787|
 
575
drop user user_bug7787@localhost|
 
576
use test|
 
577
 
 
578
#
 
579
# Bug#13575 SP funcs in select with distinct/group and order by can
 
580
#           produce bad data
 
581
#
 
582
create table t3 (f1 int, f2 varchar(3), primary key(f1)) engine=innodb|
 
583
insert into t3 values (1,'aaa'),(2,'bbb'),(3,'ccc')|
 
584
CREATE FUNCTION bug13575 ( p1 integer ) 
 
585
returns varchar(3) 
 
586
BEGIN 
 
587
DECLARE v1 VARCHAR(10) DEFAULT null;
 
588
SELECT f2 INTO v1 FROM t3 WHERE f1 = p1; 
 
589
RETURN v1;
 
590
END|
 
591
select distinct f1, bug13575(f1) from t3 order by f1|
 
592
drop function bug13575|
 
593
drop table t3|
 
594
 
 
595
 
 
596
#
 
597
# BUG#NNNN: New bug synopsis
 
598
#
 
599
#--disable_warnings
 
600
#drop procedure if exists bugNNNN|
 
601
#--enable_warnings
 
602
#create procedure bugNNNN...
 
603
 
 
604
delimiter ;|