1--source include/have_partition.inc
2--source include/have_sequence.inc
3
4#
5# Basic stored PROCEDURE tests
6#
7# Test cases for bugs are added at the end. See template there.
8#
9# Some tests that require --error go into sp-error.test
10# Tests that require innodb go into sp_trans.test
11# Tests that check privilege and security issues go to sp-security.test.
12# Tests that require multiple connections, except security/privilege tests,
13#   go to sp-thread.
14# Tests that uses 'goto' to into sp-goto.test (currently disabled)
15# Tests that destroys system tables (e.g. mysql.proc) for error testing
16#   go to sp-destruct.
17# Tests that require --with-geometry go into sp_gis.test
18# Tests that require multibyte character sets, which are not always available,
19#   go into separate files (e.g. sp-ucs2.test)
20
21--source include/default_charset.inc
22set @save_character_set_client=@@character_set_client;
23set @save_userstat=@@global.userstat, @@global.userstat= 0;
24
25use test;
26
27# Test tables
28#
29# t1 and t2 are reused throughout the file, and dropped at the end.
30# t3 and up are created and dropped when needed.
31#
32--disable_warnings
33drop table if exists t1,t2,t3,t4;
34drop view if exists v1;
35drop procedure if exists p1;
36drop procedure if exists p2;
37drop function if exists f1;
38drop function if exists f2;
39--enable_warnings
40create table t1 (
41	id   char(16) not null default '',
42        data int not null
43);
44create table t2 (
45	s   char(16),
46        i   int,
47	d   double
48);
49
50
51# Single statement, no params.
52--disable_warnings
53drop procedure if exists foo42;
54--enable_warnings
55create procedure foo42()
56  insert into test.t1 values ("foo", 42);
57
58call foo42();
59select * from t1;
60delete from t1;
61drop procedure foo42;
62
63
64# Single statement, two IN params.
65--disable_warnings
66drop procedure if exists bar;
67--enable_warnings
68create procedure bar(x char(16), y int)
69  insert into test.t1 values (x, y);
70
71call bar("bar", 666);
72select * from t1;
73delete from t1;
74# Don't drop procedure yet...
75
76
77# Now for multiple statements...
78delimiter |;
79
80# Empty statement
81--disable_warnings
82drop procedure if exists empty|
83--enable_warnings
84create procedure empty()
85begin
86end|
87
88call empty()|
89drop procedure empty|
90
91# Scope test. This is legal (warnings might be possible in the future,
92# but for the time being, we just accept it).
93--disable_warnings
94drop procedure if exists scope|
95--enable_warnings
96create procedure scope(a int, b float)
97begin
98  declare b int;
99  declare c float;
100
101  begin
102    declare c int;
103  end;
104end|
105
106drop procedure scope|
107
108# Two statements.
109--disable_warnings
110drop procedure if exists two|
111--enable_warnings
112create procedure two(x1 char(16), x2 char(16), y int)
113begin
114  insert into test.t1 values (x1, y);
115  insert into test.t1 values (x2, y);
116end|
117
118call two("one", "two", 3)|
119select * from t1|
120delete from t1|
121drop procedure two|
122
123
124# Simple test of local variables and SET.
125--disable_warnings
126drop procedure if exists locset|
127--enable_warnings
128create procedure locset(x char(16), y int)
129begin
130  declare z1, z2 int;
131  set z1 = y;
132  set z2 = z1+2;
133  insert into test.t1 values (x, z2);
134end|
135
136call locset("locset", 19)|
137select * from t1|
138delete from t1|
139drop procedure locset|
140
141
142# In some contexts local variables are not recognized
143# (and in some, you have to qualify the identifier).
144--disable_warnings
145drop procedure if exists setcontext|
146--enable_warnings
147create procedure setcontext()
148begin
149  declare data int default 2;
150
151  insert into t1 (id, data) values ("foo", 1);
152  replace t1 set data = data, id = "bar";
153  update t1 set id = "kaka", data = 3 where t1.data = data;
154end|
155
156call setcontext()|
157select * from t1 order by data|
158delete from t1|
159drop procedure setcontext|
160
161
162# Set things to null
163create table t3 ( d date, i int, f double, s varchar(32) )|
164
165--disable_warnings
166drop procedure if exists nullset|
167--enable_warnings
168create procedure nullset()
169begin
170  declare ld date;
171  declare li int;
172  declare lf double;
173  declare ls varchar(32);
174
175  set ld = null, li = null, lf = null, ls = null;
176  insert into t3 values (ld, li, lf, ls);
177
178  insert into t3 (i, f, s) values ((ld is null), 1,    "ld is null"),
179                                  ((li is null), 1,    "li is null"),
180				  ((li = 0),     null, "li = 0"),
181				  ((lf is null), 1,    "lf is null"),
182				  ((lf = 0),     null, "lf = 0"),
183				  ((ls is null), 1,    "ls is null");
184end|
185
186call nullset()|
187select * from t3|
188drop table t3|
189drop procedure nullset|
190
191
192# The peculiar (non-standard) mixture of variables types in SET.
193--disable_warnings
194drop procedure if exists mixset|
195--enable_warnings
196create procedure mixset(x char(16), y int)
197begin
198  declare z int;
199
200  set @z = y, z = 666, max_join_size = 100;
201  insert into test.t1 values (x, z);
202end|
203
204let $start_value= `SELECT @@max_join_size`|
205call mixset("mixset", 19)|
206show variables like 'max_join_size'|
207select id,data,@z from t1|
208delete from t1|
209drop procedure mixset|
210--disable_query_log
211eval SET @@max_join_size= $start_value|
212--enable_query_log
213
214# Multiple CALL statements, one with OUT parameter.
215--disable_warnings
216drop procedure if exists zip|
217--enable_warnings
218create procedure zip(x char(16), y int)
219begin
220  declare z int;
221  call zap(y, z);
222  call bar(x, z);
223end|
224
225# SET local variables and OUT parameter.
226--disable_warnings
227drop procedure if exists zap|
228--enable_warnings
229create procedure zap(x int, out y int)
230begin
231  declare z int;
232  set z = x+1, y = z;
233end|
234
235call zip("zip", 99)|
236select * from t1|
237delete from t1|
238drop procedure zip|
239drop procedure bar|
240
241# Top-level OUT parameter
242call zap(7, @zap)|
243select @zap|
244
245drop procedure zap|
246
247
248# "Deep" calls...
249--disable_warnings
250drop procedure if exists c1|
251--enable_warnings
252create procedure c1(x int)
253  call c2("c", x)|
254--disable_warnings
255drop procedure if exists c2|
256--enable_warnings
257create procedure c2(s char(16), x int)
258  call c3(x, s)|
259--disable_warnings
260drop procedure if exists c3|
261--enable_warnings
262create procedure c3(x int, s char(16))
263  call c4("level", x, s)|
264--disable_warnings
265drop procedure if exists c4|
266--enable_warnings
267create procedure c4(l char(8), x int, s char(16))
268  insert into t1 values (concat(l,s), x)|
269
270call c1(42)|
271select * from t1|
272delete from t1|
273drop procedure c1|
274drop procedure c2|
275drop procedure c3|
276drop procedure c4|
277
278# INOUT test
279--disable_warnings
280drop procedure if exists iotest|
281--enable_warnings
282create procedure iotest(x1 char(16), x2 char(16), y int)
283begin
284  call inc2(x2, y);
285  insert into test.t1 values (x1, y);
286end|
287
288--disable_warnings
289drop procedure if exists inc2|
290--enable_warnings
291create procedure inc2(x char(16), y int)
292begin
293  call inc(y);
294  insert into test.t1 values (x, y);
295end|
296
297--disable_warnings
298drop procedure if exists inc|
299--enable_warnings
300create procedure inc(inout io int)
301  set io = io + 1|
302
303call iotest("io1", "io2", 1)|
304select * from t1 order by data desc|
305delete from t1|
306drop procedure iotest|
307drop procedure inc2|
308
309# Propagating top-level @-vars
310--disable_warnings
311drop procedure if exists incr|
312--enable_warnings
313create procedure incr(inout x int)
314  call inc(x)|
315
316# Before
317select @zap|
318call incr(@zap)|
319# After
320select @zap|
321
322drop procedure inc|
323drop procedure incr|
324
325# Call-by-value test
326#  The expected result is:
327#    ("cbv2", 4)
328#    ("cbv1", 4711)
329--disable_warnings
330drop procedure if exists cbv1|
331--enable_warnings
332create procedure cbv1()
333begin
334  declare y int default 3;
335
336  call cbv2(y+1, y);
337  insert into test.t1 values ("cbv1", y);
338end|
339
340--disable_warnings
341drop procedure if exists cbv2|
342--enable_warnings
343create procedure cbv2(y1 int, inout y2 int)
344begin
345  set y2 = 4711;
346  insert into test.t1 values ("cbv2", y1);
347end|
348
349call cbv1()|
350select * from t1 order by data|
351delete from t1|
352drop procedure cbv1|
353drop procedure cbv2|
354
355
356# Subselect arguments
357
358insert into t2 values ("a", 1, 1.1), ("b", 2, 1.2), ("c", 3, 1.3)|
359
360--disable_warnings
361drop procedure if exists sub1|
362--enable_warnings
363create procedure sub1(id char(16), x int)
364  insert into test.t1 values (id, x)|
365
366--disable_warnings
367drop procedure if exists sub2|
368--enable_warnings
369create procedure sub2(id char(16))
370begin
371  declare x int;
372  set x = (select sum(t.i) from test.t2 t);
373  insert into test.t1 values (id, x);
374end|
375
376--disable_warnings
377drop procedure if exists sub3|
378--enable_warnings
379create function sub3(i int) returns int deterministic
380  return i+1|
381
382call sub1("sub1a", (select 7))|
383call sub1("sub1b", (select max(i) from t2))|
384--error ER_OPERAND_COLUMNS
385call sub1("sub1c", (select i,d from t2 limit 1))|
386call sub1("sub1d", (select 1 from (select 1) a))|
387call sub2("sub2")|
388select * from t1 order by id|
389select sub3((select max(i) from t2))|
390drop procedure sub1|
391drop procedure sub2|
392drop function sub3|
393delete from t1|
394delete from t2|
395
396# Basic tests of the flow control constructs
397
398# Just test on 'x'...
399--disable_warnings
400drop procedure if exists a0|
401--enable_warnings
402create procedure a0(x int)
403while x do
404  set x = x-1;
405  insert into test.t1 values ("a0", x);
406end while|
407
408call a0(3)|
409select * from t1 order by data desc|
410delete from t1|
411drop procedure a0|
412
413
414# The same, but with a more traditional test.
415--disable_warnings
416drop procedure if exists a|
417--enable_warnings
418create procedure a(x int)
419while x > 0 do
420  set x = x-1;
421  insert into test.t1 values ("a", x);
422end while|
423
424call a(3)|
425select * from t1 order by data desc|
426delete from t1|
427drop procedure a|
428
429
430# REPEAT
431--disable_warnings
432drop procedure if exists b|
433--enable_warnings
434create procedure b(x int)
435repeat
436  insert into test.t1 values (repeat("b",3), x);
437  set x = x-1;
438until x = 0 end repeat|
439
440call b(3)|
441select * from t1 order by data desc|
442delete from t1|
443drop procedure b|
444
445
446# Check that repeat isn't parsed the wrong way
447--disable_warnings
448drop procedure if exists b2|
449--enable_warnings
450create procedure b2(x int)
451repeat(select 1) into outfile 'b2';
452  insert into test.t1 values (repeat("b2",3), x);
453  set x = x-1;
454until x = 0 end repeat|
455
456# We don't actually want to call it.
457drop procedure b2|
458
459
460# Labelled WHILE with ITERATE (pointless really)
461--disable_warnings
462drop procedure if exists c|
463--enable_warnings
464create procedure c(x int)
465hmm: while x > 0 do
466  insert into test.t1 values ("c", x);
467  set x = x-1;
468  iterate hmm;
469  insert into test.t1 values ("x", x);
470end while hmm|
471
472call c(3)|
473select * from t1 order by data desc|
474delete from t1|
475drop procedure c|
476
477
478# Labelled WHILE with LEAVE
479--disable_warnings
480drop procedure if exists d|
481--enable_warnings
482create procedure d(x int)
483hmm: while x > 0 do
484  insert into test.t1 values ("d", x);
485  set x = x-1;
486  leave hmm;
487  insert into test.t1 values ("x", x);
488end while|
489
490call d(3)|
491select * from t1|
492delete from t1|
493drop procedure d|
494
495
496# LOOP, with simple IF statement
497--disable_warnings
498drop procedure if exists e|
499--enable_warnings
500create procedure e(x int)
501foo: loop
502  if x = 0 then
503    leave foo;
504  end if;
505  insert into test.t1 values ("e", x);
506  set x = x-1;
507end loop foo|
508
509call e(3)|
510select * from t1 order by data desc|
511delete from t1|
512drop procedure e|
513
514
515# A full IF statement
516--disable_warnings
517drop procedure if exists f|
518--enable_warnings
519create procedure f(x int)
520if x < 0 then
521  insert into test.t1 values ("f", 0);
522elseif x = 0 then
523  insert into test.t1 values ("f", 1);
524else
525  insert into test.t1 values ("f", 2);
526end if|
527
528call f(-2)|
529call f(0)|
530call f(4)|
531select * from t1 order by data|
532delete from t1|
533drop procedure f|
534
535
536# This form of CASE is really just syntactic sugar for IF-ELSEIF-...
537--disable_warnings
538drop procedure if exists g|
539--enable_warnings
540create procedure g(x int)
541case
542when x < 0 then
543  insert into test.t1 values ("g", 0);
544when x = 0 then
545  insert into test.t1 values ("g", 1);
546else
547  insert into test.t1 values ("g", 2);
548end case|
549
550call g(-42)|
551call g(0)|
552call g(1)|
553select * from t1 order by data|
554delete from t1|
555drop procedure g|
556
557
558# The "simple CASE"
559--disable_warnings
560drop procedure if exists h|
561--enable_warnings
562create procedure h(x int)
563case x
564when 0 then
565  insert into test.t1 values ("h0", x);
566when 1 then
567  insert into test.t1 values ("h1", x);
568else
569  insert into test.t1 values ("h?", x);
570end case|
571
572call h(0)|
573call h(1)|
574call h(17)|
575select * from t1 order by data|
576delete from t1|
577drop procedure h|
578
579
580# It's actually possible to LEAVE a BEGIN-END block
581--disable_warnings
582drop procedure if exists i|
583--enable_warnings
584create procedure i(x int)
585foo:
586begin
587  if x = 0 then
588    leave foo;
589  end if;
590  insert into test.t1 values ("i", x);
591end foo|
592
593call i(0)|
594call i(3)|
595select * from t1|
596delete from t1|
597drop procedure i|
598
599
600# SELECT with one of more result set sent back to the clinet
601insert into t1 values ("foo", 3), ("bar", 19)|
602insert into t2 values ("x", 9, 4.1), ("y", -1, 19.2), ("z", 3, 2.2)|
603
604--disable_warnings
605drop procedure if exists sel1|
606--enable_warnings
607create procedure sel1()
608begin
609  select * from t1 order by data;
610end|
611
612call sel1()|
613drop procedure sel1|
614
615--disable_warnings
616drop procedure if exists sel2|
617--enable_warnings
618create procedure sel2()
619begin
620  select * from t1 order by data;
621  select * from t2 order by s;
622end|
623
624call sel2()|
625drop procedure sel2|
626delete from t1|
627delete from t2|
628
629# SELECT INTO local variables
630--disable_warnings
631drop procedure if exists into_test|
632--enable_warnings
633create procedure into_test(x char(16), y int)
634begin
635  insert into test.t1 values (x, y);
636  select id,data into x,y from test.t1 limit 1;
637  insert into test.t1 values (concat(x, "2"), y+2);
638end|
639
640call into_test("into", 100)|
641select * from t1 order by data|
642delete from t1|
643drop procedure into_test|
644
645
646# SELECT INTO with a mix of local and global variables
647--disable_warnings
648drop procedure if exists into_tes2|
649--enable_warnings
650create procedure into_test2(x char(16), y int)
651begin
652  insert into test.t1 values (x, y);
653  select id,data into x,@z from test.t1 limit 1;
654  insert into test.t1 values (concat(x, "2"), y+2);
655end|
656
657call into_test2("into", 100)|
658select id,data,@z from t1 order by data|
659delete from t1|
660drop procedure into_test2|
661
662
663# SELECT * INTO ... (bug test)
664--disable_warnings
665drop procedure if exists into_test3|
666--enable_warnings
667create procedure into_test3()
668begin
669  declare x char(16);
670  declare y int;
671
672  select * into x,y from test.t1 limit 1;
673  insert into test.t2 values (x, y, 0.0);
674end|
675
676insert into t1 values ("into3", 19)|
677# Two call needed for bug test
678call into_test3()|
679call into_test3()|
680select * from t2|
681delete from t1|
682delete from t2|
683drop procedure into_test3|
684
685
686# SELECT INTO with no data is a warning ("no data", which we will
687# not see normally). When not caught, execution proceeds.
688--disable_warnings
689drop procedure if exists into_test4|
690--enable_warnings
691create procedure into_test4()
692begin
693  declare x int;
694
695  select data into x from test.t1 limit 1;
696  insert into test.t3 values ("into4", x);
697end|
698
699delete from t1|
700create table t3 ( s char(16), d int)|
701call into_test4()|
702select * from t3|
703insert into t1 values ("i4", 77)|
704call into_test4()|
705select * from t3|
706delete from t1|
707drop table t3|
708drop procedure into_test4|
709
710
711# These two (and the two procedures above) caused an assert() to fail in
712# sql_base.cc:lock_tables() at some point.
713--disable_warnings
714drop procedure if exists into_outfile|
715--enable_warnings
716--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
717eval create procedure into_outfile(x char(16), y int)
718begin
719  insert into test.t1 values (x, y);
720  select * into outfile "$MYSQLTEST_VARDIR/tmp/spout" from test.t1;
721  insert into test.t1 values (concat(x, "2"), y+2);
722end|
723
724# Check that file does not exists
725--error 1
726--file_exists $MYSQLTEST_VARDIR/tmp/spout
727call into_outfile("ofile", 1)|
728--remove_file $MYSQLTEST_VARDIR/tmp/spout
729delete from t1|
730drop procedure into_outfile|
731
732--disable_warnings
733drop procedure if exists into_dumpfile|
734--enable_warnings
735--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
736eval create procedure into_dumpfile(x char(16), y int)
737begin
738  insert into test.t1 values (x, y);
739  select * into dumpfile "$MYSQLTEST_VARDIR/tmp/spdump" from test.t1 limit 1;
740  insert into test.t1 values (concat(x, "2"), y+2);
741end|
742
743# Check that file does not exists
744--error 1
745--file_exists $MYSQLTEST_VARDIR/tmp/spdump
746call into_dumpfile("dfile", 1)|
747--remove_file $MYSQLTEST_VARDIR/tmp/spdump
748delete from t1|
749drop procedure into_dumpfile|
750
751--disable_warnings
752drop procedure if exists create_select|
753--enable_warnings
754create procedure create_select(x char(16), y int)
755begin
756  insert into test.t1 values (x, y);
757  create temporary table test.t3 select * from test.t1;
758  insert into test.t3 values (concat(x, "2"), y+2);
759end|
760
761call create_select("cs", 90)|
762select * from t1, t3|
763drop table t3|
764delete from t1|
765drop procedure create_select|
766
767
768# A minimal, constant FUNCTION.
769--disable_warnings
770drop function if exists e|
771--enable_warnings
772create function e() returns double
773  return 2.7182818284590452354|
774
775set @e = e()|
776select e(), @e|
777
778# A minimal function with one argument
779--disable_warnings
780drop function if exists inc|
781--enable_warnings
782create function inc(i int) returns int
783  return i+1|
784
785select inc(1), inc(99), inc(-71)|
786
787# A minimal function with two arguments
788--disable_warnings
789drop function if exists mul|
790--enable_warnings
791create function mul(x int, y int) returns int
792  return x*y|
793
794select mul(1,1), mul(3,5), mul(4711, 666)|
795
796# A minimal string function
797--disable_warnings
798drop function if exists append|
799--enable_warnings
800create function append(s1 char(8), s2 char(8)) returns char(16)
801  return concat(s1, s2)|
802
803select append("foo", "bar")|
804
805# A function with flow control
806--disable_warnings
807drop function if exists fac|
808--enable_warnings
809create function fac(n int unsigned) returns bigint unsigned
810begin
811  declare f bigint unsigned default 1;
812
813  while n > 1 do
814    set f = f * n;
815    set n = n - 1;
816  end while;
817  return f;
818end|
819
820select fac(1), fac(2), fac(5), fac(10)|
821
822# Nested calls
823--disable_warnings
824drop function if exists fun|
825--enable_warnings
826create function fun(d double, i int, u int unsigned) returns double
827  return mul(inc(i), fac(u)) / e()|
828
829select fun(2.3, 3, 5)|
830
831
832# Various function calls in differen statements
833
834insert into t2 values (append("xxx", "yyy"), mul(4,3), e())|
835insert into t2 values (append("a", "b"), mul(2,mul(3,4)), fun(1.7, 4, 6))|
836
837# Disable PS because double's give a bit different values
838--disable_ps_protocol
839select * from t2 where s = append("a", "b")|
840select * from t2 where i = mul(4,3) or i = mul(mul(3,4),2) order by i|
841select * from t2 where d = e()|
842select * from t2 order by i|
843--enable_ps_protocol
844delete from t2|
845
846drop function e|
847drop function inc|
848drop function mul|
849drop function append|
850drop function fun|
851
852
853#
854# CONDITIONs and HANDLERs
855#
856
857--disable_warnings
858drop procedure if exists hndlr1|
859--enable_warnings
860create procedure hndlr1(val int)
861begin
862  declare x int default 0;
863  declare foo condition for 1136;
864  declare bar condition for sqlstate '42S98';        # Just for testing syntax
865  declare zip condition for sqlstate value '42S99';  # Just for testing syntax
866  declare continue handler for foo set x = 1;
867
868  insert into test.t1 values ("hndlr1", val, 2);  # Too many values
869  if (x) then
870    insert into test.t1 values ("hndlr1", val);   # This instead then
871  end if;
872end|
873
874call hndlr1(42)|
875select * from t1|
876delete from t1|
877drop procedure hndlr1|
878
879--disable_warnings
880drop procedure if exists hndlr2|
881--enable_warnings
882create procedure hndlr2(val int)
883begin
884  declare x int default 0;
885
886  begin
887    declare exit handler for sqlstate '21S01' set x = 1;
888
889    insert into test.t1 values ("hndlr2", val, 2); # Too many values
890  end;
891
892  insert into test.t1 values ("hndlr2", x);
893end|
894
895call hndlr2(42)|
896select * from t1|
897delete from t1|
898drop procedure hndlr2|
899
900
901--disable_warnings
902drop procedure if exists hndlr3|
903--enable_warnings
904create procedure hndlr3(val int)
905begin
906  declare x int default 0;
907  declare continue handler for sqlexception        # Any error
908  begin
909    declare z int;
910
911    set z = 2 * val;
912    set x = 1;
913  end;
914
915  if val < 10 then
916    begin
917      declare y int;
918
919      set y = val + 10;
920      insert into test.t1 values ("hndlr3", y, 2);  # Too many values
921      if x then
922        insert into test.t1 values ("hndlr3", y);
923      end if;
924    end;
925  end if;
926end|
927
928call hndlr3(3)|
929select * from t1|
930delete from t1|
931drop procedure hndlr3|
932
933
934# Variables might be uninitialized when using handlers
935# (Otherwise the compiler can detect if a variable is not set, but
936#  not in this case.)
937create table t3 ( id   char(16), data int )|
938
939--disable_warnings
940drop procedure if exists hndlr4|
941--enable_warnings
942create procedure hndlr4()
943begin
944  declare x int default 0;
945  declare val int;	                           # No default
946  declare continue handler for sqlstate '02000' set x=1;
947
948  select data into val from test.t3 where id='z' limit 1;  # No hits
949
950  insert into test.t3 values ('z', val);
951end|
952
953call hndlr4()|
954select * from t3|
955drop table t3|
956drop procedure hndlr4|
957
958
959#
960# Cursors
961#
962--disable_warnings
963drop procedure if exists cur1|
964--enable_warnings
965create procedure cur1()
966begin
967  declare a char(16);
968  declare b int;
969  declare c double;
970  declare done int default 0;
971  declare c cursor for select * from test.t2;
972  declare continue handler for sqlstate '02000' set done = 1;
973
974  open c;
975  repeat
976    fetch c into a, b, c;
977    if not done then
978       insert into test.t1 values (a, b+c);
979    end if;
980  until done end repeat;
981  close c;
982end|
983
984insert into t2 values ("foo", 42, -1.9), ("bar", 3, 12.1), ("zap", 666, -3.14)|
985call cur1()|
986select * from t1|
987drop procedure cur1|
988
989create table t3 ( s char(16), i int )|
990
991--disable_warnings
992drop procedure if exists cur2|
993--enable_warnings
994create procedure cur2()
995begin
996  declare done int default 0;
997  declare c1 cursor for select id,data from test.t1 order by id,data;
998  declare c2 cursor for select i from test.t2 order by i;
999  declare continue handler for sqlstate '02000' set done = 1;
1000
1001  open c1;
1002  open c2;
1003  repeat
1004  begin
1005    declare a char(16);
1006    declare b,c int;
1007
1008    fetch from c1 into a, b;
1009    fetch next from c2 into c;
1010    if not done then
1011      if b < c then
1012        insert into test.t3 values (a, b);
1013      else
1014        insert into test.t3 values (a, c);
1015      end if;
1016    end if;
1017  end;
1018  until done end repeat;
1019  close c1;
1020  close c2;
1021end|
1022
1023call cur2()|
1024select * from t3 order by i,s|
1025delete from t1|
1026delete from t2|
1027drop table t3|
1028drop procedure cur2|
1029
1030
1031# The few characteristics we parse
1032--disable_warnings
1033drop procedure if exists chistics|
1034--enable_warnings
1035create procedure chistics()
1036    language sql
1037    modifies sql data
1038    not deterministic
1039    sql security definer
1040    comment 'Characteristics procedure test'
1041  insert into t1 values ("chistics", 1)|
1042
1043show create procedure chistics|
1044# Call it, just to make sure.
1045call chistics()|
1046select * from t1|
1047delete from t1|
1048alter procedure chistics sql security invoker|
1049show create procedure chistics|
1050drop procedure chistics|
1051
1052--disable_warnings
1053drop function if exists chistics|
1054--enable_warnings
1055create function chistics() returns int
1056    language sql
1057    deterministic
1058    sql security invoker
1059    comment 'Characteristics procedure test'
1060  return 42|
1061
1062show create function chistics|
1063# Call it, just to make sure.
1064select chistics()|
1065alter function chistics
1066   no sql
1067   comment 'Characteristics function test'|
1068show create function chistics|
1069drop function chistics|
1070
1071
1072# Check mode settings
1073insert into t1 values ("foo", 1), ("bar", 2), ("zip", 3)|
1074
1075set @@sql_mode = 'ANSI'|
1076delimiter $|
1077--disable_warnings
1078drop procedure if exists modes$
1079--enable_warnings
1080create procedure modes(out c1 int, out c2 int)
1081begin
1082  declare done int default 0;
1083  declare x int;
1084  declare c cursor for select data from t1;
1085  declare continue handler for sqlstate '02000' set done = 1;
1086
1087  select 1 || 2 into c1;
1088  set c2 = 0;
1089  open c;
1090  repeat
1091    fetch c into x;
1092    if not done then
1093      set c2 = c2 + 1;
1094    end if;
1095  until done end repeat;
1096  close c;
1097end$
1098delimiter |$
1099set @@sql_mode = ''|
1100
1101set sql_select_limit = 1|
1102call modes(@c1, @c2)|
1103set sql_select_limit = default|
1104
1105select @c1, @c2|
1106delete from t1|
1107drop procedure modes|
1108
1109
1110# Check that dropping a database without routines works.
1111# (Dropping with routines is tested in sp-security.test)
1112# First an empty db.
1113create database sp_db1|
1114drop database sp_db1|
1115
1116# Again, with a table.
1117create database sp_db2|
1118use sp_db2|
1119# Just put something in here...
1120create table t3 ( s char(4), t int )|
1121insert into t3 values ("abcd", 42), ("dcba", 666)|
1122use test|
1123drop database sp_db2|
1124
1125# And yet again, with just a procedure.
1126create database sp_db3|
1127use sp_db3|
1128--disable_warnings
1129drop procedure if exists dummy|
1130--enable_warnings
1131create procedure dummy(out x int)
1132  set x = 42|
1133use test|
1134drop database sp_db3|
1135# Check that it's gone
1136select type,db,name from mysql.proc where db = 'sp_db3'|
1137
1138
1139# ROW_COUNT() function after a CALL
1140# We test the other cases here too, although it's not strictly SP specific
1141--disable_warnings
1142drop procedure if exists rc|
1143--enable_warnings
1144create procedure rc()
1145begin
1146  delete from t1;
1147  insert into t1 values ("a", 1), ("b", 2), ("c", 3);
1148end|
1149
1150call rc()|
1151select row_count()|
1152--disable_ps_protocol
1153update t1 set data=42 where id = "b";
1154select row_count()|
1155--enable_ps_protocol
1156delete from t1|
1157select row_count()|
1158delete from t1|
1159select row_count()|
1160select * from t1|
1161select row_count()|
1162drop procedure rc|
1163
1164
1165#
1166# Let us test how well new locking scheme works.
1167#
1168
1169# Let us prepare playground
1170--disable_warnings
1171drop function if exists f0|
1172drop function if exists f1|
1173drop function if exists f2|
1174drop function if exists f3|
1175drop function if exists f4|
1176drop function if exists f5|
1177drop function if exists f6|
1178drop function if exists f7|
1179drop function if exists f8|
1180drop function if exists f9|
1181drop function if exists f10|
1182drop function if exists f11|
1183drop function if exists f12_1|
1184drop function if exists f12_2|
1185drop view if exists v0|
1186drop view if exists v1|
1187drop view if exists v2|
1188--enable_warnings
1189delete from t1|
1190delete from t2|
1191insert into t1 values ("a", 1), ("b", 2) |
1192insert into t2 values ("a", 1, 1.0), ("b", 2, 2.0), ("c", 3, 3.0) |
1193
1194# Test the simplest function using tables
1195create function f1() returns int
1196  return (select sum(data) from t1)|
1197select f1()|
1198# This should work too (and give 2 rows as result)
1199select id, f1() from t1 order by id|
1200
1201# Function which uses two instances of table simultaneously
1202create function f2() returns int
1203  return (select data from t1 where data <= (select sum(data) from t1) order by data limit 1)|
1204select f2()|
1205select id, f2() from t1 order by id|
1206
1207# Function which uses the same table twice in different queries
1208create function f3() returns int
1209begin
1210  declare n int;
1211  declare m int;
1212  set n:= (select min(data) from t1);
1213  set m:= (select max(data) from t1);
1214  return n < m;
1215end|
1216select f3()|
1217select id, f3() from t1 order by id|
1218
1219# Calling two functions using same table
1220select f1(), f3()|
1221select id, f1(), f3() from t1 order by id|
1222
1223# Function which uses two different tables
1224create function f4() returns double
1225  return (select d from t1, t2 where t1.data = t2.i and t1.id= "b")|
1226select f4()|
1227select s, f4() from t2 order by s|
1228
1229# Recursive functions which due to this recursion require simultaneous
1230# access to several instance of the same table won't work
1231create function f5(i int) returns int
1232begin
1233  if i <= 0 then
1234    return 0;
1235  elseif i = 1  then
1236    return (select count(*) from t1 where data = i);
1237  else
1238    return (select count(*) + f5( i - 1) from t1 where data = i);
1239  end if;
1240end|
1241select f5(1)|
1242# Since currently recursive functions are disallowed ER_SP_NO_RECURSION
1243# error will be returned, once we will allow them error about
1244# insufficient number of locked tables will be returned instead.
1245--error ER_SP_NO_RECURSION
1246select f5(2)|
1247--error ER_SP_NO_RECURSION
1248select f5(3)|
1249
1250# OTOH this should work
1251create function f6() returns int
1252begin
1253  declare n int;
1254  set n:= f1();
1255  return (select count(*) from t1 where data <= f7() and data <= n);
1256end|
1257create function f7() returns int
1258  return (select sum(data) from t1 where data <= f1())|
1259select f6()|
1260select id, f6() from t1 order by id|
1261
1262#
1263# Let us test how new locking work with views
1264#
1265# The most trivial view
1266create view v1 (a) as select f1()|
1267select * from v1|
1268select id, a from t1, v1 order by id|
1269select * from v1, v1 as v|
1270# A bit more complex construction
1271create view v2 (a) as select a*10 from v1|
1272select * from v2|
1273select id, a from t1, v2 order by id|
1274select * from v1, v2|
1275
1276# Nice example where the same view is used on
1277# on different expression levels
1278create function f8 () returns int
1279  return (select count(*) from v2)|
1280
1281select *, f8() from v1|
1282
1283# Let us test what will happen if function is missing
1284drop function f1|
1285--error ER_VIEW_INVALID
1286select * from v1|
1287
1288# And what will happen if we have recursion which involves
1289# views and functions ?
1290create function f1() returns int
1291  return (select sum(data) from t1) + (select sum(data) from v1)|
1292--error ER_SP_NO_RECURSION
1293select f1()|
1294--error ER_SP_NO_RECURSION
1295select * from v1|
1296--error ER_SP_NO_RECURSION
1297select * from v2|
1298# Back to the normal cases
1299drop function f1|
1300create function f1() returns int
1301  return (select sum(data) from t1)|
1302
1303# Let us also test some weird cases where no real tables is used
1304create function f0() returns int
1305  return (select * from (select 100) as r)|
1306select f0()|
1307select *, f0() from (select 1) as t|
1308create view v0 as select f0()|
1309select * from v0|
1310select *, f0() from v0|
1311
1312#
1313# Let us test how well prelocking works with explicit LOCK TABLES.
1314#
1315lock tables t1 read, t1 as t11 read|
1316# These should work well
1317select f3()|
1318select id, f3() from t1 as t11 order by id|
1319# Degenerate cases work too :)
1320select f0()|
1321# But these should not (particularly views should be locked explicitly).
1322--error ER_TABLE_NOT_LOCKED
1323select * from v0|
1324--error ER_TABLE_NOT_LOCKED
1325select *, f0() from v0, (select 123) as d1|
1326--error ER_TABLE_NOT_LOCKED
1327select id, f3() from t1|
1328--error ER_TABLE_NOT_LOCKED
1329select f4()|
1330unlock tables|
1331
1332# Let us test how LOCK TABLES which implicitly depends on functions
1333# works
1334lock tables v2 read, mysql.proc read|
1335select * from v2|
1336select * from v1|
1337# These should not work as we have too little instances of tables locked
1338--error ER_TABLE_NOT_LOCKED
1339select * from v1, t1|
1340--error ER_TABLE_NOT_LOCKED
1341select f4()|
1342unlock tables|
1343
1344# Tests for handling of temporary tables in functions.
1345#
1346# Unlike for permanent tables we should be able to create, use
1347# and drop such tables in functions.
1348#
1349# Simplest function using temporary table. It is also test case for bug
1350# #12198 "Temporary table aliasing does not work inside stored functions"
1351create function f9() returns int
1352begin
1353  declare a, b int;
1354  drop temporary table if exists t3;
1355  create temporary table t3 (id int);
1356  insert into t3 values (1), (2), (3);
1357  set a:= (select count(*) from t3);
1358  set b:= (select count(*) from t3 t3_alias);
1359  return a + b;
1360end|
1361# This will emit warning as t3 was not existing before.
1362select f9()|
1363select f9() from t1 limit 1|
1364
1365# Function which uses both temporary and permanent tables.
1366create function f10() returns int
1367begin
1368  drop temporary table if exists t3;
1369  create temporary table t3 (id int);
1370  insert into t3 select id from t4;
1371  return (select count(*) from t3);
1372end|
1373# Check that we don't ignore completely tables used in function
1374--error ER_NO_SUCH_TABLE
1375select f10()|
1376create table t4 as select 1 as id|
1377select f10()|
1378
1379create function f11() returns int
1380begin
1381  drop temporary table if exists t3;
1382  create temporary table t3 (id int);
1383  insert into t3 values (1), (2), (3);
1384  return (select count(*) from t3 as a, t3 as b);
1385end|
1386select f11()|
1387select f11() from t1|
1388# Test that using a single table instance at a time works
1389create function f12_1() returns int
1390begin
1391  drop temporary table if exists t3;
1392  create temporary table t3 (id int);
1393  insert into t3 values (1), (2), (3);
1394  return f12_2();
1395end|
1396create function f12_2() returns int
1397  return (select count(*) from t3)|
1398
1399drop temporary table t3|
1400select f12_1()|
1401drop temporary table t3|
1402select f12_1() from t1 limit 1|
1403
1404# Cleanup
1405drop function f0|
1406drop function f1|
1407drop function f2|
1408drop function f3|
1409drop function f4|
1410drop function f5|
1411drop function f6|
1412drop function f7|
1413drop function f8|
1414drop function f9|
1415drop function f10|
1416drop function f11|
1417drop function f12_1|
1418drop function f12_2|
1419drop view v0|
1420drop view v1|
1421drop view v2|
1422truncate table t1 |
1423truncate table t2 |
1424drop table t4|
1425
1426# End of non-bug tests
1427
1428
1429#
1430# Some "real" examples
1431#
1432
1433# fac
1434
1435--disable_warnings
1436drop table if exists t3|
1437--enable_warnings
1438create table t3 (n int unsigned not null primary key, f bigint unsigned)|
1439
1440--disable_warnings
1441drop procedure if exists ifac|
1442--enable_warnings
1443create procedure ifac(n int unsigned)
1444begin
1445  declare i int unsigned default 1;
1446
1447  if n > 20 then
1448    set n = 20;		# bigint overflow otherwise
1449  end if;
1450  while i <= n do
1451    begin
1452      insert into test.t3 values (i, fac(i));
1453      set i = i + 1;
1454    end;
1455  end while;
1456end|
1457
1458call ifac(20)|
1459select * from t3|
1460drop table t3|
1461--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1462show function status like '%f%'|
1463drop procedure ifac|
1464drop function fac|
1465--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1466show function status like '%f%'|
1467
1468
1469# primes
1470
1471--disable_warnings
1472drop table if exists t3|
1473--enable_warnings
1474
1475create table t3 (
1476  i int unsigned not null primary key,
1477  p bigint unsigned not null
1478)|
1479
1480insert into t3 values
1481 ( 0,   3), ( 1,   5), ( 2,   7), ( 3,  11), ( 4,  13),
1482 ( 5,  17), ( 6,  19), ( 7,  23), ( 8,  29), ( 9,  31),
1483 (10,  37), (11,  41), (12,  43), (13,  47), (14,  53),
1484 (15,  59), (16,  61), (17,  67), (18,  71), (19,  73),
1485 (20,  79), (21,  83), (22,  89), (23,  97), (24, 101),
1486 (25, 103), (26, 107), (27, 109), (28, 113), (29, 127),
1487 (30, 131), (31, 137), (32, 139), (33, 149), (34, 151),
1488 (35, 157), (36, 163), (37, 167), (38, 173), (39, 179),
1489 (40, 181), (41, 191), (42, 193), (43, 197), (44, 199)|
1490
1491--disable_warnings
1492drop procedure if exists opp|
1493--enable_warnings
1494create procedure opp(n bigint unsigned, out pp bool)
1495begin
1496  declare r double;
1497  declare b, s bigint unsigned default 0;
1498
1499  set r = sqrt(n);
1500
1501 again:
1502  loop
1503    if s = 45 then
1504      set b = b+200, s = 0;
1505    else
1506      begin
1507        declare p bigint unsigned;
1508
1509        select t.p into p from test.t3 t where t.i = s;
1510        if b+p > r then
1511          set pp = 1;
1512          leave again;
1513        end if;
1514        if mod(n, b+p) = 0 then
1515          set pp = 0;
1516          leave again;
1517        end if;
1518        set s = s+1;
1519      end;
1520    end if;
1521  end loop;
1522end|
1523
1524--disable_warnings
1525drop procedure if exists ip|
1526--enable_warnings
1527create procedure ip(m int unsigned)
1528begin
1529  declare p bigint unsigned;
1530  declare i int unsigned;
1531
1532  set i=45, p=201;
1533
1534  while i < m do
1535    begin
1536      declare pp bool default 0;
1537
1538      call opp(p, pp);
1539      if pp then
1540        insert into test.t3 values (i, p);
1541        set i = i+1;
1542      end if;
1543      set p = p+2;
1544    end;
1545  end while;
1546end|
1547show create procedure opp|
1548--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1549show procedure status where name like '%p%' and db='test'|
1550
1551# This isn't the fastest way in the world to compute prime numbers, so
1552# don't be too ambitious. ;-)
1553call ip(200)|
1554# We don't want to select the entire table here, just pick a few
1555# examples.
1556# The expected result is:
1557#    i      p
1558#   ---   ----
1559#    45    211
1560#   100    557
1561#   199   1229
1562select * from t3 where i=45 or i=100 or i=199|
1563drop table t3|
1564drop procedure opp|
1565drop procedure ip|
1566--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1567show procedure status where name like '%p%' and db='test'|
1568
1569
1570#
1571# Comment & suid
1572#
1573
1574--disable_warnings
1575drop procedure if exists bar|
1576--enable_warnings
1577create procedure bar(x char(16), y int)
1578 comment "111111111111" sql security invoker
1579 insert into test.t1 values (x, y)|
1580--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1581show procedure status like 'bar'|
1582alter procedure bar comment "2222222222" sql security definer|
1583alter procedure bar comment "3333333333"|
1584alter procedure bar|
1585show create procedure bar|
1586--replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1587show procedure status like 'bar'|
1588drop procedure bar|
1589
1590#
1591# rexecution
1592#
1593--disable_warnings
1594drop procedure if exists p1|
1595--enable_warnings
1596create procedure p1 ()
1597  select (select s1 from t3) from t3|
1598
1599create table t3 (s1 int)|
1600
1601call p1()|
1602insert into t3 values (1)|
1603call p1()|
1604drop procedure p1|
1605drop table t3|
1606
1607#
1608# backticks
1609#
1610--disable_warnings
1611drop function if exists foo|
1612--enable_warnings
1613create function `foo` () returns int
1614  return 5|
1615select `foo` ()|
1616drop function `foo`|
1617
1618#
1619# Implicit LOCK/UNLOCK TABLES for table access in functions
1620#
1621
1622--disable_warnings
1623drop function if exists t1max|
1624--enable_warnings
1625create function t1max() returns int
1626begin
1627  declare x int;
1628  select max(data) into x from t1;
1629  return x;
1630end|
1631
1632insert into t1 values ("foo", 3), ("bar", 2), ("zip", 5), ("zap", 1)|
1633select t1max()|
1634drop function t1max|
1635
1636create table t3 (
1637  v char(16) not null primary key,
1638  c int unsigned not null
1639)|
1640
1641create function getcount(s char(16)) returns int
1642begin
1643  declare x int;
1644
1645  select count(*) into x from t3 where v = s;
1646  if x = 0 then
1647    insert into t3 values (s, 1);
1648  else
1649    update t3 set c = c+1 where v = s;
1650  end if;
1651  return x;
1652end|
1653select * from t1 where data = getcount("bar")|
1654select * from t3|
1655select getcount("zip")|
1656select getcount("zip")|
1657select * from t3|
1658select getcount(id) from t1 where data = 3|
1659select getcount(id) from t1 where data = 5|
1660select * from t3|
1661drop table t3|
1662drop function getcount|
1663
1664
1665# Test cases for different combinations of condition handlers in nested
1666# begin-end blocks in stored procedures.
1667#
1668# Note that the standard specifies that the most specific handler should
1669# be triggered even if it's an outer handler masked by a less specific
1670# handler in an inner block.
1671# Note also that '02000' is more specific than NOT FOUND; there might be
1672# other '02xxx' states, even if we currently do not issue them in any
1673# situation (e.g. '02001').
1674#
1675# The combinations we test are these:
1676#
1677#                                         Inner
1678#              errcode      sqlstate     not found    sqlwarning   sqlexception
1679#  Outer      +------------+------------+------------+------------+------------+
1680#errcode      | h_ee (i)   | h_es (o)   | h_en (o)   | h_ew (o)   | h_ex (o)   |
1681#sqlstate     | h_se (i)   | h_ss (i)   | h_sn (o)   | h_sw (o)   | h_sx (o)   |
1682#not found    | h_ne (i)   | h_ns (i)   | h_nn (i)   |            |            |
1683#sqlwarning   | h_we (i)   | h_ws (i)   |            | h_ww (i)   |            |
1684#sqlexception | h_xe (i)   | h_xs (i)   |            |            | h_xx (i)   |
1685#             +------------+---------------------------------------------------+
1686#
1687# (i) means that the inner handler is the one that should be invoked,
1688# (o) means that the outer handler should be invoked.
1689#
1690# ('not found', 'sqlwarning' and 'sqlexception' are mutually exclusive, hence
1691#  no tests for those combinations.)
1692#
1693
1694--disable_warnings
1695drop table if exists t3|
1696drop procedure if exists h_ee|
1697drop procedure if exists h_es|
1698drop procedure if exists h_en|
1699drop procedure if exists h_ew|
1700drop procedure if exists h_ex|
1701drop procedure if exists h_se|
1702drop procedure if exists h_ss|
1703drop procedure if exists h_sn|
1704drop procedure if exists h_sw|
1705drop procedure if exists h_sx|
1706drop procedure if exists h_ne|
1707drop procedure if exists h_ns|
1708drop procedure if exists h_nn|
1709drop procedure if exists h_we|
1710drop procedure if exists h_ws|
1711drop procedure if exists h_ww|
1712drop procedure if exists h_xe|
1713drop procedure if exists h_xs|
1714drop procedure if exists h_xx|
1715--enable_warnings
1716
1717# smallint    - to get out of range warnings
1718# primary key - to get constraint errors
1719create table t3 (a smallint primary key)|
1720
1721insert into t3 (a) values (1)|
1722
1723create procedure h_ee()
1724    deterministic
1725begin
1726  declare continue handler for 1062 -- ER_DUP_ENTRY
1727    select 'Outer (bad)' as 'h_ee';
1728
1729  begin
1730    declare continue handler for 1062 -- ER_DUP_ENTRY
1731        select 'Inner (good)' as 'h_ee';
1732
1733    insert into t3 values (1);
1734  end;
1735end|
1736
1737create procedure h_es()
1738    deterministic
1739begin
1740  declare continue handler for 1062 -- ER_DUP_ENTRY
1741    select 'Outer (good)' as 'h_es';
1742
1743  begin
1744    -- integrity constraint violation
1745    declare continue handler for sqlstate '23000'
1746      select 'Inner (bad)' as 'h_es';
1747
1748    insert into t3 values (1);
1749  end;
1750end|
1751
1752create procedure h_en()
1753    deterministic
1754begin
1755  declare continue handler for 1329 -- ER_SP_FETCH_NO_DATA
1756    select 'Outer (good)' as 'h_en';
1757
1758  begin
1759    declare x int;
1760    declare continue handler for sqlstate '02000' -- no data
1761      select 'Inner (bad)' as 'h_en';
1762
1763    select a into x from t3 where a = 42;
1764  end;
1765end|
1766
1767create procedure h_ew()
1768    deterministic
1769begin
1770  declare continue handler for 1264 -- ER_WARN_DATA_OUT_OF_RANGE
1771    select 'Outer (good)' as 'h_ew';
1772
1773  begin
1774    declare continue handler for sqlwarning
1775      select 'Inner (bad)' as 'h_ew';
1776
1777    insert into t3 values (123456789012);
1778  end;
1779  delete from t3;
1780  insert into t3 values (1);
1781end|
1782
1783create procedure h_ex()
1784    deterministic
1785begin
1786  declare continue handler for 1062 -- ER_DUP_ENTRY
1787    select 'Outer (good)' as 'h_ex';
1788
1789  begin
1790    declare continue handler for sqlexception
1791      select 'Inner (bad)' as 'h_ex';
1792
1793    insert into t3 values (1);
1794  end;
1795end|
1796
1797create procedure h_se()
1798    deterministic
1799begin
1800  -- integrity constraint violation
1801  declare continue handler for sqlstate '23000'
1802    select 'Outer (bad)' as 'h_se';
1803
1804  begin
1805    declare continue handler for 1062 -- ER_DUP_ENTRY
1806      select 'Inner (good)' as 'h_se';
1807
1808    insert into t3 values (1);
1809  end;
1810end|
1811
1812create procedure h_ss()
1813    deterministic
1814begin
1815  -- integrity constraint violation
1816  declare continue handler for sqlstate '23000'
1817    select 'Outer (bad)' as 'h_ss';
1818
1819  begin
1820    -- integrity constraint violation
1821    declare continue handler for sqlstate '23000'
1822      select 'Inner (good)' as 'h_ss';
1823
1824    insert into t3 values (1);
1825  end;
1826end|
1827
1828create procedure h_sn()
1829    deterministic
1830begin
1831  -- Note: '02000' is more specific than NOT FOUND ;
1832  --       there might be other not found states
1833  declare continue handler for sqlstate '02000' -- no data
1834    select 'Outer (good)' as 'h_sn';
1835
1836  begin
1837    declare x int;
1838    declare continue handler for not found
1839      select 'Inner (bad)' as 'h_sn';
1840
1841    select a into x from t3 where a = 42;
1842  end;
1843end|
1844
1845create procedure h_sw()
1846    deterministic
1847begin
1848  -- data exception - numeric value out of range
1849  declare continue handler for sqlstate '22003'
1850    select 'Outer (good)' as 'h_sw';
1851
1852  begin
1853    declare continue handler for sqlwarning
1854      select 'Inner (bad)' as 'h_sw';
1855
1856    insert into t3 values (123456789012);
1857  end;
1858  delete from t3;
1859  insert into t3 values (1);
1860end|
1861
1862create procedure h_sx()
1863    deterministic
1864begin
1865  -- integrity constraint violation
1866  declare continue handler for sqlstate '23000'
1867    select 'Outer (good)' as 'h_sx';
1868
1869  begin
1870    declare continue handler for sqlexception
1871      select 'Inner (bad)' as 'h_sx';
1872
1873    insert into t3 values (1);
1874  end;
1875end|
1876
1877create procedure h_ne()
1878    deterministic
1879begin
1880  declare continue handler for not found
1881    select 'Outer (bad)' as 'h_ne';
1882
1883  begin
1884    declare x int;
1885    declare continue handler for 1329 -- ER_SP_FETCH_NO_DATA
1886      select 'Inner (good)' as 'h_ne';
1887
1888    select a into x from t3 where a = 42;
1889  end;
1890end|
1891
1892create procedure h_ns()
1893    deterministic
1894begin
1895  declare continue handler for not found
1896    select 'Outer (bad)' as 'h_ns';
1897
1898  begin
1899    declare x int;
1900    declare continue handler for sqlstate '02000' -- no data
1901      select 'Inner (good)' as 'h_ns';
1902
1903    select a into x from t3 where a = 42;
1904  end;
1905end|
1906
1907create procedure h_nn()
1908    deterministic
1909begin
1910  declare continue handler for not found
1911    select 'Outer (bad)' as 'h_nn';
1912
1913  begin
1914    declare x int;
1915    declare continue handler for not found
1916      select 'Inner (good)' as 'h_nn';
1917
1918    select a into x from t3 where a = 42;
1919  end;
1920end|
1921
1922create procedure h_we()
1923    deterministic
1924begin
1925  declare continue handler for sqlwarning
1926    select 'Outer (bad)' as 'h_we';
1927
1928  begin
1929    declare continue handler for 1264 -- ER_WARN_DATA_OUT_OF_RANGE
1930      select 'Inner (good)' as 'h_we';
1931
1932    insert into t3 values (123456789012);
1933  end;
1934  delete from t3;
1935  insert into t3 values (1);
1936end|
1937
1938create procedure h_ws()
1939    deterministic
1940begin
1941  declare continue handler for sqlwarning
1942    select 'Outer (bad)' as 'h_ws';
1943
1944  begin
1945    -- data exception - numeric value out of range
1946    declare continue handler for sqlstate '22003'
1947      select 'Inner (good)' as 'h_ws';
1948
1949    insert into t3 values (123456789012);
1950  end;
1951  delete from t3;
1952  insert into t3 values (1);
1953end|
1954
1955create procedure h_ww()
1956    deterministic
1957begin
1958  declare continue handler for sqlwarning
1959    select 'Outer (bad)' as 'h_ww';
1960
1961  begin
1962    declare continue handler for sqlwarning
1963      select 'Inner (good)' as 'h_ww';
1964
1965    insert into t3 values (123456789012);
1966  end;
1967  delete from t3;
1968  insert into t3 values (1);
1969end|
1970
1971create procedure h_xe()
1972    deterministic
1973begin
1974  declare continue handler for sqlexception
1975    select 'Outer (bad)' as 'h_xe';
1976
1977  begin
1978    declare continue handler for 1062 -- ER_DUP_ENTRY
1979      select 'Inner (good)' as 'h_xe';
1980
1981    insert into t3 values (1);
1982  end;
1983end|
1984
1985create procedure h_xs()
1986    deterministic
1987begin
1988  declare continue handler for sqlexception
1989    select 'Outer (bad)' as 'h_xs';
1990
1991  begin
1992    -- integrity constraint violation
1993    declare continue handler for sqlstate '23000'
1994      select 'Inner (good)' as 'h_xs';
1995
1996    insert into t3 values (1);
1997  end;
1998end|
1999
2000create procedure h_xx()
2001    deterministic
2002begin
2003  declare continue handler for sqlexception
2004    select 'Outer (bad)' as 'h_xx';
2005
2006  begin
2007    declare continue handler for sqlexception
2008      select 'Inner (good)' as 'h_xx';
2009
2010    insert into t3 values (1);
2011  end;
2012end|
2013
2014call h_ee()|
2015call h_es()|
2016call h_en()|
2017call h_ew()|
2018call h_ex()|
2019call h_se()|
2020call h_ss()|
2021call h_sn()|
2022call h_sw()|
2023call h_sx()|
2024call h_ne()|
2025call h_ns()|
2026call h_nn()|
2027call h_we()|
2028call h_ws()|
2029call h_ww()|
2030call h_xe()|
2031call h_xs()|
2032call h_xx()|
2033
2034drop table t3|
2035drop procedure h_ee|
2036drop procedure h_es|
2037drop procedure h_en|
2038drop procedure h_ew|
2039drop procedure h_ex|
2040drop procedure h_se|
2041drop procedure h_ss|
2042drop procedure h_sn|
2043drop procedure h_sw|
2044drop procedure h_sx|
2045drop procedure h_ne|
2046drop procedure h_ns|
2047drop procedure h_nn|
2048drop procedure h_we|
2049drop procedure h_ws|
2050drop procedure h_ww|
2051drop procedure h_xe|
2052drop procedure h_xs|
2053drop procedure h_xx|
2054
2055
2056#
2057# Test cases for old bugs
2058#
2059
2060#
2061# BUG#822
2062#
2063--disable_warnings
2064drop procedure if exists bug822|
2065--enable_warnings
2066create procedure bug822(a_id char(16), a_data int)
2067begin
2068  declare n int;
2069  select count(*) into n from t1 where id = a_id and data = a_data;
2070  if n = 0 then
2071    insert into t1 (id, data) values (a_id, a_data);
2072  end if;
2073end|
2074
2075delete from t1|
2076call bug822('foo', 42)|
2077call bug822('foo', 42)|
2078call bug822('bar', 666)|
2079select * from t1 order by data|
2080delete from t1|
2081drop procedure bug822|
2082
2083#
2084# BUG#1495
2085#
2086--disable_warnings
2087drop procedure if exists bug1495|
2088--enable_warnings
2089create procedure bug1495()
2090begin
2091  declare x int;
2092
2093  select data into x from t1 order by id limit 1;
2094  if x > 10 then
2095    insert into t1 values ("less", x-10);
2096  else
2097    insert into t1 values ("more", x+10);
2098  end if;
2099end|
2100
2101insert into t1 values ('foo', 12)|
2102call bug1495()|
2103delete from t1 where id='foo'|
2104insert into t1 values ('bar', 7)|
2105call bug1495()|
2106delete from t1 where id='bar'|
2107select * from t1 order by data|
2108delete from t1|
2109drop procedure bug1495|
2110
2111#
2112# BUG#1547
2113#
2114--disable_warnings
2115drop procedure if exists bug1547|
2116--enable_warnings
2117create procedure bug1547(s char(16))
2118begin
2119  declare x int;
2120
2121  select data into x from t1 where s = id limit 1;
2122  if x > 10 then
2123    insert into t1 values ("less", x-10);
2124  else
2125    insert into t1 values ("more", x+10);
2126  end if;
2127end|
2128
2129insert into t1 values ("foo", 12), ("bar", 7)|
2130call bug1547("foo")|
2131call bug1547("bar")|
2132select * from t1 order by id|
2133delete from t1|
2134drop procedure bug1547|
2135
2136#
2137# BUG#1656
2138#
2139--disable_warnings
2140drop table if exists t70|
2141--enable_warnings
2142create table t70 (s1 int,s2 int)|
2143insert into t70 values (1,2)|
2144
2145--disable_warnings
2146drop procedure if exists bug1656|
2147--enable_warnings
2148create procedure bug1656(out p1 int, out p2 int)
2149  select * into p1, p1 from t70|
2150
2151call bug1656(@1, @2)|
2152select @1, @2|
2153drop table t70|
2154drop procedure bug1656|
2155
2156#
2157# BUG#1862
2158#
2159create table t3(a int)|
2160
2161--disable_warnings
2162drop procedure if exists bug1862|
2163--enable_warnings
2164create procedure bug1862()
2165begin
2166  insert into t3 values(2);
2167  flush tables;
2168end|
2169
2170call bug1862()|
2171# the second call caused a segmentation
2172call bug1862()|
2173select * from t3|
2174drop table t3|
2175drop procedure bug1862|
2176
2177#
2178# BUG#1874
2179#
2180--disable_warnings
2181drop procedure if exists bug1874|
2182--enable_warnings
2183create procedure bug1874()
2184begin
2185  declare x int;
2186  declare y double;
2187  select max(data) into x from t1;
2188  insert into t2 values ("max", x, 0);
2189  select min(data) into x from t1;
2190  insert into t2 values ("min", x, 0);
2191  select sum(data) into x from t1;
2192  insert into t2 values ("sum", x, 0);
2193  select avg(data) into y from t1;
2194  insert into t2 values ("avg", 0, y);
2195end|
2196
2197insert into t1 (data) values (3), (1), (5), (9), (4)|
2198call bug1874()|
2199select * from t2 order by i|
2200delete from t1|
2201delete from t2|
2202drop procedure bug1874|
2203
2204#
2205# BUG#2260
2206#
2207--disable_warnings
2208drop procedure if exists bug2260|
2209--enable_warnings
2210create procedure bug2260()
2211begin
2212  declare v1 int;
2213  declare c1 cursor for select data from t1;
2214  declare continue handler for not found set @x2 = 1;
2215
2216  open c1;
2217  fetch c1 into v1;
2218  set @x2 = 2;
2219  close c1;
2220end|
2221
2222call bug2260()|
2223select @x2|
2224drop procedure bug2260|
2225
2226#
2227# BUG#2267 "Lost connect if stored procedure has SHOW FUNCTION STATUS"
2228#
2229--disable_warnings
2230drop procedure if exists bug2267_1|
2231--enable_warnings
2232create procedure bug2267_1()
2233begin
2234  show procedure status where db='test';
2235end|
2236
2237--disable_warnings
2238drop procedure if exists bug2267_2|
2239--enable_warnings
2240create procedure bug2267_2()
2241begin
2242  show function status where db='test';
2243end|
2244
2245--disable_warnings
2246drop procedure if exists bug2267_3|
2247--enable_warnings
2248create procedure bug2267_3()
2249begin
2250  show create procedure bug2267_1;
2251end|
2252
2253--disable_warnings
2254drop procedure if exists bug2267_4|
2255drop function if exists bug2267_4|
2256--enable_warnings
2257create procedure bug2267_4()
2258begin
2259  show create function bug2267_4;
2260end|
2261create function bug2267_4() returns int return 100|
2262
2263--replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
2264call bug2267_1()|
2265--replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
2266call bug2267_2()|
2267call bug2267_3()|
2268call bug2267_4()|
2269
2270drop procedure bug2267_1|
2271drop procedure bug2267_2|
2272drop procedure bug2267_3|
2273drop procedure bug2267_4|
2274drop function bug2267_4|
2275
2276#
2277# BUG#2227
2278#
2279--disable_warnings
2280drop procedure if exists bug2227|
2281--enable_warnings
2282create procedure bug2227(x int)
2283begin
2284  declare y float default 2.6;
2285  declare z char(16) default "zzz";
2286
2287  select 1.3, x, y, 42, z;
2288end|
2289
2290call bug2227(9)|
2291drop procedure bug2227|
2292
2293#
2294# BUG#2614 "Stored procedure with INSERT ... SELECT that does not
2295#           contain any tables crashes server"
2296#
2297--disable_warnings
2298drop procedure if exists bug2614|
2299--enable_warnings
2300create procedure bug2614()
2301begin
2302  drop table if exists t3;
2303  create table t3 (id int default '0' not null);
2304  insert into t3 select 12;
2305  insert into t3 select * from t3;
2306end|
2307
2308--disable_warnings
2309call bug2614()|
2310--enable_warnings
2311call bug2614()|
2312drop table t3|
2313drop procedure bug2614|
2314
2315#
2316# BUG#2674
2317#
2318--disable_warnings
2319drop function if exists bug2674|
2320--enable_warnings
2321create function bug2674() returns int
2322  return @@sort_buffer_size|
2323
2324set @osbs = @@sort_buffer_size|
2325set @@sort_buffer_size = 262000|
2326select bug2674()|
2327drop function bug2674|
2328set @@sort_buffer_size = @osbs|
2329
2330#
2331# BUG#3259
2332#
2333--disable_warnings
2334drop procedure if exists bug3259_1 |
2335--enable_warnings
2336create procedure bug3259_1 () begin end|
2337--disable_warnings
2338drop procedure if exists BUG3259_2 |
2339--enable_warnings
2340create procedure BUG3259_2 () begin end|
2341--disable_warnings
2342drop procedure if exists Bug3259_3 |
2343--enable_warnings
2344create procedure Bug3259_3 () begin end|
2345
2346call BUG3259_1()|
2347call BUG3259_1()|
2348call bug3259_2()|
2349call Bug3259_2()|
2350call bug3259_3()|
2351call bUG3259_3()|
2352
2353drop procedure bUg3259_1|
2354drop procedure BuG3259_2|
2355drop procedure BUG3259_3|
2356
2357#
2358# BUG#2772
2359#
2360--disable_warnings
2361drop function if exists bug2772|
2362--enable_warnings
2363create function bug2772() returns char(10) character set latin2
2364  return 'a'|
2365
2366select bug2772()|
2367drop function bug2772|
2368
2369#
2370# BUG#2776
2371#
2372--disable_warnings
2373drop procedure if exists bug2776_1|
2374--enable_warnings
2375create procedure bug2776_1(out x int)
2376begin
2377  declare v int;
2378
2379  set v = default;
2380  set x = v;
2381end|
2382
2383--disable_warnings
2384drop procedure if exists bug2776_2|
2385--enable_warnings
2386create procedure bug2776_2(out x int)
2387begin
2388  declare v int default 42;
2389
2390  set v = default;
2391  set x = v;
2392end|
2393
2394set @x = 1|
2395call bug2776_1(@x)|
2396select @x|
2397call bug2776_2(@x)|
2398select @x|
2399drop procedure bug2776_1|
2400drop procedure bug2776_2|
2401
2402#
2403# BUG#2780
2404#
2405create table t3 (s1 smallint)|
2406
2407insert into t3 values (123456789012)|
2408
2409--disable_warnings
2410drop procedure if exists bug2780|
2411--enable_warnings
2412create procedure bug2780()
2413begin
2414  declare exit handler for sqlwarning set @x = 1;
2415
2416  set @x = 0;
2417  insert into t3 values (123456789012);
2418  insert into t3 values (0);
2419end|
2420
2421call bug2780()|
2422select @x|
2423select * from t3|
2424
2425drop procedure bug2780|
2426drop table t3|
2427
2428#
2429# BUG#1863
2430#
2431create table t3 (content varchar(10) )|
2432insert into t3 values ("test1")|
2433insert into t3 values ("test2")|
2434create table t4 (f1 int, rc int, t3 int)|
2435
2436--disable_warnings
2437drop procedure if exists bug1863|
2438--enable_warnings
2439create procedure bug1863(in1 int)
2440begin
2441
2442  declare ind int default 0;
2443  declare t1 int;
2444  declare t2 int;
2445  declare t3 int;
2446
2447  declare rc int default 0;
2448  declare continue handler for 1065 set rc = 1;
2449
2450  drop temporary table if exists temp_t1;
2451  create temporary table temp_t1 (
2452    f1 int auto_increment, f2 varchar(20), primary key (f1)
2453  );
2454
2455  insert into temp_t1 (f2) select content from t3;
2456
2457  select f2 into t3 from temp_t1 where f1 = 10;
2458
2459  if (rc) then
2460       insert into t4 values (1, rc, t3);
2461  end if;
2462
2463  insert into t4 values (2, rc, t3);
2464
2465end|
2466
2467call bug1863(10)|
2468call bug1863(10)|
2469select * from t4|
2470
2471drop procedure bug1863|
2472drop temporary table temp_t1;
2473drop table t3, t4|
2474
2475#
2476# BUG#2656
2477#
2478
2479create table t3 (
2480  OrderID  int not null,
2481  MarketID int,
2482  primary key (OrderID)
2483)|
2484
2485create table t4 (
2486  MarketID int not null,
2487  Market varchar(60),
2488  Status char(1),
2489  primary key (MarketID)
2490)|
2491
2492insert t3 (OrderID,MarketID) values (1,1)|
2493insert t3 (OrderID,MarketID) values (2,2)|
2494insert t4 (MarketID,Market,Status) values (1,"MarketID One","A")|
2495insert t4 (MarketID,Market,Status) values (2,"MarketID Two","A")|
2496
2497--disable_warnings
2498drop procedure if exists bug2656_1|
2499--enable_warnings
2500create procedure bug2656_1()
2501begin
2502  select
2503    m.Market
2504  from  t4 m JOIN t3 o
2505        ON o.MarketID != 1 and o.MarketID = m.MarketID;
2506end |
2507
2508--disable_warnings
2509drop procedure if exists bug2656_2|
2510--enable_warnings
2511create procedure bug2656_2()
2512begin
2513  select
2514    m.Market
2515  from
2516    t4 m, t3 o
2517  where
2518    m.MarketID != 1 and m.MarketID = o.MarketID;
2519
2520end |
2521
2522call bug2656_1()|
2523call bug2656_1()|
2524call bug2656_2()|
2525call bug2656_2()|
2526drop procedure bug2656_1|
2527drop procedure bug2656_2|
2528drop table t3, t4|
2529
2530
2531#
2532# BUG#3426
2533#
2534--disable_warnings
2535drop procedure if exists bug3426|
2536--enable_warnings
2537create procedure bug3426(in_time int unsigned, out x int)
2538begin
2539  if in_time is null then
2540    set @stamped_time=10;
2541    set x=1;
2542  else
2543    set @stamped_time=in_time;
2544    set x=2;
2545  end if;
2546end|
2547
2548# so that from_unixtime() has a deterministic result
2549set time_zone='+03:00';
2550
2551call bug3426(1000, @i)|
2552select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2553call bug3426(NULL, @i)|
2554select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2555# Clear SP cache
2556alter procedure bug3426 sql security invoker|
2557call bug3426(NULL, @i)|
2558select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2559call bug3426(1000, @i)|
2560select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2561
2562drop procedure bug3426|
2563
2564#
2565# BUG#3734
2566#
2567create table t3 (
2568  id int unsigned auto_increment not null primary key,
2569  title VARCHAR(200),
2570  body text,
2571  fulltext (title,body)
2572)|
2573
2574insert into t3 (title,body) values
2575  ('MySQL Tutorial','DBMS stands for DataBase ...'),
2576  ('How To Use MySQL Well','After you went through a ...'),
2577  ('Optimizing MySQL','In this tutorial we will show ...'),
2578  ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
2579  ('MySQL vs. YourSQL','In the following database comparison ...'),
2580  ('MySQL Security','When configured properly, MySQL ...')|
2581
2582--disable_warnings
2583drop procedure if exists bug3734 |
2584--enable_warnings
2585create procedure bug3734 (param1 varchar(100))
2586  select * from t3 where match (title,body) against (param1)|
2587
2588call bug3734('database')|
2589call bug3734('Security')|
2590
2591drop procedure bug3734|
2592drop table t3|
2593
2594#
2595# BUG#3863
2596#
2597--disable_warnings
2598drop procedure if exists bug3863|
2599--enable_warnings
2600create procedure bug3863()
2601begin
2602  set @a = 0;
2603  while @a < 5 do
2604    set @a = @a + 1;
2605  end while;
2606end|
2607
2608call bug3863()|
2609select @a|
2610call bug3863()|
2611select @a|
2612
2613drop procedure bug3863|
2614
2615#
2616# BUG#2460
2617#
2618
2619create table t3 (
2620  id int(10) unsigned not null default 0,
2621  rid int(10) unsigned not null default 0,
2622  msg text not null,
2623  primary key (id),
2624  unique key rid (rid, id)
2625)|
2626
2627--disable_warnings
2628drop procedure if exists bug2460_1|
2629--enable_warnings
2630create procedure bug2460_1(in v int)
2631begin
2632    ( select n0.id from t3 as n0 where n0.id = v )
2633  union
2634    ( select n0.id from t3 as n0, t3 as n1
2635        where n0.id = n1.rid and n1.id = v )
2636  union
2637    ( select n0.id from t3 as n0, t3 as n1, t3 as n2
2638        where n0.id = n1.rid and n1.id = n2.rid and n2.id = v );
2639end|
2640
2641call bug2460_1(2)|
2642call bug2460_1(2)|
2643insert into t3 values (1, 1, 'foo'), (2, 1, 'bar'), (3, 1, 'zip zap')|
2644call bug2460_1(2)|
2645call bug2460_1(2)|
2646
2647--disable_warnings
2648drop procedure if exists bug2460_2|
2649--enable_warnings
2650create procedure bug2460_2()
2651begin
2652  drop table if exists t3;
2653  create temporary table t3 (s1 int);
2654  insert into t3 select 1 union select 1;
2655end|
2656
2657call bug2460_2()|
2658call bug2460_2()|
2659select * from t3|
2660
2661drop procedure bug2460_1|
2662drop procedure bug2460_2|
2663drop table t3|
2664
2665
2666#
2667# BUG#2564
2668#
2669set @@sql_mode = ''|
2670--disable_warnings
2671drop procedure if exists bug2564_1|
2672--enable_warnings
2673create procedure bug2564_1()
2674    comment 'Joe''s procedure'
2675  insert into `t1` values ("foo", 1)|
2676
2677set @@sql_mode = 'ANSI_QUOTES'|
2678--disable_warnings
2679drop procedure if exists bug2564_2|
2680--enable_warnings
2681create procedure bug2564_2()
2682  insert into "t1" values ('foo', 1)|
2683
2684delimiter $|
2685set @@sql_mode = ''$
2686--disable_warnings
2687drop function if exists bug2564_3$
2688--enable_warnings
2689create function bug2564_3(x int, y int) returns int
2690  return x || y$
2691
2692set @@sql_mode = 'ANSI'$
2693--disable_warnings
2694drop function if exists bug2564_4$
2695--enable_warnings
2696create function bug2564_4(x int, y int) returns int
2697  return x || y$
2698delimiter |$
2699
2700set @@sql_mode = ''|
2701show create procedure bug2564_1|
2702show create procedure bug2564_2|
2703show create function bug2564_3|
2704show create function bug2564_4|
2705
2706drop procedure bug2564_1|
2707drop procedure bug2564_2|
2708drop function bug2564_3|
2709drop function bug2564_4|
2710
2711#
2712# BUG#3132
2713#
2714--disable_warnings
2715drop function if exists bug3132|
2716--enable_warnings
2717create function bug3132(s char(20)) returns char(50)
2718  return concat('Hello, ', s, '!')|
2719
2720select bug3132('Bob') union all select bug3132('Judy')|
2721drop function bug3132|
2722
2723#
2724# BUG#3843
2725#
2726--disable_warnings
2727drop procedure if exists bug3843|
2728--enable_warnings
2729create procedure bug3843()
2730  analyze table t1|
2731
2732# Testing for packets out of order
2733call bug3843()|
2734call bug3843()|
2735select 1+2|
2736
2737drop procedure bug3843|
2738
2739#
2740# BUG#3368
2741#
2742create table t3 ( s1 char(10) )|
2743insert into t3 values ('a'), ('b')|
2744
2745--disable_warnings
2746drop procedure if exists bug3368|
2747--enable_warnings
2748create procedure bug3368(v char(10))
2749begin
2750  select group_concat(v) from t3;
2751end|
2752
2753call bug3368('x')|
2754call bug3368('yz')|
2755drop procedure bug3368|
2756drop table t3|
2757
2758#
2759# BUG#4579
2760#
2761create table t3 (f1 int, f2 int)|
2762insert into t3 values (1,1)|
2763
2764--disable_warnings
2765drop procedure if exists bug4579_1|
2766--enable_warnings
2767create procedure bug4579_1 ()
2768begin
2769  declare sf1 int;
2770
2771  select f1 into sf1 from t3 where f1=1 and f2=1;
2772  update t3 set f2 = f2 + 1 where f1=1 and f2=1;
2773  call bug4579_2();
2774end|
2775
2776--disable_warnings
2777drop procedure if exists bug4579_2|
2778--enable_warnings
2779create procedure bug4579_2 ()
2780begin
2781end|
2782
2783call bug4579_1()|
2784call bug4579_1()|
2785call bug4579_1()|
2786
2787drop procedure bug4579_1|
2788drop procedure bug4579_2|
2789drop table t3|
2790
2791#
2792# BUG#2773: Function's data type ignored in stored procedures
2793#
2794--disable_warnings
2795drop procedure if exists bug2773|
2796--enable_warnings
2797
2798create function bug2773() returns int return null|
2799create table t3 as select bug2773()|
2800show create table t3|
2801drop table t3|
2802drop function bug2773|
2803
2804#
2805# BUG#3788: Stored procedure packet error
2806#
2807--disable_warnings
2808drop procedure if exists bug3788|
2809--enable_warnings
2810
2811create function bug3788() returns date return cast("2005-03-04" as date)|
2812select bug3788()|
2813drop function bug3788|
2814
2815create function bug3788() returns binary(1) return 5|
2816select bug3788()|
2817drop function bug3788|
2818
2819
2820#
2821# BUG#4726
2822#
2823create table t3 (f1 int, f2 int, f3 int)|
2824insert into t3 values (1,1,1)|
2825
2826--disable_warnings
2827drop procedure if exists bug4726|
2828--enable_warnings
2829create procedure bug4726()
2830begin
2831   declare tmp_o_id INT;
2832   declare tmp_d_id INT default 1;
2833
2834   while tmp_d_id <= 2 do
2835   begin
2836     select f1 into tmp_o_id from t3 where f2=1 and f3=1;
2837     set tmp_d_id = tmp_d_id + 1;
2838   end;
2839   end while;
2840end|
2841
2842call bug4726()|
2843call bug4726()|
2844call bug4726()|
2845
2846drop procedure bug4726|
2847drop table t3|
2848
2849#
2850# BUG#4318
2851#
2852
2853--disable_parsing # Don't know if HANDLER commands can work with SPs, or at all..
2854create table t3 (s1 int)|
2855insert into t3 values (3), (4)|
2856
2857--disable_warnings
2858drop procedure if exists bug4318|
2859--enable_warnings
2860create procedure bug4318()
2861  handler t3 read next|
2862
2863handler t3 open|
2864# Expect no results, as tables are closed, but there shouldn't be any errors
2865call bug4318()|
2866call bug4318()|
2867handler t3 close|
2868
2869drop procedure bug4318|
2870drop table t3|
2871--enable_parsing
2872
2873#
2874# BUG#4902: Stored procedure with SHOW WARNINGS leads to packet error
2875#
2876# Added tests for most other show commands we could find too.
2877# (Skipping those already tested, and the ones depending on optional handlers.)
2878#
2879# Note: This will return a large number of results of different formats,
2880#       which makes it impossible to filter with --replace_column.
2881#       It's possible that some of these are not deterministic across
2882#       platforms. If so, just remove the offending command.
2883#
2884--disable_warnings
2885drop procedure if exists bug4902|
2886--enable_warnings
2887create procedure bug4902()
2888begin
2889  show charset like 'foo';
2890  show collation like 'foo';
2891  show create table t1;
2892  show create database test;
2893  show databases like 'foo';
2894  show errors;
2895  show columns from t1;
2896  show keys from t1;
2897  show open tables like 'foo';
2898  # Removed because result will differ in embedded mode.
2899  #show privileges;
2900  show status like 'foo';
2901  show tables like 'foo';
2902  show variables like 'foo';
2903  show warnings;
2904end|
2905--disable_parsing
2906--replace_regex /table_id: [0-9]+/table_id: #/
2907show binlog events|
2908show storage engines|
2909show master status|
2910show slave hosts|
2911show slave status|
2912--enable_parsing
2913
2914call bug4902()|
2915call bug4902()|
2916
2917drop procedure bug4902|
2918
2919#
2920# BUG#4904
2921#
2922--disable_warnings
2923drop procedure if exists bug4904|
2924--enable_warnings
2925create procedure bug4904()
2926begin
2927  declare continue handler for sqlstate 'HY000' begin end;
2928
2929  create table not_existing_table as select * from t3;
2930end|
2931
2932-- error 1146
2933call bug4904()|
2934
2935drop procedure bug4904|
2936
2937create table t3 (s1 char character set latin1, s2 char character set latin2)|
2938
2939--disable_warnings
2940drop procedure if exists bug4904|
2941--enable_warnings
2942create procedure bug4904 ()
2943begin
2944  declare continue handler for sqlstate 'HY000' begin end;
2945
2946  select s1 from t3 union select s2 from t3;
2947end|
2948
2949call bug4904()|
2950
2951drop procedure bug4904|
2952drop table t3|
2953
2954#
2955# BUG#336
2956#
2957--disable_warnings
2958drop procedure if exists bug336|
2959--enable_warnings
2960create procedure bug336(out y int)
2961begin
2962  declare x int;
2963  set x = (select sum(t.data) from test.t1 t);
2964  set y = x;
2965end|
2966
2967insert into t1 values ("a", 2), ("b", 3)|
2968call bug336(@y)|
2969select @y|
2970delete from t1|
2971drop procedure bug336|
2972
2973#
2974# BUG#3157
2975#
2976--disable_warnings
2977drop procedure if exists bug3157|
2978--enable_warnings
2979create procedure bug3157()
2980begin
2981  if exists(select * from t1) then
2982    set @n= @n + 1;
2983  end if;
2984  if (select count(*) from t1) then
2985    set @n= @n + 1;
2986  end if;
2987end|
2988
2989set @n = 0|
2990insert into t1 values ("a", 1)|
2991call bug3157()|
2992select @n|
2993delete from t1|
2994drop procedure bug3157|
2995
2996#
2997# BUG#5251: mysql changes creation time of a procedure/function when altering
2998#
2999--disable_warnings
3000drop procedure if exists bug5251|
3001--enable_warnings
3002create procedure bug5251()
3003begin
3004end|
3005
3006select created into @c1 from mysql.proc
3007  where db='test' and name='bug5251'|
3008--sleep 2
3009alter procedure bug5251 comment 'foobar'|
3010select count(*) from mysql.proc
3011  where  db='test' and name='bug5251' and created = @c1|
3012
3013drop procedure bug5251|
3014
3015#
3016# BUG#5279: Stored procedure packets out of order if CHECKSUM TABLE
3017#
3018--disable_warnings
3019drop procedure if exists bug5251|
3020--enable_warnings
3021create procedure bug5251()
3022  checksum table t1|
3023
3024call bug5251()|
3025call bug5251()|
3026drop procedure bug5251|
3027
3028#
3029# BUG#5287: Stored procedure crash if leave outside loop
3030#
3031--disable_warnings
3032drop procedure if exists bug5287|
3033--enable_warnings
3034create procedure bug5287(param1 int)
3035label1:
3036  begin
3037    declare c cursor for select 5;
3038
3039    loop
3040      if param1 >= 0 then
3041        leave label1;
3042      end if;
3043    end loop;
3044end|
3045call bug5287(1)|
3046drop procedure bug5287|
3047
3048
3049#
3050# BUG#5307: Stored procedure allows statement after BEGIN ... END
3051#
3052--disable_warnings
3053drop procedure if exists bug5307|
3054--enable_warnings
3055create procedure bug5307()
3056begin
3057end; set @x = 3|
3058
3059call bug5307()|
3060select @x|
3061drop procedure bug5307|
3062
3063#
3064# BUG#5258: Stored procedure modified date is 0000-00-00
3065# (This was a design flaw)
3066--disable_warnings
3067drop procedure if exists bug5258|
3068--enable_warnings
3069create procedure bug5258()
3070begin
3071end|
3072
3073--disable_warnings
3074drop procedure if exists bug5258_aux|
3075--enable_warnings
3076create procedure bug5258_aux()
3077begin
3078  declare c, m char(19);
3079
3080  select created,modified into c,m from mysql.proc where name = 'bug5258';
3081  if c = m then
3082    select 'Ok';
3083  else
3084    select c, m;
3085  end if;
3086end|
3087
3088call bug5258_aux()|
3089
3090drop procedure bug5258|
3091drop procedure bug5258_aux|
3092
3093#
3094# BUG#4487: Stored procedure connection aborted if uninitialized char
3095#
3096--disable_warnings
3097drop function if exists bug4487|
3098--enable_warnings
3099create function bug4487() returns char
3100begin
3101  declare v char;
3102  return v;
3103end|
3104
3105select bug4487()|
3106drop function bug4487|
3107
3108
3109#
3110# BUG#4941: Stored procedure crash fetching null value into variable.
3111#
3112--disable_warnings
3113drop procedure if exists bug4941|
3114--enable_warnings
3115--disable_warnings
3116drop procedure if exists bug4941|
3117--enable_warnings
3118create procedure bug4941(out x int)
3119begin
3120  declare c cursor for select i from t2 limit 1;
3121  open c;
3122  fetch c into x;
3123  close c;
3124end|
3125
3126insert into t2 values (null, null, null)|
3127set @x = 42|
3128call bug4941(@x)|
3129select @x|
3130delete from t1|
3131drop procedure bug4941|
3132
3133#
3134# BUG#4905: Stored procedure doesn't clear for "Rows affected"
3135#
3136--disable_warnings
3137drop procedure if exists bug4905|
3138--enable_warnings
3139
3140create table t3 (s1 int,primary key (s1))|
3141
3142--disable_warnings
3143drop procedure if exists bug4905|
3144--enable_warnings
3145create procedure bug4905()
3146begin
3147  declare v int;
3148  declare continue handler for sqlstate '23000' set v = 5;
3149
3150  insert into t3 values (1);
3151end|
3152
3153call bug4905()|
3154select row_count()|
3155call bug4905()|
3156select row_count()|
3157call bug4905()|
3158select row_count()|
3159select * from t3|
3160
3161drop procedure bug4905|
3162drop table t3|
3163
3164#
3165# BUG#6022: Stored procedure shutdown problem with self-calling function.
3166#
3167
3168--disable_parsing # until we implement support for recursive stored functions.
3169--disable_warnings
3170drop function if exists bug6022|
3171--enable_warnings
3172
3173--disable_warnings
3174drop function if exists bug6022|
3175--enable_warnings
3176create function bug6022(x int) returns int
3177begin
3178  if x < 0 then
3179    return 0;
3180  else
3181    return bug6022(x-1);
3182  end if;
3183end|
3184
3185select bug6022(5)|
3186drop function bug6022|
3187--enable_parsing
3188
3189#
3190# BUG#6029: Stored procedure specific handlers should have priority
3191#
3192--disable_warnings
3193drop procedure if exists bug6029|
3194--enable_warnings
3195
3196--disable_warnings
3197drop procedure if exists bug6029|
3198--enable_warnings
3199create procedure bug6029()
3200begin
3201  declare exit handler for 1136  select '1136';
3202  declare exit handler for sqlstate '23000'  select 'sqlstate 23000';
3203  declare continue handler for sqlexception  select 'sqlexception';
3204
3205  insert into t3 values (1);
3206  insert into t3 values (1,2);
3207end|
3208
3209create table t3 (s1 int, primary key (s1))|
3210insert into t3 values (1)|
3211call bug6029()|
3212delete from t3|
3213call bug6029()|
3214
3215drop procedure bug6029|
3216drop table t3|
3217
3218#
3219# BUG#8540: Local variable overrides an alias
3220#
3221--disable_warnings
3222drop procedure if exists bug8540|
3223--enable_warnings
3224
3225create procedure bug8540()
3226begin
3227  declare x int default 1;
3228  select x as y, x+0 as z;
3229end|
3230
3231call bug8540()|
3232drop procedure bug8540|
3233
3234#
3235# BUG#6642: Stored procedure crash if expression with set function
3236#
3237create table t3 (s1 int)|
3238
3239--disable_warnings
3240drop procedure if exists bug6642|
3241--enable_warnings
3242
3243create procedure bug6642()
3244  select abs(count(s1)) from t3|
3245
3246call bug6642()|
3247call bug6642()|
3248drop procedure bug6642|
3249
3250#
3251# BUG#7013: Stored procedure crash if group by ... with rollup
3252#
3253insert into t3 values (0),(1)|
3254--disable_warnings
3255drop procedure if exists bug7013|
3256--enable_warnings
3257create procedure bug7013()
3258  select s1,count(s1) from t3 group by s1 with rollup|
3259call bug7013()|
3260call bug7013()|
3261drop procedure bug7013|
3262
3263#
3264# BUG#7743: 'Lost connection to MySQL server during query' on Stored Procedure
3265#
3266--disable_warnings
3267drop table if exists t4|
3268--enable_warnings
3269create table t4 (
3270  a mediumint(8) unsigned not null auto_increment,
3271  b smallint(5) unsigned not null,
3272  c char(32) not null,
3273  primary key  (a)
3274) engine=myisam default charset=latin1|
3275insert into t4 values (1, 2, 'oneword')|
3276insert into t4 values (2, 2, 'anotherword')|
3277
3278--disable_warnings
3279drop procedure if exists bug7743|
3280--enable_warnings
3281create procedure bug7743 ( searchstring char(28) )
3282begin
3283  declare var mediumint(8) unsigned;
3284  select a into var from t4 where b = 2 and c = binary searchstring limit 1;
3285  select var;
3286end|
3287
3288call bug7743("oneword")|
3289call bug7743("OneWord")|
3290call bug7743("anotherword")|
3291call bug7743("AnotherWord")|
3292drop procedure bug7743|
3293drop table t4|
3294
3295#
3296# BUG#7992: SELECT .. INTO variable .. within Stored Procedure crashes
3297#           the server
3298#
3299delete from t3|
3300insert into t3 values(1)|
3301drop procedure if exists bug7992_1|
3302drop procedure if exists bug7992_2|
3303create procedure bug7992_1()
3304begin
3305  declare i int;
3306  select max(s1)+1 into i from t3;
3307end|
3308create procedure bug7992_2()
3309  insert into t3 (s1) select max(t4.s1)+1 from t3 as t4|
3310
3311call bug7992_1()|
3312call bug7992_1()|
3313call bug7992_2()|
3314call bug7992_2()|
3315
3316drop procedure bug7992_1|
3317drop procedure bug7992_2|
3318drop table t3|
3319
3320#
3321# BUG#8116: calling simple stored procedure twice in a row results
3322#           in server crash
3323#
3324create table t3 (  userid bigint(20) not null default 0 )|
3325
3326--disable_warnings
3327drop procedure if exists bug8116|
3328--enable_warnings
3329create procedure bug8116(in _userid int)
3330   select * from t3 where userid = _userid|
3331
3332call bug8116(42)|
3333call bug8116(42)|
3334drop procedure bug8116|
3335drop table t3|
3336
3337#
3338# BUG#6857: current_time() in STORED PROCEDURES
3339#
3340--disable_warnings
3341drop procedure if exists bug6857|
3342--enable_warnings
3343create procedure bug6857()
3344begin
3345  declare t0, t1 int;
3346  declare plus bool default 0;
3347  set t0 = unix_timestamp();
3348  select sleep(1.1);
3349  set t1 = unix_timestamp();
3350  if t1 > t0 then
3351    set plus = 1;
3352  end if;
3353  select plus;
3354end|
3355
3356call bug6857()|
3357
3358drop procedure bug6857|
3359
3360#
3361# BUG#8757: Stored Procedures: Scope of Begin and End Statements do not
3362#           work properly.
3363--disable_warnings
3364drop procedure if exists bug8757|
3365--enable_warnings
3366create procedure bug8757()
3367begin
3368  declare x int;
3369  declare c1 cursor for select data from t1 limit 1;
3370
3371  begin
3372    declare y int;
3373    declare c2 cursor for select i from t2 limit 1;
3374
3375    open c2;
3376    fetch c2 into y;
3377    close c2;
3378    select 2,y;
3379  end;
3380  open c1;
3381  fetch c1 into x;
3382  close c1;
3383  select 1,x;
3384end|
3385
3386delete from t1|
3387delete from t2|
3388insert into t1 values ("x", 1)|
3389insert into t2 values ("y", 2, 0.0)|
3390
3391call bug8757()|
3392
3393delete from t1|
3394delete from t2|
3395drop procedure bug8757|
3396
3397
3398#
3399# BUG#8762: Stored Procedures: Inconsistent behavior
3400#           of DROP PROCEDURE IF EXISTS statement.
3401--disable_warnings
3402drop procedure if exists bug8762|
3403--enable_warnings
3404# Doesn't exist
3405drop procedure if exists bug8762; create procedure bug8762() begin end|
3406# Does exist
3407drop procedure if exists bug8762; create procedure bug8762() begin end|
3408drop procedure bug8762|
3409
3410
3411#
3412# BUG#5240: Stored procedure crash if function has cursor declaration
3413#
3414--disable_warnings
3415drop function if exists bug5240|
3416--enable_warnings
3417create function bug5240 () returns int
3418begin
3419  declare x int;
3420  declare c cursor for select data from t1 limit 1;
3421
3422  open c;
3423  fetch c into x;
3424  close c;
3425  return x;
3426end|
3427
3428delete from t1|
3429insert into t1 values ("answer", 42)|
3430select id, bug5240() from t1|
3431drop function bug5240|
3432
3433#
3434# BUG#7992: rolling back temporary Item tree changes in SP
3435#
3436--disable_warnings
3437drop procedure if exists p1|
3438--enable_warnings
3439create table t3(id int)|
3440insert into t3 values(1)|
3441create procedure bug7992()
3442begin
3443  declare i int;
3444  select max(id)+1 into i from t3;
3445end|
3446
3447call bug7992()|
3448call bug7992()|
3449drop procedure bug7992|
3450drop table t3|
3451delimiter ;|
3452
3453#
3454# BUG#8849: problem with insert statement with table alias's
3455#
3456# Rolling back changes to AND/OR structure of ON and WHERE clauses  in SP
3457#
3458
3459delimiter |;
3460create table t3 (
3461  lpitnumber int(11) default null,
3462  lrecordtype int(11) default null
3463)|
3464
3465create table t4 (
3466  lbsiid int(11) not null default '0',
3467  ltradingmodeid int(11) not null default '0',
3468  ltradingareaid int(11) not null default '0',
3469  csellingprice decimal(19,4) default null,
3470  primary key  (lbsiid,ltradingmodeid,ltradingareaid)
3471)|
3472
3473create table t5 (
3474  lbsiid int(11) not null default '0',
3475  ltradingareaid int(11) not null default '0',
3476  primary key  (lbsiid,ltradingareaid)
3477)|
3478
3479--disable_warnings
3480drop procedure if exists bug8849|
3481--enable_warnings
3482create procedure bug8849()
3483begin
3484  insert into t5
3485  (
3486   t5.lbsiid,
3487   t5.ltradingareaid
3488  )
3489  select distinct t3.lpitnumber, t4.ltradingareaid
3490  from
3491    t4 join t3 on
3492      t3.lpitnumber = t4.lbsiid
3493      and t3.lrecordtype = 1
3494    left join t4 as price01 on
3495      price01.lbsiid = t4.lbsiid and
3496      price01.ltradingmodeid = 1 and
3497      t4.ltradingareaid = price01.ltradingareaid;
3498end|
3499
3500call bug8849()|
3501call bug8849()|
3502call bug8849()|
3503drop procedure bug8849|
3504drop tables t3,t4,t5|
3505
3506#
3507# BUG#8937: Stored Procedure: AVG() works as SUM() in SELECT ... INTO statement
3508#
3509--disable_warnings
3510drop procedure if exists bug8937|
3511--enable_warnings
3512create procedure bug8937()
3513begin
3514  declare s,x,y,z int;
3515  declare a float;
3516
3517  select sum(data),avg(data),min(data),max(data) into s,x,y,z from t1;
3518  select s,x,y,z;
3519  select avg(data) into a from t1;
3520  select a;
3521end|
3522
3523delete from t1|
3524insert into t1 (data) values (1), (2), (3), (4), (6)|
3525call bug8937()|
3526
3527drop procedure bug8937|
3528delete from t1|
3529
3530
3531#
3532# BUG#6900: Stored procedure inner handler ignored
3533# BUG#9074: STORED PROC: The scope of every handler declared is not
3534#                        properly applied
3535#
3536--disable_warnings
3537drop procedure if exists bug6900|
3538drop procedure if exists bug9074|
3539drop procedure if exists bug6900_9074|
3540--enable_warnings
3541
3542create table t3 (w char unique, x char)|
3543insert into t3 values ('a', 'b')|
3544
3545create procedure bug6900()
3546begin
3547  declare exit handler for sqlexception select '1';
3548
3549  begin
3550    declare exit handler for sqlexception select '2';
3551
3552    insert into t3 values ('x', 'y', 'z');
3553  end;
3554end|
3555
3556create procedure bug9074()
3557begin
3558  declare x1, x2, x3, x4, x5, x6 int default 0;
3559
3560  begin
3561    declare continue handler for sqlstate '23000' set x5 = 1;
3562
3563    insert into t3 values ('a', 'b');
3564    set x6 = 1;
3565  end;
3566
3567 begin1_label:
3568  begin
3569    declare continue handler for sqlstate '23000' set x1 = 1;
3570
3571    insert into t3 values ('a', 'b');
3572    set x2 = 1;
3573
3574   begin2_label:
3575    begin
3576      declare exit handler for sqlstate '23000' set x3 = 1;
3577
3578      set x4= 1;
3579      insert into t3 values ('a','b');
3580      set x4= 0;
3581    end begin2_label;
3582  end begin1_label;
3583
3584  select x1, x2, x3, x4, x5, x6;
3585end|
3586
3587create procedure bug6900_9074(z int)
3588begin
3589  declare exit handler for sqlstate '23000' select '23000';
3590
3591  begin
3592    declare exit handler for sqlexception select 'sqlexception';
3593
3594    if z = 1 then
3595      insert into t3 values ('a', 'b');
3596    else
3597      insert into t3 values ('x', 'y', 'z');
3598    end if;
3599  end;
3600end|
3601
3602call bug6900()|
3603call bug9074()|
3604call bug6900_9074(0)|
3605call bug6900_9074(1)|
3606
3607drop procedure bug6900|
3608drop procedure bug9074|
3609drop procedure bug6900_9074|
3610drop table t3|
3611
3612
3613#
3614# BUG#7185: Stored procedure crash if identifier is AVG
3615#
3616--disable_warnings
3617drop procedure if exists avg|
3618--enable_warnings
3619create procedure avg ()
3620begin
3621end|
3622
3623call avg ()|
3624drop procedure avg|
3625
3626
3627#
3628# BUG#6129: Stored procedure won't display @@sql_mode value
3629#
3630--disable_warnings
3631drop procedure if exists bug6129|
3632--enable_warnings
3633set @old_mode= @@sql_mode;
3634set @@sql_mode= "ERROR_FOR_DIVISION_BY_ZERO";
3635create procedure bug6129()
3636  select @@sql_mode|
3637call bug6129()|
3638set @@sql_mode= "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO"|
3639call bug6129()|
3640set @@sql_mode= "NO_ZERO_IN_DATE"|
3641call bug6129()|
3642set @@sql_mode=@old_mode;
3643
3644drop procedure bug6129|
3645
3646
3647#
3648# BUG#9856: Stored procedures: crash if handler for sqlexception, not found
3649#
3650--disable_warnings
3651drop procedure if exists bug9856|
3652--enable_warnings
3653create procedure bug9856()
3654begin
3655  declare v int;
3656  declare c cursor for select data from t1;
3657  declare exit handler for sqlexception, not found select '16';
3658
3659  open c;
3660  fetch c into v;
3661  select v;
3662end|
3663
3664delete from t1|
3665call bug9856()|
3666call bug9856()|
3667drop procedure bug9856|
3668
3669
3670#
3671# BUG##9674: Stored Procs: Using declared vars in algebric operation causes
3672#            system crash.
3673#
3674--disable_warnings
3675drop procedure if exists bug9674_1|
3676drop procedure if exists bug9674_2|
3677--enable_warnings
3678create procedure bug9674_1(out arg int)
3679begin
3680  declare temp_in1 int default 0;
3681  declare temp_fl1 int default 0;
3682
3683  set temp_in1 = 100;
3684  set temp_fl1 = temp_in1/10;
3685  set arg = temp_fl1;
3686end|
3687
3688create procedure bug9674_2()
3689begin
3690  declare v int default 100;
3691
3692  select v/10;
3693end|
3694
3695call bug9674_1(@sptmp)|
3696call bug9674_1(@sptmp)|
3697select @sptmp|
3698call bug9674_2()|
3699call bug9674_2()|
3700drop procedure bug9674_1|
3701drop procedure bug9674_2|
3702
3703
3704#
3705# BUG#9598: stored procedure call within stored procedure overwrites IN variable
3706#
3707--disable_warnings
3708drop procedure if exists bug9598_1|
3709drop procedure if exists bug9598_2|
3710--enable_warnings
3711create procedure bug9598_1(in var_1 char(16),
3712                           out var_2 integer, out var_3 integer)
3713begin
3714  set var_2 = 50;
3715  set var_3 = 60;
3716end|
3717
3718create procedure bug9598_2(in v1 char(16),
3719                           in v2 integer,
3720                           in v3 integer,
3721                           in v4 integer,
3722                           in v5 integer)
3723begin
3724  select v1,v2,v3,v4,v5;
3725  call bug9598_1(v1,@tmp1,@tmp2);
3726  select v1,v2,v3,v4,v5;
3727end|
3728
3729call bug9598_2('Test',2,3,4,5)|
3730select @tmp1, @tmp2|
3731
3732drop procedure bug9598_1|
3733drop procedure bug9598_2|
3734
3735
3736#
3737# BUG#9902: Crash with simple stored function using user defined variables
3738#
3739--disable_warnings
3740drop procedure if exists bug9902|
3741--enable_warnings
3742create function bug9902() returns int(11)
3743begin
3744  set @x = @x + 1;
3745  return @x;
3746end|
3747
3748--disable_query_log
3749--echo # Set query cache size, if we have query cache
3750if (`select @@have_query_cache='YES'`) {
3751  set @qcs1 = @@query_cache_size|
3752  set global query_cache_size = 102400|
3753}
3754--enable_query_log
3755set @x = 1|
3756insert into t1 values ("qc", 42)|
3757select bug9902() from t1|
3758select bug9902() from t1|
3759select @x|
3760
3761--echo # Restore the old query cache size
3762--disable_query_log
3763if (`select @@have_query_cache='YES'`) {
3764  set global query_cache_size = @qcs1|
3765}
3766--enable_query_log
3767delete from t1|
3768drop function bug9902|
3769
3770
3771#
3772# BUG#9102: Stored proccedures: function which returns blob causes crash
3773#
3774--disable_warnings
3775drop function if exists bug9102|
3776--enable_warnings
3777create function bug9102() returns blob return 'a'|
3778select bug9102()|
3779drop function bug9102|
3780
3781
3782#
3783# BUG#7648: Stored procedure crash when invoking a function that returns a bit
3784#
3785--disable_warnings
3786drop function if exists bug7648|
3787--enable_warnings
3788create function bug7648() returns bit(8) return 'a'|
3789select bug7648()|
3790drop function bug7648|
3791
3792
3793#
3794# BUG#9775: crash if create function that returns enum or set
3795#
3796--disable_warnings
3797drop function if exists bug9775|
3798--enable_warnings
3799create function bug9775(v1 char(1)) returns enum('a','b') return v1|
3800select bug9775('a'),bug9775('b'),bug9775('c')|
3801drop function bug9775|
3802create function bug9775(v1 int) returns enum('a','b') return v1|
3803select bug9775(1),bug9775(2),bug9775(3)|
3804drop function bug9775|
3805
3806create function bug9775(v1 char(1)) returns set('a','b') return v1|
3807select bug9775('a'),bug9775('b'),bug9775('a,b'),bug9775('c')|
3808drop function bug9775|
3809create function bug9775(v1 int) returns set('a','b') return v1|
3810select bug9775(1),bug9775(2),bug9775(3),bug9775(4)|
3811drop function bug9775|
3812
3813
3814#
3815# BUG#8861: If Return is a YEAR data type, value is not shown in year format
3816#
3817--disable_warnings
3818drop function if exists bug8861|
3819--enable_warnings
3820create function bug8861(v1 int) returns year return v1|
3821select bug8861(05)|
3822set @x = bug8861(05)|
3823select @x|
3824drop function bug8861|
3825
3826
3827#
3828# BUG#9004: Inconsistent behaviour of SP re. warnings
3829#
3830--disable_warnings
3831drop procedure if exists bug9004_1|
3832drop procedure if exists bug9004_2|
3833--enable_warnings
3834create procedure bug9004_1(x char(16))
3835begin
3836  insert into t1 values (x, 42);
3837  insert into t1 values (x, 17);
3838end|
3839create procedure bug9004_2(x char(16))
3840  call bug9004_1(x)|
3841
3842# Truncation warnings expected...
3843call bug9004_1('12345678901234567')|
3844call bug9004_2('12345678901234567890')|
3845
3846delete from t1|
3847drop procedure bug9004_1|
3848drop procedure bug9004_2|
3849
3850#
3851# BUG#7293: Stored procedure crash with soundex
3852#
3853--disable_warnings
3854drop procedure if exists bug7293|
3855--enable_warnings
3856insert into t1 values ('secret', 0)|
3857create procedure bug7293(p1 varchar(100))
3858begin
3859  if exists (select id from t1 where soundex(p1)=soundex(id)) then
3860    select 'yes';
3861  end if;
3862end;|
3863call bug7293('secret')|
3864call bug7293 ('secrete')|
3865drop procedure bug7293|
3866delete from t1|
3867
3868
3869#
3870# BUG#9841: Unexpected read lock when trying to update a view in a
3871#           stored procedure
3872#
3873--disable_warnings
3874drop procedure if exists bug9841|
3875drop view if exists v1|
3876--enable_warnings
3877
3878create view v1 as select * from t1, t2 where id = s|
3879create procedure bug9841 ()
3880  update v1 set data = 10|
3881call bug9841()|
3882
3883drop view v1|
3884drop procedure bug9841|
3885
3886
3887#
3888# BUG#5963 subqueries in SET/IF
3889#
3890--disable_warnings
3891drop procedure if exists bug5963|
3892--enable_warnings
3893
3894create procedure bug5963_1 () begin declare v int; set v = (select s1 from t3); select v; end;|
3895create table t3 (s1 int)|
3896insert into t3 values (5)|
3897call bug5963_1()|
3898call bug5963_1()|
3899drop procedure bug5963_1|
3900drop table t3|
3901
3902create procedure bug5963_2 (cfk_value int)
3903begin
3904  if cfk_value in (select cpk from t3) then
3905    set @x = 5;
3906  end if;
3907  end;
3908|
3909create table t3 (cpk int)|
3910insert into t3 values (1)|
3911call bug5963_2(1)|
3912call bug5963_2(1)|
3913drop procedure bug5963_2|
3914drop table t3|
3915
3916
3917#
3918# BUG#9559: Functions: Numeric Operations using -ve value gives incorrect
3919#           results.
3920#
3921--disable_warnings
3922drop function if exists bug9559|
3923--enable_warnings
3924create function bug9559()
3925  returns int
3926begin
3927  set @y = -6/2;
3928  return @y;
3929end|
3930
3931select bug9559()|
3932
3933drop function bug9559|
3934
3935
3936#
3937# BUG#10961: Stored procedures: crash if select * from dual
3938#
3939--disable_warnings
3940drop procedure if exists bug10961|
3941--enable_warnings
3942# "select * from dual" results in an error, so the cursor will not open
3943create procedure bug10961()
3944begin
3945  declare v char;
3946  declare x int;
3947  declare c cursor for select * from dual;
3948  declare continue handler for sqlexception select x;
3949
3950  set x = 1;
3951  open c;
3952  set x = 2;
3953  fetch c into v;
3954  set x = 3;
3955  close c;
3956end|
3957
3958call bug10961()|
3959call bug10961()|
3960
3961drop procedure bug10961|
3962
3963#
3964# BUG #6866: Second call of a stored procedure using a view with on expressions
3965#
3966
3967--disable_warnings
3968DROP PROCEDURE IF EXISTS bug6866|
3969--enable_warnings
3970
3971DROP VIEW IF EXISTS tv|
3972DROP TABLE IF EXISTS tt1,tt2,tt3|
3973
3974CREATE TABLE tt1 (a1 int, a2 int, a3 int, data varchar(10))|
3975CREATE TABLE tt2 (a2 int, data2 varchar(10))|
3976CREATE TABLE tt3 (a3 int, data3 varchar(10))|
3977
3978INSERT INTO tt1 VALUES (1, 1, 4, 'xx')|
3979
3980INSERT INTO tt2 VALUES (1, 'a')|
3981INSERT INTO tt2 VALUES (2, 'b')|
3982INSERT INTO tt2 VALUES (3, 'c')|
3983
3984INSERT INTO tt3 VALUES (4, 'd')|
3985INSERT INTO tt3 VALUES (5, 'e')|
3986INSERT INTO tt3 VALUES (6, 'f')|
3987
3988CREATE VIEW tv AS
3989SELECT tt1.*, tt2.data2, tt3.data3
3990  FROM tt1 INNER JOIN tt2 ON tt1.a2 = tt2.a2
3991         LEFT JOIN tt3 ON tt1.a3 = tt3.a3
3992    ORDER BY tt1.a1, tt2.a2, tt3.a3|
3993
3994CREATE PROCEDURE bug6866 (_a1 int)
3995BEGIN
3996SELECT * FROM tv WHERE a1 = _a1;
3997END|
3998
3999CALL bug6866(1)|
4000CALL bug6866(1)|
4001CALL bug6866(1)|
4002
4003DROP PROCEDURE bug6866;
4004
4005DROP VIEW tv|
4006DROP TABLE tt1, tt2, tt3|
4007
4008#
4009# BUG#10136: items cleunup
4010#
4011--disable_warnings
4012DROP PROCEDURE IF EXISTS bug10136|
4013--enable_warnings
4014create table t3 ( name char(5) not null primary key, val float not null)|
4015insert into t3 values ('aaaaa', 1), ('bbbbb', 2), ('ccccc', 3)|
4016create procedure bug10136()
4017begin
4018  declare done int default 3;
4019
4020  repeat
4021    select * from t3;
4022    set done = done - 1;
4023  until done <= 0 end repeat;
4024
4025end|
4026call bug10136()|
4027call bug10136()|
4028call bug10136()|
4029drop procedure bug10136|
4030drop table t3|
4031
4032#
4033# BUG#11529: crash server after use stored procedure
4034#
4035--disable_warnings
4036drop procedure if exists bug11529|
4037--enable_warnings
4038create procedure bug11529()
4039begin
4040  declare c cursor for select id, data from t1 where data in (10,13);
4041
4042  open c;
4043  begin
4044    declare vid char(16);
4045    declare vdata int;
4046    declare exit handler for not found begin end;
4047
4048    while true do
4049      fetch c into vid, vdata;
4050    end while;
4051  end;
4052  close c;
4053end|
4054
4055insert into t1 values
4056  ('Name1', 10),
4057  ('Name2', 11),
4058  ('Name3', 12),
4059  ('Name4', 13),
4060  ('Name5', 14)|
4061
4062call bug11529()|
4063call bug11529()|
4064delete from t1|
4065drop procedure bug11529|
4066
4067
4068#
4069# BUG#6063: Stored procedure labels are subject to restrictions (partial)
4070# BUG#7088: Stored procedures: labels won't work if character set is utf8
4071#
4072
4073set character set utf8|
4074
4075--disable_warnings
4076drop procedure if exists bug6063|
4077drop procedure if exists bug7088_1|
4078drop procedure if exists bug7088_2|
4079--enable_warnings
4080
4081create procedure bug6063()
4082begin
4083  lâbel: begin end;
4084  label: begin end;
4085  label1: begin end;
4086end|
4087
4088create procedure bug7088_1()
4089  label1: begin end label1|
4090
4091create procedure bug7088_2()
4092  läbel1: begin end|
4093
4094call bug6063()|
4095call bug7088_1()|
4096call bug7088_2()|
4097
4098set @@character_set_client=@save_character_set_client|
4099set @@character_set_results=@save_character_set_client|
4100
4101show create procedure bug6063|
4102show create procedure bug7088_1|
4103show create procedure bug7088_2|
4104
4105drop procedure bug6063|
4106drop procedure bug7088_1|
4107drop procedure bug7088_2|
4108
4109#
4110# BUG#9565: "Wrong locking in stored procedure if a sub-sequent procedure
4111#           is called".
4112#
4113--disable_warnings
4114drop procedure if exists bug9565_sub|
4115drop procedure if exists bug9565|
4116--enable_warnings
4117create procedure bug9565_sub()
4118begin
4119  select * from t1;
4120end|
4121create procedure bug9565()
4122begin
4123  insert into t1 values ("one", 1);
4124  call bug9565_sub();
4125end|
4126call bug9565()|
4127delete from t1|
4128drop procedure bug9565_sub|
4129drop procedure bug9565|
4130
4131
4132#
4133# BUG#9538: SProc: Creation fails if we try to SET system variable
4134#           using @@var_name in proc
4135#
4136--disable_warnings
4137drop procedure if exists bug9538|
4138--enable_warnings
4139create procedure bug9538()
4140  set @@sort_buffer_size = 1000000|
4141
4142set @x = @@sort_buffer_size|
4143set @@sort_buffer_size = 2000000|
4144select @@sort_buffer_size|
4145call bug9538()|
4146select @@sort_buffer_size|
4147set @@sort_buffer_size = @x|
4148
4149drop procedure bug9538|
4150
4151
4152#
4153# BUG#8692: Cursor fetch of empty string
4154#
4155--disable_warnings
4156drop procedure if exists bug8692|
4157--enable_warnings
4158create table t3 (c1 varchar(5), c2 char(5), c3 enum('one','two'), c4 text, c5 blob, c6 char(5), c7 varchar(5))|
4159insert into t3 values ('', '', '', '', '', '', NULL)|
4160
4161create procedure bug8692()
4162begin
4163    declare v1 VARCHAR(10);
4164    declare v2 VARCHAR(10);
4165    declare v3 VARCHAR(10);
4166    declare v4 VARCHAR(10);
4167    declare v5 VARCHAR(10);
4168    declare v6 VARCHAR(10);
4169    declare v7 VARCHAR(10);
4170    declare c8692 cursor for select c1,c2,c3,c4,c5,c6,c7 from t3;
4171    open c8692;
4172    fetch c8692 into v1,v2,v3,v4,v5,v6,v7;
4173    select v1, v2, v3, v4, v5, v6, v7;
4174end|
4175
4176call bug8692()|
4177drop procedure bug8692|
4178drop table t3|
4179
4180#
4181# Bug#10055 "Using stored function with information_schema causes empty
4182#            result set"
4183#
4184--disable_warnings
4185drop function if exists bug10055|
4186--enable_warnings
4187create function bug10055(v char(255)) returns char(255) return lower(v)|
4188# This select should not crash server and should return all fields in t1
4189select t.column_name, bug10055(t.column_name)
4190from information_schema.columns as t
4191where t.table_schema = 'test' and t.table_name = 't1'|
4192drop function bug10055|
4193
4194#
4195# Bug #12297 "SP crashes the server if data inserted inside a lon loop"
4196# The test for memleak bug, so actually there is no way to test it
4197# from the suite. The test below could be used to check SP memory
4198# consumption by passing large input parameter.
4199#
4200
4201--disable_warnings
4202drop procedure if exists bug12297|
4203--enable_warnings
4204
4205create procedure bug12297(lim int)
4206begin
4207  set @x = 0;
4208  repeat
4209    insert into t1(id,data)
4210    values('aa', @x);
4211    set @x = @x + 1;
4212  until @x >= lim
4213  end repeat;
4214end|
4215
4216call bug12297(10)|
4217drop procedure bug12297|
4218
4219#
4220# Bug #11247 "Stored procedures: Function calls in long loops leak memory"
4221# One more memleak bug test. One could use this test to check that the memory
4222# isn't leaking by increasing the input value for p_bug11247.
4223#
4224
4225--disable_warnings
4226drop function if exists f_bug11247|
4227drop procedure if exists p_bug11247|
4228--enable_warnings
4229
4230create function f_bug11247(param int)
4231  returns int
4232return param + 1|
4233
4234create procedure p_bug11247(lim int)
4235begin
4236  declare v int default 0;
4237
4238  while v < lim do
4239    set v= f_bug11247(v);
4240  end while;
4241end|
4242
4243call p_bug11247(10)|
4244drop function f_bug11247|
4245drop procedure p_bug11247|
4246#
4247# BUG#12168: "'DECLARE CONTINUE HANDLER FOR NOT FOUND ...' in conditional
4248# handled incorrectly"
4249#
4250--disable_warnings
4251drop procedure if exists bug12168|
4252drop table if exists t3, t4|
4253--enable_warnings
4254
4255create table t3 (a int)|
4256insert into t3 values (1),(2),(3),(4)|
4257
4258create table t4 (a int)|
4259
4260create procedure bug12168(arg1 char(1))
4261begin
4262  declare b, c integer;
4263  if arg1 = 'a' then
4264    begin
4265      declare c1 cursor for select a from t3 where a % 2;
4266      declare continue handler for not found set b = 1;
4267      set b = 0;
4268      open c1;
4269      c1_repeat: repeat
4270        fetch c1 into c;
4271        if (b = 1) then
4272          leave c1_repeat;
4273        end if;
4274
4275        insert into t4 values (c);
4276        until b = 1
4277      end repeat;
4278    end;
4279  end if;
4280  if arg1 = 'b' then
4281    begin
4282      declare c2 cursor for select a from t3 where not a % 2;
4283      declare continue handler for not found set b = 1;
4284      set b = 0;
4285      open c2;
4286      c2_repeat: repeat
4287        fetch c2 into c;
4288        if (b = 1) then
4289          leave c2_repeat;
4290        end if;
4291
4292        insert into t4 values (c);
4293        until b = 1
4294      end repeat;
4295    end;
4296  end if;
4297end|
4298
4299call bug12168('a')|
4300select * from t4|
4301truncate t4|
4302call bug12168('b')|
4303select * from t4|
4304truncate t4|
4305call bug12168('a')|
4306select * from t4|
4307truncate t4|
4308call bug12168('b')|
4309select * from t4|
4310truncate t4|
4311drop table t3, t4|
4312drop procedure if exists bug12168|
4313
4314#
4315# Bug #11333 "Stored Procedure: Memory blow up on repeated SELECT ... INTO
4316# query"
4317# One more memleak bug. Use the test to check memory consumption.
4318#
4319
4320--disable_warnings
4321drop table if exists t3|
4322drop procedure if exists bug11333|
4323--enable_warnings
4324
4325create table t3 (c1 char(128))|
4326
4327insert into t3 values
4328  ('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')|
4329
4330
4331create procedure bug11333(i int)
4332begin
4333    declare tmp varchar(128);
4334    set @x = 0;
4335    repeat
4336        select c1 into tmp from t3
4337          where c1 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
4338        set @x = @x + 1;
4339        until @x >= i
4340    end repeat;
4341end|
4342
4343call bug11333(10)|
4344
4345drop procedure bug11333|
4346drop table t3|
4347
4348#
4349# BUG#9048: Creating a function with char binary IN parameter fails
4350#
4351--disable_warnings
4352drop function if exists bug9048|
4353--enable_warnings
4354create function bug9048(f1 char binary) returns char
4355begin
4356  set f1= concat( 'hello', f1 );
4357  return f1;
4358end|
4359drop function bug9048|
4360
4361create function bug9048(f1 char binary) returns char binary
4362begin
4363  set f1= concat( 'hello', f1 );
4364  return f1;
4365end|
4366select collation(bug9048("foo"))|
4367drop function bug9048|
4368
4369# Bug #12849 Stored Procedure: Crash on procedure call with CHAR type
4370# 'INOUT' parameter
4371#
4372
4373--disable_warnings
4374drop procedure if exists bug12849_1|
4375--enable_warnings
4376create procedure bug12849_1(inout x char) select x into x|
4377set @var='a'|
4378call bug12849_1(@var)|
4379select @var|
4380drop procedure bug12849_1|
4381
4382--disable_warnings
4383drop procedure if exists bug12849_2|
4384--enable_warnings
4385create procedure bug12849_2(inout foo varchar(15))
4386begin
4387select concat(foo, foo) INTO foo;
4388end|
4389set @var='abcd'|
4390call bug12849_2(@var)|
4391select @var|
4392drop procedure bug12849_2|
4393
4394#
4395# BUG#13133: Local variables in stored procedures are not initialized correctly.
4396#
4397--disable_warnings
4398drop procedure if exists bug131333|
4399drop function if exists bug131333|
4400--enable_warnings
4401create procedure bug131333()
4402begin
4403  begin
4404    declare a int;
4405
4406    select a;
4407    set a = 1;
4408    select a;
4409  end;
4410  begin
4411    declare b int;
4412
4413    select b;
4414  end;
4415end|
4416
4417create function bug131333()
4418  returns int
4419begin
4420  begin
4421    declare a int;
4422
4423    set a = 1;
4424  end;
4425  begin
4426    declare b int;
4427
4428    return b;
4429  end;
4430end|
4431
4432call bug131333()|
4433select bug131333()|
4434
4435drop procedure bug131333|
4436drop function bug131333|
4437
4438#
4439# BUG#12379: PROCEDURE with HANDLER calling FUNCTION with error get
4440#            strange result
4441#
4442--disable_warnings
4443drop function if exists bug12379|
4444drop procedure if exists bug12379_1|
4445drop procedure if exists bug12379_2|
4446drop procedure if exists bug12379_3|
4447drop table if exists t3|
4448--enable_warnings
4449
4450create table t3 (c1 char(1) primary key not null)|
4451
4452create function bug12379()
4453  returns integer
4454begin
4455   insert into t3 values('X');
4456   insert into t3 values('X');
4457   return 0;
4458end|
4459
4460create procedure bug12379_1()
4461begin
4462   declare exit handler for sqlexception select 42;
4463
4464   select bug12379();
4465END|
4466create procedure bug12379_2()
4467begin
4468   declare exit handler for sqlexception begin end;
4469
4470   select bug12379();
4471end|
4472create procedure bug12379_3()
4473begin
4474   select bug12379();
4475end|
4476
4477--error ER_DUP_ENTRY
4478select bug12379()|
4479select 1|
4480# statement-based binlogging will show warning which row-based won't;
4481# so we hide it (this warning is already tested in rpl_stm_sp.test)
4482--disable_warnings
4483call bug12379_1()|
4484select 2|
4485call bug12379_2()|
4486--enable_warnings
4487select 3|
4488--error ER_DUP_ENTRY
4489call bug12379_3()|
4490select 4|
4491
4492drop function bug12379|
4493drop procedure bug12379_1|
4494drop procedure bug12379_2|
4495drop procedure bug12379_3|
4496drop table t3|
4497
4498#
4499# Bug #13124    Stored Procedure using SELECT INTO crashes server
4500#
4501
4502--disable_warnings
4503drop procedure if exists bug13124|
4504--enable_warnings
4505create procedure bug13124()
4506begin
4507  declare y integer;
4508  set @x=y;
4509end|
4510call bug13124()|
4511drop procedure  bug13124|
4512
4513#
4514# Bug #12979  Stored procedures: crash if inout decimal parameter
4515#
4516
4517# check NULL inout parameters processing
4518
4519--disable_warnings
4520drop procedure if exists bug12979_1|
4521--enable_warnings
4522create procedure bug12979_1(inout d decimal(5)) set d = d / 2|
4523set @bug12979_user_var = NULL|
4524call bug12979_1(@bug12979_user_var)|
4525drop procedure bug12979_1|
4526
4527# check NULL local variables processing
4528
4529--disable_warnings
4530drop procedure if exists bug12979_2|
4531--enable_warnings
4532create procedure bug12979_2()
4533begin
4534declare internal_var decimal(5);
4535set internal_var= internal_var / 2;
4536select internal_var;
4537end|
4538call bug12979_2()|
4539drop procedure bug12979_2|
4540
4541
4542#
4543# BUG#6127: Stored procedure handlers within handlers don't work
4544#
4545--disable_warnings
4546drop table if exists t3|
4547drop procedure if exists bug6127|
4548--enable_warnings
4549create table t3 (s1 int unique)|
4550
4551set @sm=@@sql_mode|
4552set sql_mode='traditional'|
4553
4554create procedure bug6127()
4555begin
4556  declare continue handler for sqlstate '23000'
4557    begin
4558      declare continue handler for sqlstate '22003'
4559        insert into t3 values (0);
4560
4561      insert into t3 values (1000000000000000);
4562    end;
4563
4564  insert into t3 values (1);
4565  insert into t3 values (1);
4566end|
4567
4568call bug6127()|
4569select * from t3|
4570--error ER_DUP_ENTRY
4571call bug6127()|
4572select * from t3|
4573set sql_mode=@sm|
4574drop table t3|
4575drop procedure bug6127|
4576
4577
4578#
4579# BUG#12589: Assert when creating temp. table from decimal stored procedure
4580#            variable
4581#
4582--disable_warnings
4583drop procedure if exists bug12589_1|
4584drop procedure if exists bug12589_2|
4585drop procedure if exists bug12589_3|
4586--enable_warnings
4587create procedure bug12589_1()
4588begin
4589  declare spv1 decimal(3,3);
4590  set spv1= 123.456;
4591
4592  set spv1 = 'test';
4593  create temporary table tm1 as select spv1;
4594  show create table tm1;
4595  drop temporary table tm1;
4596end|
4597
4598create procedure bug12589_2()
4599begin
4600  declare spv1 decimal(6,3);
4601  set spv1= 123.456;
4602
4603  create temporary table tm1 as select spv1;
4604  show create table tm1;
4605  drop temporary table tm1;
4606end|
4607
4608create procedure bug12589_3()
4609begin
4610  declare spv1 decimal(6,3);
4611  set spv1= -123.456;
4612
4613  create temporary table tm1 as select spv1;
4614  show create table tm1;
4615  drop temporary table tm1;
4616end|
4617
4618# Note: The type of the field will match the value, not the declared
4619#       type of the variable. (This is a type checking issue which
4620#       might be changed later.)
4621
4622# Warning expected from "set spv1 = 'test'", the value is set to decimal "0".
4623call bug12589_1()|
4624# No warnings here
4625call bug12589_2()|
4626call bug12589_3()|
4627drop procedure bug12589_1|
4628drop procedure bug12589_2|
4629drop procedure bug12589_3|
4630
4631#
4632# BUG#7049: Stored procedure CALL errors are ignored
4633#
4634--disable_warnings
4635drop table if exists t3|
4636drop procedure if exists bug7049_1|
4637drop procedure if exists bug7049_2|
4638drop procedure if exists bug7049_3|
4639drop procedure if exists bug7049_4|
4640drop function if exists bug7049_1|
4641drop function if exists bug7049_2|
4642--enable_warnings
4643
4644create table t3 ( x int unique )|
4645
4646create procedure bug7049_1()
4647begin
4648  insert into t3 values (42);
4649  insert into t3 values (42);
4650end|
4651
4652create procedure bug7049_2()
4653begin
4654  declare exit handler for sqlexception
4655    select 'Caught it' as 'Result';
4656
4657  call bug7049_1();
4658  select 'Missed it' as 'Result';
4659end|
4660
4661create procedure bug7049_3()
4662  call bug7049_1()|
4663
4664create procedure bug7049_4()
4665begin
4666  declare exit handler for sqlexception
4667    select 'Caught it' as 'Result';
4668
4669  call bug7049_3();
4670  select 'Missed it' as 'Result';
4671end|
4672
4673create function bug7049_1()
4674  returns int
4675begin
4676  insert into t3 values (42);
4677  insert into t3 values (42);
4678  return 42;
4679end|
4680
4681create function bug7049_2()
4682  returns int
4683begin
4684  declare x int default 0;
4685  declare continue handler for sqlexception
4686    set x = 1;
4687
4688  set x = bug7049_1();
4689  return x;
4690end|
4691
4692call bug7049_2()|
4693select * from t3|
4694delete from t3|
4695call bug7049_4()|
4696select * from t3|
4697select bug7049_2()|
4698
4699drop table t3|
4700drop procedure bug7049_1|
4701drop procedure bug7049_2|
4702drop procedure bug7049_3|
4703drop procedure bug7049_4|
4704drop function bug7049_1|
4705drop function bug7049_2|
4706
4707
4708#
4709# BUG#13941: replace() string fuction behaves badly inside stored procedure
4710# (BUG#13914: IFNULL is returning garbage in stored procedure)
4711#
4712--disable_warnings
4713drop function if exists bug13941|
4714drop procedure if exists bug13941|
4715--enable_warnings
4716
4717create function bug13941(p_input_str text)
4718  returns text
4719begin
4720  declare p_output_str text;
4721
4722  set p_output_str = p_input_str;
4723
4724  set p_output_str = replace(p_output_str, 'xyzzy', 'plugh');
4725  set p_output_str = replace(p_output_str, 'test', 'prova');
4726  set p_output_str = replace(p_output_str, 'this', 'questo');
4727  set p_output_str = replace(p_output_str, ' a ', 'una ');
4728  set p_output_str = replace(p_output_str, 'is', '');
4729
4730  return p_output_str;
4731end|
4732
4733create procedure bug13941(out sout varchar(128))
4734begin
4735  set sout = 'Local';
4736  set sout = ifnull(sout, 'DEF');
4737end|
4738
4739# Note: The bug showed different behaviour in different types of builds,
4740#  giving garbage results in some, and seemingly working in others.
4741#  Running with valgrind (or purify) is the safe way to check that it's
4742#  really working correctly.
4743select bug13941('this is a test')|
4744call bug13941(@a)|
4745select @a|
4746
4747drop function bug13941|
4748drop procedure bug13941|
4749
4750
4751#
4752# BUG#13095: Cannot create VIEWs in prepared statements
4753#
4754
4755delimiter ;|
4756
4757--disable_warnings
4758DROP PROCEDURE IF EXISTS bug13095;
4759DROP TABLE IF EXISTS bug13095_t1;
4760DROP VIEW IF EXISTS bug13095_v1;
4761--enable_warnings
4762
4763delimiter |;
4764
4765CREATE PROCEDURE bug13095(tbl_name varchar(32))
4766BEGIN
4767  SET @str =
4768    CONCAT("CREATE TABLE ", tbl_name, "(stuff char(15))");
4769  SELECT @str;
4770  PREPARE stmt FROM @str;
4771  EXECUTE stmt;
4772
4773  SET @str =
4774    CONCAT("INSERT INTO ", tbl_name, " VALUES('row1'),('row2'),('row3')" );
4775  SELECT @str;
4776  PREPARE stmt FROM @str;
4777  EXECUTE stmt;
4778
4779  SET @str =
4780    CONCAT("CREATE VIEW bug13095_v1(c1) AS SELECT stuff FROM ", tbl_name);
4781  SELECT @str;
4782  PREPARE stmt FROM @str;
4783  EXECUTE stmt;
4784
4785  SELECT * FROM bug13095_v1;
4786
4787  SET @str =
4788    "DROP VIEW bug13095_v1";
4789  SELECT @str;
4790  PREPARE stmt FROM @str;
4791  EXECUTE stmt;
4792END|
4793
4794delimiter ;|
4795
4796CALL bug13095('bug13095_t1');
4797
4798--disable_warnings
4799DROP PROCEDURE IF EXISTS bug13095;
4800DROP VIEW IF EXISTS bug13095_v1;
4801DROP TABLE IF EXISTS bug13095_t1;
4802--enable_warnings
4803
4804delimiter |;
4805
4806#
4807# BUG#1473: Dumping of stored functions seems to cause corruption in
4808#           the function body
4809#
4810--disable_warnings
4811drop function if exists bug14723|
4812drop procedure if exists bug14723|
4813--enable_warnings
4814
4815delimiter ;;|
4816/*!50003 create function bug14723()
4817 returns bigint(20)
4818main_loop: begin
4819  return 42;
4820end */;;
4821show create function bug14723;;
4822select bug14723();;
4823
4824/*!50003 create procedure bug14723()
4825main_loop: begin
4826  select 42;
4827end */;;
4828show create procedure bug14723;;
4829call bug14723();;
4830
4831delimiter |;;
4832
4833drop function bug14723|
4834drop procedure bug14723|
4835
4836#
4837# Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
4838# Check that when fetching from a cursor, COUNT(*) works properly.
4839#
4840create procedure bug14845()
4841begin
4842  declare a char(255);
4843  declare done int default 0;
4844  declare c cursor for select count(*) from t1 where 1 = 0;
4845  declare continue handler for sqlstate '02000' set done = 1;
4846  open c;
4847  repeat
4848    fetch c into a;
4849    if not done then
4850      select a;
4851    end if;
4852  until done end repeat;
4853  close c;
4854end|
4855call bug14845()|
4856drop procedure bug14845|
4857
4858#
4859# BUG#13549 "Server crash with nested stored procedures".
4860# Server should not crash when during execution of stored procedure
4861# we have to parse trigger/function definition and this new trigger/
4862# function has more local variables declared than invoking stored
4863# procedure and last of these variables is used in argument of NOT
4864# operator.
4865#
4866--disable_warnings
4867drop procedure if exists bug13549_1|
4868drop procedure if exists bug13549_2|
4869--enable_warnings
4870CREATE PROCEDURE `bug13549_2`()
4871begin
4872  call bug13549_1();
4873end|
4874CREATE PROCEDURE `bug13549_1`()
4875begin
4876  declare done int default 0;
4877  set done= not done;
4878end|
4879CALL bug13549_2()|
4880drop procedure bug13549_2|
4881drop procedure bug13549_1|
4882
4883#
4884# BUG#10100: function (and stored procedure?) recursivity problem
4885#
4886--disable_warnings
4887drop function if exists bug10100f|
4888drop procedure if exists bug10100p|
4889drop procedure if exists bug10100t|
4890drop procedure if exists bug10100pt|
4891drop procedure if exists bug10100pv|
4892drop procedure if exists bug10100pd|
4893drop procedure if exists bug10100pc|
4894--enable_warnings
4895# routines with simple recursion
4896create function bug10100f(prm int) returns int
4897begin
4898  if prm > 1 then
4899    return prm * bug10100f(prm - 1);
4900  end if;
4901  return 1;
4902end|
4903create procedure bug10100p(prm int, inout res int)
4904begin
4905  set res = res * prm;
4906  if prm > 1 then
4907    call bug10100p(prm - 1, res);
4908  end if;
4909end|
4910create procedure bug10100t(prm int)
4911begin
4912  declare res int;
4913  set res = 1;
4914  call bug10100p(prm, res);
4915  select res;
4916end|
4917
4918# a procedure which use tables and recursion
4919create table t3 (a int)|
4920insert into t3 values (0)|
4921create view v1 as select a from t3|
4922create procedure bug10100pt(level int, lim int)
4923begin
4924  if level < lim then
4925    update t3 set a=level;
4926    FLUSH TABLES;
4927    call bug10100pt(level+1, lim);
4928  else
4929    select * from t3;
4930  end if;
4931end|
4932# view & recursion
4933create procedure bug10100pv(level int, lim int)
4934begin
4935  if level < lim then
4936    update v1 set a=level;
4937    FLUSH TABLES;
4938    call bug10100pv(level+1, lim);
4939  else
4940    select * from v1;
4941  end if;
4942end|
4943# dynamic sql & recursion
4944prepare stmt2 from "select * from t3;"|
4945create procedure bug10100pd(level int, lim int)
4946begin
4947  if level < lim then
4948    select level;
4949    prepare stmt1 from "update t3 set a=a+2";
4950    execute stmt1;
4951    FLUSH TABLES;
4952    execute stmt1;
4953    FLUSH TABLES;
4954    execute stmt1;
4955    FLUSH TABLES;
4956    deallocate prepare stmt1;
4957    execute stmt2;
4958    select * from t3;
4959    call bug10100pd(level+1, lim);
4960  else
4961    execute stmt2;
4962  end if;
4963end|
4964# cursor & recursion
4965create procedure bug10100pc(level int, lim int)
4966begin
4967  declare lv int;
4968  declare c cursor for select a from t3;
4969  open c;
4970  if level < lim then
4971    select level;
4972    fetch c into lv;
4973    select lv;
4974    update t3 set a=level+lv;
4975    FLUSH TABLES;
4976    call bug10100pc(level+1, lim);
4977  else
4978    select * from t3;
4979  end if;
4980  close c;
4981end|
4982
4983set @@max_sp_recursion_depth=4|
4984select @@max_sp_recursion_depth|
4985-- error ER_SP_NO_RECURSION
4986select bug10100f(3)|
4987-- error ER_SP_NO_RECURSION
4988select bug10100f(6)|
4989call bug10100t(5)|
4990call bug10100pt(1,5)|
4991call bug10100pv(1,5)|
4992update t3 set a=1|
4993call bug10100pd(1,5)|
4994select * from t3|
4995update t3 set a=1|
4996call bug10100pc(1,5)|
4997select * from t3|
4998set @@max_sp_recursion_depth=0|
4999select @@max_sp_recursion_depth|
5000-- error ER_SP_NO_RECURSION
5001select bug10100f(5)|
5002-- error ER_SP_RECURSION_LIMIT
5003call bug10100t(5)|
5004
5005#end of the stack checking
5006deallocate prepare stmt2|
5007
5008drop function bug10100f|
5009drop procedure bug10100p|
5010drop procedure bug10100t|
5011drop procedure bug10100pt|
5012drop procedure bug10100pv|
5013drop procedure bug10100pd|
5014drop procedure bug10100pc|
5015drop view v1|
5016
5017#
5018# BUG#13729: Stored procedures: packet error after exception handled
5019#
5020--disable_warnings
5021drop procedure if exists bug13729|
5022drop table if exists t3|
5023--enable_warnings
5024
5025create table t3 (s1 int, primary key (s1))|
5026
5027insert into t3 values (1),(2)|
5028
5029create procedure bug13729()
5030begin
5031  declare continue handler for sqlexception select 55;
5032
5033  update t3 set s1 = 1;
5034end|
5035
5036call bug13729()|
5037# Used to cause Packets out of order
5038select * from t3|
5039
5040drop procedure bug13729|
5041drop table t3|
5042
5043#
5044# BUG#14643: Stored Procedure: Continuing after failed var. initialization
5045#            crashes server.
5046#
5047--disable_warnings
5048drop procedure if exists bug14643_1|
5049drop procedure if exists bug14643_2|
5050--enable_warnings
5051
5052--error ER_SP_UNDECLARED_VAR
5053create procedure bug14643_1()
5054begin
5055  declare continue handler for sqlexception select 'boo' as 'Handler';
5056
5057  begin
5058    declare v int default undefined_var;
5059
5060    if v = 1 then
5061      select 1;
5062    else
5063      select v, isnull(v);
5064    end if;
5065  end;
5066end|
5067
5068--error ER_SP_UNDECLARED_VAR
5069create procedure bug14643_2()
5070begin
5071  declare continue handler for sqlexception select 'boo' as 'Handler';
5072
5073  case undefined_var
5074  when 1 then
5075    select 1;
5076  else
5077    select 2;
5078  end case;
5079
5080  select undefined_var;
5081end|
5082
5083
5084#
5085# BUG#14304: auto_increment field incorrect set in SP
5086#
5087--disable_warnings
5088drop procedure if exists bug14304|
5089drop table if exists t3, t4|
5090--enable_warnings
5091
5092create table t3(a int primary key auto_increment)|
5093create table t4(a int primary key auto_increment)|
5094
5095create procedure bug14304()
5096begin
5097  insert into t3 set a=null;
5098  insert into t4 set a=null;
5099  insert into t4 set a=null;
5100  insert into t4 set a=null;
5101  insert into t4 set a=null;
5102  insert into t4 set a=null;
5103  insert into t4 select null as a;
5104
5105  insert into t3 set a=null;
5106  insert into t3 set a=null;
5107
5108  select * from t3;
5109end|
5110
5111call bug14304()|
5112
5113drop procedure bug14304|
5114drop table t3, t4|
5115
5116#
5117# BUG#14376: MySQL crash on scoped variable (re)initialization
5118#
5119--disable_warnings
5120drop procedure if exists bug14376|
5121--enable_warnings
5122
5123--error ER_SP_UNDECLARED_VAR
5124create procedure bug14376()
5125begin
5126  declare x int default x;
5127end|
5128
5129
5130create procedure bug14376()
5131begin
5132  declare x int default 42;
5133
5134  begin
5135    declare x int default x;
5136
5137    select x;
5138  end;
5139end|
5140
5141call bug14376()|
5142
5143drop procedure bug14376|
5144
5145create procedure bug14376(x int)
5146begin
5147  declare x int default x;
5148
5149  select x;
5150end|
5151
5152call bug14376(4711)|
5153
5154drop procedure bug14376|
5155
5156#
5157# Bug#5967 "Stored procedure declared variable used instead of column"
5158# The bug should be fixed later.
5159# Test precedence of names of parameters, variable declarations,
5160# variable declarations in nested compound statements, table columns,
5161# table columns in cursor declarations.
5162# According to the standard, table columns take precedence over
5163# variable declarations. In MySQL 5.0 it's vice versa.
5164#
5165
5166--disable_warnings
5167drop procedure if exists bug5967|
5168drop table if exists t3|
5169--enable_warnings
5170create table t3 (a varchar(255))|
5171insert into t3 (a) values ("a - table column")|
5172create procedure bug5967(a varchar(255))
5173begin
5174  declare i varchar(255);
5175  declare c cursor for select a from t3;
5176  select a;
5177  select a from t3 into i;
5178  select i as 'Parameter takes precedence over table column';                     open c;
5179  fetch c into i;
5180  close c;
5181  select i as 'Parameter takes precedence over table column in cursors';
5182  begin
5183    declare a varchar(255) default 'a - local variable';
5184    declare c1 cursor for select a from t3;
5185    select a as 'A local variable takes precedence over parameter';
5186    open c1;
5187    fetch c1 into i;
5188    close c1;
5189    select i as 'A local variable takes precedence over parameter in cursors';
5190    begin
5191      declare a varchar(255) default 'a - local variable in a nested compound statement';
5192      declare c2 cursor for select a from t3;
5193      select a as 'A local variable in a nested compound statement takes precedence over a local variable in the outer statement';
5194      select a from t3 into i;
5195      select i as  'A local variable in a nested compound statement takes precedence over table column';
5196      open c2;
5197      fetch c2 into i;
5198      close c2;
5199      select i as  'A local variable in a nested compound statement takes precedence over table column in cursors';
5200    end;
5201  end;
5202end|
5203call bug5967("a - stored procedure parameter")|
5204drop procedure bug5967|
5205
5206#
5207# Bug#13012 "SP: REPAIR/BACKUP/RESTORE TABLE crashes the server"
5208#
5209--let $backupdir = $MYSQLTEST_VARDIR/tmp/
5210--error 0,1
5211--remove_file $backupdir/t1.frm
5212--error 0,1
5213--remove_file $backupdir/t1.MYD
5214
5215--disable_warnings
5216drop procedure if exists bug13012|
5217--enable_warnings
5218# Disable warnings also for BACKUP/RESTORE: they are deprecated.
5219eval create procedure bug13012()
5220    BEGIN
5221       REPAIR TABLE t1;
5222    END|
5223call bug13012()|
5224
5225--enable_warnings
5226
5227drop procedure bug13012|
5228
5229create view v1 as select * from t1|
5230create procedure bug13012()
5231BEGIN
5232  REPAIR TABLE t1,t2,t3,v1;
5233  OPTIMIZE TABLE t1,t2,t3,v1;
5234  ANALYZE TABLE t1,t2,t3,v1;
5235END|
5236call bug13012()|
5237call bug13012()|
5238call bug13012()|
5239drop procedure bug13012|
5240drop view v1|
5241select * from t1 order by data|
5242
5243#
5244# A test case for Bug#15392 "Server crashes during prepared statement
5245# execute": make sure that stored procedure check for error conditions
5246# properly and do not continue execution if an error has been set.
5247#
5248# It's necessary to use several DBs because in the original code
5249# the successful return of mysql_change_db overrode the error from
5250# execution.
5251drop schema if exists mysqltest1|
5252drop schema if exists mysqltest2|
5253drop schema if exists mysqltest3|
5254create schema mysqltest1|
5255create schema mysqltest2|
5256create schema mysqltest3|
5257use mysqltest3|
5258
5259create procedure mysqltest1.p1 (out prequestid varchar(100))
5260begin
5261  call mysqltest2.p2('call mysqltest3.p3(1, 2)');
5262end|
5263
5264create procedure mysqltest2.p2(in psql text)
5265begin
5266  declare lsql text;
5267  set @lsql= psql;
5268  prepare lstatement from @lsql;
5269  execute lstatement;
5270  deallocate prepare lstatement;
5271end|
5272
5273create procedure mysqltest3.p3(in p1 int)
5274begin
5275  select p1;
5276end|
5277
5278--error ER_SP_WRONG_NO_OF_ARGS
5279call mysqltest1.p1(@rs)|
5280--error ER_SP_WRONG_NO_OF_ARGS
5281call mysqltest1.p1(@rs)|
5282--error ER_SP_WRONG_NO_OF_ARGS
5283call mysqltest1.p1(@rs)|
5284drop schema if exists mysqltest1|
5285drop schema if exists mysqltest2|
5286drop schema if exists mysqltest3|
5287use test|
5288
5289#
5290# Bug#15441 "Running SP causes Server to Crash": check that an SP variable
5291# can not be used in VALUES() function.
5292#
5293--disable_warnings
5294drop table if exists t3|
5295drop procedure if exists bug15441|
5296--enable_warnings
5297create table t3 (id int not null primary key, county varchar(25))|
5298insert into t3 (id, county) values (1, 'York')|
5299
5300# First check that a stored procedure that refers to a parameter in VALUES()
5301# function won't parse.
5302
5303create procedure bug15441(c varchar(25))
5304begin
5305  update t3 set id=2, county=value(c);
5306end|
5307--error ER_BAD_FIELD_ERROR
5308call bug15441('county')|
5309drop procedure bug15441|
5310
5311# Now check the case when there is an ambiguity between column names
5312# and stored procedure parameters: the parser shall resolve the argument
5313# of VALUES() function to the column name.
5314
5315# It's hard to deduce what county refers to in every case (INSERT statement):
5316# 1st county refers to the column
5317# 2nd county refers to the procedure parameter
5318# 3d and 4th county refers to the column, again, but
5319# for 4th county it has the value of SP parameter
5320
5321# In UPDATE statement, just check that values() function returns NULL for
5322# non- INSERT...UPDATE statements, as stated in the manual.
5323
5324create procedure bug15441(county varchar(25))
5325begin
5326  declare c varchar(25) default "hello";
5327
5328  insert into t3 (id, county) values (1, county)
5329  on duplicate key update county= values(county);
5330  select * from t3;
5331
5332  update t3 set id=2, county=value(id);
5333  select * from t3;
5334end|
5335call bug15441('Yale')|
5336drop table t3|
5337drop procedure bug15441|
5338
5339#
5340# BUG#14498: Stored procedures: hang if undefined variable and exception
5341#
5342--disable_warnings
5343drop procedure if exists bug14498_1|
5344drop procedure if exists bug14498_2|
5345drop procedure if exists bug14498_3|
5346drop procedure if exists bug14498_4|
5347drop procedure if exists bug14498_5|
5348--enable_warnings
5349
5350--error ER_SP_UNDECLARED_VAR
5351create procedure bug14498_1()
5352begin
5353  declare continue handler for sqlexception select 'error' as 'Handler';
5354
5355  if v then
5356    select 'yes' as 'v';
5357  else
5358    select 'no' as 'v';
5359  end if;
5360  select 'done' as 'End';
5361end|
5362
5363--error ER_SP_UNDECLARED_VAR
5364create procedure bug14498_2()
5365begin
5366  declare continue handler for sqlexception select 'error' as 'Handler';
5367
5368  while v do
5369    select 'yes' as 'v';
5370  end while;
5371  select 'done' as 'End';
5372end|
5373
5374--error ER_SP_UNDECLARED_VAR
5375create procedure bug14498_3()
5376begin
5377  declare continue handler for sqlexception select 'error' as 'Handler';
5378
5379  repeat
5380    select 'maybe' as 'v';
5381  until v end repeat;
5382  select 'done' as 'End';
5383end|
5384
5385--error ER_SP_UNDECLARED_VAR
5386create procedure bug14498_4()
5387begin
5388  declare continue handler for sqlexception select 'error' as 'Handler';
5389
5390  case v
5391  when 1 then
5392    select '1' as 'v';
5393  when 2 then
5394    select '2' as 'v';
5395  else
5396    select '?' as 'v';
5397  end case;
5398  select 'done' as 'End';
5399end|
5400
5401--error ER_SP_UNDECLARED_VAR
5402create procedure bug14498_5()
5403begin
5404  declare continue handler for sqlexception select 'error' as 'Handler';
5405
5406  case
5407  when v = 1 then
5408    select '1' as 'v';
5409  when v = 2 then
5410    select '2' as 'v';
5411  else
5412    select '?' as 'v';
5413  end case;
5414  select 'done' as 'End';
5415end|
5416
5417
5418#
5419# BUG#15231: Stored procedure bug with not found condition handler
5420#
5421--disable_warnings
5422drop table if exists t3|
5423drop procedure if exists bug15231_1|
5424drop procedure if exists bug15231_2|
5425drop procedure if exists bug15231_3|
5426drop procedure if exists bug15231_4|
5427drop procedure if exists bug15231_5|
5428drop procedure if exists bug15231_6|
5429--enable_warnings
5430
5431create table t3 (id int not null)|
5432
5433create procedure bug15231_1()
5434begin
5435  declare xid integer;
5436  declare xdone integer default 0;
5437  declare continue handler for not found set xdone = 1;
5438
5439  set xid=null;
5440  call bug15231_2(xid);
5441  select xid, xdone;
5442end|
5443
5444create procedure bug15231_2(inout ioid integer)
5445begin
5446  select "Before NOT FOUND condition is triggered" as '1';
5447  select id into ioid from t3 where id=ioid;
5448  select "After NOT FOUND condtition is triggered" as '2';
5449
5450  if ioid is null then
5451    set ioid=1;
5452  end if;
5453end|
5454
5455create procedure bug15231_3()
5456begin
5457  declare exit handler for sqlwarning
5458    select 'Caught it (correct)' as 'Result';
5459
5460  call bug15231_4();
5461end|
5462
5463create procedure bug15231_4()
5464begin
5465  declare x decimal(2,1);
5466
5467  set x = 'zap';
5468  select 'Missed it (correct)' as 'Result';
5469  show warnings;
5470end|
5471
5472create procedure bug15231_5()
5473begin
5474  declare exit handler for sqlwarning
5475    select 'Caught it (wrong)' as 'Result';
5476
5477  call bug15231_6();
5478end|
5479
5480create procedure bug15231_6()
5481begin
5482  declare x decimal(2,1);
5483
5484  set x = 'zap';
5485  select 'Missed it (correct)' as 'Result';
5486  select id from t3;
5487end|
5488
5489call bug15231_1()|
5490call bug15231_3()|
5491call bug15231_5()|
5492
5493drop table t3|
5494drop procedure bug15231_1|
5495drop procedure bug15231_2|
5496drop procedure bug15231_3|
5497drop procedure bug15231_4|
5498drop procedure bug15231_5|
5499drop procedure bug15231_6|
5500
5501
5502#
5503# BUG#15011: error handler in nested block not activated
5504#
5505--disable_warnings
5506drop procedure if exists bug15011|
5507--enable_warnings
5508
5509create table t3 (c1 int primary key)|
5510
5511insert into t3 values (1)|
5512
5513create procedure bug15011()
5514  deterministic
5515begin
5516  declare continue handler for 1062
5517    select 'Outer' as 'Handler';
5518
5519  begin
5520    declare continue handler for 1062
5521      select 'Inner' as 'Handler';
5522
5523    insert into t3 values (1);
5524  end;
5525end|
5526
5527call bug15011()|
5528
5529drop procedure bug15011|
5530drop table t3|
5531
5532
5533#
5534# BUG#17476: Stored procedure not returning data when it is called first
5535#            time per connection
5536#
5537--disable_warnings
5538drop procedure if exists bug17476|
5539--enable_warnings
5540
5541create table t3 ( d date )|
5542insert into t3 values
5543  ( '2005-01-01' ), ( '2005-01-02' ), ( '2005-01-03' ),
5544  ( '2005-01-04' ), ( '2005-02-01' ), ( '2005-02-02' )|
5545
5546create procedure bug17476(pDateFormat varchar(10))
5547  select date_format(t3.d, pDateFormat), count(*)
5548    from t3
5549    group by date_format(t3.d, pDateFormat)|
5550
5551call bug17476('%Y-%m')|
5552call bug17476('%Y-%m')|
5553
5554drop table t3|
5555drop procedure bug17476|
5556
5557
5558#
5559# BUG#16887: Cursor causes server segfault
5560#
5561--disable_warnings
5562drop table if exists t3|
5563drop procedure if exists bug16887|
5564--enable_warnings
5565
5566create table t3 ( c varchar(1) )|
5567
5568insert into t3 values
5569  (' '),('.'),(';'),(','),('-'),('_'),('('),(')'),('/'),('\\')|
5570
5571create procedure bug16887()
5572begin
5573  declare i int default 10;
5574
5575 again:
5576  while i > 0 do
5577  begin
5578    declare breakchar varchar(1);
5579    declare done int default 0;
5580    declare t3_cursor cursor for select c from t3;
5581    declare continue handler for not found set done = 1;
5582
5583    set i = i - 1;
5584    select i;
5585
5586    if i = 3 then
5587      iterate again;
5588    end if;
5589
5590    open t3_cursor;
5591
5592    loop
5593      fetch t3_cursor into breakchar;
5594
5595      if done = 1 then
5596        begin
5597          close t3_cursor;
5598          iterate again;
5599        end;
5600      end if;
5601     end loop;
5602   end;
5603   end while;
5604end|
5605
5606call bug16887()|
5607
5608drop table t3|
5609drop procedure bug16887|
5610
5611#
5612# BUG#16474: SP crashed MySQL
5613# (when using "order by localvar", where 'localvar' is just that.
5614#
5615--disable_warnings
5616drop procedure if exists bug16474_1|
5617drop procedure if exists bug16474_2|
5618--enable_warnings
5619
5620delete from t1|
5621insert into t1 values ('c', 2), ('b', 3), ('a', 1)|
5622
5623create procedure bug16474_1()
5624begin
5625  declare x int;
5626
5627  select id from t1 order by x, id;
5628end|
5629
5630#
5631# BUG#14945: Truncate table doesn't reset the auto_increment counter
5632#
5633--disable_warnings
5634drop procedure if exists bug14945|
5635--enable_warnings
5636create table t3 (id int not null auto_increment primary key)|
5637create procedure bug14945() deterministic truncate t3|
5638insert into t3 values (null)|
5639call bug14945()|
5640insert into t3 values (null)|
5641select * from t3|
5642drop table t3|
5643drop procedure bug14945|
5644
5645# This does NOT order by column index; variable is an expression.
5646create procedure bug16474_2(x int)
5647  select id from t1 order by x, id|
5648
5649call bug16474_1()|
5650call bug16474_2(1)|
5651call bug16474_2(2)|
5652drop procedure bug16474_1|
5653drop procedure bug16474_2|
5654
5655# For reference: user variables are expressions too and do not affect ordering.
5656set @x = 2|
5657select * from t1 order by @x, data|
5658
5659delete from t1|
5660
5661
5662#
5663# BUG#15728: LAST_INSERT_ID function inside a stored function returns 0
5664#
5665# The solution is not to reset last_insert_id on enter to sub-statement.
5666#
5667--disable_warnings
5668drop function if exists bug15728|
5669drop table if exists t3|
5670--enable_warnings
5671
5672create table t3 (
5673  id int not null auto_increment,
5674  primary key (id)
5675)|
5676create function bug15728() returns int(11)
5677  return last_insert_id()|
5678
5679insert into t3 values (0)|
5680select last_insert_id()|
5681select bug15728()|
5682
5683drop function bug15728|
5684drop table t3|
5685
5686
5687#
5688# BUG#18787: Server crashed when calling a stored procedure containing
5689#            a misnamed function
5690#
5691--disable_warnings
5692drop procedure if exists bug18787|
5693--enable_warnings
5694create procedure bug18787()
5695begin
5696  declare continue handler for sqlexception begin end;
5697
5698  select no_such_function();
5699end|
5700
5701call bug18787()|
5702drop procedure bug18787|
5703
5704
5705#
5706# BUG#18344: DROP DATABASE does not drop associated routines
5707# (... if the database name is longer than 21 characters)
5708#
5709#               1234567890123456789012
5710create database bug18344_012345678901|
5711use bug18344_012345678901|
5712create procedure bug18344() begin end|
5713create procedure bug18344_2() begin end|
5714
5715create database bug18344_0123456789012|
5716use bug18344_0123456789012|
5717create procedure bug18344() begin end|
5718create procedure bug18344_2() begin end|
5719
5720use test|
5721
5722--sorted_result
5723select schema_name from information_schema.schemata where
5724  schema_name like 'bug18344%'|
5725--sorted_result
5726select routine_name,routine_schema from information_schema.routines where
5727  routine_schema like 'bug18344%'|
5728
5729drop database bug18344_012345678901|
5730drop database bug18344_0123456789012|
5731
5732# Should be nothing left.
5733select schema_name from information_schema.schemata where
5734  schema_name like 'bug18344%'|
5735select routine_name,routine_schema from information_schema.routines where
5736  routine_schema like 'bug18344%'|
5737
5738
5739#
5740# BUG#12472/BUG#15137 'CREATE TABLE ... SELECT ... which explicitly or
5741# implicitly uses stored function gives "Table not locked" error'.
5742#
5743--disable_warnings
5744drop function if exists bug12472|
5745--enable_warnings
5746create function bug12472() returns int return (select count(*) from t1)|
5747# Check case when function is used directly
5748create table t3 as select bug12472() as i|
5749show create table t3|
5750select * from t3|
5751drop table t3|
5752# Check case when function is used indirectly through view
5753create view v1 as select bug12472() as j|
5754create table t3 as select * from v1|
5755show create table t3|
5756select * from t3|
5757drop table t3|
5758drop view v1|
5759drop function bug12472|
5760
5761
5762#
5763# BUG#18587: Function that accepts and returns TEXT garbles data if longer than
5764# 766 chars
5765#
5766
5767# Prepare.
5768
5769--disable_warnings
5770DROP FUNCTION IF EXISTS bug18589_f1|
5771DROP PROCEDURE IF EXISTS bug18589_p1|
5772DROP PROCEDURE IF EXISTS bug18589_p2|
5773--enable_warnings
5774
5775CREATE FUNCTION bug18589_f1(arg TEXT) RETURNS TEXT
5776BEGIN
5777  RETURN CONCAT(arg, "");
5778END|
5779
5780CREATE PROCEDURE bug18589_p1(arg TEXT, OUT ret TEXT)
5781BEGIN
5782  SET ret = CONCAT(arg, "");
5783END|
5784
5785CREATE PROCEDURE bug18589_p2(arg TEXT)
5786BEGIN
5787  DECLARE v TEXT;
5788  CALL bug18589_p1(arg, v);
5789  SELECT v;
5790END|
5791
5792# Test case.
5793
5794SELECT bug18589_f1(REPEAT("a", 767))|
5795
5796SET @bug18589_v1 = ""|
5797CALL bug18589_p1(REPEAT("a", 767), @bug18589_v1)|
5798SELECT @bug18589_v1|
5799
5800CALL bug18589_p2(REPEAT("a", 767))|
5801
5802# Cleanup.
5803
5804DROP FUNCTION bug18589_f1|
5805DROP PROCEDURE bug18589_p1|
5806DROP PROCEDURE bug18589_p2|
5807
5808
5809#
5810# BUG#18037: Server crash when returning system variable in stored procedures
5811# BUG#19633: Stack corruption in fix_fields()/THD::rollback_item_tree_changes()
5812#
5813
5814# Prepare.
5815
5816--disable_warnings
5817DROP FUNCTION IF EXISTS bug18037_f1|
5818DROP PROCEDURE IF EXISTS bug18037_p1|
5819DROP PROCEDURE IF EXISTS bug18037_p2|
5820--enable_warnings
5821
5822# Test case.
5823
5824CREATE FUNCTION bug18037_f1() RETURNS INT
5825BEGIN
5826  RETURN @@server_id;
5827END|
5828
5829CREATE PROCEDURE bug18037_p1()
5830BEGIN
5831  DECLARE v INT DEFAULT @@server_id;
5832END|
5833
5834CREATE PROCEDURE bug18037_p2()
5835BEGIN
5836  CASE @@server_id
5837  WHEN -1 THEN
5838    SELECT 0;
5839  ELSE
5840    SELECT 1;
5841  END CASE;
5842END|
5843
5844SELECT bug18037_f1()|
5845CALL bug18037_p1()|
5846CALL bug18037_p2()|
5847
5848# Cleanup.
5849
5850DROP FUNCTION bug18037_f1|
5851DROP PROCEDURE bug18037_p1|
5852DROP PROCEDURE bug18037_p2|
5853
5854#
5855# Bug#17199: "Table not found" error occurs if the query contains a call
5856#            to a function from another database.
5857#            See also ps.test for an additional test case for this bug.
5858#
5859use test|
5860create table t3 (i int)|
5861insert into t3 values (1), (2)|
5862create database mysqltest1|
5863use mysqltest1|
5864create function bug17199() returns varchar(2) deterministic return 'ok'|
5865use test|
5866select *, mysqltest1.bug17199() from t3|
5867#
5868# Bug#18444: Fully qualified stored function names don't work correctly
5869#            in select statements
5870#
5871use mysqltest1|
5872create function bug18444(i int) returns int no sql deterministic return i + 1|
5873use test|
5874select mysqltest1.bug18444(i) from t3|
5875drop database mysqltest1|
5876#
5877# Check that current database has no influence to a stored procedure
5878#
5879create database mysqltest1 charset=utf8|
5880create database mysqltest2 charset=utf8|
5881create procedure mysqltest1.p1()
5882begin
5883-- alters the default collation of database test
5884  alter database character set koi8r;
5885end|
5886use mysqltest1|
5887call p1()|
5888show create database mysqltest1|
5889show create database mysqltest2|
5890alter database mysqltest1 character set utf8|
5891use mysqltest2|
5892call mysqltest1.p1()|
5893show create database mysqltest1|
5894show create database mysqltest2|
5895drop database mysqltest1|
5896drop database mysqltest2|
5897#
5898# Restore the old environemnt
5899use test|
5900#
5901# Bug#15217 "Using a SP cursor on a table created with PREPARE fails with
5902#           weird error". Check that the code that is supposed to work at
5903#           the first execution of a stored procedure actually works for
5904#           sp_instr_copen.
5905
5906--disable_warnings
5907drop table if exists t3|
5908drop procedure if exists bug15217|
5909--enable_warnings
5910create table t3 as select 1|
5911create procedure bug15217()
5912begin
5913  declare var1 char(255);
5914  declare cur1 cursor for select * from t3;
5915  open cur1;
5916  fetch cur1 into var1;
5917  select concat('data was: /', var1, '/');
5918  close cur1;
5919end |
5920# Returns expected result
5921call bug15217()|
5922flush tables |
5923# Returns error with garbage as column name
5924call bug15217()|
5925drop table t3|
5926drop procedure bug15217|
5927
5928
5929#
5930# BUG#21013: Performance Degrades when importing data that uses
5931# Trigger and Stored Procedure
5932#
5933# This is a performance and memory leak test.  Run with large number
5934# passed to bug21013() procedure.
5935#
5936--disable_warnings
5937DROP PROCEDURE IF EXISTS bug21013 |
5938--enable_warnings
5939
5940CREATE PROCEDURE bug21013(IN lim INT)
5941BEGIN
5942  DECLARE i INT DEFAULT 0;
5943  WHILE (i < lim) DO
5944    SET @b = LOCATE(_latin1'b', @a, 1);
5945    SET i = i + 1;
5946  END WHILE;
5947END |
5948
5949SET @a = _latin2"aaaaaaaaaa" |
5950CALL bug21013(10) |
5951
5952DROP PROCEDURE bug21013 |
5953
5954
5955#
5956# BUG#16211: Stored function return type for strings is ignored
5957#
5958
5959# Prepare: create database with fixed, pre-defined character set.
5960
5961--disable_warnings
5962DROP DATABASE IF EXISTS mysqltest1|
5963DROP DATABASE IF EXISTS mysqltest2|
5964--enable_warnings
5965
5966CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
5967CREATE DATABASE mysqltest2 DEFAULT CHARACTER SET utf8|
5968
5969# Test case:
5970
5971use mysqltest1|
5972
5973#   - Create two stored functions -- with and without explicit CHARSET-clause
5974#     for return value;
5975
5976CREATE FUNCTION bug16211_f1() RETURNS CHAR(10)
5977  RETURN ""|
5978
5979CREATE FUNCTION bug16211_f2() RETURNS CHAR(10) CHARSET koi8r
5980  RETURN ""|
5981
5982CREATE FUNCTION mysqltest2.bug16211_f3() RETURNS CHAR(10)
5983  RETURN ""|
5984
5985CREATE FUNCTION mysqltest2.bug16211_f4() RETURNS CHAR(10) CHARSET koi8r
5986  RETURN ""|
5987
5988#   - Check that CHARSET-clause is specified for the second function;
5989
5990SHOW CREATE FUNCTION bug16211_f1|
5991SHOW CREATE FUNCTION bug16211_f2|
5992
5993SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
5994SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
5995
5996SELECT dtd_identifier
5997FROM INFORMATION_SCHEMA.ROUTINES
5998WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
5999
6000SELECT dtd_identifier
6001FROM INFORMATION_SCHEMA.ROUTINES
6002WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
6003
6004SELECT dtd_identifier
6005FROM INFORMATION_SCHEMA.ROUTINES
6006WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
6007
6008SELECT dtd_identifier
6009FROM INFORMATION_SCHEMA.ROUTINES
6010WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
6011
6012SELECT CHARSET(bug16211_f1())|
6013SELECT CHARSET(bug16211_f2())|
6014
6015SELECT CHARSET(mysqltest2.bug16211_f3())|
6016SELECT CHARSET(mysqltest2.bug16211_f4())|
6017
6018#   - Alter database character set.
6019
6020ALTER DATABASE mysqltest1 CHARACTER SET cp1251|
6021ALTER DATABASE mysqltest2 CHARACTER SET cp1251|
6022
6023#   - Check that CHARSET-clause has not changed.
6024
6025SHOW CREATE FUNCTION bug16211_f1|
6026SHOW CREATE FUNCTION bug16211_f2|
6027
6028SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
6029SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
6030
6031SELECT dtd_identifier
6032FROM INFORMATION_SCHEMA.ROUTINES
6033WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
6034
6035SELECT dtd_identifier
6036FROM INFORMATION_SCHEMA.ROUTINES
6037WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
6038
6039SELECT dtd_identifier
6040FROM INFORMATION_SCHEMA.ROUTINES
6041WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
6042
6043SELECT dtd_identifier
6044FROM INFORMATION_SCHEMA.ROUTINES
6045WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
6046
6047SELECT CHARSET(bug16211_f1())|
6048SELECT CHARSET(bug16211_f2())|
6049
6050SELECT CHARSET(mysqltest2.bug16211_f3())|
6051SELECT CHARSET(mysqltest2.bug16211_f4())|
6052
6053# Cleanup.
6054
6055use test|
6056
6057DROP DATABASE mysqltest1|
6058DROP DATABASE mysqltest2|
6059
6060
6061#
6062# BUG#16676: Database CHARSET not used for stored procedures
6063#
6064
6065# Prepare: create database with fixed, pre-defined character set.
6066
6067--disable_warnings
6068DROP DATABASE IF EXISTS mysqltest1|
6069--enable_warnings
6070
6071CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
6072
6073# Test case:
6074
6075use mysqltest1|
6076
6077#   - Create two stored procedures -- with and without explicit CHARSET-clause;
6078
6079CREATE PROCEDURE bug16676_p1(
6080  IN p1 CHAR(10),
6081  INOUT p2 CHAR(10),
6082  OUT p3 CHAR(10))
6083BEGIN
6084  SELECT CHARSET(p1), COLLATION(p1);
6085  SELECT CHARSET(p2), COLLATION(p2);
6086  SELECT CHARSET(p3), COLLATION(p3);
6087END|
6088
6089CREATE PROCEDURE bug16676_p2(
6090  IN p1 CHAR(10) CHARSET koi8r,
6091  INOUT p2 CHAR(10) CHARSET cp1251,
6092  OUT p3 CHAR(10) CHARSET greek)
6093BEGIN
6094  SELECT CHARSET(p1), COLLATION(p1);
6095  SELECT CHARSET(p2), COLLATION(p2);
6096  SELECT CHARSET(p3), COLLATION(p3);
6097END|
6098
6099#   - Call procedures.
6100
6101SET @v2 = 'b'|
6102SET @v3 = 'c'|
6103
6104CALL bug16676_p1('a', @v2, @v3)|
6105CALL bug16676_p2('a', @v2, @v3)|
6106
6107# Cleanup.
6108
6109use test|
6110
6111DROP DATABASE mysqltest1|
6112#
6113# BUG#8153: Stored procedure with subquery and continue handler, wrong result
6114#
6115
6116--disable_warnings
6117drop table if exists t3|
6118drop table if exists t4|
6119drop procedure if exists bug8153_subselect|
6120drop procedure if exists bug8153_subselect_a|
6121drop procedure if exists bug8153_subselect_b|
6122drop procedure if exists bug8153_proc_a|
6123drop procedure if exists bug8153_proc_b|
6124--enable_warnings
6125
6126create table t3 (a int)|
6127create table t4 (a int)|
6128insert into t3 values (1), (1), (2), (3)|
6129insert into t4 values (1), (1)|
6130
6131## Testing the use case reported in Bug#8153
6132
6133create procedure bug8153_subselect()
6134begin
6135  declare continue handler for sqlexception
6136  begin
6137    select 'statement failed';
6138  end;
6139  update t3 set a=a+1 where (select a from t4 where a=1) is null;
6140  select 'statement after update';
6141end|
6142
6143call bug8153_subselect()|
6144select * from t3|
6145
6146call bug8153_subselect()|
6147select * from t3|
6148
6149drop procedure bug8153_subselect|
6150
6151## Testing a subselect with a non local handler
6152
6153create procedure bug8153_subselect_a()
6154begin
6155  declare continue handler for sqlexception
6156  begin
6157    select 'in continue handler';
6158  end;
6159
6160  select 'reachable code a1';
6161  call bug8153_subselect_b();
6162  select 'reachable code a2';
6163end|
6164
6165create procedure bug8153_subselect_b()
6166begin
6167  select 'reachable code b1';
6168  update t3 set a=a+1 where (select a from t4 where a=1) is null;
6169  select 'unreachable code b2';
6170end|
6171
6172call bug8153_subselect_a()|
6173select * from t3|
6174
6175call bug8153_subselect_a()|
6176select * from t3|
6177
6178drop procedure bug8153_subselect_a|
6179drop procedure bug8153_subselect_b|
6180
6181## Testing extra use cases, found while investigating
6182## This is related to BUG#18787, with a non local handler
6183
6184create procedure bug8153_proc_a()
6185begin
6186  declare continue handler for sqlexception
6187  begin
6188    select 'in continue handler';
6189  end;
6190
6191  select 'reachable code a1';
6192  call bug8153_proc_b();
6193  select 'reachable code a2';
6194end|
6195
6196create procedure bug8153_proc_b()
6197begin
6198  select 'reachable code b1';
6199  select no_such_function();
6200  select 'unreachable code b2';
6201end|
6202
6203call bug8153_proc_a()|
6204
6205drop procedure bug8153_proc_a|
6206drop procedure bug8153_proc_b|
6207drop table t3|
6208drop table t4|
6209
6210#
6211# BUG#19862: Sort with filesort by function evaluates function twice
6212#
6213--disable_warnings
6214drop procedure if exists bug19862|
6215--enable_warnings
6216CREATE TABLE t11 (a INT)|
6217CREATE TABLE t12 (a INT)|
6218CREATE FUNCTION bug19862(x INT) RETURNS INT
6219  BEGIN
6220    INSERT INTO t11 VALUES (x);
6221    RETURN x+1;
6222  END|
6223INSERT INTO t12 VALUES (1), (2)|
6224SELECT bug19862(a) FROM t12 ORDER BY 1|
6225SELECT * FROM t11|
6226DROP TABLE t11, t12|
6227DROP FUNCTION bug19862|
6228
6229
6230# Bug#21002 "Derived table not selecting from a "real" table fails in JOINs"
6231#
6232# A regression caused by the fix for Bug#18444: for derived tables we should
6233# set an empty string as the current database. They do not belong to any
6234# database and must be usable even if there is no database
6235# selected.
6236--disable_warnings
6237drop table if exists t3|
6238drop database if exists mysqltest1|
6239--enable_warnings
6240create table t3 (a int)|
6241insert into t3 (a) values (1), (2)|
6242
6243create database mysqltest1|
6244use mysqltest1|
6245drop database mysqltest1|
6246
6247# No current database
6248select database()|
6249
6250select * from (select 1 as a) as t1 natural join (select * from test.t3) as t2|
6251use test|
6252drop table t3|
6253
6254
6255# Test for BUG#16899: Possible buffer overflow in handling of DEFINER-clause.
6256#
6257# Prepare.
6258
6259--disable_warnings
6260DROP PROCEDURE IF EXISTS bug16899_p1|
6261DROP FUNCTION IF EXISTS bug16899_f1|
6262--enable_warnings
6263
6264--error ER_WRONG_STRING_LENGTH
6265CREATE DEFINER=longer_than_80_456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789@localhost PROCEDURE bug16899_p1()
6266BEGIN
6267  SET @a = 1;
6268END|
6269
6270--error ER_WRONG_STRING_LENGTH
6271CREATE DEFINER=some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY
6272  FUNCTION bug16899_f1() RETURNS INT
6273BEGIN
6274  RETURN 1;
6275END|
6276
6277
6278#
6279# BUG#21416: SP: Recursion level higher than zero needed for non-recursive call
6280#
6281--disable_warnings
6282drop procedure if exists bug21416|
6283--enable_warnings
6284create procedure bug21416() show create procedure bug21416|
6285call bug21416()|
6286drop procedure bug21416|
6287
6288
6289#
6290# BUG#21414: SP: Procedure undroppable, to some extent
6291#
6292--disable_warnings
6293DROP PROCEDURE IF EXISTS bug21414|
6294--enable_warnings
6295
6296CREATE PROCEDURE bug21414() SELECT 1|
6297
6298FLUSH TABLES WITH READ LOCK|
6299
6300--error ER_CANT_UPDATE_WITH_READLOCK
6301DROP PROCEDURE bug21414|
6302
6303UNLOCK TABLES|
6304
6305--echo The following should succeed.
6306DROP PROCEDURE bug21414|
6307
6308
6309#
6310# BUG#21311: Possible stack overrun if SP has non-latin1 name
6311#
6312set names utf8|
6313--disable_warnings
6314drop database if exists това_е_дълго_име_за_база_данни_нали|
6315--enable_warnings
6316create database това_е_дълго_име_за_база_данни_нали|
6317INSERT INTO mysql.proc VALUES ('това_е_дълго_име_за_база_данни_нали','това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго','PROCEDURE','това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго','SQL','CONTAINS_SQL','NO','DEFINER','','','bad_body','root@localhost',now(), now(),'','', 'utf8', 'utf8_general_ci', 'utf8_general_ci', 'n/a', 'NONE')|
6318--error ER_SP_PROC_TABLE_CORRUPT
6319call това_е_дълго_име_за_база_данни_нали.това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго()|
6320drop database това_е_дълго_име_за_база_данни_нали|
6321show warnings|
6322
6323#
6324# BUG#21493: Crash on the second call of a procedure containing
6325#            a select statement that uses an IN aggregating subquery
6326#
6327
6328CREATE TABLE t3 (
6329  Member_ID varchar(15) NOT NULL,
6330  PRIMARY KEY (Member_ID)
6331)|
6332
6333CREATE TABLE t4 (
6334  ID int(10) unsigned NOT NULL auto_increment,
6335  Member_ID varchar(15) NOT NULL default '',
6336  Action varchar(12) NOT NULL,
6337  Action_Date datetime NOT NULL,
6338  Track varchar(15) default NULL,
6339  User varchar(12) default NULL,
6340  Date_Updated timestamp NOT NULL default CURRENT_TIMESTAMP on update
6341    CURRENT_TIMESTAMP,
6342  PRIMARY KEY (ID),
6343  KEY Action (Action),
6344  KEY Action_Date (Action_Date)
6345)|
6346
6347
6348INSERT INTO t3(Member_ID) VALUES
6349  ('111111'), ('222222'), ('333333'), ('444444'), ('555555'), ('666666')|
6350
6351INSERT INTO t4(Member_ID, Action, Action_Date, Track) VALUES
6352  ('111111', 'Disenrolled', '2006-03-01', 'CAD' ),
6353  ('111111', 'Enrolled', '2006-03-01', 'CAD' ),
6354  ('111111', 'Disenrolled', '2006-07-03', 'CAD' ),
6355  ('222222', 'Enrolled', '2006-03-07', 'CAD' ),
6356  ('222222', 'Enrolled', '2006-03-07', 'CHF' ),
6357  ('222222', 'Disenrolled', '2006-08-02', 'CHF' ),
6358  ('333333', 'Enrolled', '2006-03-01', 'CAD' ),
6359  ('333333', 'Disenrolled', '2006-03-01', 'CAD' ),
6360  ('444444', 'Enrolled', '2006-03-01', 'CAD' ),
6361  ('555555', 'Disenrolled', '2006-03-01', 'CAD' ),
6362  ('555555', 'Enrolled', '2006-07-21', 'CAD' ),
6363  ('555555', 'Disenrolled', '2006-03-01', 'CHF' ),
6364  ('666666', 'Enrolled', '2006-02-09', 'CAD' ),
6365  ('666666', 'Enrolled', '2006-05-12', 'CHF' ),
6366  ('666666', 'Disenrolled', '2006-06-01', 'CAD' )|
6367
6368--disable_warnings
6369DROP FUNCTION IF EXISTS bug21493|
6370--enable_warnings
6371
6372CREATE FUNCTION bug21493(paramMember VARCHAR(15)) RETURNS varchar(45)
6373BEGIN
6374DECLARE tracks VARCHAR(45);
6375SELECT GROUP_CONCAT(Track SEPARATOR ', ') INTO tracks FROM t4
6376  WHERE Member_ID=paramMember AND Action='Enrolled' AND
6377        (Track,Action_Date) IN (SELECT Track, MAX(Action_Date) FROM t4
6378                                  WHERE Member_ID=paramMember GROUP BY Track);
6379RETURN tracks;
6380END|
6381
6382SELECT bug21493('111111')|
6383SELECT bug21493('222222')|
6384
6385SELECT bug21493(Member_ID) FROM t3|
6386
6387DROP FUNCTION bug21493|
6388DROP TABLE t3,t4|
6389
6390#
6391# Bug#20028 Function with select return no data
6392#
6393
6394--disable_warnings
6395drop function if exists func_20028_a|
6396drop function if exists func_20028_b|
6397drop function if exists func_20028_c|
6398drop procedure if exists proc_20028_a|
6399drop procedure if exists proc_20028_b|
6400drop procedure if exists proc_20028_c|
6401drop table if exists table_20028|
6402--enable_warnings
6403
6404create table table_20028 (i int)|
6405
6406SET @save_sql_mode=@@sql_mode|
6407
6408SET sql_mode=''|
6409
6410create function func_20028_a() returns integer
6411begin
6412  declare temp integer;
6413  select i into temp from table_20028 limit 1;
6414  return ifnull(temp, 0);
6415end|
6416
6417create function func_20028_b() returns integer
6418begin
6419  return func_20028_a();
6420end|
6421
6422create function func_20028_c() returns integer
6423begin
6424  declare div_zero integer;
6425  set SQL_MODE='TRADITIONAL';
6426  select 1/0 into div_zero;
6427  return div_zero;
6428end|
6429
6430create procedure proc_20028_a()
6431begin
6432  declare temp integer;
6433  select i into temp from table_20028 limit 1;
6434end|
6435
6436create procedure proc_20028_b()
6437begin
6438  call proc_20028_a();
6439end|
6440
6441create procedure proc_20028_c()
6442begin
6443  declare div_zero integer;
6444  set SQL_MODE='TRADITIONAL';
6445  select 1/0 into div_zero;
6446end|
6447
6448select func_20028_a()|
6449select func_20028_b()|
6450--error ER_DIVISION_BY_ZERO
6451select func_20028_c()|
6452call proc_20028_a()|
6453call proc_20028_b()|
6454--error ER_DIVISION_BY_ZERO
6455call proc_20028_c()|
6456
6457SET sql_mode='TRADITIONAL'|
6458
6459drop function func_20028_a|
6460drop function func_20028_b|
6461drop function func_20028_c|
6462drop procedure proc_20028_a|
6463drop procedure proc_20028_b|
6464drop procedure proc_20028_c|
6465
6466create function func_20028_a() returns integer
6467begin
6468  declare temp integer;
6469  select i into temp from table_20028 limit 1;
6470  return ifnull(temp, 0);
6471end|
6472
6473create function func_20028_b() returns integer
6474begin
6475  return func_20028_a();
6476end|
6477
6478create function func_20028_c() returns integer
6479begin
6480  declare div_zero integer;
6481  set SQL_MODE='';
6482  select 1/0 into div_zero;
6483  return div_zero;
6484end|
6485
6486create procedure proc_20028_a()
6487begin
6488  declare temp integer;
6489  select i into temp from table_20028 limit 1;
6490end|
6491
6492create procedure proc_20028_b()
6493begin
6494  call proc_20028_a();
6495end|
6496
6497create procedure proc_20028_c()
6498begin
6499  declare div_zero integer;
6500  set SQL_MODE='';
6501  select 1/0 into div_zero;
6502end|
6503
6504select func_20028_a()|
6505select func_20028_b()|
6506select func_20028_c()|
6507call proc_20028_a()|
6508call proc_20028_b()|
6509call proc_20028_c()|
6510
6511SET @@sql_mode=@save_sql_mode|
6512
6513drop function func_20028_a|
6514drop function func_20028_b|
6515drop function func_20028_c|
6516drop procedure proc_20028_a|
6517drop procedure proc_20028_b|
6518drop procedure proc_20028_c|
6519drop table table_20028|
6520
6521#
6522# Bug#21462 Stored procedures with no arguments require parenthesis
6523#
6524
6525--disable_warnings
6526drop procedure if exists proc_21462_a|
6527drop procedure if exists proc_21462_b|
6528--enable_warnings
6529
6530create procedure proc_21462_a()
6531begin
6532  select "Called A";
6533end|
6534
6535create procedure proc_21462_b(x int)
6536begin
6537  select "Called B";
6538end|
6539
6540call proc_21462_a|
6541call proc_21462_a()|
6542-- error ER_SP_WRONG_NO_OF_ARGS
6543call proc_21462_a(1)|
6544
6545-- error ER_SP_WRONG_NO_OF_ARGS
6546call proc_21462_b|
6547-- error ER_SP_WRONG_NO_OF_ARGS
6548call proc_21462_b()|
6549call proc_21462_b(1)|
6550
6551drop procedure proc_21462_a|
6552drop procedure proc_21462_b|
6553
6554
6555#
6556# Bug#19733 "Repeated alter, or repeated create/drop, fails"
6557# Check that CREATE/DROP INDEX is re-execution friendly.
6558#
6559--disable_warnings
6560drop table if exists t3|
6561drop procedure if exists proc_bug19733|
6562--enable_warnings
6563create table t3 (s1 int)|
6564
6565create procedure proc_bug19733()
6566begin
6567  declare v int default 0;
6568  while v < 100 do
6569    create index i on t3 (s1);
6570    drop index i on t3;
6571    set v = v + 1;
6572  end while;
6573end|
6574
6575call proc_bug19733()|
6576call proc_bug19733()|
6577call proc_bug19733()|
6578
6579drop procedure proc_bug19733|
6580drop table t3|
6581
6582
6583#
6584# BUG#20492: Subsequent calls to stored procedure yeild incorrect
6585# result if join is used
6586#
6587# Optimized ON expression in join wasn't properly saved for reuse.
6588#
6589--disable_warnings
6590DROP PROCEDURE IF EXISTS p1|
6591DROP VIEW IF EXISTS v1, v2|
6592DROP TABLE IF EXISTS t3, t4|
6593--enable_warnings
6594
6595CREATE TABLE t3 (t3_id INT)|
6596
6597INSERT INTO t3 VALUES (0)|
6598INSERT INTO t3 VALUES (1)|
6599
6600CREATE TABLE t4 (t4_id INT)|
6601
6602INSERT INTO t4 VALUES (2)|
6603
6604CREATE VIEW v1 AS
6605SELECT t3.t3_id, t4.t4_id
6606FROM t3 JOIN t4 ON t3.t3_id = 0|
6607
6608CREATE VIEW v2 AS
6609SELECT t3.t3_id AS t3_id_1, v1.t3_id AS t3_id_2, v1.t4_id
6610FROM t3 LEFT JOIN v1 ON t3.t3_id = 0|
6611
6612CREATE PROCEDURE p1() SELECT * FROM v2|
6613
6614# Results should not differ.
6615CALL p1()|
6616CALL p1()|
6617
6618DROP PROCEDURE p1|
6619DROP VIEW v1, v2|
6620DROP TABLE t3, t4|
6621
6622--echo End of 5.0 tests
6623
6624--echo Begin of 5.1 tests
6625
6626#
6627# BUG#18239: Possible to overload internal functions with stored functions
6628#
6629
6630delimiter ;|
6631
6632--disable_warnings
6633drop function if exists pi;
6634--enable_warnings
6635
6636create function pi() returns varchar(50)
6637return "pie, my favorite desert.";
6638
6639SET @save_sql_mode=@@sql_mode;
6640
6641SET SQL_MODE='IGNORE_SPACE';
6642
6643select pi(), pi ();
6644
6645# Non deterministic warnings from db_load_routine
6646--disable_warnings
6647select test.pi(), test.pi ();
6648--enable_warnings
6649
6650SET SQL_MODE='';
6651
6652select pi(), pi ();
6653
6654# Non deterministic warnings from db_load_routine
6655--disable_warnings
6656select test.pi(), test.pi ();
6657--enable_warnings
6658
6659SET @@sql_mode=@save_sql_mode;
6660
6661drop function pi;
6662# End of BUG#18239
6663
6664#
6665# BUG#22619: Spaces considered harmful
6666#
6667
6668--disable_warnings
6669drop function if exists test.database;
6670drop function if exists test.current_user;
6671drop function if exists test.md5;
6672--enable_warnings
6673
6674create database nowhere;
6675use nowhere;
6676drop database nowhere;
6677
6678SET @save_sql_mode=@@sql_mode;
6679
6680SET SQL_MODE='IGNORE_SPACE';
6681
6682select database(), database ();
6683select current_user(), current_user ();
6684select md5("aaa"), md5 ("aaa");
6685
6686SET SQL_MODE='';
6687
6688select database(), database ();
6689select current_user(), current_user ();
6690select md5("aaa"), md5 ("aaa");
6691
6692use test;
6693
6694create function `database`() returns varchar(50)
6695return "Stored function database";
6696
6697create function `current_user`() returns varchar(50)
6698return "Stored function current_user";
6699
6700create function md5(x varchar(50)) returns varchar(50)
6701return "Stored function md5";
6702
6703SET SQL_MODE='IGNORE_SPACE';
6704
6705select database(), database ();
6706select current_user(), current_user ();
6707select md5("aaa"), md5 ("aaa");
6708
6709# Non deterministic warnings from db_load_routine
6710--disable_warnings
6711select test.database(), test.database ();
6712select test.current_user(), test.current_user ();
6713select test.md5("aaa"), test.md5 ("aaa");
6714--enable_warnings
6715
6716SET SQL_MODE='';
6717
6718select database(), database ();
6719select current_user(), current_user ();
6720select md5("aaa"), md5 ("aaa");
6721
6722# Non deterministic warnings from db_load_routine
6723--disable_warnings
6724select test.database(), test.database ();
6725select test.current_user(), test.current_user ();
6726select test.md5("aaa"), test.md5 ("aaa");
6727--enable_warnings
6728
6729SET @@sql_mode=@save_sql_mode;
6730
6731drop function test.database;
6732drop function test.current_user;
6733drop function md5;
6734
6735use test;
6736delimiter |;
6737# End of BUG#22619
6738
6739--echo End of 5.1 tests
6740
6741#
6742# BUG#23760: ROW_COUNT() and store procedure not owrking together
6743#
6744--disable_warnings
6745DROP TABLE IF EXISTS bug23760|
6746DROP TABLE IF EXISTS bug23760_log|
6747DROP PROCEDURE IF EXISTS bug23760_update_log|
6748DROP PROCEDURE IF EXISTS bug23760_test_row_count|
6749DROP FUNCTION IF EXISTS bug23760_rc_test|
6750--enable_warnings
6751CREATE TABLE bug23760 (
6752  id INT NOT NULL AUTO_INCREMENT ,
6753  num INT NOT NULL ,
6754  PRIMARY KEY ( id )
6755)|
6756
6757CREATE TABLE bug23760_log (
6758 id INT NOT NULL AUTO_INCREMENT ,
6759 reason VARCHAR(50)NULL ,
6760 ammount INT NOT NULL ,
6761  PRIMARY KEY ( id )
6762)|
6763
6764CREATE PROCEDURE bug23760_update_log(r Varchar(50), a INT)
6765BEGIN
6766  INSERT INTO bug23760_log (reason, ammount) VALUES(r, a);
6767END|
6768
6769CREATE PROCEDURE bug23760_test_row_count()
6770BEGIN
6771  UPDATE bug23760 SET num = num + 1;
6772  CALL bug23760_update_log('Test is working', ROW_COUNT());
6773  UPDATE bug23760 SET num = num - 1;
6774END|
6775
6776
6777CREATE PROCEDURE bug23760_test_row_count2(level INT)
6778BEGIN
6779  IF level THEN
6780    UPDATE bug23760 SET num = num + 1;
6781    CALL bug23760_update_log('Test2 is working', ROW_COUNT());
6782    CALL bug23760_test_row_count2(level - 1);
6783  END IF;
6784END|
6785
6786CREATE FUNCTION bug23760_rc_test(in_var INT) RETURNS INT RETURN in_var|
6787
6788INSERT INTO bug23760 (num) VALUES (0), (1), (1), (2), (3), (5), (8)|
6789SELECT ROW_COUNT()|
6790
6791CALL bug23760_test_row_count()|
6792SELECT * FROM bug23760_log ORDER BY id|
6793
6794SET @save_max_sp_recursion= @@max_sp_recursion_depth|
6795SELECT @save_max_sp_recursion|
6796SET max_sp_recursion_depth= 5|
6797SELECT @@max_sp_recursion_depth|
6798CALL bug23760_test_row_count2(2)|
6799SELECT ROW_COUNT()|
6800SELECT * FROM bug23760_log ORDER BY id|
6801SELECT * FROM bug23760 ORDER by ID|
6802SET max_sp_recursion_depth= @save_max_sp_recursion|
6803
6804SELECT bug23760_rc_test(123)|
6805INSERT INTO bug23760 (num) VALUES (13), (21), (34), (55)|
6806SELECT bug23760_rc_test(ROW_COUNT())|
6807
6808DROP TABLE bug23760, bug23760_log|
6809DROP PROCEDURE bug23760_update_log|
6810DROP PROCEDURE bug23760_test_row_count|
6811DROP PROCEDURE bug23760_test_row_count2|
6812DROP FUNCTION bug23760_rc_test|
6813
6814#
6815# BUG#24117: server crash on a FETCH with a cursor on a table which is not in
6816#            the table cache
6817#
6818
6819--disable_warnings
6820DROP PROCEDURE IF EXISTS bug24117|
6821DROP TABLE IF EXISTS t3|
6822--enable_warnings
6823CREATE TABLE t3(c1 ENUM('abc'))|
6824INSERT INTO t3 VALUES('abc')|
6825CREATE PROCEDURE bug24117()
6826BEGIN
6827  DECLARE t3c1 ENUM('abc');
6828  DECLARE mycursor CURSOR FOR SELECT c1 FROM t3;
6829  OPEN mycursor;
6830  FLUSH TABLES;
6831  FETCH mycursor INTO t3c1;
6832  CLOSE mycursor;
6833END|
6834CALL bug24117()|
6835DROP PROCEDURE bug24117|
6836DROP TABLE t3|
6837
6838#
6839# Bug#8407(Stored functions/triggers ignore exception handler)
6840#
6841
6842--disable_warnings
6843drop function if exists func_8407_a|
6844drop function if exists func_8407_b|
6845--enable_warnings
6846
6847create function func_8407_a() returns int
6848begin
6849  declare x int;
6850
6851  declare continue handler for sqlexception
6852  begin
6853  end;
6854
6855  select 1 from no_such_view limit 1 into x;
6856
6857  return x;
6858end|
6859
6860create function func_8407_b() returns int
6861begin
6862  declare x int default 0;
6863
6864  declare continue handler for sqlstate '42S02'
6865  begin
6866    set x:= x+1000;
6867  end;
6868
6869  case (select 1 from no_such_view limit 1)
6870    when 1 then set x:= x+1;
6871    when 2 then set x:= x+2;
6872    else set x:= x+100;
6873  end case;
6874  set x:=x + 500;
6875
6876  return x;
6877end|
6878
6879select func_8407_a()|
6880select func_8407_b()|
6881
6882drop function func_8407_a|
6883drop function func_8407_b|
6884
6885#
6886# Bug#26503 (Illegal SQL exception handler code causes the server to crash)
6887#
6888
6889--disable_warnings
6890drop table if exists table_26503|
6891drop procedure if exists proc_26503_ok_1|
6892drop procedure if exists proc_26503_ok_2|
6893drop procedure if exists proc_26503_ok_3|
6894drop procedure if exists proc_26503_ok_4|
6895--enable_warnings
6896
6897create table table_26503(a int unique)|
6898
6899create procedure proc_26503_ok_1(v int)
6900begin
6901  declare i int default 5;
6902
6903  declare continue handler for sqlexception
6904  begin
6905    select 'caught something';
6906    retry:
6907    while i > 0 do
6908      begin
6909        set i = i - 1;
6910        select 'looping', i;
6911        iterate retry;
6912        select 'dead code';
6913      end;
6914    end while retry;
6915    select 'leaving handler';
6916  end;
6917
6918  select 'do something';
6919  insert into table_26503 values (v);
6920  select 'do something again';
6921  insert into table_26503 values (v);
6922end|
6923
6924create procedure proc_26503_ok_2(v int)
6925begin
6926  declare i int default 5;
6927
6928  declare continue handler for sqlexception
6929  begin
6930    select 'caught something';
6931    retry:
6932    while i > 0 do
6933      begin
6934        set i = i - 1;
6935        select 'looping', i;
6936        leave retry;
6937        select 'dead code';
6938      end;
6939    end while;
6940    select 'leaving handler';
6941  end;
6942
6943  select 'do something';
6944  insert into table_26503 values (v);
6945  select 'do something again';
6946  insert into table_26503 values (v);
6947end|
6948
6949## The outer retry label should not prevent using the inner label.
6950
6951create procedure proc_26503_ok_3(v int)
6952begin
6953  declare i int default 5;
6954
6955retry:
6956  begin
6957    declare continue handler for sqlexception
6958    begin
6959      select 'caught something';
6960      retry:
6961      while i > 0 do
6962        begin
6963          set i = i - 1;
6964          select 'looping', i;
6965          iterate retry;
6966          select 'dead code';
6967        end;
6968      end while retry;
6969      select 'leaving handler';
6970    end;
6971
6972    select 'do something';
6973    insert into table_26503 values (v);
6974    select 'do something again';
6975    insert into table_26503 values (v);
6976  end;
6977end|
6978
6979## The outer retry label should not prevent using the inner label.
6980
6981create procedure proc_26503_ok_4(v int)
6982begin
6983  declare i int default 5;
6984
6985retry:
6986  begin
6987    declare continue handler for sqlexception
6988    begin
6989      select 'caught something';
6990      retry:
6991      while i > 0 do
6992        begin
6993          set i = i - 1;
6994          select 'looping', i;
6995          leave retry;
6996          select 'dead code';
6997        end;
6998      end while;
6999      select 'leaving handler';
7000    end;
7001
7002    select 'do something';
7003    insert into table_26503 values (v);
7004    select 'do something again';
7005    insert into table_26503 values (v);
7006  end;
7007end|
7008
7009call proc_26503_ok_1(1)|
7010call proc_26503_ok_2(2)|
7011call proc_26503_ok_3(3)|
7012call proc_26503_ok_4(4)|
7013
7014drop table table_26503|
7015drop procedure proc_26503_ok_1|
7016drop procedure proc_26503_ok_2|
7017drop procedure proc_26503_ok_3|
7018drop procedure proc_26503_ok_4|
7019
7020#
7021# Bug#25373: Stored functions wasn't compared correctly which leads to a wrong
7022#            result.
7023#
7024--disable_warnings
7025DROP FUNCTION IF EXISTS bug25373|
7026--disable_warnings
7027CREATE FUNCTION bug25373(p1 INTEGER) RETURNS INTEGER
7028LANGUAGE SQL DETERMINISTIC
7029RETURN p1;|
7030CREATE TABLE t3 (f1 INT, f2 FLOAT)|
7031INSERT INTO t3 VALUES (1, 3.4), (1, 2), (1, 0.9), (2, 8), (2, 7)|
7032SELECT SUM(f2), bug25373(f1) FROM t3 GROUP BY bug25373(f1) WITH ROLLUP|
7033DROP FUNCTION bug25373|
7034DROP TABLE t3|
7035
7036
7037#
7038# BUG#25082: Default database change on trigger execution breaks replication.
7039#
7040# As it turned out, this bug has actually two bugs. So, here we have two test
7041# cases -- one in sp.test, the other in sp-security.test.
7042#
7043
7044#
7045# Test case 1: error on dropping the current database.
7046#
7047
7048# Prepare.
7049
7050--disable_warnings
7051DROP DATABASE IF EXISTS mysqltest1|
7052DROP DATABASE IF EXISTS mysqltest2|
7053--enable_warnings
7054
7055CREATE DATABASE mysqltest1|
7056CREATE DATABASE mysqltest2|
7057
7058# Test.
7059
7060CREATE PROCEDURE mysqltest1.p1()
7061  DROP DATABASE mysqltest2|
7062
7063use mysqltest2|
7064
7065CALL mysqltest1.p1()|
7066
7067SELECT DATABASE()|
7068
7069# Cleanup.
7070
7071DROP DATABASE mysqltest1|
7072
7073use test|
7074
7075
7076#
7077# Bug#20777: Function w BIGINT UNSIGNED shows diff. behaviour --ps-protocol
7078--disable_warnings
7079drop function if exists bug20777|
7080drop table if exists examplebug20777|
7081--enable_warnings
7082create function bug20777(f1 bigint unsigned) returns bigint unsigned
7083begin
7084  set f1 = (f1 - 10); set f1 = (f1 + 10);
7085return f1;
7086end|
7087delimiter ;|
7088select bug20777(9223372036854775803) as '9223372036854775803   2**63-5';
7089select bug20777(9223372036854775804) as '9223372036854775804   2**63-4';
7090select bug20777(9223372036854775805) as '9223372036854775805   2**63-3';
7091select bug20777(9223372036854775806) as '9223372036854775806   2**63-2';
7092select bug20777(9223372036854775807) as '9223372036854775807   2**63-1';
7093select bug20777(9223372036854775808) as '9223372036854775808   2**63+0';
7094select bug20777(9223372036854775809) as '9223372036854775809   2**63+1';
7095select bug20777(9223372036854775810) as '9223372036854775810   2**63+2';
7096--error ER_DATA_OUT_OF_RANGE
7097select bug20777(-9223372036854775808) as 'lower bounds signed bigint';
7098select bug20777(9223372036854775807) as 'upper bounds signed bigint';
7099--error ER_DATA_OUT_OF_RANGE
7100select bug20777(0) as 'lower bounds unsigned bigint';
7101select bug20777(18446744073709551615) as 'upper bounds unsigned bigint';
7102select bug20777(18446744073709551616) as 'upper bounds unsigned bigint + 1';
7103--error ER_DATA_OUT_OF_RANGE
7104select bug20777(-1) as 'lower bounds unsigned bigint - 1';
7105
7106create table examplebug20777 as select
7107  0 as 'i',
7108  bug20777(9223372036854775806) as '2**63-2',
7109  bug20777(9223372036854775807) as '2**63-1',
7110  bug20777(9223372036854775808) as '2**63',
7111  bug20777(9223372036854775809) as '2**63+1',
7112  bug20777(18446744073709551614) as '2**64-2',
7113  bug20777(18446744073709551615) as '2**64-1',
7114  bug20777(18446744073709551616) as '2**64';
7115insert into examplebug20777 values (1, 9223372036854775806, 9223372036854775807, 223372036854775808, 9223372036854775809, 18446744073709551614, 18446744073709551615, 8446744073709551616);
7116show create table examplebug20777;
7117select * from examplebug20777 order by i;
7118
7119drop table examplebug20777;
7120select bug20777(18446744073709551613)+1;
7121drop function bug20777;
7122delimiter |;
7123
7124
7125#
7126# BUG#5274: Stored procedure crash if length of CHAR variable too great.
7127#
7128
7129# Prepare.
7130
7131--disable_warnings
7132DROP FUNCTION IF EXISTS bug5274_f1|
7133DROP FUNCTION IF EXISTS bug5274_f2|
7134--enable_warnings
7135
7136# Test.
7137
7138CREATE FUNCTION bug5274_f1(p1 CHAR) RETURNS CHAR
7139  RETURN CONCAT(p1, p1)|
7140
7141CREATE FUNCTION bug5274_f2() RETURNS CHAR
7142BEGIN
7143  DECLARE v1 INT DEFAULT 0;
7144  DECLARE v2 CHAR DEFAULT 'x';
7145
7146  WHILE v1 < 30 DO
7147    SET v1 = v1 + 1;
7148    SET v2 = bug5274_f1(v2);
7149  END WHILE;
7150
7151  RETURN v2;
7152END|
7153
7154SELECT bug5274_f2()|
7155
7156# Cleanup.
7157
7158DROP FUNCTION bug5274_f1|
7159DROP FUNCTION bug5274_f2|
7160
7161#
7162# Bug#21513 (SP having body starting with quoted label rendered unusable)
7163#
7164--disable_warnings
7165drop procedure if exists proc_21513|
7166--enable_warnings
7167
7168create procedure proc_21513()`my_label`:BEGIN END|
7169show create procedure proc_21513|
7170
7171drop procedure proc_21513|
7172
7173###
7174--echo End of 5.0 tests.
7175
7176#
7177# BUG#NNNN: New bug synopsis
7178#
7179#--disable_warnings
7180#drop procedure if exists bugNNNN|
7181#--enable_warnings
7182#create procedure bugNNNN...
7183#
7184# Add bugs above this line. Use existing tables t1 and t2 when
7185# practical, or create table t3,t4 etc temporarily (and drop them).
7186# NOTE: The delimiter is `|`, and not `;`. It is changed to `;`
7187#       at the end of the file!
7188#
7189
7190delimiter ;|
7191drop table t1,t2;
7192
7193# Disable warnings to allow test run without InnoDB
7194--disable_warnings
7195CREATE TABLE t1 (a int auto_increment primary key) engine=MyISAM;
7196CREATE TABLE t2 (a int auto_increment primary key, b int) engine=innodb;
7197--enable_warnings
7198set @a=0;
7199
7200delimiter |;
7201CREATE function bug27354() RETURNS int not deterministic
7202begin
7203insert into t1 values (null);
7204set @a=@a+1;
7205return @a;
7206end|
7207
7208delimiter ;|
7209update t2 set b=1 where a=bug27354();
7210select count(t_1.a),count(t_2.a) from t1 as t_1, t2 as t_2 /* must be 0,0 */;
7211insert into t2 values (1,1),(2,2),(3,3);
7212update t2 set b=-b where a=bug27354();
7213select * from t2 /* must return 1,-1 ... */;
7214select count(*) from t1 /* must be 3 */;
7215
7216
7217drop table t1,t2;
7218drop function   bug27354;
7219
7220#
7221# Bug #28605: SHOW CREATE VIEW with views using stored_procedures no longer
7222# showing SP names.
7223#
7224CREATE TABLE t1 (a INT);
7225INSERT INTO t1 VALUES (1),(2);
7226
7227CREATE FUNCTION metered(a INT) RETURNS INT RETURN 12;
7228
7229CREATE VIEW v1 AS SELECT test.metered(a) as metered FROM t1;
7230
7231SHOW CREATE VIEW v1;
7232
7233DROP VIEW v1;
7234DROP FUNCTION metered;
7235DROP TABLE t1;
7236
7237#
7238# Bug#29834: Accessing a view column by name in SP/PS causes a memory leak.
7239#
7240# This is leak test. Run with large number assigned to $execute_cnt,
7241# $p1_cnt, $p2_cnt, @p1_p2_cnt, $f1_normal_cnt or $f1_prep_cnt variables.
7242#
7243
7244let $execute_cnt= 2;
7245let $p1_cnt= 2;
7246let $p2_cnt= 2;
7247SET @p1_p2_cnt= 2;
7248let $f1_normal_cnt= 2;
7249let $f1_prep_cnt= 2;
7250
7251CREATE TABLE t1 (c1 INT);
7252CREATE VIEW v1 AS SELECT * FROM t1;
7253
7254PREPARE s1 FROM 'SELECT c1 FROM v1';
7255while ($execute_cnt)
7256{
7257  EXECUTE s1;
7258  dec $execute_cnt;
7259}
7260
7261DELIMITER |;
7262
7263CREATE PROCEDURE p1(IN loops BIGINT(19) UNSIGNED)
7264BEGIN
7265  WHILE loops > 0 DO
7266    SELECT c1 FROM v1;
7267    SET loops = loops - 1;
7268  END WHILE;
7269END|
7270
7271CREATE PROCEDURE p2(IN loops BIGINT(19) UNSIGNED)
7272BEGIN
7273  WHILE loops > 0 DO
7274    SELECT c1 FROM v1;
7275    CALL p1(@p1_p2_cnt);
7276    SET loops = loops - 1;
7277  END WHILE;
7278END|
7279
7280CREATE FUNCTION f1(loops INT UNSIGNED)
7281  RETURNS INT
7282BEGIN
7283  DECLARE tmp INT;
7284  WHILE loops > 0 DO
7285    SELECT c1 INTO tmp FROM v1;
7286    SET loops = loops - 1;
7287  END WHILE;
7288  RETURN loops;
7289END|
7290
7291DELIMITER ;|
7292
7293eval CALL p1($p1_cnt);
7294eval CALL p2($p2_cnt);
7295
7296eval SELECT f1($f1_normal_cnt);
7297
7298eval PREPARE s1 FROM 'SELECT f1($f1_prep_cnt)';
7299EXECUTE s1;
7300EXECUTE s1;
7301
7302DROP PROCEDURE p1;
7303DROP PROCEDURE p2;
7304DROP FUNCTION f1;
7305DROP VIEW v1;
7306DROP TABLE t1;
7307
7308#
7309# Bug#28551 "The warning 'No database selected' is reported when calling
7310# stored procedures"
7311#
7312--disable_warnings
7313drop database if exists mysqltest_db1;
7314--enable_warnings
7315create database mysqltest_db1;
7316create procedure mysqltest_db1.sp_bug28551() begin end;
7317call mysqltest_db1.sp_bug28551();
7318show warnings;
7319drop database mysqltest_db1;
7320#
7321# Bug#29050 Creation of a legal stored procedure fails if a database is not
7322# selected prior
7323#
7324--disable_warnings
7325drop database if exists mysqltest_db1;
7326drop table if exists test.t1;
7327--enable_warnings
7328create database mysqltest_db1;
7329use mysqltest_db1;
7330# For the sake of its side effect
7331drop database mysqltest_db1;
7332# Now we have no current database selected.
7333create table test.t1 (id int);
7334insert into test.t1 (id) values (1);
7335delimiter //;
7336create procedure test.sp_bug29050() begin select * from t1; end//
7337delimiter ;//
7338show warnings;
7339call test.sp_bug29050();
7340show warnings;
7341# Restore the old current database
7342use test;
7343drop procedure sp_bug29050;
7344drop table t1;
7345
7346#
7347# Bug #30120 SP with local variables with non-ASCII names crashes server.
7348#
7349
7350SET NAMES latin1;
7351
7352DELIMITER |;
7353
7354CREATE PROCEDURE p1()
7355BEGIN
7356  DECLARE ��� INT;
7357  SELECT ���;
7358END|
7359
7360DELIMITER ;|
7361
7362CALL p1();
7363
7364set @@character_set_client=@save_character_set_client;
7365set @@character_set_results=@save_character_set_client;
7366DROP PROCEDURE p1;
7367
7368#
7369# Bug#25411 (trigger code truncated)
7370#
7371
7372--disable_warnings
7373drop procedure if exists proc_25411_a;
7374drop procedure if exists proc_25411_b;
7375drop procedure if exists proc_25411_c;
7376--enable_warnings
7377
7378delimiter $$;
7379
7380create procedure proc_25411_a()
7381begin
7382  /* real comment */
7383  select 1;
7384  /*! select 2; */
7385  select 3;
7386  /*!00000 select 4; */
7387  /*!999999 select 5; */
7388end
7389$$
7390
7391create procedure proc_25411_b(
7392/* real comment */
7393/*! p1 int, */
7394/*!00000 p2 int */
7395/*!999999 ,p3 int */
7396)
7397begin
7398  select p1, p2;
7399end
7400$$
7401
7402create procedure proc_25411_c()
7403begin
7404  select 1/*!,2*//*!00000,3*//*!999999,4*/;
7405  select 1/*! ,2*//*!00000 ,3*//*!999999 ,4*/;
7406  select 1/*!,2 *//*!00000,3 *//*!999999,4 */;
7407  select 1/*! ,2 *//*!00000 ,3 *//*!999999 ,4 */;
7408  select 1 /*!,2*/ /*!00000,3*/ /*!999999,4*/ ;
7409end
7410$$
7411
7412delimiter ;$$
7413
7414show create procedure proc_25411_a;
7415call proc_25411_a();
7416
7417show create procedure proc_25411_b;
7418select name, param_list, body from mysql.proc where name like "%25411%";
7419call proc_25411_b(10, 20);
7420
7421show create procedure proc_25411_c;
7422call proc_25411_c();
7423
7424drop procedure proc_25411_a;
7425drop procedure proc_25411_b;
7426drop procedure proc_25411_c;
7427
7428
7429#
7430# Bug#26302 (MySQL server cuts off trailing "*/" from comments in SP/func)
7431#
7432
7433--disable_warnings
7434drop procedure if exists proc_26302;
7435--enable_warnings
7436
7437create procedure proc_26302()
7438select 1 /* testing */;
7439
7440show create procedure proc_26302;
7441
7442select ROUTINE_NAME, ROUTINE_DEFINITION from information_schema.ROUTINES
7443where ROUTINE_NAME = "proc_26302";
7444
7445drop procedure proc_26302;
7446
7447
7448# Bug #29338: no optimization for stored functions with a trivial body
7449# always returning constant.
7450#
7451
7452CREATE FUNCTION f1() RETURNS INT DETERMINISTIC RETURN 2;
7453CREATE FUNCTION f2(I INT) RETURNS INT DETERMINISTIC RETURN 3;
7454
7455CREATE TABLE t1 (c1 INT, INDEX(c1));
7456
7457INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
7458
7459CREATE VIEW v1 AS SELECT c1 FROM t1;
7460
7461EXPLAIN SELECT * FROM t1 WHERE c1=1;
7462EXPLAIN SELECT * FROM t1 WHERE c1=f1();
7463
7464EXPLAIN SELECT * FROM v1 WHERE c1=1;
7465EXPLAIN SELECT * FROM v1 WHERE c1=f1();
7466
7467EXPLAIN SELECT * FROM t1 WHERE c1=f2(10);
7468EXPLAIN SELECT * FROM t1 WHERE c1=f2(c1);
7469EXPLAIN SELECT * FROM t1 WHERE c1=f2(rand());
7470
7471
7472DROP VIEW v1;
7473DROP FUNCTION f1;
7474DROP FUNCTION f2;
7475DROP TABLE t1;
7476
7477#
7478# Bug#29408 Cannot find view in columns table if the selection contains a function
7479#
7480delimiter |;
7481
7482create function f1()
7483    returns int(11)
7484not deterministic
7485contains sql
7486sql security definer
7487comment ''
7488begin
7489  declare x int(11);
7490  set x=-1;
7491   return x;
7492end|
7493
7494delimiter ;|
7495
7496create view v1 as select 1 as one, f1() as days;
7497
7498connect (bug29408, localhost, root,,*NO-ONE*);
7499connection bug29408;
7500
7501show create view test.v1;
7502select column_name from information_schema.columns
7503where table_name='v1' and table_schema='test';
7504
7505connection default;
7506disconnect bug29408;
7507drop view v1;
7508drop function f1;
7509
7510#
7511# Bug#13675: DATETIME/DATE type in store proc param seems to be converted as
7512# varbinary
7513#
7514
7515--echo
7516--echo # Bug#13675.
7517--echo
7518
7519--disable_warnings
7520DROP PROCEDURE IF EXISTS p1;
7521DROP PROCEDURE IF EXISTS p2;
7522
7523DROP TABLE IF EXISTS t1;
7524--enable_warnings
7525
7526--echo
7527
7528CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v;
7529
7530CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v;
7531
7532--echo
7533CALL p1(NOW());
7534SHOW CREATE TABLE t1;
7535
7536--echo
7537DROP TABLE t1;
7538
7539--echo
7540CALL p1('text');
7541SHOW CREATE TABLE t1;
7542
7543--echo
7544DROP TABLE t1;
7545
7546--echo
7547CALL p2(10);
7548SHOW CREATE TABLE t1;
7549
7550--echo
7551DROP TABLE t1;
7552
7553--echo
7554CALL p2('text');
7555SHOW CREATE TABLE t1;
7556
7557--echo
7558DROP TABLE t1;
7559
7560--echo
7561DROP PROCEDURE p1;
7562DROP PROCEDURE p2;
7563
7564###########################################################################
7565
7566#
7567# Bug#31035: select from function, group by result crasher.
7568#
7569
7570###########################################################################
7571
7572--echo
7573
7574--echo #
7575--echo # Bug#31035.
7576--echo #
7577
7578--echo
7579
7580--echo #
7581--echo # - Prepare.
7582--echo #
7583
7584--echo
7585
7586--disable_warnings
7587DROP TABLE IF EXISTS t1;
7588DROP FUNCTION IF EXISTS f1;
7589DROP FUNCTION IF EXISTS f2;
7590DROP FUNCTION IF EXISTS f3;
7591DROP FUNCTION IF EXISTS f4;
7592--enable_warnings
7593
7594--echo
7595
7596--echo #
7597--echo # - Create required objects.
7598--echo #
7599
7600--echo
7601
7602CREATE TABLE t1(c1 INT);
7603
7604--echo
7605
7606INSERT INTO t1 VALUES (1), (2), (3);
7607
7608--echo
7609
7610CREATE FUNCTION f1()
7611  RETURNS INT
7612  NOT DETERMINISTIC
7613    RETURN 1;
7614
7615--echo
7616
7617CREATE FUNCTION f2(p INT)
7618  RETURNS INT
7619  NOT DETERMINISTIC
7620    RETURN 1;
7621
7622--echo
7623
7624CREATE FUNCTION f3()
7625  RETURNS INT
7626  DETERMINISTIC
7627    RETURN 1;
7628
7629--echo
7630
7631CREATE FUNCTION f4(p INT)
7632  RETURNS INT
7633  DETERMINISTIC
7634    RETURN 1;
7635
7636--echo
7637
7638--echo #
7639--echo # - Check.
7640--echo #
7641
7642--echo
7643
7644# Not deterministic function, no arguments.
7645
7646SELECT f1() AS a FROM t1 GROUP BY a;
7647
7648--echo
7649
7650# Not deterministic function, non-constant argument.
7651
7652SELECT f2(@a) AS a FROM t1 GROUP BY a;
7653
7654--echo
7655
7656# Deterministic function, no arguments.
7657
7658SELECT f3() AS a FROM t1 GROUP BY a;
7659
7660--echo
7661
7662# Deterministic function, constant argument.
7663
7664SELECT f4(0) AS a FROM t1 GROUP BY a;
7665
7666--echo
7667
7668# Deterministic function, non-constant argument.
7669
7670SELECT f4(@a) AS a FROM t1 GROUP BY a;
7671
7672--echo
7673
7674--echo #
7675--echo # - Cleanup.
7676--echo #
7677
7678--echo
7679
7680DROP TABLE t1;
7681DROP FUNCTION f1;
7682DROP FUNCTION f2;
7683DROP FUNCTION f3;
7684DROP FUNCTION f4;
7685
7686--echo
7687
7688###########################################################################
7689
7690#
7691# Bug#31191: JOIN in combination with stored function crashes the server.
7692#
7693
7694###########################################################################
7695
7696--echo #
7697--echo # Bug#31191.
7698--echo #
7699
7700--echo
7701
7702--echo #
7703--echo # - Prepare.
7704--echo #
7705
7706--echo
7707
7708--disable_warnings
7709DROP TABLE IF EXISTS t1;
7710DROP TABLE IF EXISTS t2;
7711DROP FUNCTION IF EXISTS f1;
7712--enable_warnings
7713
7714--echo
7715
7716--echo #
7717--echo # - Create required objects.
7718--echo #
7719
7720--echo
7721
7722CREATE TABLE t1 (
7723   id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7724   barcode INT(8) UNSIGNED ZEROFILL nOT NULL,
7725   PRIMARY KEY  (id),
7726   UNIQUE KEY barcode (barcode)
7727);
7728
7729--echo
7730
7731INSERT INTO t1 (id, barcode) VALUES (1, 12345678);
7732INSERT INTO t1 (id, barcode) VALUES (2, 12345679);
7733
7734--echo
7735
7736CREATE TABLE test.t2 (
7737   id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7738   barcode BIGINT(11) UNSIGNED ZEROFILL NOT NULL,
7739   PRIMARY KEY  (id)
7740);
7741
7742--echo
7743
7744INSERT INTO test.t2 (id, barcode) VALUES (1, 12345106708);
7745INSERT INTO test.t2 (id, barcode) VALUES (2, 12345106709);
7746
7747--echo
7748
7749CREATE FUNCTION f1(p INT(8))
7750  RETURNS BIGINT(11) UNSIGNED
7751  READS SQL DATA
7752    RETURN FLOOR(p/1000)*1000000 + 100000 + FLOOR((p MOD 1000)/10)*100 + (p MOD 10);
7753
7754--echo
7755
7756--echo #
7757--echo # - Check.
7758--echo #
7759
7760--echo
7761
7762SELECT DISTINCT t1.barcode, f1(t1.barcode)
7763FROM t1
7764INNER JOIN t2
7765ON f1(t1.barcode) = t2.barcode
7766WHERE t1.barcode=12345678;
7767
7768--echo
7769
7770--echo #
7771--echo # - Cleanup.
7772--echo #
7773
7774--echo
7775
7776DROP TABLE t1;
7777DROP TABLE t2;
7778DROP FUNCTION f1;
7779
7780--echo
7781
7782###########################################################################
7783
7784#
7785# Bug#31226: Group by function crashes mysql.
7786#
7787
7788###########################################################################
7789
7790--echo #
7791--echo # Bug#31226.
7792--echo #
7793
7794--echo
7795
7796--echo #
7797--echo # - Prepare.
7798--echo #
7799
7800--echo
7801
7802--disable_warnings
7803DROP TABLE IF EXISTS t1;
7804DROP FUNCTION IF EXISTS f1;
7805--enable_warnings
7806
7807--echo
7808
7809--echo #
7810--echo # - Create required objects.
7811--echo #
7812
7813--echo
7814
7815CREATE TABLE t1(id INT);
7816
7817--echo
7818
7819INSERT INTO t1 VALUES (1), (2), (3);
7820
7821--echo
7822
7823CREATE FUNCTION f1()
7824  RETURNS DATETIME
7825  NOT DETERMINISTIC NO SQL
7826    RETURN NOW();
7827
7828--echo
7829
7830--echo #
7831--echo # - Check.
7832--echo #
7833
7834--echo
7835
7836--replace_column 1 <timestamp>
7837SELECT f1() FROM t1 GROUP BY 1;
7838
7839--echo
7840
7841--echo #
7842--echo # - Cleanup.
7843--echo #
7844
7845--echo
7846
7847DROP TABLE t1;
7848DROP FUNCTION f1;
7849
7850--echo
7851
7852###########################################################################
7853
7854#
7855# Bug#28318 (CREATE FUNCTION (UDF) requires a schema)
7856#
7857
7858--disable_warnings
7859DROP PROCEDURE IF EXISTS db28318_a.t1;
7860DROP PROCEDURE IF EXISTS db28318_b.t2;
7861DROP DATABASE IF EXISTS db28318_a;
7862DROP DATABASE IF EXISTS db28318_b;
7863--enable_warnings
7864
7865CREATE DATABASE db28318_a;
7866CREATE DATABASE db28318_b;
7867
7868CREATE PROCEDURE db28318_a.t1() SELECT "db28318_a.t1";
7869CREATE PROCEDURE db28318_b.t2() CALL t1();
7870
7871use db28318_a;
7872
7873# In db28318_b.t2, t1 refers to db28318_b.t1
7874--error ER_SP_DOES_NOT_EXIST
7875CALL db28318_b.t2();
7876
7877DROP PROCEDURE db28318_a.t1;
7878DROP PROCEDURE db28318_b.t2;
7879DROP DATABASE db28318_a;
7880DROP DATABASE db28318_b;
7881use test;
7882
7883###########################################################################
7884
7885#
7886# Bug#29770 Two handlers are allowed to catch an error in an stored procedure.
7887#
7888
7889--disable_warnings
7890DROP TABLE IF EXISTS t1;
7891DROP PROCEDURE IF EXISTS bug29770;
7892--enable_warnings
7893
7894CREATE TABLE t1(a int);
7895delimiter |;
7896CREATE PROCEDURE bug29770()
7897BEGIN
7898  DECLARE CONTINUE HANDLER FOR SQLSTATE '42S22' SET @state:= 'run';
7899  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @exception:= 'run';
7900  SELECT x FROM t1;
7901END|
7902delimiter ;|
7903CALL bug29770();
7904SELECT @state, @exception;
7905DROP TABLE t1;
7906DROP PROCEDURE bug29770;
7907
7908#
7909# Bug#33618 Crash in sp_rcontext
7910#
7911
7912use test;
7913
7914--disable_warnings
7915drop table if exists t_33618;
7916drop procedure if exists proc_33618;
7917--enable_warnings
7918
7919create table t_33618 (`a` int, unique(`a`), `b` varchar(30)) engine=myisam;
7920insert into t_33618 (`a`,`b`) values (1,'1'),(2,'2');
7921
7922delimiter //;
7923
7924create procedure proc_33618(num int)
7925begin
7926  declare count1 int default '0';
7927  declare vb varchar(30);
7928  declare last_row int;
7929
7930  while(num>=1) do
7931    set num=num-1;
7932    begin
7933      declare cur1 cursor for select `a` from t_33618;
7934      declare continue handler for not found set last_row = 1;
7935      set last_row:=0;
7936      open cur1;
7937      rep1:
7938      repeat
7939        begin
7940          declare exit handler for 1062 begin end;
7941          fetch cur1 into vb;
7942          if (last_row = 1) then
7943            leave rep1;
7944          end if;
7945        end;
7946        until last_row=1
7947      end repeat;
7948      close cur1;
7949    end;
7950  end while;
7951end//
7952
7953delimiter ;//
7954
7955call proc_33618(20);
7956
7957drop table t_33618;
7958drop procedure proc_33618;
7959
7960--echo #
7961--echo # Bug#30787: Stored function ignores user defined alias.
7962--echo #
7963use test;
7964--disable_warnings
7965drop function if exists func30787;
7966--enable_warnings
7967create table t1(f1 int);
7968insert into t1 values(1),(2);
7969delimiter |;
7970create function func30787(p1 int) returns int
7971begin
7972  return p1;
7973end |
7974delimiter ;|
7975select (select func30787(f1)) as ttt from t1;
7976drop function func30787;
7977drop table t1;
7978
7979#
7980# Bug #33811: Call to stored procedure with SELECT * / RIGHT JOIN fails
7981# after the first time
7982#
7983CREATE TABLE t1 (id INT);
7984INSERT INTO t1 VALUES (1),(2),(3),(4);
7985
7986CREATE PROCEDURE test_sp()
7987  SELECT t1.* FROM t1 RIGHT JOIN t1 t2 ON t1.id=t2.id;
7988
7989CALL test_sp();
7990CALL test_sp();
7991
7992DROP PROCEDURE test_sp;
7993DROP TABLE t1;
7994
7995
7996###########################################################################
7997#
7998# Bug#38291 memory corruption and server crash with view/sp/function
7999#
8000
8001create table t1(c1 INT);
8002create function f1(p1 int) returns varchar(32)
8003  return 'aaa';
8004create view v1 as select f1(c1) as parent_control_name from t1;
8005
8006delimiter //;
8007create procedure p1()
8008begin
8009    select parent_control_name as c1 from v1;
8010end //
8011delimiter ;//
8012
8013call p1();
8014call p1();
8015
8016drop procedure p1;
8017drop function f1;
8018drop view v1;
8019drop table t1;
8020
8021#
8022# Bug#38469 invalid memory read and/or crash with utf8 text field, stored procedure, uservar
8023#
8024delimiter $;
8025--disable_warnings
8026drop procedure if exists `p2` $
8027--enable_warnings
8028create procedure `p2`(in `a` text charset utf8)
8029begin
8030        declare `pos` int default 1;
8031        declare `str` text charset utf8;
8032        set `str` := `a`;
8033        select substr(`str`, `pos`+ 1 ) into `str`;
8034end $
8035delimiter ;$
8036call `p2`('s s s s s s');
8037drop procedure `p2`;
8038
8039#
8040# Bug#38823: Invalid memory access when a SP statement does wildcard expansion
8041#
8042
8043--disable_warnings
8044drop table if exists t1;
8045drop procedure if exists p1;
8046--enable_warnings
8047
8048delimiter $;
8049create procedure p1() begin select * from t1; end$
8050--error ER_NO_SUCH_TABLE
8051call p1$
8052create table t1 (a integer)$
8053call p1$
8054alter table t1 add b integer$
8055call p1$
8056delimiter ;$
8057
8058drop table t1;
8059drop procedure p1;
8060
8061--echo # ------------------------------------------------------------------
8062--echo # -- End of 5.0 tests
8063--echo # ------------------------------------------------------------------
8064
8065###########################################################################
8066
8067#
8068# Bug#20550: Stored function: wrong RETURN type metadata when used in a VIEW.
8069#
8070
8071###########################################################################
8072
8073--echo
8074
8075--echo #
8076--echo # Bug#20550.
8077--echo #
8078
8079--echo
8080
8081--echo #
8082--echo # - Prepare.
8083--echo #
8084
8085--echo
8086
8087--disable_warnings
8088DROP VIEW IF EXISTS v1;
8089DROP VIEW IF EXISTS v2;
8090DROP FUNCTION IF EXISTS f1;
8091DROP FUNCTION IF EXISTS f2;
8092--enable_warnings
8093
8094--echo
8095
8096--echo #
8097--echo # - Create required objects.
8098--echo #
8099
8100--echo
8101
8102CREATE FUNCTION f1() RETURNS VARCHAR(65525) RETURN 'Hello';
8103
8104--echo
8105
8106CREATE FUNCTION f2() RETURNS TINYINT RETURN 1;
8107
8108--echo
8109
8110CREATE VIEW v1 AS SELECT f1();
8111
8112--echo
8113
8114CREATE VIEW v2 AS SELECT f2();
8115
8116--echo
8117
8118--echo #
8119--echo # - Check.
8120--echo #
8121
8122--echo
8123
8124SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v1';
8125
8126--echo
8127
8128SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v2';
8129
8130--echo
8131
8132--echo #
8133--echo # - Cleanup.
8134--echo #
8135
8136--echo
8137
8138DROP FUNCTION f1;
8139DROP FUNCTION f2;
8140DROP VIEW v1;
8141DROP VIEW v2;
8142
8143--echo
8144
8145###########################################################################
8146
8147#
8148# Bug#24923: Functions with ENUM issues.
8149#
8150
8151###########################################################################
8152
8153--echo #
8154--echo # - Bug#24923: prepare.
8155--echo #
8156
8157--echo
8158
8159--disable_warnings
8160DROP FUNCTION IF EXISTS f1;
8161--enable_warnings
8162
8163--echo
8164
8165--echo #
8166--echo # - Bug#24923: create required objects.
8167--echo #
8168
8169--echo
8170
8171delimiter |;
8172
8173CREATE FUNCTION f1(p INT)
8174  RETURNS ENUM ('Very_long_enum_element_identifier',
8175                'Another_very_long_enum_element_identifier')
8176  BEGIN
8177    CASE p
8178    WHEN 1 THEN
8179      RETURN 'Very_long_enum_element_identifier';
8180    ELSE
8181      RETURN 'Another_very_long_enum_element_identifier';
8182    END CASE;
8183  END|
8184
8185delimiter ;|
8186
8187--echo
8188
8189--echo #
8190--echo # - Bug#24923: check.
8191--echo #
8192
8193--echo
8194
8195SELECT f1(1);
8196
8197--echo
8198
8199SELECT f1(2);
8200
8201--echo
8202
8203SHOW CREATE FUNCTION f1;
8204
8205--echo #
8206--echo # - Bug#24923: cleanup.
8207--echo #
8208
8209--echo
8210
8211DROP FUNCTION f1;
8212
8213--echo
8214
8215###########################################################################
8216
8217#
8218# Bug#32633 Can not create any routine if SQL_MODE=no_engine_substitution
8219#
8220# Ensure that when new SQL modes are introduced, they are also added to
8221# the mysql.proc table.
8222#
8223
8224--disable_warnings
8225drop procedure if exists p;
8226--enable_warnings
8227set @old_mode= @@sql_mode;
8228set @@sql_mode= cast(pow(2,32)-1 as unsigned integer);
8229select @@sql_mode into @full_mode;
8230create procedure p() as begin end;
8231call p();
8232set @@sql_mode= @old_mode;
8233# Rename SQL modes that differ in name between the server and the table definition.
8234select replace(@full_mode, 'ALLOW_INVALID_DATES', 'INVALID_DATES') into @full_mode;
8235select name from mysql.proc where name = 'p' and sql_mode = @full_mode;
8236drop procedure p;
8237
8238#
8239# Bug#43962 "Packets out of order" calling a SHOW TABLE STATUS
8240#
8241
8242DELIMITER //;
8243CREATE DEFINER = 'root'@'localhost' PROCEDURE p1()
8244NOT DETERMINISTIC
8245CONTAINS SQL
8246SQL SECURITY DEFINER
8247COMMENT ''
8248BEGIN
8249 SHOW TABLE STATUS like 't1';
8250END;//
8251DELIMITER ;//
8252
8253
8254CREATE TABLE t1 (f1 INT);
8255--disable_result_log
8256let $tab_count= 4;
8257while ($tab_count)
8258{
8259 EVAL CALL p1();
8260 dec $tab_count ;
8261}
8262--enable_result_log
8263DROP PROCEDURE p1;
8264DROP TABLE t1;
8265
8266#
8267# Bug#47649 crash during CALL procedure
8268#
8269CREATE TABLE t1 ( f1 integer, primary key (f1));
8270CREATE TABLE t2 LIKE t1;
8271CREATE TEMPORARY TABLE t3 LIKE t1;
8272delimiter |;
8273CREATE PROCEDURE p1 () BEGIN SELECT f1 FROM t3 AS A WHERE A.f1 IN ( SELECT f1 FROM t3 ) ;
8274END|
8275delimiter ;|
8276CALL p1;
8277CREATE VIEW t3 AS SELECT f1 FROM t2 A WHERE A.f1 IN ( SELECT f1 FROM t2 );
8278DROP TABLE t3;
8279CALL p1;
8280CALL p1;
8281DROP PROCEDURE p1;
8282DROP TABLE t1, t2;
8283DROP VIEW t3;
8284
8285--echo #
8286--echo # Bug #46629: Item_in_subselect::val_int(): Assertion `0'
8287--echo # on subquery inside a SP
8288--echo #
8289CREATE TABLE t1(a INT);
8290CREATE TABLE t2(a INT, b INT PRIMARY KEY);
8291
8292DELIMITER |;
8293CREATE PROCEDURE p1 ()
8294BEGIN
8295  SELECT a FROM t1 A WHERE A.b IN (SELECT b FROM t2 AS B);
8296END|
8297DELIMITER ;|
8298--error ER_BAD_FIELD_ERROR
8299CALL p1;
8300--error ER_BAD_FIELD_ERROR
8301CALL p1;
8302DROP PROCEDURE p1;
8303DROP TABLE t1, t2;
8304
8305--echo #
8306--echo # Bug#47627: SET @@{global.session}.local_variable in stored routine causes crash
8307--echo # Bug#48626: Crash or lost connection using SET for declared variables with @@
8308--echo #
8309
8310--disable_warnings
8311DROP PROCEDURE IF EXISTS p1;
8312DROP PROCEDURE IF EXISTS p2;
8313DROP PROCEDURE IF EXISTS p3;
8314--enable_warnings
8315
8316delimiter //;
8317
8318--error ER_UNKNOWN_SYSTEM_VARIABLE
8319CREATE PROCEDURE p1()
8320BEGIN
8321  DECLARE v INT DEFAULT 0;
8322  SET @@SESSION.v= 10;
8323END//
8324
8325CREATE PROCEDURE p2()
8326BEGIN
8327  DECLARE v INT DEFAULT 0;
8328  SET v= 10;
8329END//
8330call p2()//
8331
8332--error ER_UNKNOWN_SYSTEM_VARIABLE
8333CREATE PROCEDURE p3()
8334BEGIN
8335  DECLARE v INT DEFAULT 0;
8336  SELECT @@SESSION.v;
8337END//
8338
8339--error ER_UNKNOWN_SYSTEM_VARIABLE
8340CREATE PROCEDURE p4()
8341BEGIN
8342  DECLARE v INT DEFAULT 0;
8343  SET @@GLOBAL.v= 10;
8344END//
8345
8346CREATE PROCEDURE p5()
8347BEGIN
8348  DECLARE init_connect INT DEFAULT 0;
8349  SET init_connect= 10;
8350  SET @@GLOBAL.init_connect= 'SELECT 1';
8351  SET @@SESSION.IDENTITY= 1;
8352  SELECT @@SESSION.IDENTITY;
8353  SELECT @@GLOBAL.init_connect;
8354  SELECT init_connect;
8355END//
8356
8357--error ER_UNKNOWN_SYSTEM_VARIABLE
8358CREATE PROCEDURE p6()
8359BEGIN
8360  DECLARE v INT DEFAULT 0;
8361  SET @@v= 0;
8362END//
8363
8364delimiter ;//
8365
8366SET @old_init_connect= @@GLOBAL.init_connect;
8367CALL p5();
8368SET @@GLOBAL.init_connect= @old_init_connect;
8369
8370DROP PROCEDURE p2;
8371DROP PROCEDURE p5;
8372
8373
8374--echo #
8375--echo # Bug#11840395 (formerly known as bug#60347):
8376--echo # The string "versiondata" seems
8377--echo # to be 'leaking' into the schema name space
8378--echo #
8379--disable_warnings
8380DROP DATABASE IF EXISTS mixedCaseDbName;
8381--enable_warnings
8382CREATE DATABASE mixedCaseDbName;
8383DELIMITER |;
8384CREATE PROCEDURE mixedCaseDbName.tryMyProc() begin end|
8385CREATE FUNCTION mixedCaseDbName.tryMyFunc() returns text begin return 'IT WORKS'; end
8386|
8387DELIMITER ;|
8388call mixedCaseDbName.tryMyProc();
8389select mixedCaseDbName.tryMyFunc();
8390DROP DATABASE mixedCaseDbName;
8391
8392
8393--echo #
8394--echo # Bug#11766594  59736: SELECT DISTINCT.. INCORRECT RESULT WITH DETERMINISTIC FUNCTION IN WHERE C
8395--echo #
8396
8397CREATE TABLE t1 (a INT, b INT, KEY(b));
8398CREATE TABLE t2 (c INT, d INT, KEY(c));
8399INSERT INTO t1 VALUES (1,1),(1,1),(1,2);
8400INSERT INTO t2 VALUES (1,1),(1,2);
8401
8402DELIMITER $;
8403
8404CREATE FUNCTION f1() RETURNS INT DETERMINISTIC
8405BEGIN
8406  DECLARE a int;
8407  -- SQL statement inside
8408  SELECT 1 INTO a;
8409  RETURN a;
8410END $
8411
8412DELIMITER ;$
8413
8414SELECT COUNT(DISTINCT d) FROM t1, t2  WHERE a = c AND b = f1();
8415
8416DROP FUNCTION f1;
8417DROP TABLE t1, t2;
8418
8419
8420--echo # ------------------------------------------------------------------
8421--echo # -- End of 5.1 tests
8422--echo # ------------------------------------------------------------------
8423
8424#
8425# Bug#39255: Stored procedures: crash if function references nonexistent table
8426#
8427
8428--disable_warnings
8429DROP FUNCTION IF EXISTS f1;
8430DROP TABLE IF EXISTS t_non_existing;
8431DROP TABLE IF EXISTS t1;
8432--enable_warnings
8433
8434delimiter |;
8435CREATE FUNCTION f1() RETURNS INT
8436BEGIN
8437   DECLARE v INT;
8438   SELECT a INTO v FROM t_non_existing;
8439   RETURN 1;
8440END|
8441delimiter ;|
8442
8443CREATE TABLE t1 (a INT) ENGINE = myisam;
8444INSERT INTO t1 VALUES (1);
8445
8446--error ER_NO_SUCH_TABLE
8447SELECT * FROM t1 WHERE a = f1();
8448
8449DROP FUNCTION f1;
8450DROP TABLE t1;
8451
8452#
8453# Bug#36649: Condition area is not properly cleaned up after stored routine invocation
8454#
8455
8456--disable_warnings
8457DROP PROCEDURE IF EXISTS p1;
8458--enable_warnings
8459
8460delimiter |;
8461CREATE PROCEDURE p1(a INT, b CHAR)
8462BEGIN
8463  IF a > 0 THEN
8464    CALL p1(a-1, 'ab');
8465  ELSE
8466    SELECT 1;
8467  END IF;
8468END|
8469delimiter ;|
8470
8471SET @save_max_sp_recursion= @@max_sp_recursion_depth;
8472SET @@max_sp_recursion_depth= 5;
8473CALL p1(4, 'a');
8474SET @@max_sp_recursion_depth= @save_max_sp_recursion;
8475
8476DROP PROCEDURE p1;
8477
8478#
8479# Ensure that rules for message list clean up are being respected.
8480#
8481
8482--disable_warnings
8483DROP PROCEDURE IF EXISTS p1;
8484--enable_warnings
8485
8486delimiter |;
8487CREATE PROCEDURE p1(a CHAR)
8488BEGIN
8489  SELECT 1;
8490  SELECT CAST('10 ' as UNSIGNED INTEGER);
8491  SELECT 1;
8492END|
8493delimiter ;|
8494
8495CALL p1('data truncated parameter');
8496
8497DROP PROCEDURE p1;
8498
8499#
8500# Cascading stored procedure/function calls.
8501#
8502
8503--disable_warnings
8504DROP PROCEDURE IF EXISTS p1;
8505DROP PROCEDURE IF EXISTS p2;
8506DROP PROCEDURE IF EXISTS p3;
8507DROP PROCEDURE IF EXISTS p4;
8508--enable_warnings
8509
8510delimiter |;
8511CREATE PROCEDURE p1()
8512  CALL p2()|
8513CREATE PROCEDURE p2()
8514  CALL p3()|
8515CREATE PROCEDURE p3()
8516  CALL p4()|
8517CREATE PROCEDURE p4()
8518BEGIN
8519  SELECT 1;
8520  SELECT CAST('10 ' as UNSIGNED INTEGER);
8521  SELECT 2;
8522END|
8523delimiter ;|
8524
8525CALL p1();
8526
8527DROP PROCEDURE p1;
8528DROP PROCEDURE p2;
8529DROP PROCEDURE p3;
8530DROP PROCEDURE p4;
8531
8532--disable_warnings
8533DROP FUNCTION IF EXISTS f1;
8534DROP FUNCTION IF EXISTS f2;
8535DROP FUNCTION IF EXISTS f3;
8536DROP FUNCTION IF EXISTS f4;
8537DROP TABLE IF EXISTS t1;
8538--enable_warnings
8539
8540CREATE TABLE t1 (a CHAR(2));
8541
8542INSERT INTO t1 VALUES ('aa');
8543
8544delimiter |;
8545CREATE FUNCTION f1() RETURNS CHAR
8546  RETURN (SELECT f2())|
8547CREATE FUNCTION f2() RETURNS CHAR
8548  RETURN (SELECT f3())|
8549CREATE FUNCTION f3() RETURNS CHAR
8550  RETURN (SELECT f4())|
8551CREATE FUNCTION f4() RETURNS CHAR
8552BEGIN
8553  RETURN (SELECT a FROM t1);
8554END|
8555delimiter ;|
8556
8557SELECT f1();
8558
8559DROP FUNCTION f1;
8560DROP FUNCTION f2;
8561DROP FUNCTION f3;
8562DROP FUNCTION f4;
8563DROP TABLE t1;
8564
8565--echo #
8566--echo # Bug#34197: CREATE PROCEDURE fails when COMMENT truncated in non
8567--echo #            strict SQL mode
8568--echo #
8569
8570--disable_warnings
8571DROP PROCEDURE IF EXISTS p1;
8572--enable_warnings
8573
8574CREATE PROCEDURE p1 ()
8575COMMENT
8576'12345678901234567890123456789012345678901234567890123456789012345678901234567890'
8577BEGIN
8578END;
8579
8580SELECT comment FROM mysql.proc WHERE name = "p1";
8581
8582SELECT routine_comment FROM information_schema.routines WHERE routine_name = "p1";
8583
8584DROP PROCEDURE p1;
8585
8586
8587--echo #
8588--echo # Bug #47313 assert in check_key_in_view during CALL procedure
8589--echo #
8590
8591--disable_warnings
8592DROP TABLE IF EXISTS t1;
8593DROP VIEW IF EXISTS t1, t2_unrelated;
8594DROP PROCEDURE IF EXISTS p1;
8595--enable_warnings
8596
8597CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8598CREATE VIEW t1 AS SELECT 10 AS f1;
8599
8600--echo # t1 refers to the view
8601--error ER_NON_INSERTABLE_TABLE
8602CALL p1(1);
8603
8604CREATE TEMPORARY TABLE t1 (f1 INT);
8605
8606--echo # t1 still refers to the view since it was inlined
8607--error ER_NON_INSERTABLE_TABLE
8608CALL p1(2);
8609
8610DROP VIEW t1;
8611
8612--echo # t1 now refers to the temporary table
8613CALL p1(3);
8614
8615--echo # Check which values were inserted into the temp table.
8616SELECT * FROM t1;
8617
8618DROP TEMPORARY TABLE t1;
8619DROP PROCEDURE p1;
8620
8621--echo # Now test what happens if the sp cache is invalidated.
8622
8623CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8624CREATE VIEW t1 AS SELECT 10 AS f1;
8625CREATE VIEW v2_unrelated AS SELECT 1 AS r1;
8626
8627--echo # Load the procedure into the sp cache
8628--error ER_NON_INSERTABLE_TABLE
8629CALL p1(4);
8630
8631CREATE TEMPORARY TABLE t1 (f1 int);
8632
8633ALTER VIEW v2_unrelated AS SELECT 2 AS r1;
8634
8635--echo # Alter view causes the sp cache to be invalidated.
8636--echo # Now t1 refers to the temporary table, not the view.
8637CALL p1(5);
8638
8639--echo # Check which values were inserted into the temp table.
8640SELECT * FROM t1;
8641
8642DROP TEMPORARY TABLE t1;
8643DROP VIEW t1, v2_unrelated;
8644DROP PROCEDURE p1;
8645
8646CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8647CREATE TEMPORARY TABLE t1 (f1 INT);
8648
8649--echo # t1 refers to the temporary table
8650CALL p1(6);
8651
8652CREATE VIEW t1 AS SELECT 10 AS f1;
8653
8654--echo # Create view causes the sp cache to be invalidated.
8655--echo # t1 still refers to the temporary table since it shadows the view.
8656CALL p1(7);
8657
8658DROP VIEW t1;
8659
8660--echo # Check which values were inserted into the temp table.
8661SELECT * FROM t1;
8662
8663DROP TEMPORARY TABLE t1;
8664DROP PROCEDURE p1;
8665
8666--echo #
8667--echo # Bug #11918 Can't use a declared variable in LIMIT clause
8668--echo #
8669--disable_warnings
8670drop table if exists t1;
8671drop procedure if exists p1;
8672--enable_warnings
8673create table t1 (c1 int);
8674insert into t1 (c1) values (1), (2), (3), (4), (5);
8675
8676delimiter |;
8677create procedure p1()
8678begin
8679  declare a integer;
8680  declare b integer;
8681  select * from t1 limit a, b;
8682end|
8683delimiter ;|
8684--echo # How do we handle NULL limit values?
8685call p1();
8686drop table t1;
8687create table t1 (a int);
8688insert into t1 (a) values (1), (2), (3), (4), (5);
8689--echo #
8690--echo # Do we correctly resolve identifiers in LIMIT?
8691--echo # Since DROP and CREATE did not invalidate
8692--echo # the SP cache, we can't test until
8693--echo # we drop and re-create the procedure.
8694--echo #
8695--error ER_BAD_FIELD_ERROR
8696call p1();
8697--echo #
8698--echo # Drop and recreate the procedure, then repeat
8699--echo #
8700drop procedure p1;
8701delimiter |;
8702create procedure p1()
8703begin
8704  declare a integer;
8705  declare b integer;
8706  select * from t1 limit a, b;
8707end|
8708delimiter ;|
8709--echo # Stored procedure variables are resolved correctly in the LIMIT
8710call p1();
8711drop table t1;
8712create table t1 (c1 int);
8713insert into t1 (c1) values (1), (2), (3), (4), (5);
8714drop procedure p1;
8715--echo # Try to create a procedure that
8716--echo # refers to non-existing variables.
8717--error ER_SP_UNDECLARED_VAR
8718create procedure p1(p1 integer, p2 integer)
8719  select * from t1 limit a, b;
8720--echo #
8721--echo # Try to use data types not allowed in LIMIT
8722--echo #
8723--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8724create procedure p1(p1 date, p2 date) select * from t1 limit p1, p2;
8725--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8726create procedure p1(p1 integer, p2 float) select * from t1 limit p1, p2;
8727--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8728create procedure p1(p1 integer, p2 char(1)) select * from t1 limit p1, p2;
8729--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8730create procedure p1(p1 varchar(5), p2 char(1)) select * from t1 limit p1, p2;
8731--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8732create procedure p1(p1 decimal, p2 decimal) select * from t1 limit p1, p2;
8733--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8734create procedure p1(p1 double, p2 double) select * from t1 limit p1, p2;
8735
8736--echo #
8737--echo # Finally, test the valid case.
8738--echo #
8739create procedure p1(p1 integer, p2 integer)
8740  select * from t1 limit p1, p2;
8741
8742call p1(NULL, NULL);
8743call p1(0, 0);
8744call p1(0, -1);
8745call p1(-1, 0);
8746call p1(-1, -1);
8747call p1(0, 1);
8748call p1(1, 0);
8749call p1(1, 5);
8750call p1(3, 2);
8751
8752delimiter |;
8753--echo # Try to create a function that
8754--echo # refers to non-existing variables.
8755--error ER_SP_UNDECLARED_VAR
8756create function f1(p1 integer, p2 integer)
8757  returns int
8758begin
8759  declare a int;
8760  set a = (select count(*) from t1 limit a, b);
8761  return a;
8762end|
8763
8764create function f1()
8765  returns int
8766begin
8767  declare a, b, c int;
8768  set a = (select count(*) from t1 limit b, c);
8769  return a;
8770end|
8771
8772delimiter ;|
8773--echo # How do we handle NULL limit values?
8774select f1();
8775
8776drop function f1;
8777
8778delimiter |;
8779--echo #
8780--echo # Try to use data types not allowed in LIMIT
8781--echo #
8782--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8783create function f1(p1 date, p2 date)
8784  returns int
8785begin
8786  declare a int;
8787  set a = (select count(*) from t1 limit p1, p2);
8788  return a;
8789end|
8790
8791--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8792create function f1(p1 integer, p2 float)
8793  returns int
8794begin
8795  declare a int;
8796  set a = (select count(*) from t1 limit p1, p2);
8797  return a;
8798end|
8799
8800--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8801create function f1(p1 integer, p2 char(1))
8802  returns int
8803begin
8804  declare a int;
8805  set a = (select count(*) from t1 limit p1, p2);
8806  return a;
8807end|
8808
8809--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8810create function f1(p1 varchar(5), p2 char(1))
8811  returns int
8812begin
8813  declare a int;
8814  set a = (select count(*) from t1 limit p1, p2);
8815  return a;
8816end|
8817
8818--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8819create function f1(p1 decimal, p2 decimal)
8820  returns int
8821begin
8822  declare a int;
8823  set a = (select count(*) from t1 limit p1, p2);
8824  return a;
8825end|
8826
8827--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8828create function f1(p1 double, p2 double)
8829  returns int
8830begin
8831  declare a int;
8832  set a = (select count(*) from t1 limit p1, p2);
8833  return a;
8834end|
8835
8836--echo #
8837--echo # Finally, test the valid case.
8838--echo #
8839
8840create function f1(p1 integer, p2 integer)
8841returns int
8842begin
8843  declare count int;
8844  set count= (select count(*) from (select * from t1 limit p1, p2) t_1);
8845  return count;
8846end|
8847
8848delimiter ;|
8849
8850select f1(0, 0);
8851select f1(0, -1);
8852select f1(-1, 0);
8853select f1(-1, -1);
8854select f1(0, 1);
8855select f1(1, 0);
8856select f1(1, 5);
8857select f1(3, 2);
8858
8859--echo # Cleanup
8860drop table t1;
8861drop procedure p1;
8862drop function f1;
8863
8864--echo #
8865--echo # BUG#11766234: 59299: ASSERT (TABLE_REF->TABLE || TABLE_REF->VIEW)
8866--echo #               FAILS IN SET_FIELD_ITERATOR
8867--echo #
8868
8869CREATE TABLE t1 (a INT);
8870CREATE TABLE t2 (a INT);
8871CREATE VIEW v1 AS SELECT a FROM t2;
8872CREATE PROCEDURE proc() SELECT * FROM t1 NATURAL JOIN v1;
8873ALTER TABLE t2 CHANGE COLUMN a b CHAR;
8874
8875--echo
8876--error ER_VIEW_INVALID
8877CALL proc();
8878--error ER_VIEW_INVALID
8879CALL proc();
8880
8881--echo
8882DROP TABLE t1,t2;
8883DROP VIEW v1;
8884DROP PROCEDURE proc;
8885
8886
8887--echo
8888--echo # --
8889--echo # -- Bug 11765684 - 58674: SP-cache does not detect changes in
8890--echo # -- pre-locking list caused by triggers
8891--echo # ---
8892
8893--disable_warnings
8894DROP TABLE IF EXISTS t1;
8895DROP TABLE IF EXISTS t2;
8896DROP TABLE IF EXISTS t3;
8897DROP PROCEDURE IF EXISTS p1;
8898--enable_warnings
8899
8900CREATE TABLE t1(a INT);
8901CREATE TABLE t2(a INT);
8902CREATE TABLE t3(a INT);
8903
8904CREATE PROCEDURE p1()
8905  INSERT INTO t1(a) VALUES (1);
8906
8907--echo
8908CREATE TRIGGER t1_ai AFTER INSERT ON t1
8909  FOR EACH ROW
8910    INSERT INTO t2(a) VALUES (new.a);
8911
8912--echo
8913CALL p1();
8914
8915--echo
8916CREATE TRIGGER t1_bi BEFORE INSERT ON t1
8917  FOR EACH ROW
8918    INSERT INTO t3(a) VALUES (new.a);
8919
8920--echo
8921CALL p1();
8922
8923--echo
8924DROP TABLE t1, t2, t3;
8925DROP PROCEDURE p1;
8926--echo
8927
8928
8929--echo
8930--echo # --
8931--echo # -- Bug#12652769 - 61470: case operator in stored routine retains old
8932--echo # -- value of input parameter
8933--echo # ---
8934
8935--disable_warnings
8936DROP TABLE IF EXISTS t1;
8937DROP PROCEDURE IF EXISTS p1;
8938--enable_warnings
8939
8940CREATE TABLE t1 (s1 CHAR(5) CHARACTER SET utf8);
8941INSERT INTO t1 VALUES ('a');
8942
8943delimiter |;
8944
8945CREATE PROCEDURE p1(dt DATETIME, i INT)
8946BEGIN
8947  SELECT
8948    CASE
8949      WHEN i = 1 THEN 2
8950      ELSE dt
8951    END AS x1;
8952
8953  SELECT
8954    CASE _latin1'a'
8955      WHEN _utf8'a' THEN 'A'
8956    END AS x2;
8957
8958  SELECT
8959    CASE _utf8'a'
8960      WHEN _latin1'a' THEN _utf8'A'
8961    END AS x3;
8962
8963  SELECT
8964    CASE s1
8965      WHEN _latin1'a' THEN _latin1'b'
8966      ELSE _latin1'c'
8967    END AS x4
8968  FROM t1;
8969END|
8970
8971delimiter ;|
8972
8973--echo
8974CALL p1('2011-04-03 05:14:10', 1);
8975CALL p1('2011-04-03 05:14:11', 2);
8976CALL p1('2011-04-03 05:14:12', 2);
8977CALL p1('2011-04-03 05:14:13', 2);
8978
8979--echo
8980DROP TABLE t1;
8981DROP PROCEDURE p1;
8982--echo
8983
8984--echo #
8985--echo # Bug#12621017 - Crash if a sp variable is used in the
8986--echo #                limit clause of a set statement
8987--echo #
8988
8989--disable_warnings
8990DROP TABLE IF EXISTS t1;
8991DROP PROCEDURE IF EXISTS p1;
8992DROP PROCEDURE IF EXISTS p2;
8993--enable_warnings
8994
8995CREATE TABLE t1 (c1 INT);
8996INSERT INTO t1 VALUES (1);
8997
8998delimiter |;
8999
9000CREATE PROCEDURE p1()
9001BEGIN
9002  DECLARE foo, cnt INT UNSIGNED DEFAULT 1;
9003  SET foo = (SELECT MIN(c1) FROM t1 LIMIT cnt);
9004END|
9005
9006CREATE PROCEDURE p2()
9007BEGIN
9008
9009DECLARE iLimit INT;
9010DECLARE iVal INT;
9011
9012DECLARE cur1 CURSOR FOR
9013  SELECT c1 FROM t1
9014  LIMIT iLimit;
9015
9016SET iLimit=1;
9017
9018OPEN cur1;
9019FETCH cur1 INTO iVal;
9020
9021END|
9022
9023delimiter ;|
9024
9025CALL p1();
9026CALL p2();
9027
9028DROP PROCEDURE p1;
9029DROP PROCEDURE p2;
9030DROP TABLE t1;
9031
9032--echo
9033--echo # Bug#13805127: Stored program cache produces wrong result in same THD
9034--echo
9035
9036delimiter |;
9037
9038CREATE PROCEDURE p1(x INT UNSIGNED)
9039BEGIN
9040  SELECT c1, t2.c2, count(c3)
9041  FROM
9042    (
9043    SELECT 3 as c2 FROM dual WHERE x = 1
9044    UNION
9045    SELECT 2       FROM dual WHERE x = 1 OR x = 2
9046    ) AS t1,
9047    (
9048    SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
9049    UNION
9050    SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
9051    UNION
9052    SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
9053    ) AS t2
9054  WHERE t2.c2 = t1.c2
9055  GROUP BY c1, c2
9056  ;
9057END|
9058
9059delimiter ;|
9060
9061--echo
9062CALL p1(1);
9063CALL p1(2);
9064CALL p1(1);
9065
9066DROP PROCEDURE p1;
9067
9068--echo # End of 5.5 test
9069
9070#MDEV-17610
9071FLUSH USER_STATISTICS;
9072CREATE PROCEDURE sp() ALTER TABLE non_existing_table OPTIMIZE PARTITION p0;
9073CALL sp;
9074SELECT 1;
9075DROP PROCEDURE sp;
9076CREATE PROCEDURE sp() SET STATEMENT SQL_SELECT_LIMIT=0 FOR SHOW USER_STATISTICS;
9077CALL sp;
9078SELECT 1;
9079DROP PROCEDURE sp;
9080
9081--echo #
9082--echo # Bug#12663165 SP DEAD CODE REMOVAL DOESN'T UNDERSTAND CONTINUE HANDLERS
9083--echo #
9084
9085--disable_warnings
9086DROP FUNCTION IF EXISTS f1;
9087--enable_warnings
9088
9089delimiter $;
9090CREATE FUNCTION f1() RETURNS INT
9091BEGIN
9092  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
9093  BEGIN
9094    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
9095    BEGIN
9096     DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
9097     RETURN f1();
9098    END;
9099  END;
9100RETURN 1;
9101END $
9102delimiter ;$
9103
9104# This used to cause an assertion.
9105SELECT f1();
9106
9107DROP FUNCTION f1;
9108
9109--echo # ------------------------------------------------------------------
9110--echo # -- End of 5.1 tests
9111--echo # ------------------------------------------------------------------
9112
9113--echo #
9114--echo # lp:993459 Execution of PS for a query with GROUP BY
9115--echo # returns wrong result (see also mysql bug#13805127)
9116--echo #
9117
9118--echo
9119--echo # Bug#13805127: Stored program cache produces wrong result in same THD
9120--echo
9121
9122delimiter |;
9123
9124CREATE PROCEDURE p1(x INT UNSIGNED)
9125BEGIN
9126  SELECT c1, t2.c2, count(c3)
9127  FROM
9128    (
9129    SELECT 3 as c2 FROM dual WHERE x = 1
9130    UNION
9131    SELECT 2       FROM dual WHERE x = 1 OR x = 2
9132    ) AS t1,
9133    (
9134    SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
9135    UNION
9136    SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
9137    UNION
9138    SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
9139    ) AS t2
9140  WHERE t2.c2 = t1.c2
9141  GROUP BY c1, c2
9142  ;
9143END|
9144
9145delimiter ;|
9146
9147--echo
9148CALL p1(1);
9149CALL p1(2);
9150CALL p1(1);
9151
9152DROP PROCEDURE p1;
9153
9154--echo
9155--echo MDEV-3900 Optimizer difference between MySQL and MariaDB with stored functions in WHERE clause of UPDATE or DELETE statements
9156--echo
9157
9158CREATE FUNCTION tdn() RETURNS int(7) DETERMINISTIC RETURN to_days(now());
9159
9160CREATE TABLE t1 (pk INT NOT NULL AUTO_INCREMENT PRIMARY KEY, daynum INT, a CHAR(1), INDEX(daynum), INDEX(a)) ENGINE=MyISAM;
9161INSERT INTO t1 (daynum) VALUES (1),(2),(3),(4),(5),(TO_DAYS(NOW())),(7),(8);
9162INSERT INTO t1 (daynum) SELECT a1.daynum FROM t1 a1, t1 a2, t1 a3, t1 a4, t1 a5;
9163
9164FLUSH TABLES;
9165FLUSH STATUS;
9166
9167SHOW STATUS LIKE '%Handler_read%';
9168UPDATE t1 SET a = '+' WHERE daynum=tdn();
9169SHOW STATUS LIKE '%Handler_read%';
9170
9171drop function tdn;
9172drop table t1;
9173
9174--echo #
9175--echo # lp:1002157 : testing stored function
9176--echo # bug#62125        result for null incorrectly yields 1292 warning.
9177--echo #
9178
9179--disable_warnings
9180DROP FUNCTION IF EXISTS f1;
9181DROP FUNCTION IF EXISTS f2;
9182DROP FUNCTION IF EXISTS f3;
9183DROP FUNCTION IF EXISTS f4;
9184--enable_warnings
9185
9186delimiter /;
9187
9188CREATE FUNCTION f1() RETURNS VARCHAR(1)
9189BEGIN RETURN 'X'; END;/
9190
9191CREATE FUNCTION f2() RETURNS CHAR(1)
9192BEGIN RETURN 'X'; END;/
9193
9194CREATE FUNCTION f3() RETURNS VARCHAR(1)
9195BEGIN RETURN NULL; END;/
9196
9197CREATE FUNCTION f4() RETURNS CHAR(1)
9198BEGIN RETURN NULL; END;/
9199
9200delimiter ;/
9201
9202SELECT f1() IS NULL;
9203SELECT f2() IS NULL;
9204SELECT f3() IS NULL;
9205SELECT f4() IS NULL;
9206
9207DROP FUNCTION f1;
9208DROP FUNCTION f2;
9209DROP FUNCTION f3;
9210DROP FUNCTION f4;
9211
9212--echo
9213--echo Stored procedures and a condition handler in a nested procedure call
9214--echo doesn't suppress the condition from being passed on to the calling
9215--echo procedure
9216--echo
9217
9218--disable_warnings
9219drop procedure if exists p1;
9220drop procedure if exists p0;
9221--enable_warnings
9222
9223create table t1 (id int);
9224delimiter $$;
9225create procedure p1 () begin
9226declare i int default 0;
9227declare continue handler for not found begin
9228select "You should see this message and the warning that generated this" as "message";
9229show warnings;
9230end;
9231select id into i from t1;
9232end$$
9233create procedure p0 () begin
9234declare continue handler for not found begin
9235select "You should NOT see this message" as "message";
9236end;
9237call p1();
9238end$$
9239delimiter ;$$
9240call p0();
9241
9242drop procedure p1;
9243drop procedure p0;
9244drop table t1;
9245
9246--echo
9247--echo Test if stored procedures propagates errors
9248--echo
9249
9250create table t1 (id int primary key);
9251delimiter $$;
9252create procedure p1 () begin
9253insert into t1 values(1);
9254insert into t1 values(2);
9255insert into t1 values(2);
9256insert into t1 values(3);
9257end$$
9258create procedure p2 () begin
9259    declare x int;
9260    select id into x from t1 where id=5;
9261end$$
9262delimiter ;$$
9263--error ER_DUP_ENTRY
9264call p1();
9265show warnings;
9266select * from t1;
9267call p2();
9268
9269drop procedure p1;
9270drop procedure p2;
9271
9272drop table t1;
9273
9274--echo #
9275--echo # MDEV-4978 - Server cursor is broken with blobs in the select list,
9276--echo #             ORDER BY does not work
9277--echo #
9278CREATE TABLE t1(a INT, b BLOB);
9279INSERT INTO t1 VALUES(1,REPEAT('a',4835)),(2,'b'),(3,'c'),(4,'d'),(5,REPEAT('e',805)),(6,'f');
9280
9281DELIMITER |;
9282CREATE PROCEDURE p1()
9283BEGIN
9284  DECLARE done INT DEFAULT 0;
9285  DECLARE v1 INT;
9286  DECLARE v2 BLOB;
9287  DECLARE c1 CURSOR FOR SELECT * FROM t1 ORDER BY a;
9288  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
9289  OPEN c1;
9290  REPEAT
9291    FETCH c1 INTO v1, v2;
9292    IF NOT done THEN
9293      SELECT v1;
9294    END IF;
9295  UNTIL done END REPEAT;
9296  CLOSE c1;
9297END|
9298DELIMITER ;|
9299
9300CALL p1;
9301
9302DROP PROCEDURE p1;
9303DROP TABLE t1;
9304
9305--echo #
9306--echo # MDEV-10713: signal 11 error on multi-table update - crash in
9307--echo # handler::increment_statistics or in make_select or assertion
9308--echo # failure pfs_thread == ((PFS_thread*) pthread_getspecific((THR_PFS)))
9309--echo #
9310
9311CREATE TABLE `t1` (
9312  `CLOSE_YN` varchar(10) COLLATE utf8_bin DEFAULT NULL
9313) DEFAULT CHARSET=utf8 COLLATE=utf8_bin ;
9314
9315
9316CREATE TABLE `t2` (
9317  `ap_close_to` varchar(8) COLLATE utf8_bin DEFAULT NULL
9318) DEFAULT CHARSET=utf8 COLLATE=utf8_bin ;
9319insert t1 values (1);
9320
9321
9322--delimiter $$
9323
9324CREATE  FUNCTION `f1`(`P_DC_CD` VARBINARY(50), `P_SYS_DATE` DATETIME) RETURNS datetime
9325    DETERMINISTIC
9326    SQL SECURITY INVOKER
9327BEGIN
9328  DECLARE V_SYS_DATE DATETIME;
9329  SELECT now() AS LOC_DATE INTO V_SYS_DATE ;
9330  RETURN v_sys_date ;
9331END $$
9332
9333--delimiter ;
9334
9335update t1 S
9336JOIN
9337(
9338  SELECT CASE
9339           WHEN DATE_FORMAT( f1('F01', NOW()) , '%Y%m%d') <= CLOSE_YMD
9340             THEN '99991231'
9341	     ELSE '' END ACCOUNT_APPLY_YYYYMMDD
9342  FROM (
9343        select case
9344                 when 'AP'='AP'
9345                   then ap_close_to
9346                   end AS CLOSE_YMD
9347	from t2
9348       ) A
9349) X
9350SET S.CLOSE_YN = ''
9351where 1=1;
9352
9353drop function if exists f1;
9354drop table t1,t2;
9355
9356--echo #
9357--echo # MDEV-16957: Server crashes in Field_iterator_natural_join::next
9358--echo # upon 2nd execution of SP
9359--echo #
9360
9361CREATE TABLE t1 (a INT, b VARCHAR(32));
9362CREATE PROCEDURE sp() SELECT * FROM t1 AS t1x JOIN t1 AS t1y USING (c);
9363--error ER_BAD_FIELD_ERROR
9364CALL sp;
9365--error ER_BAD_FIELD_ERROR
9366CALL sp;
9367--error ER_BAD_FIELD_ERROR
9368CALL sp;
9369alter table t1 add column c int;
9370CALL sp;
9371
9372# Cleanup
9373DROP PROCEDURE sp;
9374DROP TABLE t1;
9375
9376--echo #
9377--echo # MDEV-17055: Server crashes in find_order_in_list upon
9378--echo # 2nd (3rd) execution of SP with UPDATE
9379--echo #
9380
9381CREATE TABLE t1 (a INT);
9382CREATE VIEW v1 AS SELECT * FROM t1;
9383CREATE TABLE t2 (c INT);
9384
9385CREATE PROCEDURE sp() UPDATE v1 SET a = 1 ORDER BY a, b LIMIT 1;
9386LOCK TABLE t2 READ;
9387--error ER_TABLE_NOT_LOCKED
9388CALL sp;
9389UNLOCK TABLES;
9390--error ER_BAD_FIELD_ERROR
9391CALL sp;
9392--error ER_BAD_FIELD_ERROR
9393CALL sp;
9394--error ER_BAD_FIELD_ERROR
9395CALL sp;
9396
9397# Cleanup
9398DROP PROCEDURE sp;
9399
9400CREATE PROCEDURE sp() UPDATE v1 SET a = 1 WHERE a=1 and b=2;
9401LOCK TABLE t2 READ;
9402--error ER_TABLE_NOT_LOCKED
9403CALL sp;
9404UNLOCK TABLES;
9405--error ER_BAD_FIELD_ERROR
9406CALL sp;
9407--error ER_BAD_FIELD_ERROR
9408CALL sp;
9409--error ER_BAD_FIELD_ERROR
9410CALL sp;
9411
9412# Cleanup
9413DROP PROCEDURE sp;
9414
9415DROP VIEW v1;
9416DROP TABLE t1, t2;
9417
9418--echo # End of 5.5 test
9419
9420--echo #
9421--echo #  MDEV-7040: Crash in field_conv, memcpy_field_possible, part#2
9422--echo #
9423create table t1 (
9424  col1 bigint(20),
9425  col2 char(1),
9426  col3 char(2)
9427);
9428insert into t1 values (1,'a','a'), (2,'b','b');
9429
9430create table t2 as select * from t1;
9431create table t3 as select * from t1;
9432create table t4 as select * from t1;
9433create table t5 as select * from t1;
9434create table t6 as select * from t1;
9435
9436flush tables;
9437
9438DELIMITER |;
9439
9440CREATE PROCEDURE p1()
9441begin
9442   DECLARE _var1 bigint(20) UNSIGNED;
9443   DECLARE _var2 CHAR(1) DEFAULT NULL;
9444   DECLARE _var3 CHAR(1) DEFAULT NULL;
9445
9446   DECLARE _done BOOLEAN DEFAULT 0;
9447
9448   declare cur1 cursor for
9449   select col1, col2, col3
9450   from t1
9451   where
9452     col1 in (select t2.col1 from t2 where t2.col2=t1.col2) or
9453     col2 in (select t3.col3 from t3 where t3.col3=t1.col2) ;
9454
9455   DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1;
9456
9457   OPEN cur1;
9458
9459   set _var1 = (select _var1 from t4 limit 1);
9460   set _var1 = (select _var1 from t5 limit 1);
9461   set _var1 = (select _var1 from t6 limit 1);
9462label1:
9463   LOOP
9464     SET _done = 0;
9465     FETCH cur1 INTO _var1, _var2, _var3;
9466     IF _done THEN
9467         LEAVE label1;
9468     END IF;
9469   END LOOP label1;
9470   CLOSE cur1;
9471end|
9472DELIMITER ;|
9473
9474set @tmp_toc= @@table_open_cache;
9475set @tmp_tdc= @@table_definition_cache;
9476
9477set global table_open_cache=10;
9478set global table_definition_cache=1;
9479call p1();
9480
9481set global table_open_cache= @tmp_toc;
9482set global table_definition_cache= @tmp_tdc;
9483drop procedure p1;
9484
9485drop table t1,t2,t3,t4,t5,t6;
9486
9487--echo #
9488--echo # MDEV-11935: Queries in stored procedures with and
9489--echo # EXISTS(SELECT * FROM VIEW) crashes and closes hte conneciton.
9490--echo #
9491
9492CREATE TABLE ANY_TABLE (
9493  ENTITY_UID    BIGINT NOT NULL
9494);
9495CREATE TABLE SECURITY_PATH(
9496origid BIGINT UNSIGNED NOT NULL,
9497destid BIGINT UNSIGNED NOT NULL,
9498KEY (destid)
9499);
9500CREATE VIEW ENTITY_ACCESS (
9501ENTITY_UID,
9502OWNER_UID
9503) AS
9504SELECT SP1.origid,
9505       SP2.destid
9506FROM SECURITY_PATH SP1
9507JOIN SECURITY_PATH SP2 ON SP1.destid = SP2.origid
9508;
9509--delimiter //
9510CREATE PROCEDURE SP_EXAMPLE_SELECT ()
9511BEGIN
9512   SELECT *
9513   FROM ANY_TABLE AT1
9514   WHERE EXISTS ( SELECT *
9515                  FROM ENTITY_ACCESS EA
9516                  WHERE AT1.ENTITY_UID = EA.ENTITY_UID
9517                  AND   EA.OWNER_UID IS NULL );
9518END
9519//
9520--delimiter ;
9521CALL SP_EXAMPLE_SELECT ();
9522CALL SP_EXAMPLE_SELECT ();
9523
9524drop procedure SP_EXAMPLE_SELECT;
9525drop view ENTITY_ACCESS;
9526drop table ANY_TABLE, SECURITY_PATH;
9527--echo # End of 10.0 test
9528
9529DELIMITER |;
9530CREATE FUNCTION f(f1 VARCHAR(64) COLLATE latin1_german2_ci)
9531  RETURNS VARCHAR(64)
9532BEGIN
9533  RETURN 'str';
9534END|
9535DROP FUNCTION f|
9536
9537CREATE FUNCTION f(f1 VARCHAR(64))
9538  RETURNS VARCHAR(64) COLLATE latin1_german2_ci
9539BEGIN
9540  RETURN 'str';
9541END|
9542DROP FUNCTION f|
9543
9544CREATE FUNCTION f(f1 VARCHAR(64))
9545  RETURNS VARCHAR(64)
9546BEGIN
9547  DECLARE f2 VARCHAR(64) COLLATE latin1_german2_ci;
9548  RETURN 'str';
9549END|
9550DROP FUNCTION f|
9551DELIMITER ;|
9552
9553--echo #
9554--echo # MDEV-7023: Error 2027: Malformed packet and assertion
9555--echo # `field_types == 0 || field_types[field_pos] == MYSQL_TYPE_INT24 ||
9556--echo #field_types[field_pos] == MYSQL_TYPE_LONG' failure in
9557--echo #Protocol_text::store_long
9558--echo #
9559create table t1 (i int);
9560create table t2 (i int);
9561--delimiter |
9562--error ER_SP_NO_RETSET
9563create function f() returns int
9564begin
9565  analyze insert into t1 values (1);
9566  return 1;
9567end |
9568--error ER_SP_NO_RETSET
9569create function f() returns int
9570begin
9571  analyze insert t1 select * from t2;
9572  return 1;
9573end |
9574--error ER_SP_NO_RETSET
9575create function f() returns int
9576begin
9577  analyze delete from t1;
9578  return 1;
9579end |
9580--error ER_SP_NO_RETSET
9581create function f() returns int
9582begin
9583  analyze delete t1 from t1,t2;
9584  return 1;
9585end |
9586--error ER_SP_NO_RETSET
9587create function f() returns int
9588begin
9589  analyze update t1 set i=1;
9590  return 1;
9591end |
9592--error ER_SP_NO_RETSET
9593create function f() returns int
9594begin
9595  analyze update t1,t2 set i=1;
9596  return 1;
9597end |
9598--error ER_SP_NO_RETSET
9599create function f() returns int
9600begin
9601  analyze replace t1 set i=1;
9602  return 1;
9603end |
9604--error ER_SP_NO_RETSET
9605create function f() returns int
9606begin
9607  analyze replace t1 select * from t2;
9608  return 1;
9609end |
9610--delimiter ;
9611
9612drop table t1,t2;
9613
9614--echo #
9615--echo # MDEV-11584: GRANT inside an SP does not work well on 2nd execution
9616--echo #
9617
9618CREATE PROCEDURE sp1()
9619  GRANT ALL PRIVILEGES ON *.* TO 'foo'@'%' IDENTIFIED BY 'pass';
9620CALL sp1();
9621CALL sp1();
9622drop user 'foo'@'%';
9623drop procedure sp1;
9624
9625--echo #
9626--echo # MDEV-10972: Insert from select / view / union --
9627--echo # repeatable crash in 10.1, 10.2 Linux/Mac/Windows
9628--echo #
9629
9630create table t (id int auto_increment primary key);
9631insert into t values (9494),(9495),(9496),(9497),(9498),(9499),(9500),(9501),(9502),(9503);
9632
9633create VIEW v AS
9634select id from t
9635union
9636select id from t
9637;
9638
9639drop procedure if exists p;
9640create procedure p()
9641insert into tmp_t select t.id from (
9642    select id from v
9643    union
9644    select id from v
9645) sq
9646inner join t on (sq.id = t.id);
9647
9648--error ER_NO_SUCH_TABLE
9649CALL p();
9650create table tmp_t (id int null);
9651CALL p();
9652
9653drop procedure p;
9654drop view v;
9655drop table t, tmp_t;
9656
9657
9658--echo #
9659--echo # MDEV-13936: Server crashes in Time_and_counter_tracker::incr_loops
9660--echo #
9661CREATE TABLE t1 (i INT);
9662CREATE VIEW v1 AS SELECT * FROM t1 WHERE RAND() > 0.5;
9663CREATE FUNCTION f1() RETURNS INT RETURN ( SELECT MAX(i) FROM v1 );
9664
9665--error ER_NON_INSERTABLE_TABLE
9666REPLACE INTO v1 VALUES (f1());
9667SET @aux = f1();
9668
9669# Cleanup
9670DROP FUNCTION f1;
9671DROP VIEW v1;
9672DROP TABLE t1;
9673
9674--echo #
9675--echo # MDEV-14857: problem with 10.2.11 server crashing when
9676--echo # executing stored procedure
9677--echo #
9678
9679SET max_sp_recursion_depth=10;
9680
9681CREATE TABLE t1 (a INT);
9682CREATE TABLE t2 (b INT);
9683
9684delimiter ||;
9685
9686CREATE PROCEDURE proc_0()
9687BEGIN
9688  CALL empty_1();
9689  CALL proc_1();
9690END ||
9691
9692CREATE PROCEDURE proc_1()
9693BEGIN
9694  CALL proc_2();
9695  CALL proc_3();
9696  CALL proc_4();
9697  CALL proc_5();
9698END ||
9699
9700CREATE PROCEDURE proc_2()
9701  CALL proc_6();
9702||
9703
9704CREATE PROCEDURE proc_3()
9705BEGIN
9706  CALL empty_2();
9707  CALL empty_3();
9708END ||
9709
9710CREATE PROCEDURE proc_4()
9711  CALL proc_7();
9712||
9713
9714CREATE PROCEDURE proc_5()
9715  CALL proc_select();
9716||
9717
9718CREATE PROCEDURE proc_6()
9719BEGIN
9720  CALL empty_4();
9721  CALL empty_5();
9722  CALL empty_6();
9723  CALL empty_7();
9724  CALL proc_8();
9725END ||
9726
9727CREATE PROCEDURE proc_7()
9728  CALL proc_9('foo');
9729||
9730
9731CREATE PROCEDURE proc_8()
9732  CALL proc_10();
9733||
9734
9735CREATE PROCEDURE proc_9(IN opt VARCHAR(40))
9736  IF LEFT(opt,1) <> '_' THEN
9737    CALL proc_11();
9738  END IF;
9739||
9740
9741CREATE PROCEDURE proc_10()
9742  CALL proc_12();
9743||
9744
9745CREATE PROCEDURE proc_11()
9746BEGIN
9747  CALL empty_8();
9748  CALL empty_9();
9749  CALL empty_10();
9750  CALL proc_13();
9751END ||
9752
9753CREATE PROCEDURE proc_12()
9754BEGIN
9755  CALL empty_11();
9756  CALL empty_12();
9757  CALL empty_13();
9758END ||
9759
9760CREATE PROCEDURE proc_13()
9761BEGIN
9762  CALL proc_9('_bar');
9763  CALL empty_14();
9764END ||
9765
9766delimiter ;||
9767
9768CREATE PROCEDURE empty_1() BEGIN END ;
9769CREATE PROCEDURE empty_2() BEGIN END ;
9770CREATE PROCEDURE empty_3() BEGIN END ;
9771CREATE PROCEDURE empty_4() BEGIN END ;
9772CREATE PROCEDURE empty_5() BEGIN END ;
9773CREATE PROCEDURE empty_6() BEGIN END ;
9774CREATE PROCEDURE empty_7() BEGIN END ;
9775CREATE PROCEDURE empty_8() BEGIN END ;
9776CREATE PROCEDURE empty_9() BEGIN END ;
9777CREATE PROCEDURE empty_10() BEGIN END ;
9778CREATE PROCEDURE empty_11() BEGIN END ;
9779CREATE PROCEDURE empty_12() BEGIN END ;
9780CREATE PROCEDURE empty_13() BEGIN END ;
9781CREATE PROCEDURE empty_14() BEGIN END ;
9782
9783CREATE PROCEDURE proc_select()
9784  SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2)
9785;
9786
9787CALL proc_0();
9788
9789# Cleanup
9790DROP PROCEDURE empty_1;
9791DROP PROCEDURE empty_2;
9792DROP PROCEDURE empty_3;
9793DROP PROCEDURE empty_4;
9794DROP PROCEDURE empty_5;
9795DROP PROCEDURE empty_6;
9796DROP PROCEDURE empty_7;
9797DROP PROCEDURE empty_8;
9798DROP PROCEDURE empty_9;
9799DROP PROCEDURE empty_10;
9800DROP PROCEDURE empty_11;
9801DROP PROCEDURE empty_12;
9802DROP PROCEDURE empty_13;
9803DROP PROCEDURE empty_14;
9804DROP PROCEDURE proc_0;
9805DROP PROCEDURE proc_1;
9806DROP PROCEDURE proc_2;
9807DROP PROCEDURE proc_3;
9808DROP PROCEDURE proc_4;
9809DROP PROCEDURE proc_5;
9810DROP PROCEDURE proc_6;
9811DROP PROCEDURE proc_7;
9812DROP PROCEDURE proc_8;
9813DROP PROCEDURE proc_9;
9814DROP PROCEDURE proc_10;
9815DROP PROCEDURE proc_11;
9816DROP PROCEDURE proc_12;
9817DROP PROCEDURE proc_13;
9818DROP PROCEDURE proc_select;
9819DROP TABLE t1, t2;
9820
9821SET max_sp_recursion_depth=default;
9822
9823--echo #
9824--echo # MDEV-15347: Valgrind or ASAN errors in mysql_make_view on query
9825--echo # from information_schema
9826--echo #
9827
9828CREATE VIEW v AS SELECT 1;
9829CREATE FUNCTION f() RETURNS INT RETURN 1;
9830--disable_result_log
9831SELECT * FROM INFORMATION_SCHEMA.TABLES JOIN INFORMATION_SCHEMA.PARAMETERS
9832UNION
9833SELECT * FROM INFORMATION_SCHEMA.TABLES JOIN INFORMATION_SCHEMA.PARAMETERS;
9834--enable_result_log
9835DROP FUNCTION f;
9836DROP VIEW v;
9837
9838--echo #
9839--echo # MDEV-17963: Assertion `field_pos < field_count' failed in Protocol_text::store,
9840--echo # Assertion `field_handlers == 0 || field_pos < field_count'
9841--echo #
9842
9843CREATE TABLE t1 (ct time);
9844INSERT INTO t1 VALUES ('16:11:28');
9845
9846DELIMITER |;
9847--error ER_SP_NO_RETSET
9848CREATE FUNCTION f1 () RETURNS varchar(100)
9849BEGIN
9850DECLARE xxx varchar(100);
9851ANALYZE SELECT sum(ct) FROM t1 INTO xxx ;
9852RETURN xxx;
9853END|
9854
9855DELIMITER ;|
9856drop table t1;
9857
9858--echo #End of 10.1 tests
9859
9860--echo #
9861--echo # MDEV-11081: CURSOR for query with GROUP BY
9862--echo #
9863
9864CREATE TABLE t1 (name VARCHAR(10), value INT);
9865INSERT INTO t1 VALUES ('b',1);
9866INSERT INTO t1 VALUES ('b',1);
9867INSERT INTO t1 VALUES ('c',1);
9868INSERT INTO t1 VALUES ('a',1);
9869INSERT INTO t1 VALUES ('a',1);
9870INSERT INTO t1 VALUES ('a',1);
9871DELIMITER |;
9872CREATE PROCEDURE p1 ()
9873BEGIN
9874  DECLARE done INT DEFAULT FALSE;
9875  DECLARE v_name VARCHAR(10);
9876  DECLARE v_total INT;
9877  DECLARE c CURSOR FOR
9878    SELECT name, SUM(value) AS total FROM t1 GROUP BY name;
9879  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
9880  OPEN c;
9881read_loop:
9882  LOOP
9883    FETCH c INTO v_name, v_total;
9884    IF done THEN
9885      LEAVE read_loop;
9886    END IF;
9887    SELECT v_name, v_total;
9888  END LOOP;
9889  CLOSE c;
9890END;
9891|
9892DELIMITER ;|
9893CALL p1();
9894DROP PROCEDURE p1;
9895DROP TABLE t1;
9896
9897--echo #
9898--echo # MDEV-13346: CURSOR a query with GROUP BY using derived table
9899--echo #
9900
9901DELIMITER |;
9902CREATE PROCEDURE p1()
9903BEGIN
9904  DECLARE c CURSOR FOR
9905    SELECT
9906      IFNULL(NULL,1) AS col
9907    FROM
9908      ( select 1 as id ) AS t
9909    GROUP BY t.id
9910  ;
9911  OPEN c;
9912END
9913|
9914DELIMITER ;|
9915CALL p1();
9916DROP PROCEDURE p1;
9917
9918--echo #
9919--echo # MDEV-15057 Crash when using an unknown identifier as an SP parameter
9920--echo #
9921
9922CREATE OR REPLACE PROCEDURE p1 (a VARCHAR(10)) SELECT 1;
9923--error ER_BAD_FIELD_ERROR
9924CALL p1(a);
9925drop procedure p1;
9926
9927DELIMITER |;
9928
9929CREATE OR REPLACE PROCEDURE p1 (a VARCHAR(10)) SELECT a|
9930CREATE OR REPLACE PROCEDURE p2 ()
9931BEGIN
9932  DECLARE name VARCHAR(10);
9933  SET name="hello";
9934  call p1(name);
9935END|
9936
9937--error ER_SP_UNDECLARED_VAR
9938CREATE OR REPLACE PROCEDURE p3 ()
9939BEGIN
9940  DECLARE name VARCHAR(10);
9941  SET name="hello";
9942  call p1(name2);
9943END|
9944
9945DELIMITER ;|
9946
9947call p2();
9948drop procedure p1;
9949drop procedure p2;
9950
9951--echo #
9952--echo # MDEV-15328: MariaDB 10.2.13 Crashes upon CALL PROCEDURE PARAM
9953--echo # LAST_INSERT_ID ()
9954--echo # (part 1, part 2 is in query_cache.test)
9955--echo #
9956
9957CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END;
9958CALL foo( LAST_INSERT_ID() );
9959DROP PROCEDURE foo;
9960
9961--echo #
9962--echo # MDEV-15870 Using aggregate and window function in unexpected places can crash the server
9963--echo #
9964
9965CREATE PROCEDURE p1 (a TEXT) BEGIN END;
9966--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
9967CALL p1(RANK() OVER (ORDER BY 1));
9968--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
9969CALL p1(ROW_NUMBER() OVER ());
9970--error ER_INVALID_GROUP_FUNC_USE
9971CALL p1(SUM(1));
9972DROP PROCEDURE p1;
9973
9974
9975--echo #
9976--echo # MDEV-16311 Server crash when using a NAME_CONST() with a CURSOR
9977--echo #
9978
9979SET sql_mode=STRICT_ALL_TABLES;
9980CREATE TABLE t1 (a INT);
9981INSERT INTO t1 VALUES (10);
9982DELIMITER $$;
9983--error ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
9984BEGIN NOT ATOMIC
9985  DECLARE a INT;
9986  DECLARE c CURSOR FOR SELECT NAME_CONST('x','y') FROM t1;
9987  OPEN c;
9988  FETCH c INTO a;
9989  CLOSE c;
9990END;
9991$$
9992DELIMITER ;$$
9993DROP TABLE t1;
9994SET sql_mode=DEFAULT;
9995
9996--echo #
9997--echo # MDEV-24220: error when opening a table for the second call of SP
9998--echo #
9999
10000CREATE TABLE t1 (a INT, b INT);
10001INSERT INTO t1 VALUES (1,1),(2,2);
10002CREATE VIEW v1 AS SELECT MAX(a) as f FROM t1;
10003--delimiter $
10004CREATE PROCEDURE p1()
10005BEGIN
10006  SELECT * FROM v1;
10007END $
10008--delimiter ;
10009
10010CALL p1;
10011ALTER TABLE t1 DROP a;
10012-- error ER_VIEW_INVALID
10013CALL p1;
10014
10015DROP PROCEDURE p1;
10016DROP VIEW v1;
10017DROP TABLE t1;
10018
10019
10020--echo #
10021--echo # BUG#30366310: USING A FUNCTION TO ASSIGN DEFAULT VALUES TO
10022--echo # 2 OR MORE VARIABLES CRASHES SERVER
10023--echo #
10024
10025delimiter |;
10026create function f1() returns bigint return now()-1|
10027create procedure p1()
10028begin
10029  declare b, c bigint default f1();
10030  select b-c;
10031end|
10032call p1()|
10033drop procedure p1|
10034drop function f1|
10035delimiter ;|
10036
10037--echo #
10038--echo # MDEV-24827: MariaDB 10.5.5 crash (sig 11) during a SELECT
10039--echo #
10040
10041CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT);
10042CREATE TABLE t2 (c1 INT PRIMARY KEY, c2 INT, KEY idx_c2(c2));
10043
10044INSERT INTO t1 (c1, c2) SELECT seq, seq FROM seq_1_to_10000;
10045INSERT INTO t2 (c1, c2) SELECT seq, seq FROM seq_1_to_20000;
10046
10047--delimiter $
10048
10049CREATE OR REPLACE PROCEDURE p1()
10050begin
10051  DECLARE done INT DEFAULT FALSE;
10052  DECLARE a INT;
10053
10054  DECLARE cur1 CURSOR FOR
10055    SELECT t2.c1 AS c1 FROM t1 LEFT JOIN t2 ON t1.c1 = t2.c1
10056    WHERE EXISTS (SELECT 1 FROM t1 WHERE c2 = -1) ORDER BY c1;
10057
10058  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10059
10060  OPEN cur1;
10061  read_loop: LOOP
10062    FETCH cur1 INTO a;
10063    IF done THEN
10064      LEAVE read_loop;
10065    END IF;
10066  END LOOP;
10067  CLOSE cur1;
10068END $
10069
10070--delimiter ;
10071
10072CALL p1();
10073
10074DROP PROCEDURE p1;
10075DROP TABLE t1;
10076DROP TABLE t2;
10077
10078--echo #End of 10.2 tests
10079
10080--echo #
10081--echo # MDEV-12007 Allow ROW variables as a cursor FETCH target
10082--echo #
10083
10084
10085--echo # The cursor and the ROW variable in FETCH must have the same number of fields
10086DELIMITER $$;
10087CREATE PROCEDURE p1()
10088BEGIN
10089  DECLARE done INT DEFAULT FALSE;
10090  DECLARE rec ROW(aa INT, bb VARCHAR(32), cc INT);
10091  DECLARE cur CURSOR FOR SELECT 10 AS a,'b10' AS b;
10092  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10093  OPEN cur;
10094read_loop:
10095  LOOP
10096    FETCH cur INTO rec;
10097    IF done THEN
10098      LEAVE read_loop;
10099    END IF;
10100  END LOOP;
10101  CLOSE cur;
10102END;
10103$$
10104DELIMITER ;$$
10105--error ER_SP_WRONG_NO_OF_FETCH_ARGS
10106CALL p1();
10107DROP PROCEDURE p1;
10108
10109
10110--echo # Multiple ROW variables in FETCH
10111DELIMITER $$;
10112CREATE PROCEDURE p1()
10113BEGIN
10114  DECLARE done INT DEFAULT FALSE;
10115  DECLARE rec1 ROW(aa INT);
10116  DECLARE rec2 ROW(aa INT);
10117  DECLARE cur CURSOR FOR SELECT 10 AS a, 20 AS b;
10118  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10119  OPEN cur;
10120read_loop:
10121  LOOP
10122    FETCH cur INTO rec1, rec2;
10123    IF done THEN
10124      LEAVE read_loop;
10125    END IF;
10126  END LOOP;
10127  CLOSE cur;
10128END;
10129$$
10130DELIMITER ;$$
10131--error ER_OPERAND_COLUMNS
10132CALL p1();
10133DROP PROCEDURE p1;
10134
10135
10136--echo # A complete working example
10137CREATE TABLE t1 (a INT, b VARCHAR(32));
10138INSERT INTO t1 VALUES (10,'b10');
10139INSERT INTO t1 VALUES (20,'b20');
10140INSERT INTO t1 VALUES (30,'b30');
10141DELIMITER $$;
10142CREATE PROCEDURE p1()
10143BEGIN
10144  DECLARE done INT DEFAULT FALSE;
10145  DECLARE rec ROW(aa INT, bb VARCHAR(32));
10146  DECLARE cur CURSOR FOR SELECT a,b FROM t1;
10147  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10148  OPEN cur;
10149read_loop:
10150  LOOP
10151    FETCH cur INTO rec;
10152    IF done THEN
10153      LEAVE read_loop;
10154    END IF;
10155    SELECT CONCAT('rec=(',rec.aa,',',rec.bb,')') AS c;
10156  END LOOP;
10157  CLOSE cur;
10158END;
10159$$
10160DELIMITER ;$$
10161CALL p1();
10162DROP PROCEDURE p1;
10163DROP TABLE t1;
10164
10165
10166--echo # A ROW variable with a single field
10167DELIMITER $$;
10168CREATE PROCEDURE p1()
10169BEGIN
10170  DECLARE done INT DEFAULT FALSE;
10171  DECLARE rec ROW(aa INT);
10172  DECLARE cur CURSOR FOR SELECT 10 AS a UNION SELECT 20;
10173  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10174  OPEN cur;
10175read_loop:
10176  LOOP
10177    FETCH cur INTO rec;
10178    IF done THEN
10179      LEAVE read_loop;
10180    END IF;
10181    SELECT CONCAT('rec=(',rec.aa,')') AS c;
10182  END LOOP;
10183  CLOSE cur;
10184END;
10185$$
10186DELIMITER ;$$
10187CALL p1();
10188DROP PROCEDURE p1;
10189
10190--echo #
10191--echo # MDEV-14228 MariaDB crashes with function
10192--echo #
10193
10194CREATE TABLE t1 (c VARCHAR(16), KEY(c));
10195INSERT INTO t1 VALUES ('foo');
10196
10197DELIMITER $$;
10198CREATE FUNCTION f1() RETURNS VARCHAR(16)
10199BEGIN
10200  DECLARE v VARCHAR(16);
10201  FOR v IN (SELECT DISTINCT c FROM t1)
10202  DO
10203    IF (v = 'bar') THEN
10204      SELECT 1 INTO @a;
10205    END IF;
10206  END FOR;
10207  RETURN 'qux';
10208END $$
10209DELIMITER  ;$$
10210--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10211SELECT f1();
10212DROP FUNCTION f1;
10213
10214DELIMITER $$;
10215CREATE FUNCTION f1() RETURNS VARCHAR(16)
10216BEGIN
10217  DECLARE v ROW TYPE OF t1;
10218  IF v = 'bar' THEN
10219    RETURN 'eq';
10220  END IF;
10221  RETURN 'ne';
10222END $$
10223DELIMITER ;$$
10224--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10225SELECT f1();
10226DROP FUNCTION f1;
10227
10228DELIMITER $$;
10229CREATE FUNCTION f1() RETURNS VARCHAR(16)
10230BEGIN
10231  DECLARE v ROW(a INT);
10232  IF v = 'bar' THEN
10233    RETURN 'eq';
10234  END IF;
10235  RETURN 'ne';
10236END $$
10237DELIMITER ;$$
10238--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10239SELECT f1();
10240DROP FUNCTION f1;
10241
10242DROP TABLE t1;
10243
10244
10245DELIMITER $$;
10246--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10247BEGIN NOT ATOMIC
10248  DECLARE v ROW(a INT);
10249  SELECT v IN ('a','b');
10250END $$
10251DELIMITER ;$$
10252
10253DELIMITER $$;
10254--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10255BEGIN NOT ATOMIC
10256DECLARE v ROW(a INT);
10257  SELECT 'a' IN (v,'b');
10258END $$
10259DELIMITER ;$$
10260
10261DELIMITER $$;
10262--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10263BEGIN NOT ATOMIC
10264  DECLARE v ROW(a INT);
10265  SELECT 'a' IN ('b',v);
10266END $$
10267DELIMITER ;$$
10268
10269--echo #
10270--echo # MDEV-15112 Inconsistent evaluation of spvariable=0 in strict mode
10271--echo #
10272
10273SET sql_mode=STRICT_ALL_TABLES;
10274CREATE OR REPLACE TABLE t1 (e TIMESTAMP(6));
10275INSERT INTO t1 VALUES ('2001-01-01 10:20:30');
10276
10277DELIMITER $$;
10278CREATE FUNCTION f1(a VARBINARY(255))
10279RETURNS INT
10280DETERMINISTIC
10281BEGIN
10282  RETURN a = timestamp'2038-01-19 03:14:07.999999'
10283      OR a = 0;
10284END
10285$$
10286CREATE FUNCTION f2(a VARBINARY(255))
10287RETURNS INT
10288DETERMINISTIC
10289BEGIN
10290  RETURN a = 0;
10291END
10292$$
10293CREATE OR REPLACE FUNCTION f3(a VARBINARY(255))
10294RETURNS INT
10295DETERMINISTIC
10296BEGIN
10297  RETURN a = timestamp'2038-01-19 03:14:07.999999'
10298      OR a = sleep(0);
10299END
10300$$
10301DELIMITER ;$$
10302
10303--error ER_TRUNCATED_WRONG_VALUE
10304SELECT f1(e) FROM t1;
10305--error ER_TRUNCATED_WRONG_VALUE
10306SELECT f2(e) FROM t1;
10307--error ER_TRUNCATED_WRONG_VALUE
10308SELECT f3(e) FROM t1;
10309
10310DROP FUNCTION f1;
10311DROP FUNCTION f2;
10312DROP FUNCTION f3;
10313DROP TABLE t1;
10314
10315--echo # Test affected rows from an sp
10316
10317create table t1 (a int);
10318
10319DELIMITER $$;
10320create procedure p1()
10321begin
10322insert into t1 values(1);
10323insert into t1 values(2);
10324end;
10325$$
10326create procedure p2()
10327begin
10328insert into t1 values(1);
10329call p1();
10330select row_count();
10331insert into t1 values(2);
10332insert into t1 values(2);
10333end;
10334$$
10335DELIMITER ;$$
10336
10337--enable_info
10338CALL p2();
10339--disable_info
10340DROP PROCEDURE p1;
10341DROP PROCEDURE p2;
10342drop table t1;
10343
10344--echo #
10345--echo # MDEV-15957 Unexpected "Data too long" when doing CREATE..SELECT with stored functions
10346--echo #
10347
10348CREATE TABLE t1 (a INT(3));
10349INSERT INTO t1 VALUES (-999);
10350CREATE FUNCTION f1(a INT(3)) RETURNS INT(3) RETURN a;
10351CREATE TABLE t2 AS SELECT CONCAT(a) AS c1, CONCAT(COALESCE(a)) AS c2, CONCAT(f1(a)) AS c3 FROM t1;
10352SHOW CREATE TABLE t2;
10353DROP TABLE t1,t2;
10354DROP FUNCTION f1;
10355
10356
10357CREATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET latin1 RETURN '';
10358CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10359SHOW CREATE TABLE t1;
10360DROP TABLE t1;
10361DROP FUNCTION f1;
10362
10363CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET latin1 RETURN '';
10364CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10365SHOW CREATE TABLE t1;
10366DROP TABLE t1;
10367DROP FUNCTION f1;
10368
10369CREATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET latin1 RETURN '';
10370CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10371SHOW CREATE TABLE t1;
10372DROP TABLE t1;
10373DROP FUNCTION f1;
10374
10375CREATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET latin1 RETURN '';
10376CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10377SHOW CREATE TABLE t1;
10378DROP TABLE t1;
10379DROP FUNCTION f1;
10380
10381CREATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET utf8 RETURN '';
10382CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10383SHOW CREATE TABLE t1;
10384DROP TABLE t1;
10385DROP FUNCTION f1;
10386
10387CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET utf8 RETURN '';
10388CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10389SHOW CREATE TABLE t1;
10390DROP TABLE t1;
10391DROP FUNCTION f1;
10392
10393CREATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET utf8 RETURN '';
10394CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10395SHOW CREATE TABLE t1;
10396DROP TABLE t1;
10397DROP FUNCTION f1;
10398
10399CREATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET utf8 RETURN '';
10400CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10401SHOW CREATE TABLE t1;
10402DROP TABLE t1;
10403DROP FUNCTION f1;
10404
10405--echo #
10406--echo # MDEV-16036: Debug assertion failed in resignal on create
10407--echo # temporary table
10408--echo #
10409
10410set @save_sql_mode= @@sql_mode;
10411set sql_mode='ORACLE';
10412delimiter /;
10413CREATE or replace procedure p4()
10414AS
10415  CONTINUE HANDLER FOR SQLWARNING
10416  BEGIN
10417    NULL;
10418  END;
10419  EXIT HANDLER FOR OTHERS -- SQLEXCEPTION
10420  BEGIN
10421      GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
10422      SELECT @sqlstate, @errno, @text;
10423      SHOW WARNINGS;
10424      RESIGNAL;  -- cause DBG_ASSERT failed
10425  END;
10426BEGIN
10427  CREATE TEMPORARY TABLE IF NOT EXISTS t1(origine VARCHAR2(10) NOT NULL);
10428END
10429/
10430delimiter ;/
10431call p4();
10432call p4();
10433drop procedure p4;
10434drop table t1;
10435set @@sql_mode=@save_sql_mode;
10436set @@global.userstat= @save_userstat;
10437
10438--echo #
10439--echo # MDEV-17363 Compressed columns cannot be restored from dump
10440--echo # COMPRESSED conflicted between data type and SP label,
10441--echo # so it's not allowed as an SP label any more.
10442--echo #
10443
10444DELIMITER $$;
10445CREATE FUNCTION f1() RETURNS TEXT COMPRESSED
10446BEGIN
10447  RETURN '';
10448END;
10449$$
10450DELIMITER ;$$
10451SHOW CREATE FUNCTION f1;
10452DROP FUNCTION f1;
10453
10454DELIMITER $$;
10455--error ER_PARSE_ERROR
10456CREATE FUNCTION f1() RETURNS TEXT
10457COMPRESSED:
10458BEGIN
10459  RETURN '';
10460END;
10461$$
10462DELIMITER ;$$
10463
10464--echo # End of 10.3 tests
10465
10466
10467--echo #
10468--echo # Start of 10.4 tests
10469--echo #
10470
10471--echo #
10472--echo # MDEV-19637 Crash on an SP variable assignment to a wrong subselect
10473--echo #
10474
10475DELIMITER $$;
10476--error ER_CANT_USE_OPTION_HERE
10477BEGIN NOT ATOMIC
10478  DECLARE a INT;
10479  SET a=(SELECT 1 FROM DUAL UNION SELECT HIGH_PRIORITY 2 FROM DUAL);
10480END;
10481$$
10482DELIMITER ;$$
10483
10484
10485--echo #
10486--echo # End of 10.4 tests
10487--echo #
10488