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 t2 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
5052create procedure bug14643_1()
5053begin
5054  declare continue handler for sqlexception select 'boo' as 'Handler';
5055
5056  begin
5057    declare v int default undefined_var;
5058
5059    if v = 1 then
5060      select 1;
5061    else
5062      select v, isnull(v);
5063    end if;
5064  end;
5065end|
5066
5067create procedure bug14643_2()
5068begin
5069  declare continue handler for sqlexception select 'boo' as 'Handler';
5070
5071  case undefined_var
5072  when 1 then
5073    select 1;
5074  else
5075    select 2;
5076  end case;
5077
5078  select undefined_var;
5079end|
5080
5081call bug14643_1()|
5082call bug14643_2()|
5083
5084drop procedure bug14643_1|
5085drop procedure bug14643_2|
5086
5087#
5088# BUG#14304: auto_increment field incorrect set in SP
5089#
5090--disable_warnings
5091drop procedure if exists bug14304|
5092drop table if exists t3, t4|
5093--enable_warnings
5094
5095create table t3(a int primary key auto_increment)|
5096create table t4(a int primary key auto_increment)|
5097
5098create procedure bug14304()
5099begin
5100  insert into t3 set a=null;
5101  insert into t4 set a=null;
5102  insert into t4 set a=null;
5103  insert into t4 set a=null;
5104  insert into t4 set a=null;
5105  insert into t4 set a=null;
5106  insert into t4 select null as a;
5107
5108  insert into t3 set a=null;
5109  insert into t3 set a=null;
5110
5111  select * from t3;
5112end|
5113
5114call bug14304()|
5115
5116drop procedure bug14304|
5117drop table t3, t4|
5118
5119#
5120# BUG#14376: MySQL crash on scoped variable (re)initialization
5121#
5122--disable_warnings
5123drop procedure if exists bug14376|
5124--enable_warnings
5125
5126create procedure bug14376()
5127begin
5128  declare x int default x;
5129end|
5130
5131# Not the error we want, but that's what we got for now...
5132--error ER_BAD_FIELD_ERROR
5133call bug14376()|
5134drop procedure bug14376|
5135
5136create procedure bug14376()
5137begin
5138  declare x int default 42;
5139
5140  begin
5141    declare x int default x;
5142
5143    select x;
5144  end;
5145end|
5146
5147call bug14376()|
5148
5149drop procedure bug14376|
5150
5151create procedure bug14376(x int)
5152begin
5153  declare x int default x;
5154
5155  select x;
5156end|
5157
5158call bug14376(4711)|
5159
5160drop procedure bug14376|
5161
5162#
5163# Bug#5967 "Stored procedure declared variable used instead of column"
5164# The bug should be fixed later.
5165# Test precedence of names of parameters, variable declarations,
5166# variable declarations in nested compound statements, table columns,
5167# table columns in cursor declarations.
5168# According to the standard, table columns take precedence over
5169# variable declarations. In MySQL 5.0 it's vice versa.
5170#
5171
5172--disable_warnings
5173drop procedure if exists bug5967|
5174drop table if exists t3|
5175--enable_warnings
5176create table t3 (a varchar(255))|
5177insert into t3 (a) values ("a - table column")|
5178create procedure bug5967(a varchar(255))
5179begin
5180  declare i varchar(255);
5181  declare c cursor for select a from t3;
5182  select a;
5183  select a from t3 into i;
5184  select i as 'Parameter takes precedence over table column';                     open c;
5185  fetch c into i;
5186  close c;
5187  select i as 'Parameter takes precedence over table column in cursors';
5188  begin
5189    declare a varchar(255) default 'a - local variable';
5190    declare c1 cursor for select a from t3;
5191    select a as 'A local variable takes precedence over parameter';
5192    open c1;
5193    fetch c1 into i;
5194    close c1;
5195    select i as 'A local variable takes precedence over parameter in cursors';
5196    begin
5197      declare a varchar(255) default 'a - local variable in a nested compound statement';
5198      declare c2 cursor for select a from t3;
5199      select a as 'A local variable in a nested compound statement takes precedence over a local variable in the outer statement';
5200      select a from t3 into i;
5201      select i as  'A local variable in a nested compound statement takes precedence over table column';
5202      open c2;
5203      fetch c2 into i;
5204      close c2;
5205      select i as  'A local variable in a nested compound statement takes precedence over table column in cursors';
5206    end;
5207  end;
5208end|
5209call bug5967("a - stored procedure parameter")|
5210drop procedure bug5967|
5211
5212#
5213# Bug#13012 "SP: REPAIR/BACKUP/RESTORE TABLE crashes the server"
5214#
5215--let $backupdir = $MYSQLTEST_VARDIR/tmp/
5216--error 0,1
5217--remove_file $backupdir/t1.frm
5218--error 0,1
5219--remove_file $backupdir/t1.MYD
5220
5221--disable_warnings
5222drop procedure if exists bug13012|
5223--enable_warnings
5224# Disable warnings also for BACKUP/RESTORE: they are deprecated.
5225eval create procedure bug13012()
5226    BEGIN
5227       REPAIR TABLE t1;
5228    END|
5229call bug13012()|
5230
5231--enable_warnings
5232
5233drop procedure bug13012|
5234
5235create view v1 as select * from t1|
5236create procedure bug13012()
5237BEGIN
5238  REPAIR TABLE t1,t2,t3,v1;
5239  OPTIMIZE TABLE t1,t2,t3,v1;
5240  ANALYZE TABLE t1,t2,t3,v1;
5241END|
5242call bug13012()|
5243call bug13012()|
5244call bug13012()|
5245drop procedure bug13012|
5246drop view v1|
5247select * from t1 order by data|
5248
5249#
5250# A test case for Bug#15392 "Server crashes during prepared statement
5251# execute": make sure that stored procedure check for error conditions
5252# properly and do not continue execution if an error has been set.
5253#
5254# It's necessary to use several DBs because in the original code
5255# the successful return of mysql_change_db overrode the error from
5256# execution.
5257drop schema if exists mysqltest1|
5258drop schema if exists mysqltest2|
5259drop schema if exists mysqltest3|
5260create schema mysqltest1|
5261create schema mysqltest2|
5262create schema mysqltest3|
5263use mysqltest3|
5264
5265create procedure mysqltest1.p1 (out prequestid varchar(100))
5266begin
5267  call mysqltest2.p2('call mysqltest3.p3(1, 2)');
5268end|
5269
5270create procedure mysqltest2.p2(in psql text)
5271begin
5272  declare lsql text;
5273  set @lsql= psql;
5274  prepare lstatement from @lsql;
5275  execute lstatement;
5276  deallocate prepare lstatement;
5277end|
5278
5279create procedure mysqltest3.p3(in p1 int)
5280begin
5281  select p1;
5282end|
5283
5284--error ER_SP_WRONG_NO_OF_ARGS
5285call mysqltest1.p1(@rs)|
5286--error ER_SP_WRONG_NO_OF_ARGS
5287call mysqltest1.p1(@rs)|
5288--error ER_SP_WRONG_NO_OF_ARGS
5289call mysqltest1.p1(@rs)|
5290drop schema if exists mysqltest1|
5291drop schema if exists mysqltest2|
5292drop schema if exists mysqltest3|
5293use test|
5294
5295#
5296# Bug#15441 "Running SP causes Server to Crash": check that an SP variable
5297# can not be used in VALUES() function.
5298#
5299--disable_warnings
5300drop table if exists t3|
5301drop procedure if exists bug15441|
5302--enable_warnings
5303create table t3 (id int not null primary key, county varchar(25))|
5304insert into t3 (id, county) values (1, 'York')|
5305
5306# First check that a stored procedure that refers to a parameter in VALUES()
5307# function won't parse.
5308
5309create procedure bug15441(c varchar(25))
5310begin
5311  update t3 set id=2, county=value(c);
5312end|
5313--error ER_BAD_FIELD_ERROR
5314call bug15441('county')|
5315drop procedure bug15441|
5316
5317# Now check the case when there is an ambiguity between column names
5318# and stored procedure parameters: the parser shall resolve the argument
5319# of VALUES() function to the column name.
5320
5321# It's hard to deduce what county refers to in every case (INSERT statement):
5322# 1st county refers to the column
5323# 2nd county refers to the procedure parameter
5324# 3d and 4th county refers to the column, again, but
5325# for 4th county it has the value of SP parameter
5326
5327# In UPDATE statement, just check that values() function returns NULL for
5328# non- INSERT...UPDATE statements, as stated in the manual.
5329
5330create procedure bug15441(county varchar(25))
5331begin
5332  declare c varchar(25) default "hello";
5333
5334  insert into t3 (id, county) values (1, county)
5335  on duplicate key update county= values(county);
5336  select * from t3;
5337
5338  update t3 set id=2, county=value(id);
5339  select * from t3;
5340end|
5341call bug15441('Yale')|
5342drop table t3|
5343drop procedure bug15441|
5344
5345#
5346# BUG#14498: Stored procedures: hang if undefined variable and exception
5347#
5348--disable_warnings
5349drop procedure if exists bug14498_1|
5350drop procedure if exists bug14498_2|
5351drop procedure if exists bug14498_3|
5352drop procedure if exists bug14498_4|
5353drop procedure if exists bug14498_5|
5354--enable_warnings
5355
5356create procedure bug14498_1()
5357begin
5358  declare continue handler for sqlexception select 'error' as 'Handler';
5359
5360  if v then
5361    select 'yes' as 'v';
5362  else
5363    select 'no' as 'v';
5364  end if;
5365  select 'done' as 'End';
5366end|
5367
5368create procedure bug14498_2()
5369begin
5370  declare continue handler for sqlexception select 'error' as 'Handler';
5371
5372  while v do
5373    select 'yes' as 'v';
5374  end while;
5375  select 'done' as 'End';
5376end|
5377
5378create procedure bug14498_3()
5379begin
5380  declare continue handler for sqlexception select 'error' as 'Handler';
5381
5382  repeat
5383    select 'maybe' as 'v';
5384  until v end repeat;
5385  select 'done' as 'End';
5386end|
5387
5388create procedure bug14498_4()
5389begin
5390  declare continue handler for sqlexception select 'error' as 'Handler';
5391
5392  case v
5393  when 1 then
5394    select '1' as 'v';
5395  when 2 then
5396    select '2' as 'v';
5397  else
5398    select '?' as 'v';
5399  end case;
5400  select 'done' as 'End';
5401end|
5402
5403create procedure bug14498_5()
5404begin
5405  declare continue handler for sqlexception select 'error' as 'Handler';
5406
5407  case
5408  when v = 1 then
5409    select '1' as 'v';
5410  when v = 2 then
5411    select '2' as 'v';
5412  else
5413    select '?' as 'v';
5414  end case;
5415  select 'done' as 'End';
5416end|
5417
5418call bug14498_1()|
5419call bug14498_2()|
5420call bug14498_3()|
5421call bug14498_4()|
5422call bug14498_5()|
5423
5424drop procedure bug14498_1|
5425drop procedure bug14498_2|
5426drop procedure bug14498_3|
5427drop procedure bug14498_4|
5428drop procedure bug14498_5|
5429
5430#
5431# BUG#15231: Stored procedure bug with not found condition handler
5432#
5433--disable_warnings
5434drop table if exists t3|
5435drop procedure if exists bug15231_1|
5436drop procedure if exists bug15231_2|
5437drop procedure if exists bug15231_3|
5438drop procedure if exists bug15231_4|
5439drop procedure if exists bug15231_5|
5440drop procedure if exists bug15231_6|
5441--enable_warnings
5442
5443create table t3 (id int not null)|
5444
5445create procedure bug15231_1()
5446begin
5447  declare xid integer;
5448  declare xdone integer default 0;
5449  declare continue handler for not found set xdone = 1;
5450
5451  set xid=null;
5452  call bug15231_2(xid);
5453  select xid, xdone;
5454end|
5455
5456create procedure bug15231_2(inout ioid integer)
5457begin
5458  select "Before NOT FOUND condition is triggered" as '1';
5459  select id into ioid from t3 where id=ioid;
5460  select "After NOT FOUND condtition is triggered" as '2';
5461
5462  if ioid is null then
5463    set ioid=1;
5464  end if;
5465end|
5466
5467create procedure bug15231_3()
5468begin
5469  declare exit handler for sqlwarning
5470    select 'Caught it (correct)' as 'Result';
5471
5472  call bug15231_4();
5473end|
5474
5475create procedure bug15231_4()
5476begin
5477  declare x decimal(2,1);
5478
5479  set x = 'zap';
5480  select 'Missed it (correct)' as 'Result';
5481  show warnings;
5482end|
5483
5484create procedure bug15231_5()
5485begin
5486  declare exit handler for sqlwarning
5487    select 'Caught it (wrong)' as 'Result';
5488
5489  call bug15231_6();
5490end|
5491
5492create procedure bug15231_6()
5493begin
5494  declare x decimal(2,1);
5495
5496  set x = 'zap';
5497  select 'Missed it (correct)' as 'Result';
5498  select id from t3;
5499end|
5500
5501call bug15231_1()|
5502call bug15231_3()|
5503call bug15231_5()|
5504
5505drop table t3|
5506drop procedure bug15231_1|
5507drop procedure bug15231_2|
5508drop procedure bug15231_3|
5509drop procedure bug15231_4|
5510drop procedure bug15231_5|
5511drop procedure bug15231_6|
5512
5513
5514#
5515# BUG#15011: error handler in nested block not activated
5516#
5517--disable_warnings
5518drop procedure if exists bug15011|
5519--enable_warnings
5520
5521create table t3 (c1 int primary key)|
5522
5523insert into t3 values (1)|
5524
5525create procedure bug15011()
5526  deterministic
5527begin
5528  declare continue handler for 1062
5529    select 'Outer' as 'Handler';
5530
5531  begin
5532    declare continue handler for 1062
5533      select 'Inner' as 'Handler';
5534
5535    insert into t3 values (1);
5536  end;
5537end|
5538
5539call bug15011()|
5540
5541drop procedure bug15011|
5542drop table t3|
5543
5544
5545#
5546# BUG#17476: Stored procedure not returning data when it is called first
5547#            time per connection
5548#
5549--disable_warnings
5550drop procedure if exists bug17476|
5551--enable_warnings
5552
5553create table t3 ( d date )|
5554insert into t3 values
5555  ( '2005-01-01' ), ( '2005-01-02' ), ( '2005-01-03' ),
5556  ( '2005-01-04' ), ( '2005-02-01' ), ( '2005-02-02' )|
5557
5558create procedure bug17476(pDateFormat varchar(10))
5559  select date_format(t3.d, pDateFormat), count(*)
5560    from t3
5561    group by date_format(t3.d, pDateFormat)|
5562
5563call bug17476('%Y-%m')|
5564call bug17476('%Y-%m')|
5565
5566drop table t3|
5567drop procedure bug17476|
5568
5569
5570#
5571# BUG#16887: Cursor causes server segfault
5572#
5573--disable_warnings
5574drop table if exists t3|
5575drop procedure if exists bug16887|
5576--enable_warnings
5577
5578create table t3 ( c varchar(1) )|
5579
5580insert into t3 values
5581  (' '),('.'),(';'),(','),('-'),('_'),('('),(')'),('/'),('\\')|
5582
5583create procedure bug16887()
5584begin
5585  declare i int default 10;
5586
5587 again:
5588  while i > 0 do
5589  begin
5590    declare breakchar varchar(1);
5591    declare done int default 0;
5592    declare t3_cursor cursor for select c from t3;
5593    declare continue handler for not found set done = 1;
5594
5595    set i = i - 1;
5596    select i;
5597
5598    if i = 3 then
5599      iterate again;
5600    end if;
5601
5602    open t3_cursor;
5603
5604    loop
5605      fetch t3_cursor into breakchar;
5606
5607      if done = 1 then
5608        begin
5609          close t3_cursor;
5610          iterate again;
5611        end;
5612      end if;
5613     end loop;
5614   end;
5615   end while;
5616end|
5617
5618call bug16887()|
5619
5620drop table t3|
5621drop procedure bug16887|
5622
5623#
5624# BUG#16474: SP crashed MySQL
5625# (when using "order by localvar", where 'localvar' is just that.
5626#
5627--disable_warnings
5628drop procedure if exists bug16474_1|
5629drop procedure if exists bug16474_2|
5630--enable_warnings
5631
5632delete from t1|
5633insert into t1 values ('c', 2), ('b', 3), ('a', 1)|
5634
5635create procedure bug16474_1()
5636begin
5637  declare x int;
5638
5639  select id from t1 order by x, id;
5640end|
5641
5642#
5643# BUG#14945: Truncate table doesn't reset the auto_increment counter
5644#
5645--disable_warnings
5646drop procedure if exists bug14945|
5647--enable_warnings
5648create table t3 (id int not null auto_increment primary key)|
5649create procedure bug14945() deterministic truncate t3|
5650insert into t3 values (null)|
5651call bug14945()|
5652insert into t3 values (null)|
5653select * from t3|
5654drop table t3|
5655drop procedure bug14945|
5656
5657# This does NOT order by column index; variable is an expression.
5658create procedure bug16474_2(x int)
5659  select id from t1 order by x, id|
5660
5661call bug16474_1()|
5662call bug16474_2(1)|
5663call bug16474_2(2)|
5664drop procedure bug16474_1|
5665drop procedure bug16474_2|
5666
5667# For reference: user variables are expressions too and do not affect ordering.
5668set @x = 2|
5669select * from t1 order by @x, data|
5670
5671delete from t1|
5672
5673
5674#
5675# BUG#15728: LAST_INSERT_ID function inside a stored function returns 0
5676#
5677# The solution is not to reset last_insert_id on enter to sub-statement.
5678#
5679--disable_warnings
5680drop function if exists bug15728|
5681drop table if exists t3|
5682--enable_warnings
5683
5684create table t3 (
5685  id int not null auto_increment,
5686  primary key (id)
5687)|
5688create function bug15728() returns int(11)
5689  return last_insert_id()|
5690
5691insert into t3 values (0)|
5692select last_insert_id()|
5693select bug15728()|
5694
5695drop function bug15728|
5696drop table t3|
5697
5698
5699#
5700# BUG#18787: Server crashed when calling a stored procedure containing
5701#            a misnamed function
5702#
5703--disable_warnings
5704drop procedure if exists bug18787|
5705--enable_warnings
5706create procedure bug18787()
5707begin
5708  declare continue handler for sqlexception begin end;
5709
5710  select no_such_function();
5711end|
5712
5713call bug18787()|
5714drop procedure bug18787|
5715
5716
5717#
5718# BUG#18344: DROP DATABASE does not drop associated routines
5719# (... if the database name is longer than 21 characters)
5720#
5721#               1234567890123456789012
5722create database bug18344_012345678901|
5723use bug18344_012345678901|
5724create procedure bug18344() begin end|
5725create procedure bug18344_2() begin end|
5726
5727create database bug18344_0123456789012|
5728use bug18344_0123456789012|
5729create procedure bug18344() begin end|
5730create procedure bug18344_2() begin end|
5731
5732use test|
5733
5734--sorted_result
5735select schema_name from information_schema.schemata where
5736  schema_name like 'bug18344%'|
5737--sorted_result
5738select routine_name,routine_schema from information_schema.routines where
5739  routine_schema like 'bug18344%'|
5740
5741drop database bug18344_012345678901|
5742drop database bug18344_0123456789012|
5743
5744# Should be nothing left.
5745select schema_name from information_schema.schemata where
5746  schema_name like 'bug18344%'|
5747select routine_name,routine_schema from information_schema.routines where
5748  routine_schema like 'bug18344%'|
5749
5750
5751#
5752# BUG#12472/BUG#15137 'CREATE TABLE ... SELECT ... which explicitly or
5753# implicitly uses stored function gives "Table not locked" error'.
5754#
5755--disable_warnings
5756drop function if exists bug12472|
5757--enable_warnings
5758create function bug12472() returns int return (select count(*) from t1)|
5759# Check case when function is used directly
5760create table t3 as select bug12472() as i|
5761show create table t3|
5762select * from t3|
5763drop table t3|
5764# Check case when function is used indirectly through view
5765create view v1 as select bug12472() as j|
5766create table t3 as select * from v1|
5767show create table t3|
5768select * from t3|
5769drop table t3|
5770drop view v1|
5771drop function bug12472|
5772
5773
5774#
5775# BUG#18587: Function that accepts and returns TEXT garbles data if longer than
5776# 766 chars
5777#
5778
5779# Prepare.
5780
5781--disable_warnings
5782DROP FUNCTION IF EXISTS bug18589_f1|
5783DROP PROCEDURE IF EXISTS bug18589_p1|
5784DROP PROCEDURE IF EXISTS bug18589_p2|
5785--enable_warnings
5786
5787CREATE FUNCTION bug18589_f1(arg TEXT) RETURNS TEXT
5788BEGIN
5789  RETURN CONCAT(arg, "");
5790END|
5791
5792CREATE PROCEDURE bug18589_p1(arg TEXT, OUT ret TEXT)
5793BEGIN
5794  SET ret = CONCAT(arg, "");
5795END|
5796
5797CREATE PROCEDURE bug18589_p2(arg TEXT)
5798BEGIN
5799  DECLARE v TEXT;
5800  CALL bug18589_p1(arg, v);
5801  SELECT v;
5802END|
5803
5804# Test case.
5805
5806SELECT bug18589_f1(REPEAT("a", 767))|
5807
5808SET @bug18589_v1 = ""|
5809CALL bug18589_p1(REPEAT("a", 767), @bug18589_v1)|
5810SELECT @bug18589_v1|
5811
5812CALL bug18589_p2(REPEAT("a", 767))|
5813
5814# Cleanup.
5815
5816DROP FUNCTION bug18589_f1|
5817DROP PROCEDURE bug18589_p1|
5818DROP PROCEDURE bug18589_p2|
5819
5820
5821#
5822# BUG#18037: Server crash when returning system variable in stored procedures
5823# BUG#19633: Stack corruption in fix_fields()/THD::rollback_item_tree_changes()
5824#
5825
5826# Prepare.
5827
5828--disable_warnings
5829DROP FUNCTION IF EXISTS bug18037_f1|
5830DROP PROCEDURE IF EXISTS bug18037_p1|
5831DROP PROCEDURE IF EXISTS bug18037_p2|
5832--enable_warnings
5833
5834# Test case.
5835
5836CREATE FUNCTION bug18037_f1() RETURNS INT
5837BEGIN
5838  RETURN @@server_id;
5839END|
5840
5841CREATE PROCEDURE bug18037_p1()
5842BEGIN
5843  DECLARE v INT DEFAULT @@server_id;
5844END|
5845
5846CREATE PROCEDURE bug18037_p2()
5847BEGIN
5848  CASE @@server_id
5849  WHEN -1 THEN
5850    SELECT 0;
5851  ELSE
5852    SELECT 1;
5853  END CASE;
5854END|
5855
5856SELECT bug18037_f1()|
5857CALL bug18037_p1()|
5858CALL bug18037_p2()|
5859
5860# Cleanup.
5861
5862DROP FUNCTION bug18037_f1|
5863DROP PROCEDURE bug18037_p1|
5864DROP PROCEDURE bug18037_p2|
5865
5866#
5867# Bug#17199: "Table not found" error occurs if the query contains a call
5868#            to a function from another database.
5869#            See also ps.test for an additional test case for this bug.
5870#
5871use test|
5872create table t3 (i int)|
5873insert into t3 values (1), (2)|
5874create database mysqltest1|
5875use mysqltest1|
5876create function bug17199() returns varchar(2) deterministic return 'ok'|
5877use test|
5878select *, mysqltest1.bug17199() from t3|
5879#
5880# Bug#18444: Fully qualified stored function names don't work correctly
5881#            in select statements
5882#
5883use mysqltest1|
5884create function bug18444(i int) returns int no sql deterministic return i + 1|
5885use test|
5886select mysqltest1.bug18444(i) from t3|
5887drop database mysqltest1|
5888#
5889# Check that current database has no influence to a stored procedure
5890#
5891create database mysqltest1 charset=utf8|
5892create database mysqltest2 charset=utf8|
5893create procedure mysqltest1.p1()
5894begin
5895-- alters the default collation of database test
5896  alter database character set koi8r;
5897end|
5898use mysqltest1|
5899call p1()|
5900show create database mysqltest1|
5901show create database mysqltest2|
5902alter database mysqltest1 character set utf8|
5903use mysqltest2|
5904call mysqltest1.p1()|
5905show create database mysqltest1|
5906show create database mysqltest2|
5907drop database mysqltest1|
5908drop database mysqltest2|
5909#
5910# Restore the old environemnt
5911use test|
5912#
5913# Bug#15217 "Using a SP cursor on a table created with PREPARE fails with
5914#           weird error". Check that the code that is supposed to work at
5915#           the first execution of a stored procedure actually works for
5916#           sp_instr_copen.
5917
5918--disable_warnings
5919drop table if exists t3|
5920drop procedure if exists bug15217|
5921--enable_warnings
5922create table t3 as select 1|
5923create procedure bug15217()
5924begin
5925  declare var1 char(255);
5926  declare cur1 cursor for select * from t3;
5927  open cur1;
5928  fetch cur1 into var1;
5929  select concat('data was: /', var1, '/');
5930  close cur1;
5931end |
5932# Returns expected result
5933call bug15217()|
5934flush tables |
5935# Returns error with garbage as column name
5936call bug15217()|
5937drop table t3|
5938drop procedure bug15217|
5939
5940
5941#
5942# BUG#21013: Performance Degrades when importing data that uses
5943# Trigger and Stored Procedure
5944#
5945# This is a performance and memory leak test.  Run with large number
5946# passed to bug21013() procedure.
5947#
5948--disable_warnings
5949DROP PROCEDURE IF EXISTS bug21013 |
5950--enable_warnings
5951
5952CREATE PROCEDURE bug21013(IN lim INT)
5953BEGIN
5954  DECLARE i INT DEFAULT 0;
5955  WHILE (i < lim) DO
5956    SET @b = LOCATE(_latin1'b', @a, 1);
5957    SET i = i + 1;
5958  END WHILE;
5959END |
5960
5961SET @a = _latin2"aaaaaaaaaa" |
5962CALL bug21013(10) |
5963
5964DROP PROCEDURE bug21013 |
5965
5966
5967#
5968# BUG#16211: Stored function return type for strings is ignored
5969#
5970
5971# Prepare: create database with fixed, pre-defined character set.
5972
5973--disable_warnings
5974DROP DATABASE IF EXISTS mysqltest1|
5975DROP DATABASE IF EXISTS mysqltest2|
5976--enable_warnings
5977
5978CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
5979CREATE DATABASE mysqltest2 DEFAULT CHARACTER SET utf8|
5980
5981# Test case:
5982
5983use mysqltest1|
5984
5985#   - Create two stored functions -- with and without explicit CHARSET-clause
5986#     for return value;
5987
5988CREATE FUNCTION bug16211_f1() RETURNS CHAR(10)
5989  RETURN ""|
5990
5991CREATE FUNCTION bug16211_f2() RETURNS CHAR(10) CHARSET koi8r
5992  RETURN ""|
5993
5994CREATE FUNCTION mysqltest2.bug16211_f3() RETURNS CHAR(10)
5995  RETURN ""|
5996
5997CREATE FUNCTION mysqltest2.bug16211_f4() RETURNS CHAR(10) CHARSET koi8r
5998  RETURN ""|
5999
6000#   - Check that CHARSET-clause is specified for the second function;
6001
6002SHOW CREATE FUNCTION bug16211_f1|
6003SHOW CREATE FUNCTION bug16211_f2|
6004
6005SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
6006SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
6007
6008SELECT dtd_identifier
6009FROM INFORMATION_SCHEMA.ROUTINES
6010WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
6011
6012SELECT dtd_identifier
6013FROM INFORMATION_SCHEMA.ROUTINES
6014WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
6015
6016SELECT dtd_identifier
6017FROM INFORMATION_SCHEMA.ROUTINES
6018WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
6019
6020SELECT dtd_identifier
6021FROM INFORMATION_SCHEMA.ROUTINES
6022WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
6023
6024SELECT CHARSET(bug16211_f1())|
6025SELECT CHARSET(bug16211_f2())|
6026
6027SELECT CHARSET(mysqltest2.bug16211_f3())|
6028SELECT CHARSET(mysqltest2.bug16211_f4())|
6029
6030#   - Alter database character set.
6031
6032ALTER DATABASE mysqltest1 CHARACTER SET cp1251|
6033ALTER DATABASE mysqltest2 CHARACTER SET cp1251|
6034
6035#   - Check that CHARSET-clause has not changed.
6036
6037SHOW CREATE FUNCTION bug16211_f1|
6038SHOW CREATE FUNCTION bug16211_f2|
6039
6040SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
6041SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
6042
6043SELECT dtd_identifier
6044FROM INFORMATION_SCHEMA.ROUTINES
6045WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
6046
6047SELECT dtd_identifier
6048FROM INFORMATION_SCHEMA.ROUTINES
6049WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
6050
6051SELECT dtd_identifier
6052FROM INFORMATION_SCHEMA.ROUTINES
6053WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
6054
6055SELECT dtd_identifier
6056FROM INFORMATION_SCHEMA.ROUTINES
6057WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
6058
6059SELECT CHARSET(bug16211_f1())|
6060SELECT CHARSET(bug16211_f2())|
6061
6062SELECT CHARSET(mysqltest2.bug16211_f3())|
6063SELECT CHARSET(mysqltest2.bug16211_f4())|
6064
6065# Cleanup.
6066
6067use test|
6068
6069DROP DATABASE mysqltest1|
6070DROP DATABASE mysqltest2|
6071
6072
6073#
6074# BUG#16676: Database CHARSET not used for stored procedures
6075#
6076
6077# Prepare: create database with fixed, pre-defined character set.
6078
6079--disable_warnings
6080DROP DATABASE IF EXISTS mysqltest1|
6081--enable_warnings
6082
6083CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
6084
6085# Test case:
6086
6087use mysqltest1|
6088
6089#   - Create two stored procedures -- with and without explicit CHARSET-clause;
6090
6091CREATE PROCEDURE bug16676_p1(
6092  IN p1 CHAR(10),
6093  INOUT p2 CHAR(10),
6094  OUT p3 CHAR(10))
6095BEGIN
6096  SELECT CHARSET(p1), COLLATION(p1);
6097  SELECT CHARSET(p2), COLLATION(p2);
6098  SELECT CHARSET(p3), COLLATION(p3);
6099END|
6100
6101CREATE PROCEDURE bug16676_p2(
6102  IN p1 CHAR(10) CHARSET koi8r,
6103  INOUT p2 CHAR(10) CHARSET cp1251,
6104  OUT p3 CHAR(10) CHARSET greek)
6105BEGIN
6106  SELECT CHARSET(p1), COLLATION(p1);
6107  SELECT CHARSET(p2), COLLATION(p2);
6108  SELECT CHARSET(p3), COLLATION(p3);
6109END|
6110
6111#   - Call procedures.
6112
6113SET @v2 = 'b'|
6114SET @v3 = 'c'|
6115
6116CALL bug16676_p1('a', @v2, @v3)|
6117CALL bug16676_p2('a', @v2, @v3)|
6118
6119# Cleanup.
6120
6121use test|
6122
6123DROP DATABASE mysqltest1|
6124#
6125# BUG#8153: Stored procedure with subquery and continue handler, wrong result
6126#
6127
6128--disable_warnings
6129drop table if exists t3|
6130drop table if exists t4|
6131drop procedure if exists bug8153_subselect|
6132drop procedure if exists bug8153_subselect_a|
6133drop procedure if exists bug8153_subselect_b|
6134drop procedure if exists bug8153_proc_a|
6135drop procedure if exists bug8153_proc_b|
6136--enable_warnings
6137
6138create table t3 (a int)|
6139create table t4 (a int)|
6140insert into t3 values (1), (1), (2), (3)|
6141insert into t4 values (1), (1)|
6142
6143## Testing the use case reported in Bug#8153
6144
6145create procedure bug8153_subselect()
6146begin
6147  declare continue handler for sqlexception
6148  begin
6149    select 'statement failed';
6150  end;
6151  update t3 set a=a+1 where (select a from t4 where a=1) is null;
6152  select 'statement after update';
6153end|
6154
6155call bug8153_subselect()|
6156select * from t3|
6157
6158call bug8153_subselect()|
6159select * from t3|
6160
6161drop procedure bug8153_subselect|
6162
6163## Testing a subselect with a non local handler
6164
6165create procedure bug8153_subselect_a()
6166begin
6167  declare continue handler for sqlexception
6168  begin
6169    select 'in continue handler';
6170  end;
6171
6172  select 'reachable code a1';
6173  call bug8153_subselect_b();
6174  select 'reachable code a2';
6175end|
6176
6177create procedure bug8153_subselect_b()
6178begin
6179  select 'reachable code b1';
6180  update t3 set a=a+1 where (select a from t4 where a=1) is null;
6181  select 'unreachable code b2';
6182end|
6183
6184call bug8153_subselect_a()|
6185select * from t3|
6186
6187call bug8153_subselect_a()|
6188select * from t3|
6189
6190drop procedure bug8153_subselect_a|
6191drop procedure bug8153_subselect_b|
6192
6193## Testing extra use cases, found while investigating
6194## This is related to BUG#18787, with a non local handler
6195
6196create procedure bug8153_proc_a()
6197begin
6198  declare continue handler for sqlexception
6199  begin
6200    select 'in continue handler';
6201  end;
6202
6203  select 'reachable code a1';
6204  call bug8153_proc_b();
6205  select 'reachable code a2';
6206end|
6207
6208create procedure bug8153_proc_b()
6209begin
6210  select 'reachable code b1';
6211  select no_such_function();
6212  select 'unreachable code b2';
6213end|
6214
6215call bug8153_proc_a()|
6216
6217drop procedure bug8153_proc_a|
6218drop procedure bug8153_proc_b|
6219drop table t3|
6220drop table t4|
6221
6222#
6223# BUG#19862: Sort with filesort by function evaluates function twice
6224#
6225--disable_warnings
6226drop procedure if exists bug19862|
6227--enable_warnings
6228CREATE TABLE t11 (a INT)|
6229CREATE TABLE t12 (a INT)|
6230CREATE FUNCTION bug19862(x INT) RETURNS INT
6231  BEGIN
6232    INSERT INTO t11 VALUES (x);
6233    RETURN x+1;
6234  END|
6235INSERT INTO t12 VALUES (1), (2)|
6236SELECT bug19862(a) FROM t12 ORDER BY 1|
6237SELECT * FROM t11|
6238DROP TABLE t11, t12|
6239DROP FUNCTION bug19862|
6240
6241
6242# Bug#21002 "Derived table not selecting from a "real" table fails in JOINs"
6243#
6244# A regression caused by the fix for Bug#18444: for derived tables we should
6245# set an empty string as the current database. They do not belong to any
6246# database and must be usable even if there is no database
6247# selected.
6248--disable_warnings
6249drop table if exists t3|
6250drop database if exists mysqltest1|
6251--enable_warnings
6252create table t3 (a int)|
6253insert into t3 (a) values (1), (2)|
6254
6255create database mysqltest1|
6256use mysqltest1|
6257drop database mysqltest1|
6258
6259# No current database
6260select database()|
6261
6262select * from (select 1 as a) as t1 natural join (select * from test.t3) as t2|
6263use test|
6264drop table t3|
6265
6266
6267# Test for BUG#16899: Possible buffer overflow in handling of DEFINER-clause.
6268#
6269# Prepare.
6270
6271--disable_warnings
6272DROP PROCEDURE IF EXISTS bug16899_p1|
6273DROP FUNCTION IF EXISTS bug16899_f1|
6274--enable_warnings
6275
6276--error ER_WRONG_STRING_LENGTH
6277CREATE DEFINER=longer_than_80_456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789@localhost PROCEDURE bug16899_p1()
6278BEGIN
6279  SET @a = 1;
6280END|
6281
6282--error ER_WRONG_STRING_LENGTH
6283CREATE DEFINER=some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY
6284  FUNCTION bug16899_f1() RETURNS INT
6285BEGIN
6286  RETURN 1;
6287END|
6288
6289
6290#
6291# BUG#21416: SP: Recursion level higher than zero needed for non-recursive call
6292#
6293--disable_warnings
6294drop procedure if exists bug21416|
6295--enable_warnings
6296create procedure bug21416() show create procedure bug21416|
6297call bug21416()|
6298drop procedure bug21416|
6299
6300
6301#
6302# BUG#21414: SP: Procedure undroppable, to some extent
6303#
6304--disable_warnings
6305DROP PROCEDURE IF EXISTS bug21414|
6306--enable_warnings
6307
6308CREATE PROCEDURE bug21414() SELECT 1|
6309
6310FLUSH TABLES WITH READ LOCK|
6311
6312--error ER_CANT_UPDATE_WITH_READLOCK
6313DROP PROCEDURE bug21414|
6314
6315UNLOCK TABLES|
6316
6317--echo The following should succeed.
6318DROP PROCEDURE bug21414|
6319
6320
6321#
6322# BUG#21311: Possible stack overrun if SP has non-latin1 name
6323#
6324set names utf8|
6325--disable_warnings
6326drop database if exists това_е_дълго_име_за_база_данни_нали|
6327--enable_warnings
6328create database това_е_дълго_име_за_база_данни_нали|
6329INSERT 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')|
6330--error ER_SP_PROC_TABLE_CORRUPT
6331call това_е_дълго_име_за_база_данни_нали.това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго()|
6332drop database това_е_дълго_име_за_база_данни_нали|
6333
6334
6335#
6336# BUG#21493: Crash on the second call of a procedure containing
6337#            a select statement that uses an IN aggregating subquery
6338#
6339
6340CREATE TABLE t3 (
6341  Member_ID varchar(15) NOT NULL,
6342  PRIMARY KEY (Member_ID)
6343)|
6344
6345CREATE TABLE t4 (
6346  ID int(10) unsigned NOT NULL auto_increment,
6347  Member_ID varchar(15) NOT NULL default '',
6348  Action varchar(12) NOT NULL,
6349  Action_Date datetime NOT NULL,
6350  Track varchar(15) default NULL,
6351  User varchar(12) default NULL,
6352  Date_Updated timestamp NOT NULL default CURRENT_TIMESTAMP on update
6353    CURRENT_TIMESTAMP,
6354  PRIMARY KEY (ID),
6355  KEY Action (Action),
6356  KEY Action_Date (Action_Date)
6357)|
6358
6359
6360INSERT INTO t3(Member_ID) VALUES
6361  ('111111'), ('222222'), ('333333'), ('444444'), ('555555'), ('666666')|
6362
6363INSERT INTO t4(Member_ID, Action, Action_Date, Track) VALUES
6364  ('111111', 'Disenrolled', '2006-03-01', 'CAD' ),
6365  ('111111', 'Enrolled', '2006-03-01', 'CAD' ),
6366  ('111111', 'Disenrolled', '2006-07-03', 'CAD' ),
6367  ('222222', 'Enrolled', '2006-03-07', 'CAD' ),
6368  ('222222', 'Enrolled', '2006-03-07', 'CHF' ),
6369  ('222222', 'Disenrolled', '2006-08-02', 'CHF' ),
6370  ('333333', 'Enrolled', '2006-03-01', 'CAD' ),
6371  ('333333', 'Disenrolled', '2006-03-01', 'CAD' ),
6372  ('444444', 'Enrolled', '2006-03-01', 'CAD' ),
6373  ('555555', 'Disenrolled', '2006-03-01', 'CAD' ),
6374  ('555555', 'Enrolled', '2006-07-21', 'CAD' ),
6375  ('555555', 'Disenrolled', '2006-03-01', 'CHF' ),
6376  ('666666', 'Enrolled', '2006-02-09', 'CAD' ),
6377  ('666666', 'Enrolled', '2006-05-12', 'CHF' ),
6378  ('666666', 'Disenrolled', '2006-06-01', 'CAD' )|
6379
6380--disable_warnings
6381DROP FUNCTION IF EXISTS bug21493|
6382--enable_warnings
6383
6384CREATE FUNCTION bug21493(paramMember VARCHAR(15)) RETURNS varchar(45)
6385BEGIN
6386DECLARE tracks VARCHAR(45);
6387SELECT GROUP_CONCAT(Track SEPARATOR ', ') INTO tracks FROM t4
6388  WHERE Member_ID=paramMember AND Action='Enrolled' AND
6389        (Track,Action_Date) IN (SELECT Track, MAX(Action_Date) FROM t4
6390                                  WHERE Member_ID=paramMember GROUP BY Track);
6391RETURN tracks;
6392END|
6393
6394SELECT bug21493('111111')|
6395SELECT bug21493('222222')|
6396
6397SELECT bug21493(Member_ID) FROM t3|
6398
6399DROP FUNCTION bug21493|
6400DROP TABLE t3,t4|
6401
6402#
6403# Bug#20028 Function with select return no data
6404#
6405
6406--disable_warnings
6407drop function if exists func_20028_a|
6408drop function if exists func_20028_b|
6409drop function if exists func_20028_c|
6410drop procedure if exists proc_20028_a|
6411drop procedure if exists proc_20028_b|
6412drop procedure if exists proc_20028_c|
6413drop table if exists table_20028|
6414--enable_warnings
6415
6416create table table_20028 (i int)|
6417
6418SET @save_sql_mode=@@sql_mode|
6419
6420SET sql_mode=''|
6421
6422create function func_20028_a() returns integer
6423begin
6424  declare temp integer;
6425  select i into temp from table_20028 limit 1;
6426  return ifnull(temp, 0);
6427end|
6428
6429create function func_20028_b() returns integer
6430begin
6431  return func_20028_a();
6432end|
6433
6434create function func_20028_c() returns integer
6435begin
6436  declare div_zero integer;
6437  set SQL_MODE='TRADITIONAL';
6438  select 1/0 into div_zero;
6439  return div_zero;
6440end|
6441
6442create procedure proc_20028_a()
6443begin
6444  declare temp integer;
6445  select i into temp from table_20028 limit 1;
6446end|
6447
6448create procedure proc_20028_b()
6449begin
6450  call proc_20028_a();
6451end|
6452
6453create procedure proc_20028_c()
6454begin
6455  declare div_zero integer;
6456  set SQL_MODE='TRADITIONAL';
6457  select 1/0 into div_zero;
6458end|
6459
6460select func_20028_a()|
6461select func_20028_b()|
6462--error ER_DIVISION_BY_ZERO
6463select func_20028_c()|
6464call proc_20028_a()|
6465call proc_20028_b()|
6466--error ER_DIVISION_BY_ZERO
6467call proc_20028_c()|
6468
6469SET sql_mode='TRADITIONAL'|
6470
6471drop function func_20028_a|
6472drop function func_20028_b|
6473drop function func_20028_c|
6474drop procedure proc_20028_a|
6475drop procedure proc_20028_b|
6476drop procedure proc_20028_c|
6477
6478create function func_20028_a() returns integer
6479begin
6480  declare temp integer;
6481  select i into temp from table_20028 limit 1;
6482  return ifnull(temp, 0);
6483end|
6484
6485create function func_20028_b() returns integer
6486begin
6487  return func_20028_a();
6488end|
6489
6490create function func_20028_c() returns integer
6491begin
6492  declare div_zero integer;
6493  set SQL_MODE='';
6494  select 1/0 into div_zero;
6495  return div_zero;
6496end|
6497
6498create procedure proc_20028_a()
6499begin
6500  declare temp integer;
6501  select i into temp from table_20028 limit 1;
6502end|
6503
6504create procedure proc_20028_b()
6505begin
6506  call proc_20028_a();
6507end|
6508
6509create procedure proc_20028_c()
6510begin
6511  declare div_zero integer;
6512  set SQL_MODE='';
6513  select 1/0 into div_zero;
6514end|
6515
6516select func_20028_a()|
6517select func_20028_b()|
6518select func_20028_c()|
6519call proc_20028_a()|
6520call proc_20028_b()|
6521call proc_20028_c()|
6522
6523SET @@sql_mode=@save_sql_mode|
6524
6525drop function func_20028_a|
6526drop function func_20028_b|
6527drop function func_20028_c|
6528drop procedure proc_20028_a|
6529drop procedure proc_20028_b|
6530drop procedure proc_20028_c|
6531drop table table_20028|
6532
6533#
6534# Bug#21462 Stored procedures with no arguments require parenthesis
6535#
6536
6537--disable_warnings
6538drop procedure if exists proc_21462_a|
6539drop procedure if exists proc_21462_b|
6540--enable_warnings
6541
6542create procedure proc_21462_a()
6543begin
6544  select "Called A";
6545end|
6546
6547create procedure proc_21462_b(x int)
6548begin
6549  select "Called B";
6550end|
6551
6552call proc_21462_a|
6553call proc_21462_a()|
6554-- error ER_SP_WRONG_NO_OF_ARGS
6555call proc_21462_a(1)|
6556
6557-- error ER_SP_WRONG_NO_OF_ARGS
6558call proc_21462_b|
6559-- error ER_SP_WRONG_NO_OF_ARGS
6560call proc_21462_b()|
6561call proc_21462_b(1)|
6562
6563drop procedure proc_21462_a|
6564drop procedure proc_21462_b|
6565
6566
6567#
6568# Bug#19733 "Repeated alter, or repeated create/drop, fails"
6569# Check that CREATE/DROP INDEX is re-execution friendly.
6570#
6571--disable_warnings
6572drop table if exists t3|
6573drop procedure if exists proc_bug19733|
6574--enable_warnings
6575create table t3 (s1 int)|
6576
6577create procedure proc_bug19733()
6578begin
6579  declare v int default 0;
6580  while v < 100 do
6581    create index i on t3 (s1);
6582    drop index i on t3;
6583    set v = v + 1;
6584  end while;
6585end|
6586
6587call proc_bug19733()|
6588call proc_bug19733()|
6589call proc_bug19733()|
6590
6591drop procedure proc_bug19733|
6592drop table t3|
6593
6594
6595#
6596# BUG#20492: Subsequent calls to stored procedure yeild incorrect
6597# result if join is used
6598#
6599# Optimized ON expression in join wasn't properly saved for reuse.
6600#
6601--disable_warnings
6602DROP PROCEDURE IF EXISTS p1|
6603DROP VIEW IF EXISTS v1, v2|
6604DROP TABLE IF EXISTS t3, t4|
6605--enable_warnings
6606
6607CREATE TABLE t3 (t3_id INT)|
6608
6609INSERT INTO t3 VALUES (0)|
6610INSERT INTO t3 VALUES (1)|
6611
6612CREATE TABLE t4 (t4_id INT)|
6613
6614INSERT INTO t4 VALUES (2)|
6615
6616CREATE VIEW v1 AS
6617SELECT t3.t3_id, t4.t4_id
6618FROM t3 JOIN t4 ON t3.t3_id = 0|
6619
6620CREATE VIEW v2 AS
6621SELECT t3.t3_id AS t3_id_1, v1.t3_id AS t3_id_2, v1.t4_id
6622FROM t3 LEFT JOIN v1 ON t3.t3_id = 0|
6623
6624CREATE PROCEDURE p1() SELECT * FROM v2|
6625
6626# Results should not differ.
6627CALL p1()|
6628CALL p1()|
6629
6630DROP PROCEDURE p1|
6631DROP VIEW v1, v2|
6632DROP TABLE t3, t4|
6633
6634--echo End of 5.0 tests
6635
6636--echo Begin of 5.1 tests
6637
6638#
6639# BUG#18239: Possible to overload internal functions with stored functions
6640#
6641
6642delimiter ;|
6643
6644--disable_warnings
6645drop function if exists pi;
6646--enable_warnings
6647
6648create function pi() returns varchar(50)
6649return "pie, my favorite desert.";
6650
6651SET @save_sql_mode=@@sql_mode;
6652
6653SET SQL_MODE='IGNORE_SPACE';
6654
6655select pi(), pi ();
6656
6657# Non deterministic warnings from db_load_routine
6658--disable_warnings
6659select test.pi(), test.pi ();
6660--enable_warnings
6661
6662SET SQL_MODE='';
6663
6664select pi(), pi ();
6665
6666# Non deterministic warnings from db_load_routine
6667--disable_warnings
6668select test.pi(), test.pi ();
6669--enable_warnings
6670
6671SET @@sql_mode=@save_sql_mode;
6672
6673drop function pi;
6674# End of BUG#18239
6675
6676#
6677# BUG#22619: Spaces considered harmful
6678#
6679
6680--disable_warnings
6681drop function if exists test.database;
6682drop function if exists test.current_user;
6683drop function if exists test.md5;
6684--enable_warnings
6685
6686create database nowhere;
6687use nowhere;
6688drop database nowhere;
6689
6690SET @save_sql_mode=@@sql_mode;
6691
6692SET SQL_MODE='IGNORE_SPACE';
6693
6694select database(), database ();
6695select current_user(), current_user ();
6696select md5("aaa"), md5 ("aaa");
6697
6698SET SQL_MODE='';
6699
6700select database(), database ();
6701select current_user(), current_user ();
6702select md5("aaa"), md5 ("aaa");
6703
6704use test;
6705
6706create function `database`() returns varchar(50)
6707return "Stored function database";
6708
6709create function `current_user`() returns varchar(50)
6710return "Stored function current_user";
6711
6712create function md5(x varchar(50)) returns varchar(50)
6713return "Stored function md5";
6714
6715SET SQL_MODE='IGNORE_SPACE';
6716
6717select database(), database ();
6718select current_user(), current_user ();
6719select md5("aaa"), md5 ("aaa");
6720
6721# Non deterministic warnings from db_load_routine
6722--disable_warnings
6723select test.database(), test.database ();
6724select test.current_user(), test.current_user ();
6725select test.md5("aaa"), test.md5 ("aaa");
6726--enable_warnings
6727
6728SET SQL_MODE='';
6729
6730select database(), database ();
6731select current_user(), current_user ();
6732select md5("aaa"), md5 ("aaa");
6733
6734# Non deterministic warnings from db_load_routine
6735--disable_warnings
6736select test.database(), test.database ();
6737select test.current_user(), test.current_user ();
6738select test.md5("aaa"), test.md5 ("aaa");
6739--enable_warnings
6740
6741SET @@sql_mode=@save_sql_mode;
6742
6743drop function test.database;
6744drop function test.current_user;
6745drop function md5;
6746
6747use test;
6748delimiter |;
6749# End of BUG#22619
6750
6751--echo End of 5.1 tests
6752
6753#
6754# BUG#23760: ROW_COUNT() and store procedure not owrking together
6755#
6756--disable_warnings
6757DROP TABLE IF EXISTS bug23760|
6758DROP TABLE IF EXISTS bug23760_log|
6759DROP PROCEDURE IF EXISTS bug23760_update_log|
6760DROP PROCEDURE IF EXISTS bug23760_test_row_count|
6761DROP FUNCTION IF EXISTS bug23760_rc_test|
6762--enable_warnings
6763CREATE TABLE bug23760 (
6764  id INT NOT NULL AUTO_INCREMENT ,
6765  num INT NOT NULL ,
6766  PRIMARY KEY ( id )
6767)|
6768
6769CREATE TABLE bug23760_log (
6770 id INT NOT NULL AUTO_INCREMENT ,
6771 reason VARCHAR(50)NULL ,
6772 ammount INT NOT NULL ,
6773  PRIMARY KEY ( id )
6774)|
6775
6776CREATE PROCEDURE bug23760_update_log(r Varchar(50), a INT)
6777BEGIN
6778  INSERT INTO bug23760_log (reason, ammount) VALUES(r, a);
6779END|
6780
6781CREATE PROCEDURE bug23760_test_row_count()
6782BEGIN
6783  UPDATE bug23760 SET num = num + 1;
6784  CALL bug23760_update_log('Test is working', ROW_COUNT());
6785  UPDATE bug23760 SET num = num - 1;
6786END|
6787
6788
6789CREATE PROCEDURE bug23760_test_row_count2(level INT)
6790BEGIN
6791  IF level THEN
6792    UPDATE bug23760 SET num = num + 1;
6793    CALL bug23760_update_log('Test2 is working', ROW_COUNT());
6794    CALL bug23760_test_row_count2(level - 1);
6795  END IF;
6796END|
6797
6798CREATE FUNCTION bug23760_rc_test(in_var INT) RETURNS INT RETURN in_var|
6799
6800INSERT INTO bug23760 (num) VALUES (0), (1), (1), (2), (3), (5), (8)|
6801SELECT ROW_COUNT()|
6802
6803CALL bug23760_test_row_count()|
6804SELECT * FROM bug23760_log ORDER BY id|
6805
6806SET @save_max_sp_recursion= @@max_sp_recursion_depth|
6807SELECT @save_max_sp_recursion|
6808SET max_sp_recursion_depth= 5|
6809SELECT @@max_sp_recursion_depth|
6810CALL bug23760_test_row_count2(2)|
6811SELECT ROW_COUNT()|
6812SELECT * FROM bug23760_log ORDER BY id|
6813SELECT * FROM bug23760 ORDER by ID|
6814SET max_sp_recursion_depth= @save_max_sp_recursion|
6815
6816SELECT bug23760_rc_test(123)|
6817INSERT INTO bug23760 (num) VALUES (13), (21), (34), (55)|
6818SELECT bug23760_rc_test(ROW_COUNT())|
6819
6820DROP TABLE bug23760, bug23760_log|
6821DROP PROCEDURE bug23760_update_log|
6822DROP PROCEDURE bug23760_test_row_count|
6823DROP PROCEDURE bug23760_test_row_count2|
6824DROP FUNCTION bug23760_rc_test|
6825
6826#
6827# BUG#24117: server crash on a FETCH with a cursor on a table which is not in
6828#            the table cache
6829#
6830
6831--disable_warnings
6832DROP PROCEDURE IF EXISTS bug24117|
6833DROP TABLE IF EXISTS t3|
6834--enable_warnings
6835CREATE TABLE t3(c1 ENUM('abc'))|
6836INSERT INTO t3 VALUES('abc')|
6837CREATE PROCEDURE bug24117()
6838BEGIN
6839  DECLARE t3c1 ENUM('abc');
6840  DECLARE mycursor CURSOR FOR SELECT c1 FROM t3;
6841  OPEN mycursor;
6842  FLUSH TABLES;
6843  FETCH mycursor INTO t3c1;
6844  CLOSE mycursor;
6845END|
6846CALL bug24117()|
6847DROP PROCEDURE bug24117|
6848DROP TABLE t3|
6849
6850#
6851# Bug#8407(Stored functions/triggers ignore exception handler)
6852#
6853
6854--disable_warnings
6855drop function if exists func_8407_a|
6856drop function if exists func_8407_b|
6857--enable_warnings
6858
6859create function func_8407_a() returns int
6860begin
6861  declare x int;
6862
6863  declare continue handler for sqlexception
6864  begin
6865  end;
6866
6867  select 1 from no_such_view limit 1 into x;
6868
6869  return x;
6870end|
6871
6872create function func_8407_b() returns int
6873begin
6874  declare x int default 0;
6875
6876  declare continue handler for sqlstate '42S02'
6877  begin
6878    set x:= x+1000;
6879  end;
6880
6881  case (select 1 from no_such_view limit 1)
6882    when 1 then set x:= x+1;
6883    when 2 then set x:= x+2;
6884    else set x:= x+100;
6885  end case;
6886  set x:=x + 500;
6887
6888  return x;
6889end|
6890
6891select func_8407_a()|
6892select func_8407_b()|
6893
6894drop function func_8407_a|
6895drop function func_8407_b|
6896
6897#
6898# Bug#26503 (Illegal SQL exception handler code causes the server to crash)
6899#
6900
6901--disable_warnings
6902drop table if exists table_26503|
6903drop procedure if exists proc_26503_ok_1|
6904drop procedure if exists proc_26503_ok_2|
6905drop procedure if exists proc_26503_ok_3|
6906drop procedure if exists proc_26503_ok_4|
6907--enable_warnings
6908
6909create table table_26503(a int unique)|
6910
6911create procedure proc_26503_ok_1(v int)
6912begin
6913  declare i int default 5;
6914
6915  declare continue handler for sqlexception
6916  begin
6917    select 'caught something';
6918    retry:
6919    while i > 0 do
6920      begin
6921        set i = i - 1;
6922        select 'looping', i;
6923        iterate retry;
6924        select 'dead code';
6925      end;
6926    end while retry;
6927    select 'leaving handler';
6928  end;
6929
6930  select 'do something';
6931  insert into table_26503 values (v);
6932  select 'do something again';
6933  insert into table_26503 values (v);
6934end|
6935
6936create procedure proc_26503_ok_2(v int)
6937begin
6938  declare i int default 5;
6939
6940  declare continue handler for sqlexception
6941  begin
6942    select 'caught something';
6943    retry:
6944    while i > 0 do
6945      begin
6946        set i = i - 1;
6947        select 'looping', i;
6948        leave retry;
6949        select 'dead code';
6950      end;
6951    end while;
6952    select 'leaving handler';
6953  end;
6954
6955  select 'do something';
6956  insert into table_26503 values (v);
6957  select 'do something again';
6958  insert into table_26503 values (v);
6959end|
6960
6961## The outer retry label should not prevent using the inner label.
6962
6963create procedure proc_26503_ok_3(v int)
6964begin
6965  declare i int default 5;
6966
6967retry:
6968  begin
6969    declare continue handler for sqlexception
6970    begin
6971      select 'caught something';
6972      retry:
6973      while i > 0 do
6974        begin
6975          set i = i - 1;
6976          select 'looping', i;
6977          iterate retry;
6978          select 'dead code';
6979        end;
6980      end while retry;
6981      select 'leaving handler';
6982    end;
6983
6984    select 'do something';
6985    insert into table_26503 values (v);
6986    select 'do something again';
6987    insert into table_26503 values (v);
6988  end;
6989end|
6990
6991## The outer retry label should not prevent using the inner label.
6992
6993create procedure proc_26503_ok_4(v int)
6994begin
6995  declare i int default 5;
6996
6997retry:
6998  begin
6999    declare continue handler for sqlexception
7000    begin
7001      select 'caught something';
7002      retry:
7003      while i > 0 do
7004        begin
7005          set i = i - 1;
7006          select 'looping', i;
7007          leave retry;
7008          select 'dead code';
7009        end;
7010      end while;
7011      select 'leaving handler';
7012    end;
7013
7014    select 'do something';
7015    insert into table_26503 values (v);
7016    select 'do something again';
7017    insert into table_26503 values (v);
7018  end;
7019end|
7020
7021call proc_26503_ok_1(1)|
7022call proc_26503_ok_2(2)|
7023call proc_26503_ok_3(3)|
7024call proc_26503_ok_4(4)|
7025
7026drop table table_26503|
7027drop procedure proc_26503_ok_1|
7028drop procedure proc_26503_ok_2|
7029drop procedure proc_26503_ok_3|
7030drop procedure proc_26503_ok_4|
7031
7032#
7033# Bug#25373: Stored functions wasn't compared correctly which leads to a wrong
7034#            result.
7035#
7036--disable_warnings
7037DROP FUNCTION IF EXISTS bug25373|
7038--disable_warnings
7039CREATE FUNCTION bug25373(p1 INTEGER) RETURNS INTEGER
7040LANGUAGE SQL DETERMINISTIC
7041RETURN p1;|
7042CREATE TABLE t3 (f1 INT, f2 FLOAT)|
7043INSERT INTO t3 VALUES (1, 3.4), (1, 2), (1, 0.9), (2, 8), (2, 7)|
7044SELECT SUM(f2), bug25373(f1) FROM t3 GROUP BY bug25373(f1) WITH ROLLUP|
7045DROP FUNCTION bug25373|
7046DROP TABLE t3|
7047
7048
7049#
7050# BUG#25082: Default database change on trigger execution breaks replication.
7051#
7052# As it turned out, this bug has actually two bugs. So, here we have two test
7053# cases -- one in sp.test, the other in sp-security.test.
7054#
7055
7056#
7057# Test case 1: error on dropping the current database.
7058#
7059
7060# Prepare.
7061
7062--disable_warnings
7063DROP DATABASE IF EXISTS mysqltest1|
7064DROP DATABASE IF EXISTS mysqltest2|
7065--enable_warnings
7066
7067CREATE DATABASE mysqltest1|
7068CREATE DATABASE mysqltest2|
7069
7070# Test.
7071
7072CREATE PROCEDURE mysqltest1.p1()
7073  DROP DATABASE mysqltest2|
7074
7075use mysqltest2|
7076
7077CALL mysqltest1.p1()|
7078
7079SELECT DATABASE()|
7080
7081# Cleanup.
7082
7083DROP DATABASE mysqltest1|
7084
7085use test|
7086
7087
7088#
7089# Bug#20777: Function w BIGINT UNSIGNED shows diff. behaviour --ps-protocol
7090--disable_warnings
7091drop function if exists bug20777|
7092drop table if exists examplebug20777|
7093--enable_warnings
7094create function bug20777(f1 bigint unsigned) returns bigint unsigned
7095begin
7096  set f1 = (f1 - 10); set f1 = (f1 + 10);
7097return f1;
7098end|
7099delimiter ;|
7100select bug20777(9223372036854775803) as '9223372036854775803   2**63-5';
7101select bug20777(9223372036854775804) as '9223372036854775804   2**63-4';
7102select bug20777(9223372036854775805) as '9223372036854775805   2**63-3';
7103select bug20777(9223372036854775806) as '9223372036854775806   2**63-2';
7104select bug20777(9223372036854775807) as '9223372036854775807   2**63-1';
7105select bug20777(9223372036854775808) as '9223372036854775808   2**63+0';
7106select bug20777(9223372036854775809) as '9223372036854775809   2**63+1';
7107select bug20777(9223372036854775810) as '9223372036854775810   2**63+2';
7108--error ER_DATA_OUT_OF_RANGE
7109select bug20777(-9223372036854775808) as 'lower bounds signed bigint';
7110select bug20777(9223372036854775807) as 'upper bounds signed bigint';
7111--error ER_DATA_OUT_OF_RANGE
7112select bug20777(0) as 'lower bounds unsigned bigint';
7113select bug20777(18446744073709551615) as 'upper bounds unsigned bigint';
7114select bug20777(18446744073709551616) as 'upper bounds unsigned bigint + 1';
7115--error ER_DATA_OUT_OF_RANGE
7116select bug20777(-1) as 'lower bounds unsigned bigint - 1';
7117
7118create table examplebug20777 as select
7119  0 as 'i',
7120  bug20777(9223372036854775806) as '2**63-2',
7121  bug20777(9223372036854775807) as '2**63-1',
7122  bug20777(9223372036854775808) as '2**63',
7123  bug20777(9223372036854775809) as '2**63+1',
7124  bug20777(18446744073709551614) as '2**64-2',
7125  bug20777(18446744073709551615) as '2**64-1',
7126  bug20777(18446744073709551616) as '2**64';
7127insert into examplebug20777 values (1, 9223372036854775806, 9223372036854775807, 223372036854775808, 9223372036854775809, 18446744073709551614, 18446744073709551615, 8446744073709551616);
7128show create table examplebug20777;
7129select * from examplebug20777 order by i;
7130
7131drop table examplebug20777;
7132select bug20777(18446744073709551613)+1;
7133drop function bug20777;
7134delimiter |;
7135
7136
7137#
7138# BUG#5274: Stored procedure crash if length of CHAR variable too great.
7139#
7140
7141# Prepare.
7142
7143--disable_warnings
7144DROP FUNCTION IF EXISTS bug5274_f1|
7145DROP FUNCTION IF EXISTS bug5274_f2|
7146--enable_warnings
7147
7148# Test.
7149
7150CREATE FUNCTION bug5274_f1(p1 CHAR) RETURNS CHAR
7151  RETURN CONCAT(p1, p1)|
7152
7153CREATE FUNCTION bug5274_f2() RETURNS CHAR
7154BEGIN
7155  DECLARE v1 INT DEFAULT 0;
7156  DECLARE v2 CHAR DEFAULT 'x';
7157
7158  WHILE v1 < 30 DO
7159    SET v1 = v1 + 1;
7160    SET v2 = bug5274_f1(v2);
7161  END WHILE;
7162
7163  RETURN v2;
7164END|
7165
7166SELECT bug5274_f2()|
7167
7168# Cleanup.
7169
7170DROP FUNCTION bug5274_f1|
7171DROP FUNCTION bug5274_f2|
7172
7173#
7174# Bug#21513 (SP having body starting with quoted label rendered unusable)
7175#
7176--disable_warnings
7177drop procedure if exists proc_21513|
7178--enable_warnings
7179
7180create procedure proc_21513()`my_label`:BEGIN END|
7181show create procedure proc_21513|
7182
7183drop procedure proc_21513|
7184
7185###
7186--echo End of 5.0 tests.
7187
7188#
7189# BUG#NNNN: New bug synopsis
7190#
7191#--disable_warnings
7192#drop procedure if exists bugNNNN|
7193#--enable_warnings
7194#create procedure bugNNNN...
7195#
7196# Add bugs above this line. Use existing tables t1 and t2 when
7197# practical, or create table t3,t4 etc temporarily (and drop them).
7198# NOTE: The delimiter is `|`, and not `;`. It is changed to `;`
7199#       at the end of the file!
7200#
7201
7202delimiter ;|
7203drop table t1,t2;
7204
7205# Disable warnings to allow test run without InnoDB
7206--disable_warnings
7207CREATE TABLE t1 (a int auto_increment primary key) engine=MyISAM;
7208CREATE TABLE t2 (a int auto_increment primary key, b int) engine=innodb;
7209--enable_warnings
7210set @a=0;
7211
7212delimiter |;
7213CREATE function bug27354() RETURNS int not deterministic
7214begin
7215insert into t1 values (null);
7216set @a=@a+1;
7217return @a;
7218end|
7219
7220delimiter ;|
7221update t2 set b=1 where a=bug27354();
7222select count(t_1.a),count(t_2.a) from t1 as t_1, t2 as t_2 /* must be 0,0 */;
7223insert into t2 values (1,1),(2,2),(3,3);
7224update t2 set b=-b where a=bug27354();
7225select * from t2 /* must return 1,-1 ... */;
7226select count(*) from t1 /* must be 3 */;
7227
7228
7229drop table t1,t2;
7230drop function   bug27354;
7231
7232#
7233# Bug #28605: SHOW CREATE VIEW with views using stored_procedures no longer
7234# showing SP names.
7235#
7236CREATE TABLE t1 (a INT);
7237INSERT INTO t1 VALUES (1),(2);
7238
7239CREATE FUNCTION metered(a INT) RETURNS INT RETURN 12;
7240
7241CREATE VIEW v1 AS SELECT test.metered(a) as metered FROM t1;
7242
7243SHOW CREATE VIEW v1;
7244
7245DROP VIEW v1;
7246DROP FUNCTION metered;
7247DROP TABLE t1;
7248
7249#
7250# Bug#29834: Accessing a view column by name in SP/PS causes a memory leak.
7251#
7252# This is leak test. Run with large number assigned to $execute_cnt,
7253# $p1_cnt, $p2_cnt, @p1_p2_cnt, $f1_normal_cnt or $f1_prep_cnt variables.
7254#
7255
7256let $execute_cnt= 2;
7257let $p1_cnt= 2;
7258let $p2_cnt= 2;
7259SET @p1_p2_cnt= 2;
7260let $f1_normal_cnt= 2;
7261let $f1_prep_cnt= 2;
7262
7263CREATE TABLE t1 (c1 INT);
7264CREATE VIEW v1 AS SELECT * FROM t1;
7265
7266PREPARE s1 FROM 'SELECT c1 FROM v1';
7267while ($execute_cnt)
7268{
7269  EXECUTE s1;
7270  dec $execute_cnt;
7271}
7272
7273DELIMITER |;
7274
7275CREATE PROCEDURE p1(IN loops BIGINT(19) UNSIGNED)
7276BEGIN
7277  WHILE loops > 0 DO
7278    SELECT c1 FROM v1;
7279    SET loops = loops - 1;
7280  END WHILE;
7281END|
7282
7283CREATE PROCEDURE p2(IN loops BIGINT(19) UNSIGNED)
7284BEGIN
7285  WHILE loops > 0 DO
7286    SELECT c1 FROM v1;
7287    CALL p1(@p1_p2_cnt);
7288    SET loops = loops - 1;
7289  END WHILE;
7290END|
7291
7292CREATE FUNCTION f1(loops INT UNSIGNED)
7293  RETURNS INT
7294BEGIN
7295  DECLARE tmp INT;
7296  WHILE loops > 0 DO
7297    SELECT c1 INTO tmp FROM v1;
7298    SET loops = loops - 1;
7299  END WHILE;
7300  RETURN loops;
7301END|
7302
7303DELIMITER ;|
7304
7305eval CALL p1($p1_cnt);
7306eval CALL p2($p2_cnt);
7307
7308eval SELECT f1($f1_normal_cnt);
7309
7310eval PREPARE s1 FROM 'SELECT f1($f1_prep_cnt)';
7311EXECUTE s1;
7312EXECUTE s1;
7313
7314DROP PROCEDURE p1;
7315DROP PROCEDURE p2;
7316DROP FUNCTION f1;
7317DROP VIEW v1;
7318DROP TABLE t1;
7319
7320#
7321# Bug#28551 "The warning 'No database selected' is reported when calling
7322# stored procedures"
7323#
7324--disable_warnings
7325drop database if exists mysqltest_db1;
7326--enable_warnings
7327create database mysqltest_db1;
7328create procedure mysqltest_db1.sp_bug28551() begin end;
7329call mysqltest_db1.sp_bug28551();
7330show warnings;
7331drop database mysqltest_db1;
7332#
7333# Bug#29050 Creation of a legal stored procedure fails if a database is not
7334# selected prior
7335#
7336--disable_warnings
7337drop database if exists mysqltest_db1;
7338drop table if exists test.t1;
7339--enable_warnings
7340create database mysqltest_db1;
7341use mysqltest_db1;
7342# For the sake of its side effect
7343drop database mysqltest_db1;
7344# Now we have no current database selected.
7345create table test.t1 (id int);
7346insert into test.t1 (id) values (1);
7347delimiter //;
7348create procedure test.sp_bug29050() begin select * from t1; end//
7349delimiter ;//
7350show warnings;
7351call test.sp_bug29050();
7352show warnings;
7353# Restore the old current database
7354use test;
7355drop procedure sp_bug29050;
7356drop table t1;
7357
7358#
7359# Bug #30120 SP with local variables with non-ASCII names crashes server.
7360#
7361
7362SET NAMES latin1;
7363
7364DELIMITER |;
7365
7366CREATE PROCEDURE p1()
7367BEGIN
7368  DECLARE ��� INT;
7369  SELECT ���;
7370END|
7371
7372DELIMITER ;|
7373
7374CALL p1();
7375
7376set @@character_set_client=@save_character_set_client;
7377set @@character_set_results=@save_character_set_client;
7378DROP PROCEDURE p1;
7379
7380#
7381# Bug#25411 (trigger code truncated)
7382#
7383
7384--disable_warnings
7385drop procedure if exists proc_25411_a;
7386drop procedure if exists proc_25411_b;
7387drop procedure if exists proc_25411_c;
7388--enable_warnings
7389
7390delimiter $$;
7391
7392create procedure proc_25411_a()
7393begin
7394  /* real comment */
7395  select 1;
7396  /*! select 2; */
7397  select 3;
7398  /*!00000 select 4; */
7399  /*!999999 select 5; */
7400end
7401$$
7402
7403create procedure proc_25411_b(
7404/* real comment */
7405/*! p1 int, */
7406/*!00000 p2 int */
7407/*!999999 ,p3 int */
7408)
7409begin
7410  select p1, p2;
7411end
7412$$
7413
7414create procedure proc_25411_c()
7415begin
7416  select 1/*!,2*//*!00000,3*//*!999999,4*/;
7417  select 1/*! ,2*//*!00000 ,3*//*!999999 ,4*/;
7418  select 1/*!,2 *//*!00000,3 *//*!999999,4 */;
7419  select 1/*! ,2 *//*!00000 ,3 *//*!999999 ,4 */;
7420  select 1 /*!,2*/ /*!00000,3*/ /*!999999,4*/ ;
7421end
7422$$
7423
7424delimiter ;$$
7425
7426show create procedure proc_25411_a;
7427call proc_25411_a();
7428
7429show create procedure proc_25411_b;
7430select name, param_list, body from mysql.proc where name like "%25411%";
7431call proc_25411_b(10, 20);
7432
7433show create procedure proc_25411_c;
7434call proc_25411_c();
7435
7436drop procedure proc_25411_a;
7437drop procedure proc_25411_b;
7438drop procedure proc_25411_c;
7439
7440
7441#
7442# Bug#26302 (MySQL server cuts off trailing "*/" from comments in SP/func)
7443#
7444
7445--disable_warnings
7446drop procedure if exists proc_26302;
7447--enable_warnings
7448
7449create procedure proc_26302()
7450select 1 /* testing */;
7451
7452show create procedure proc_26302;
7453
7454select ROUTINE_NAME, ROUTINE_DEFINITION from information_schema.ROUTINES
7455where ROUTINE_NAME = "proc_26302";
7456
7457drop procedure proc_26302;
7458
7459
7460# Bug #29338: no optimization for stored functions with a trivial body
7461# always returning constant.
7462#
7463
7464CREATE FUNCTION f1() RETURNS INT DETERMINISTIC RETURN 2;
7465CREATE FUNCTION f2(I INT) RETURNS INT DETERMINISTIC RETURN 3;
7466
7467CREATE TABLE t1 (c1 INT, INDEX(c1));
7468
7469INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
7470
7471CREATE VIEW v1 AS SELECT c1 FROM t1;
7472
7473EXPLAIN SELECT * FROM t1 WHERE c1=1;
7474EXPLAIN SELECT * FROM t1 WHERE c1=f1();
7475
7476EXPLAIN SELECT * FROM v1 WHERE c1=1;
7477EXPLAIN SELECT * FROM v1 WHERE c1=f1();
7478
7479EXPLAIN SELECT * FROM t1 WHERE c1=f2(10);
7480EXPLAIN SELECT * FROM t1 WHERE c1=f2(c1);
7481EXPLAIN SELECT * FROM t1 WHERE c1=f2(rand());
7482
7483
7484DROP VIEW v1;
7485DROP FUNCTION f1;
7486DROP FUNCTION f2;
7487DROP TABLE t1;
7488
7489#
7490# Bug#29408 Cannot find view in columns table if the selection contains a function
7491#
7492delimiter |;
7493
7494create function f1()
7495    returns int(11)
7496not deterministic
7497contains sql
7498sql security definer
7499comment ''
7500begin
7501  declare x int(11);
7502  set x=-1;
7503   return x;
7504end|
7505
7506delimiter ;|
7507
7508create view v1 as select 1 as one, f1() as days;
7509
7510connect (bug29408, localhost, root,,*NO-ONE*);
7511connection bug29408;
7512
7513show create view test.v1;
7514select column_name from information_schema.columns
7515where table_name='v1' and table_schema='test';
7516
7517connection default;
7518disconnect bug29408;
7519drop view v1;
7520drop function f1;
7521
7522#
7523# Bug#13675: DATETIME/DATE type in store proc param seems to be converted as
7524# varbinary
7525#
7526
7527--echo
7528--echo # Bug#13675.
7529--echo
7530
7531--disable_warnings
7532DROP PROCEDURE IF EXISTS p1;
7533DROP PROCEDURE IF EXISTS p2;
7534
7535DROP TABLE IF EXISTS t1;
7536--enable_warnings
7537
7538--echo
7539
7540CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v;
7541
7542CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v;
7543
7544--echo
7545CALL p1(NOW());
7546SHOW CREATE TABLE t1;
7547
7548--echo
7549DROP TABLE t1;
7550
7551--echo
7552CALL p1('text');
7553SHOW CREATE TABLE t1;
7554
7555--echo
7556DROP TABLE t1;
7557
7558--echo
7559CALL p2(10);
7560SHOW CREATE TABLE t1;
7561
7562--echo
7563DROP TABLE t1;
7564
7565--echo
7566CALL p2('text');
7567SHOW CREATE TABLE t1;
7568
7569--echo
7570DROP TABLE t1;
7571
7572--echo
7573DROP PROCEDURE p1;
7574DROP PROCEDURE p2;
7575
7576###########################################################################
7577
7578#
7579# Bug#31035: select from function, group by result crasher.
7580#
7581
7582###########################################################################
7583
7584--echo
7585
7586--echo #
7587--echo # Bug#31035.
7588--echo #
7589
7590--echo
7591
7592--echo #
7593--echo # - Prepare.
7594--echo #
7595
7596--echo
7597
7598--disable_warnings
7599DROP TABLE IF EXISTS t1;
7600DROP FUNCTION IF EXISTS f1;
7601DROP FUNCTION IF EXISTS f2;
7602DROP FUNCTION IF EXISTS f3;
7603DROP FUNCTION IF EXISTS f4;
7604--enable_warnings
7605
7606--echo
7607
7608--echo #
7609--echo # - Create required objects.
7610--echo #
7611
7612--echo
7613
7614CREATE TABLE t1(c1 INT);
7615
7616--echo
7617
7618INSERT INTO t1 VALUES (1), (2), (3);
7619
7620--echo
7621
7622CREATE FUNCTION f1()
7623  RETURNS INT
7624  NOT DETERMINISTIC
7625    RETURN 1;
7626
7627--echo
7628
7629CREATE FUNCTION f2(p INT)
7630  RETURNS INT
7631  NOT DETERMINISTIC
7632    RETURN 1;
7633
7634--echo
7635
7636CREATE FUNCTION f3()
7637  RETURNS INT
7638  DETERMINISTIC
7639    RETURN 1;
7640
7641--echo
7642
7643CREATE FUNCTION f4(p INT)
7644  RETURNS INT
7645  DETERMINISTIC
7646    RETURN 1;
7647
7648--echo
7649
7650--echo #
7651--echo # - Check.
7652--echo #
7653
7654--echo
7655
7656# Not deterministic function, no arguments.
7657
7658SELECT f1() AS a FROM t1 GROUP BY a;
7659
7660--echo
7661
7662# Not deterministic function, non-constant argument.
7663
7664SELECT f2(@a) AS a FROM t1 GROUP BY a;
7665
7666--echo
7667
7668# Deterministic function, no arguments.
7669
7670SELECT f3() AS a FROM t1 GROUP BY a;
7671
7672--echo
7673
7674# Deterministic function, constant argument.
7675
7676SELECT f4(0) AS a FROM t1 GROUP BY a;
7677
7678--echo
7679
7680# Deterministic function, non-constant argument.
7681
7682SELECT f4(@a) AS a FROM t1 GROUP BY a;
7683
7684--echo
7685
7686--echo #
7687--echo # - Cleanup.
7688--echo #
7689
7690--echo
7691
7692DROP TABLE t1;
7693DROP FUNCTION f1;
7694DROP FUNCTION f2;
7695DROP FUNCTION f3;
7696DROP FUNCTION f4;
7697
7698--echo
7699
7700###########################################################################
7701
7702#
7703# Bug#31191: JOIN in combination with stored function crashes the server.
7704#
7705
7706###########################################################################
7707
7708--echo #
7709--echo # Bug#31191.
7710--echo #
7711
7712--echo
7713
7714--echo #
7715--echo # - Prepare.
7716--echo #
7717
7718--echo
7719
7720--disable_warnings
7721DROP TABLE IF EXISTS t1;
7722DROP TABLE IF EXISTS t2;
7723DROP FUNCTION IF EXISTS f1;
7724--enable_warnings
7725
7726--echo
7727
7728--echo #
7729--echo # - Create required objects.
7730--echo #
7731
7732--echo
7733
7734CREATE TABLE t1 (
7735   id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7736   barcode INT(8) UNSIGNED ZEROFILL nOT NULL,
7737   PRIMARY KEY  (id),
7738   UNIQUE KEY barcode (barcode)
7739);
7740
7741--echo
7742
7743INSERT INTO t1 (id, barcode) VALUES (1, 12345678);
7744INSERT INTO t1 (id, barcode) VALUES (2, 12345679);
7745
7746--echo
7747
7748CREATE TABLE test.t2 (
7749   id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7750   barcode BIGINT(11) UNSIGNED ZEROFILL NOT NULL,
7751   PRIMARY KEY  (id)
7752);
7753
7754--echo
7755
7756INSERT INTO test.t2 (id, barcode) VALUES (1, 12345106708);
7757INSERT INTO test.t2 (id, barcode) VALUES (2, 12345106709);
7758
7759--echo
7760
7761CREATE FUNCTION f1(p INT(8))
7762  RETURNS BIGINT(11) UNSIGNED
7763  READS SQL DATA
7764    RETURN FLOOR(p/1000)*1000000 + 100000 + FLOOR((p MOD 1000)/10)*100 + (p MOD 10);
7765
7766--echo
7767
7768--echo #
7769--echo # - Check.
7770--echo #
7771
7772--echo
7773
7774SELECT DISTINCT t1.barcode, f1(t1.barcode)
7775FROM t1
7776INNER JOIN t2
7777ON f1(t1.barcode) = t2.barcode
7778WHERE t1.barcode=12345678;
7779
7780--echo
7781
7782--echo #
7783--echo # - Cleanup.
7784--echo #
7785
7786--echo
7787
7788DROP TABLE t1;
7789DROP TABLE t2;
7790DROP FUNCTION f1;
7791
7792--echo
7793
7794###########################################################################
7795
7796#
7797# Bug#31226: Group by function crashes mysql.
7798#
7799
7800###########################################################################
7801
7802--echo #
7803--echo # Bug#31226.
7804--echo #
7805
7806--echo
7807
7808--echo #
7809--echo # - Prepare.
7810--echo #
7811
7812--echo
7813
7814--disable_warnings
7815DROP TABLE IF EXISTS t1;
7816DROP FUNCTION IF EXISTS f1;
7817--enable_warnings
7818
7819--echo
7820
7821--echo #
7822--echo # - Create required objects.
7823--echo #
7824
7825--echo
7826
7827CREATE TABLE t1(id INT);
7828
7829--echo
7830
7831INSERT INTO t1 VALUES (1), (2), (3);
7832
7833--echo
7834
7835CREATE FUNCTION f1()
7836  RETURNS DATETIME
7837  NOT DETERMINISTIC NO SQL
7838    RETURN NOW();
7839
7840--echo
7841
7842--echo #
7843--echo # - Check.
7844--echo #
7845
7846--echo
7847
7848--replace_column 1 <timestamp>
7849SELECT f1() FROM t1 GROUP BY 1;
7850
7851--echo
7852
7853--echo #
7854--echo # - Cleanup.
7855--echo #
7856
7857--echo
7858
7859DROP TABLE t1;
7860DROP FUNCTION f1;
7861
7862--echo
7863
7864###########################################################################
7865
7866#
7867# Bug#28318 (CREATE FUNCTION (UDF) requires a schema)
7868#
7869
7870--disable_warnings
7871DROP PROCEDURE IF EXISTS db28318_a.t1;
7872DROP PROCEDURE IF EXISTS db28318_b.t2;
7873DROP DATABASE IF EXISTS db28318_a;
7874DROP DATABASE IF EXISTS db28318_b;
7875--enable_warnings
7876
7877CREATE DATABASE db28318_a;
7878CREATE DATABASE db28318_b;
7879
7880CREATE PROCEDURE db28318_a.t1() SELECT "db28318_a.t1";
7881CREATE PROCEDURE db28318_b.t2() CALL t1();
7882
7883use db28318_a;
7884
7885# In db28318_b.t2, t1 refers to db28318_b.t1
7886--error ER_SP_DOES_NOT_EXIST
7887CALL db28318_b.t2();
7888
7889DROP PROCEDURE db28318_a.t1;
7890DROP PROCEDURE db28318_b.t2;
7891DROP DATABASE db28318_a;
7892DROP DATABASE db28318_b;
7893use test;
7894
7895###########################################################################
7896
7897#
7898# Bug#29770 Two handlers are allowed to catch an error in an stored procedure.
7899#
7900
7901--disable_warnings
7902DROP TABLE IF EXISTS t1;
7903DROP PROCEDURE IF EXISTS bug29770;
7904--enable_warnings
7905
7906CREATE TABLE t1(a int);
7907delimiter |;
7908CREATE PROCEDURE bug29770()
7909BEGIN
7910  DECLARE CONTINUE HANDLER FOR SQLSTATE '42S22' SET @state:= 'run';
7911  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @exception:= 'run';
7912  SELECT x FROM t1;
7913END|
7914delimiter ;|
7915CALL bug29770();
7916SELECT @state, @exception;
7917DROP TABLE t1;
7918DROP PROCEDURE bug29770;
7919
7920#
7921# Bug#33618 Crash in sp_rcontext
7922#
7923
7924use test;
7925
7926--disable_warnings
7927drop table if exists t_33618;
7928drop procedure if exists proc_33618;
7929--enable_warnings
7930
7931create table t_33618 (`a` int, unique(`a`), `b` varchar(30)) engine=myisam;
7932insert into t_33618 (`a`,`b`) values (1,'1'),(2,'2');
7933
7934delimiter //;
7935
7936create procedure proc_33618(num int)
7937begin
7938  declare count1 int default '0';
7939  declare vb varchar(30);
7940  declare last_row int;
7941
7942  while(num>=1) do
7943    set num=num-1;
7944    begin
7945      declare cur1 cursor for select `a` from t_33618;
7946      declare continue handler for not found set last_row = 1;
7947      set last_row:=0;
7948      open cur1;
7949      rep1:
7950      repeat
7951        begin
7952          declare exit handler for 1062 begin end;
7953          fetch cur1 into vb;
7954          if (last_row = 1) then
7955            leave rep1;
7956          end if;
7957        end;
7958        until last_row=1
7959      end repeat;
7960      close cur1;
7961    end;
7962  end while;
7963end//
7964
7965delimiter ;//
7966
7967call proc_33618(20);
7968
7969drop table t_33618;
7970drop procedure proc_33618;
7971
7972--echo #
7973--echo # Bug#30787: Stored function ignores user defined alias.
7974--echo #
7975use test;
7976--disable_warnings
7977drop function if exists func30787;
7978--enable_warnings
7979create table t1(f1 int);
7980insert into t1 values(1),(2);
7981delimiter |;
7982create function func30787(p1 int) returns int
7983begin
7984  return p1;
7985end |
7986delimiter ;|
7987select (select func30787(f1)) as ttt from t1;
7988drop function func30787;
7989drop table t1;
7990
7991#
7992# Bug #33811: Call to stored procedure with SELECT * / RIGHT JOIN fails
7993# after the first time
7994#
7995CREATE TABLE t1 (id INT);
7996INSERT INTO t1 VALUES (1),(2),(3),(4);
7997
7998CREATE PROCEDURE test_sp()
7999  SELECT t1.* FROM t1 RIGHT JOIN t1 t2 ON t1.id=t2.id;
8000
8001CALL test_sp();
8002CALL test_sp();
8003
8004DROP PROCEDURE test_sp;
8005DROP TABLE t1;
8006
8007
8008###########################################################################
8009#
8010# Bug#38291 memory corruption and server crash with view/sp/function
8011#
8012
8013create table t1(c1 INT);
8014create function f1(p1 int) returns varchar(32)
8015  return 'aaa';
8016create view v1 as select f1(c1) as parent_control_name from t1;
8017
8018delimiter //;
8019create procedure p1()
8020begin
8021    select parent_control_name as c1 from v1;
8022end //
8023delimiter ;//
8024
8025call p1();
8026call p1();
8027
8028drop procedure p1;
8029drop function f1;
8030drop view v1;
8031drop table t1;
8032
8033#
8034# Bug#38469 invalid memory read and/or crash with utf8 text field, stored procedure, uservar
8035#
8036delimiter $;
8037--disable_warnings
8038drop procedure if exists `p2` $
8039--enable_warnings
8040create procedure `p2`(in `a` text charset utf8)
8041begin
8042        declare `pos` int default 1;
8043        declare `str` text charset utf8;
8044        set `str` := `a`;
8045        select substr(`str`, `pos`+ 1 ) into `str`;
8046end $
8047delimiter ;$
8048call `p2`('s s s s s s');
8049drop procedure `p2`;
8050
8051#
8052# Bug#38823: Invalid memory access when a SP statement does wildcard expansion
8053#
8054
8055--disable_warnings
8056drop table if exists t1;
8057drop procedure if exists p1;
8058--enable_warnings
8059
8060delimiter $;
8061create procedure p1() begin select * from t1; end$
8062--error ER_NO_SUCH_TABLE
8063call p1$
8064create table t1 (a integer)$
8065call p1$
8066alter table t1 add b integer$
8067call p1$
8068delimiter ;$
8069
8070drop table t1;
8071drop procedure p1;
8072
8073--echo # ------------------------------------------------------------------
8074--echo # -- End of 5.0 tests
8075--echo # ------------------------------------------------------------------
8076
8077###########################################################################
8078
8079#
8080# Bug#20550: Stored function: wrong RETURN type metadata when used in a VIEW.
8081#
8082
8083###########################################################################
8084
8085--echo
8086
8087--echo #
8088--echo # Bug#20550.
8089--echo #
8090
8091--echo
8092
8093--echo #
8094--echo # - Prepare.
8095--echo #
8096
8097--echo
8098
8099--disable_warnings
8100DROP VIEW IF EXISTS v1;
8101DROP VIEW IF EXISTS v2;
8102DROP FUNCTION IF EXISTS f1;
8103DROP FUNCTION IF EXISTS f2;
8104--enable_warnings
8105
8106--echo
8107
8108--echo #
8109--echo # - Create required objects.
8110--echo #
8111
8112--echo
8113
8114CREATE FUNCTION f1() RETURNS VARCHAR(65525) RETURN 'Hello';
8115
8116--echo
8117
8118CREATE FUNCTION f2() RETURNS TINYINT RETURN 1;
8119
8120--echo
8121
8122CREATE VIEW v1 AS SELECT f1();
8123
8124--echo
8125
8126CREATE VIEW v2 AS SELECT f2();
8127
8128--echo
8129
8130--echo #
8131--echo # - Check.
8132--echo #
8133
8134--echo
8135
8136SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v1';
8137
8138--echo
8139
8140SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v2';
8141
8142--echo
8143
8144--echo #
8145--echo # - Cleanup.
8146--echo #
8147
8148--echo
8149
8150DROP FUNCTION f1;
8151DROP FUNCTION f2;
8152DROP VIEW v1;
8153DROP VIEW v2;
8154
8155--echo
8156
8157###########################################################################
8158
8159#
8160# Bug#24923: Functions with ENUM issues.
8161#
8162
8163###########################################################################
8164
8165--echo #
8166--echo # - Bug#24923: prepare.
8167--echo #
8168
8169--echo
8170
8171--disable_warnings
8172DROP FUNCTION IF EXISTS f1;
8173--enable_warnings
8174
8175--echo
8176
8177--echo #
8178--echo # - Bug#24923: create required objects.
8179--echo #
8180
8181--echo
8182
8183delimiter |;
8184
8185CREATE FUNCTION f1(p INT)
8186  RETURNS ENUM ('Very_long_enum_element_identifier',
8187                'Another_very_long_enum_element_identifier')
8188  BEGIN
8189    CASE p
8190    WHEN 1 THEN
8191      RETURN 'Very_long_enum_element_identifier';
8192    ELSE
8193      RETURN 'Another_very_long_enum_element_identifier';
8194    END CASE;
8195  END|
8196
8197delimiter ;|
8198
8199--echo
8200
8201--echo #
8202--echo # - Bug#24923: check.
8203--echo #
8204
8205--echo
8206
8207SELECT f1(1);
8208
8209--echo
8210
8211SELECT f1(2);
8212
8213--echo
8214
8215SHOW CREATE FUNCTION f1;
8216
8217--echo #
8218--echo # - Bug#24923: cleanup.
8219--echo #
8220
8221--echo
8222
8223DROP FUNCTION f1;
8224
8225--echo
8226
8227###########################################################################
8228
8229#
8230# Bug#32633 Can not create any routine if SQL_MODE=no_engine_substitution
8231#
8232# Ensure that when new SQL modes are introduced, they are also added to
8233# the mysql.proc table.
8234#
8235
8236--disable_warnings
8237drop procedure if exists p;
8238--enable_warnings
8239set @old_mode= @@sql_mode;
8240set @@sql_mode= cast(pow(2,32)-1 as unsigned integer);
8241select @@sql_mode into @full_mode;
8242create procedure p() as begin end;
8243call p();
8244set @@sql_mode= @old_mode;
8245# Rename SQL modes that differ in name between the server and the table definition.
8246select replace(@full_mode, 'ALLOW_INVALID_DATES', 'INVALID_DATES') into @full_mode;
8247select name from mysql.proc where name = 'p' and sql_mode = @full_mode;
8248drop procedure p;
8249
8250#
8251# Bug#43962 "Packets out of order" calling a SHOW TABLE STATUS
8252#
8253
8254DELIMITER //;
8255CREATE DEFINER = 'root'@'localhost' PROCEDURE p1()
8256NOT DETERMINISTIC
8257CONTAINS SQL
8258SQL SECURITY DEFINER
8259COMMENT ''
8260BEGIN
8261 SHOW TABLE STATUS like 't1';
8262END;//
8263DELIMITER ;//
8264
8265
8266CREATE TABLE t1 (f1 INT);
8267--disable_result_log
8268let $tab_count= 4;
8269while ($tab_count)
8270{
8271 EVAL CALL p1();
8272 dec $tab_count ;
8273}
8274--enable_result_log
8275DROP PROCEDURE p1;
8276DROP TABLE t1;
8277
8278#
8279# Bug#47649 crash during CALL procedure
8280#
8281CREATE TABLE t1 ( f1 integer, primary key (f1));
8282CREATE TABLE t2 LIKE t1;
8283CREATE TEMPORARY TABLE t3 LIKE t1;
8284delimiter |;
8285CREATE PROCEDURE p1 () BEGIN SELECT f1 FROM t3 AS A WHERE A.f1 IN ( SELECT f1 FROM t3 ) ;
8286END|
8287delimiter ;|
8288CALL p1;
8289CREATE VIEW t3 AS SELECT f1 FROM t2 A WHERE A.f1 IN ( SELECT f1 FROM t2 );
8290DROP TABLE t3;
8291CALL p1;
8292CALL p1;
8293DROP PROCEDURE p1;
8294DROP TABLE t1, t2;
8295DROP VIEW t3;
8296
8297--echo #
8298--echo # Bug #46629: Item_in_subselect::val_int(): Assertion `0'
8299--echo # on subquery inside a SP
8300--echo #
8301CREATE TABLE t1(a INT);
8302CREATE TABLE t2(a INT, b INT PRIMARY KEY);
8303
8304DELIMITER |;
8305CREATE PROCEDURE p1 ()
8306BEGIN
8307  SELECT a FROM t1 A WHERE A.b IN (SELECT b FROM t2 AS B);
8308END|
8309DELIMITER ;|
8310--error ER_BAD_FIELD_ERROR
8311CALL p1;
8312--error ER_BAD_FIELD_ERROR
8313CALL p1;
8314DROP PROCEDURE p1;
8315DROP TABLE t1, t2;
8316
8317--echo #
8318--echo # Bug#47627: SET @@{global.session}.local_variable in stored routine causes crash
8319--echo # Bug#48626: Crash or lost connection using SET for declared variables with @@
8320--echo #
8321
8322--disable_warnings
8323DROP PROCEDURE IF EXISTS p1;
8324DROP PROCEDURE IF EXISTS p2;
8325DROP PROCEDURE IF EXISTS p3;
8326--enable_warnings
8327
8328delimiter //;
8329
8330--error ER_UNKNOWN_SYSTEM_VARIABLE
8331CREATE PROCEDURE p1()
8332BEGIN
8333  DECLARE v INT DEFAULT 0;
8334  SET @@SESSION.v= 10;
8335END//
8336
8337CREATE PROCEDURE p2()
8338BEGIN
8339  DECLARE v INT DEFAULT 0;
8340  SET v= 10;
8341END//
8342call p2()//
8343
8344--error ER_UNKNOWN_SYSTEM_VARIABLE
8345CREATE PROCEDURE p3()
8346BEGIN
8347  DECLARE v INT DEFAULT 0;
8348  SELECT @@SESSION.v;
8349END//
8350
8351--error ER_UNKNOWN_SYSTEM_VARIABLE
8352CREATE PROCEDURE p4()
8353BEGIN
8354  DECLARE v INT DEFAULT 0;
8355  SET @@GLOBAL.v= 10;
8356END//
8357
8358CREATE PROCEDURE p5()
8359BEGIN
8360  DECLARE init_connect INT DEFAULT 0;
8361  SET init_connect= 10;
8362  SET @@GLOBAL.init_connect= 'SELECT 1';
8363  SET @@SESSION.IDENTITY= 1;
8364  SELECT @@SESSION.IDENTITY;
8365  SELECT @@GLOBAL.init_connect;
8366  SELECT init_connect;
8367END//
8368
8369--error ER_UNKNOWN_SYSTEM_VARIABLE
8370CREATE PROCEDURE p6()
8371BEGIN
8372  DECLARE v INT DEFAULT 0;
8373  SET @@v= 0;
8374END//
8375
8376delimiter ;//
8377
8378SET @old_init_connect= @@GLOBAL.init_connect;
8379CALL p5();
8380SET @@GLOBAL.init_connect= @old_init_connect;
8381
8382DROP PROCEDURE p2;
8383DROP PROCEDURE p5;
8384
8385
8386--echo #
8387--echo # Bug#11840395 (formerly known as bug#60347):
8388--echo # The string "versiondata" seems
8389--echo # to be 'leaking' into the schema name space
8390--echo #
8391--disable_warnings
8392DROP DATABASE IF EXISTS mixedCaseDbName;
8393--enable_warnings
8394CREATE DATABASE mixedCaseDbName;
8395DELIMITER |;
8396CREATE PROCEDURE mixedCaseDbName.tryMyProc() begin end|
8397CREATE FUNCTION mixedCaseDbName.tryMyFunc() returns text begin return 'IT WORKS'; end
8398|
8399DELIMITER ;|
8400call mixedCaseDbName.tryMyProc();
8401select mixedCaseDbName.tryMyFunc();
8402DROP DATABASE mixedCaseDbName;
8403
8404
8405--echo #
8406--echo # Bug#11766594  59736: SELECT DISTINCT.. INCORRECT RESULT WITH DETERMINISTIC FUNCTION IN WHERE C
8407--echo #
8408
8409CREATE TABLE t1 (a INT, b INT, KEY(b));
8410CREATE TABLE t2 (c INT, d INT, KEY(c));
8411INSERT INTO t1 VALUES (1,1),(1,1),(1,2);
8412INSERT INTO t2 VALUES (1,1),(1,2);
8413
8414DELIMITER $;
8415
8416CREATE FUNCTION f1() RETURNS INT DETERMINISTIC
8417BEGIN
8418  DECLARE a int;
8419  -- SQL statement inside
8420  SELECT 1 INTO a;
8421  RETURN a;
8422END $
8423
8424DELIMITER ;$
8425
8426SELECT COUNT(DISTINCT d) FROM t1, t2  WHERE a = c AND b = f1();
8427
8428DROP FUNCTION f1;
8429DROP TABLE t1, t2;
8430
8431
8432--echo # ------------------------------------------------------------------
8433--echo # -- End of 5.1 tests
8434--echo # ------------------------------------------------------------------
8435
8436#
8437# Bug#39255: Stored procedures: crash if function references nonexistent table
8438#
8439
8440--disable_warnings
8441DROP FUNCTION IF EXISTS f1;
8442DROP TABLE IF EXISTS t_non_existing;
8443DROP TABLE IF EXISTS t1;
8444--enable_warnings
8445
8446delimiter |;
8447CREATE FUNCTION f1() RETURNS INT
8448BEGIN
8449   DECLARE v INT;
8450   SELECT a INTO v FROM t_non_existing;
8451   RETURN 1;
8452END|
8453delimiter ;|
8454
8455CREATE TABLE t1 (a INT) ENGINE = myisam;
8456INSERT INTO t1 VALUES (1);
8457
8458--error ER_NO_SUCH_TABLE
8459SELECT * FROM t1 WHERE a = f1();
8460
8461DROP FUNCTION f1;
8462DROP TABLE t1;
8463
8464#
8465# Bug#36649: Condition area is not properly cleaned up after stored routine invocation
8466#
8467
8468--disable_warnings
8469DROP PROCEDURE IF EXISTS p1;
8470--enable_warnings
8471
8472delimiter |;
8473CREATE PROCEDURE p1(a INT, b CHAR)
8474BEGIN
8475  IF a > 0 THEN
8476    CALL p1(a-1, 'ab');
8477  ELSE
8478    SELECT 1;
8479  END IF;
8480END|
8481delimiter ;|
8482
8483SET @save_max_sp_recursion= @@max_sp_recursion_depth;
8484SET @@max_sp_recursion_depth= 5;
8485CALL p1(4, 'a');
8486SET @@max_sp_recursion_depth= @save_max_sp_recursion;
8487
8488DROP PROCEDURE p1;
8489
8490#
8491# Ensure that rules for message list clean up are being respected.
8492#
8493
8494--disable_warnings
8495DROP PROCEDURE IF EXISTS p1;
8496--enable_warnings
8497
8498delimiter |;
8499CREATE PROCEDURE p1(a CHAR)
8500BEGIN
8501  SELECT 1;
8502  SELECT CAST('10 ' as UNSIGNED INTEGER);
8503  SELECT 1;
8504END|
8505delimiter ;|
8506
8507CALL p1('data truncated parameter');
8508
8509DROP PROCEDURE p1;
8510
8511#
8512# Cascading stored procedure/function calls.
8513#
8514
8515--disable_warnings
8516DROP PROCEDURE IF EXISTS p1;
8517DROP PROCEDURE IF EXISTS p2;
8518DROP PROCEDURE IF EXISTS p3;
8519DROP PROCEDURE IF EXISTS p4;
8520--enable_warnings
8521
8522delimiter |;
8523CREATE PROCEDURE p1()
8524  CALL p2()|
8525CREATE PROCEDURE p2()
8526  CALL p3()|
8527CREATE PROCEDURE p3()
8528  CALL p4()|
8529CREATE PROCEDURE p4()
8530BEGIN
8531  SELECT 1;
8532  SELECT CAST('10 ' as UNSIGNED INTEGER);
8533  SELECT 2;
8534END|
8535delimiter ;|
8536
8537CALL p1();
8538
8539DROP PROCEDURE p1;
8540DROP PROCEDURE p2;
8541DROP PROCEDURE p3;
8542DROP PROCEDURE p4;
8543
8544--disable_warnings
8545DROP FUNCTION IF EXISTS f1;
8546DROP FUNCTION IF EXISTS f2;
8547DROP FUNCTION IF EXISTS f3;
8548DROP FUNCTION IF EXISTS f4;
8549DROP TABLE IF EXISTS t1;
8550--enable_warnings
8551
8552CREATE TABLE t1 (a CHAR(2));
8553
8554INSERT INTO t1 VALUES ('aa');
8555
8556delimiter |;
8557CREATE FUNCTION f1() RETURNS CHAR
8558  RETURN (SELECT f2())|
8559CREATE FUNCTION f2() RETURNS CHAR
8560  RETURN (SELECT f3())|
8561CREATE FUNCTION f3() RETURNS CHAR
8562  RETURN (SELECT f4())|
8563CREATE FUNCTION f4() RETURNS CHAR
8564BEGIN
8565  RETURN (SELECT a FROM t1);
8566END|
8567delimiter ;|
8568
8569SELECT f1();
8570
8571DROP FUNCTION f1;
8572DROP FUNCTION f2;
8573DROP FUNCTION f3;
8574DROP FUNCTION f4;
8575DROP TABLE t1;
8576
8577--echo #
8578--echo # Bug#34197: CREATE PROCEDURE fails when COMMENT truncated in non
8579--echo #            strict SQL mode
8580--echo #
8581
8582--disable_warnings
8583DROP PROCEDURE IF EXISTS p1;
8584--enable_warnings
8585
8586CREATE PROCEDURE p1 ()
8587COMMENT
8588'12345678901234567890123456789012345678901234567890123456789012345678901234567890'
8589BEGIN
8590END;
8591
8592SELECT comment FROM mysql.proc WHERE name = "p1";
8593
8594SELECT routine_comment FROM information_schema.routines WHERE routine_name = "p1";
8595
8596DROP PROCEDURE p1;
8597
8598
8599--echo #
8600--echo # Bug #47313 assert in check_key_in_view during CALL procedure
8601--echo #
8602
8603--disable_warnings
8604DROP TABLE IF EXISTS t1;
8605DROP VIEW IF EXISTS t1, t2_unrelated;
8606DROP PROCEDURE IF EXISTS p1;
8607--enable_warnings
8608
8609CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8610CREATE VIEW t1 AS SELECT 10 AS f1;
8611
8612--echo # t1 refers to the view
8613--error ER_NON_INSERTABLE_TABLE
8614CALL p1(1);
8615
8616CREATE TEMPORARY TABLE t1 (f1 INT);
8617
8618--echo # t1 still refers to the view since it was inlined
8619--error ER_NON_INSERTABLE_TABLE
8620CALL p1(2);
8621
8622DROP VIEW t1;
8623
8624--echo # t1 now refers to the temporary table
8625CALL p1(3);
8626
8627--echo # Check which values were inserted into the temp table.
8628SELECT * FROM t1;
8629
8630DROP TEMPORARY TABLE t1;
8631DROP PROCEDURE p1;
8632
8633--echo # Now test what happens if the sp cache is invalidated.
8634
8635CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8636CREATE VIEW t1 AS SELECT 10 AS f1;
8637CREATE VIEW v2_unrelated AS SELECT 1 AS r1;
8638
8639--echo # Load the procedure into the sp cache
8640--error ER_NON_INSERTABLE_TABLE
8641CALL p1(4);
8642
8643CREATE TEMPORARY TABLE t1 (f1 int);
8644
8645ALTER VIEW v2_unrelated AS SELECT 2 AS r1;
8646
8647--echo # Alter view causes the sp cache to be invalidated.
8648--echo # Now t1 refers to the temporary table, not the view.
8649CALL p1(5);
8650
8651--echo # Check which values were inserted into the temp table.
8652SELECT * FROM t1;
8653
8654DROP TEMPORARY TABLE t1;
8655DROP VIEW t1, v2_unrelated;
8656DROP PROCEDURE p1;
8657
8658CREATE PROCEDURE p1(IN x INT) INSERT INTO t1 VALUES (x);
8659CREATE TEMPORARY TABLE t1 (f1 INT);
8660
8661--echo # t1 refers to the temporary table
8662CALL p1(6);
8663
8664CREATE VIEW t1 AS SELECT 10 AS f1;
8665
8666--echo # Create view causes the sp cache to be invalidated.
8667--echo # t1 still refers to the temporary table since it shadows the view.
8668CALL p1(7);
8669
8670DROP VIEW t1;
8671
8672--echo # Check which values were inserted into the temp table.
8673SELECT * FROM t1;
8674
8675DROP TEMPORARY TABLE t1;
8676DROP PROCEDURE p1;
8677
8678--echo #
8679--echo # Bug #11918 Can't use a declared variable in LIMIT clause
8680--echo #
8681--disable_warnings
8682drop table if exists t1;
8683drop procedure if exists p1;
8684--enable_warnings
8685create table t1 (c1 int);
8686insert into t1 (c1) values (1), (2), (3), (4), (5);
8687
8688delimiter |;
8689create procedure p1()
8690begin
8691  declare a integer;
8692  declare b integer;
8693  select * from t1 limit a, b;
8694end|
8695delimiter ;|
8696--echo # How do we handle NULL limit values?
8697call p1();
8698drop table t1;
8699create table t1 (a int);
8700insert into t1 (a) values (1), (2), (3), (4), (5);
8701--echo #
8702--echo # Do we correctly resolve identifiers in LIMIT?
8703--echo # Since DROP and CREATE did not invalidate
8704--echo # the SP cache, we can't test until
8705--echo # we drop and re-create the procedure.
8706--echo #
8707--error ER_BAD_FIELD_ERROR
8708call p1();
8709--echo #
8710--echo # Drop and recreate the procedure, then repeat
8711--echo #
8712drop procedure p1;
8713delimiter |;
8714create procedure p1()
8715begin
8716  declare a integer;
8717  declare b integer;
8718  select * from t1 limit a, b;
8719end|
8720delimiter ;|
8721--echo # Stored procedure variables are resolved correctly in the LIMIT
8722call p1();
8723drop table t1;
8724create table t1 (c1 int);
8725insert into t1 (c1) values (1), (2), (3), (4), (5);
8726drop procedure p1;
8727--echo # Try to create a procedure that
8728--echo # refers to non-existing variables.
8729--error ER_SP_UNDECLARED_VAR
8730create procedure p1(p1 integer, p2 integer)
8731  select * from t1 limit a, b;
8732--echo #
8733--echo # Try to use data types not allowed in LIMIT
8734--echo #
8735--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8736create procedure p1(p1 date, p2 date) select * from t1 limit p1, p2;
8737--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8738create procedure p1(p1 integer, p2 float) select * from t1 limit p1, p2;
8739--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8740create procedure p1(p1 integer, p2 char(1)) select * from t1 limit p1, p2;
8741--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8742create procedure p1(p1 varchar(5), p2 char(1)) select * from t1 limit p1, p2;
8743--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8744create procedure p1(p1 decimal, p2 decimal) select * from t1 limit p1, p2;
8745--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8746create procedure p1(p1 double, p2 double) select * from t1 limit p1, p2;
8747
8748--echo #
8749--echo # Finally, test the valid case.
8750--echo #
8751create procedure p1(p1 integer, p2 integer)
8752  select * from t1 limit p1, p2;
8753
8754call p1(NULL, NULL);
8755call p1(0, 0);
8756call p1(0, -1);
8757call p1(-1, 0);
8758call p1(-1, -1);
8759call p1(0, 1);
8760call p1(1, 0);
8761call p1(1, 5);
8762call p1(3, 2);
8763
8764delimiter |;
8765--echo # Try to create a function that
8766--echo # refers to non-existing variables.
8767--error ER_SP_UNDECLARED_VAR
8768create function f1(p1 integer, p2 integer)
8769  returns int
8770begin
8771  declare a int;
8772  set a = (select count(*) from t1 limit a, b);
8773  return a;
8774end|
8775
8776create function f1()
8777  returns int
8778begin
8779  declare a, b, c int;
8780  set a = (select count(*) from t1 limit b, c);
8781  return a;
8782end|
8783
8784delimiter ;|
8785--echo # How do we handle NULL limit values?
8786select f1();
8787
8788drop function f1;
8789
8790delimiter |;
8791--echo #
8792--echo # Try to use data types not allowed in LIMIT
8793--echo #
8794--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8795create function f1(p1 date, p2 date)
8796  returns int
8797begin
8798  declare a int;
8799  set a = (select count(*) from t1 limit p1, p2);
8800  return a;
8801end|
8802
8803--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8804create function f1(p1 integer, p2 float)
8805  returns int
8806begin
8807  declare a int;
8808  set a = (select count(*) from t1 limit p1, p2);
8809  return a;
8810end|
8811
8812--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8813create function f1(p1 integer, p2 char(1))
8814  returns int
8815begin
8816  declare a int;
8817  set a = (select count(*) from t1 limit p1, p2);
8818  return a;
8819end|
8820
8821--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8822create function f1(p1 varchar(5), p2 char(1))
8823  returns int
8824begin
8825  declare a int;
8826  set a = (select count(*) from t1 limit p1, p2);
8827  return a;
8828end|
8829
8830--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8831create function f1(p1 decimal, p2 decimal)
8832  returns int
8833begin
8834  declare a int;
8835  set a = (select count(*) from t1 limit p1, p2);
8836  return a;
8837end|
8838
8839--error ER_WRONG_SPVAR_TYPE_IN_LIMIT
8840create function f1(p1 double, p2 double)
8841  returns int
8842begin
8843  declare a int;
8844  set a = (select count(*) from t1 limit p1, p2);
8845  return a;
8846end|
8847
8848--echo #
8849--echo # Finally, test the valid case.
8850--echo #
8851
8852create function f1(p1 integer, p2 integer)
8853returns int
8854begin
8855  declare count int;
8856  set count= (select count(*) from (select * from t1 limit p1, p2) t_1);
8857  return count;
8858end|
8859
8860delimiter ;|
8861
8862select f1(0, 0);
8863select f1(0, -1);
8864select f1(-1, 0);
8865select f1(-1, -1);
8866select f1(0, 1);
8867select f1(1, 0);
8868select f1(1, 5);
8869select f1(3, 2);
8870
8871--echo # Cleanup
8872drop table t1;
8873drop procedure p1;
8874drop function f1;
8875
8876--echo #
8877--echo # BUG#11766234: 59299: ASSERT (TABLE_REF->TABLE || TABLE_REF->VIEW)
8878--echo #               FAILS IN SET_FIELD_ITERATOR
8879--echo #
8880
8881CREATE TABLE t1 (a INT);
8882CREATE TABLE t2 (a INT);
8883CREATE VIEW v1 AS SELECT a FROM t2;
8884CREATE PROCEDURE proc() SELECT * FROM t1 NATURAL JOIN v1;
8885ALTER TABLE t2 CHANGE COLUMN a b CHAR;
8886
8887--echo
8888--error ER_VIEW_INVALID
8889CALL proc();
8890--error ER_VIEW_INVALID
8891CALL proc();
8892
8893--echo
8894DROP TABLE t1,t2;
8895DROP VIEW v1;
8896DROP PROCEDURE proc;
8897
8898
8899--echo
8900--echo # --
8901--echo # -- Bug 11765684 - 58674: SP-cache does not detect changes in
8902--echo # -- pre-locking list caused by triggers
8903--echo # ---
8904
8905--disable_warnings
8906DROP TABLE IF EXISTS t1;
8907DROP TABLE IF EXISTS t2;
8908DROP TABLE IF EXISTS t3;
8909DROP PROCEDURE IF EXISTS p1;
8910--enable_warnings
8911
8912CREATE TABLE t1(a INT);
8913CREATE TABLE t2(a INT);
8914CREATE TABLE t3(a INT);
8915
8916CREATE PROCEDURE p1()
8917  INSERT INTO t1(a) VALUES (1);
8918
8919--echo
8920CREATE TRIGGER t1_ai AFTER INSERT ON t1
8921  FOR EACH ROW
8922    INSERT INTO t2(a) VALUES (new.a);
8923
8924--echo
8925CALL p1();
8926
8927--echo
8928CREATE TRIGGER t1_bi BEFORE INSERT ON t1
8929  FOR EACH ROW
8930    INSERT INTO t3(a) VALUES (new.a);
8931
8932--echo
8933CALL p1();
8934
8935--echo
8936DROP TABLE t1, t2, t3;
8937DROP PROCEDURE p1;
8938--echo
8939
8940
8941--echo
8942--echo # --
8943--echo # -- Bug#12652769 - 61470: case operator in stored routine retains old
8944--echo # -- value of input parameter
8945--echo # ---
8946
8947--disable_warnings
8948DROP TABLE IF EXISTS t1;
8949DROP PROCEDURE IF EXISTS p1;
8950--enable_warnings
8951
8952CREATE TABLE t1 (s1 CHAR(5) CHARACTER SET utf8);
8953INSERT INTO t1 VALUES ('a');
8954
8955delimiter |;
8956
8957CREATE PROCEDURE p1(dt DATETIME, i INT)
8958BEGIN
8959  SELECT
8960    CASE
8961      WHEN i = 1 THEN 2
8962      ELSE dt
8963    END AS x1;
8964
8965  SELECT
8966    CASE _latin1'a'
8967      WHEN _utf8'a' THEN 'A'
8968    END AS x2;
8969
8970  SELECT
8971    CASE _utf8'a'
8972      WHEN _latin1'a' THEN _utf8'A'
8973    END AS x3;
8974
8975  SELECT
8976    CASE s1
8977      WHEN _latin1'a' THEN _latin1'b'
8978      ELSE _latin1'c'
8979    END AS x4
8980  FROM t1;
8981END|
8982
8983delimiter ;|
8984
8985--echo
8986CALL p1('2011-04-03 05:14:10', 1);
8987CALL p1('2011-04-03 05:14:11', 2);
8988CALL p1('2011-04-03 05:14:12', 2);
8989CALL p1('2011-04-03 05:14:13', 2);
8990
8991--echo
8992DROP TABLE t1;
8993DROP PROCEDURE p1;
8994--echo
8995
8996--echo #
8997--echo # Bug#12621017 - Crash if a sp variable is used in the
8998--echo #                limit clause of a set statement
8999--echo #
9000
9001--disable_warnings
9002DROP TABLE IF EXISTS t1;
9003DROP PROCEDURE IF EXISTS p1;
9004DROP PROCEDURE IF EXISTS p2;
9005--enable_warnings
9006
9007CREATE TABLE t1 (c1 INT);
9008INSERT INTO t1 VALUES (1);
9009
9010delimiter |;
9011
9012CREATE PROCEDURE p1()
9013BEGIN
9014  DECLARE foo, cnt INT UNSIGNED DEFAULT 1;
9015  SET foo = (SELECT MIN(c1) FROM t1 LIMIT cnt);
9016END|
9017
9018CREATE PROCEDURE p2()
9019BEGIN
9020
9021DECLARE iLimit INT;
9022DECLARE iVal INT;
9023
9024DECLARE cur1 CURSOR FOR
9025  SELECT c1 FROM t1
9026  LIMIT iLimit;
9027
9028SET iLimit=1;
9029
9030OPEN cur1;
9031FETCH cur1 INTO iVal;
9032
9033END|
9034
9035delimiter ;|
9036
9037CALL p1();
9038CALL p2();
9039
9040DROP PROCEDURE p1;
9041DROP PROCEDURE p2;
9042DROP TABLE t1;
9043
9044--echo
9045--echo # Bug#13805127: Stored program cache produces wrong result in same THD
9046--echo
9047
9048delimiter |;
9049
9050CREATE PROCEDURE p1(x INT UNSIGNED)
9051BEGIN
9052  SELECT c1, t2.c2, count(c3)
9053  FROM
9054    (
9055    SELECT 3 as c2 FROM dual WHERE x = 1
9056    UNION
9057    SELECT 2       FROM dual WHERE x = 1 OR x = 2
9058    ) AS t1,
9059    (
9060    SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
9061    UNION
9062    SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
9063    UNION
9064    SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
9065    ) AS t2
9066  WHERE t2.c2 = t1.c2
9067  GROUP BY c1, c2
9068  ;
9069END|
9070
9071delimiter ;|
9072
9073--echo
9074CALL p1(1);
9075CALL p1(2);
9076CALL p1(1);
9077
9078DROP PROCEDURE p1;
9079
9080--echo # End of 5.5 test
9081
9082#MDEV-17610
9083FLUSH USER_STATISTICS;
9084CREATE PROCEDURE sp() ALTER TABLE non_existing_table OPTIMIZE PARTITION p0;
9085CALL sp;
9086SELECT 1;
9087DROP PROCEDURE sp;
9088CREATE PROCEDURE sp() SET STATEMENT SQL_SELECT_LIMIT=0 FOR SHOW USER_STATISTICS;
9089CALL sp;
9090SELECT 1;
9091DROP PROCEDURE sp;
9092
9093--echo #
9094--echo # Bug#12663165 SP DEAD CODE REMOVAL DOESN'T UNDERSTAND CONTINUE HANDLERS
9095--echo #
9096
9097--disable_warnings
9098DROP FUNCTION IF EXISTS f1;
9099--enable_warnings
9100
9101delimiter $;
9102CREATE FUNCTION f1() RETURNS INT
9103BEGIN
9104  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
9105  BEGIN
9106    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
9107    BEGIN
9108     DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
9109     RETURN f1();
9110    END;
9111  END;
9112RETURN 1;
9113END $
9114delimiter ;$
9115
9116# This used to cause an assertion.
9117SELECT f1();
9118
9119DROP FUNCTION f1;
9120
9121--echo # ------------------------------------------------------------------
9122--echo # -- End of 5.1 tests
9123--echo # ------------------------------------------------------------------
9124
9125--echo #
9126--echo # lp:993459 Execution of PS for a query with GROUP BY
9127--echo # returns wrong result (see also mysql bug#13805127)
9128--echo #
9129
9130--echo
9131--echo # Bug#13805127: Stored program cache produces wrong result in same THD
9132--echo
9133
9134delimiter |;
9135
9136CREATE PROCEDURE p1(x INT UNSIGNED)
9137BEGIN
9138  SELECT c1, t2.c2, count(c3)
9139  FROM
9140    (
9141    SELECT 3 as c2 FROM dual WHERE x = 1
9142    UNION
9143    SELECT 2       FROM dual WHERE x = 1 OR x = 2
9144    ) AS t1,
9145    (
9146    SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
9147    UNION
9148    SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
9149    UNION
9150    SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
9151    ) AS t2
9152  WHERE t2.c2 = t1.c2
9153  GROUP BY c1, c2
9154  ;
9155END|
9156
9157delimiter ;|
9158
9159--echo
9160CALL p1(1);
9161CALL p1(2);
9162CALL p1(1);
9163
9164DROP PROCEDURE p1;
9165
9166--echo
9167--echo MDEV-3900 Optimizer difference between MySQL and MariaDB with stored functions in WHERE clause of UPDATE or DELETE statements
9168--echo
9169
9170CREATE FUNCTION tdn() RETURNS int(7) DETERMINISTIC RETURN to_days(now());
9171
9172CREATE TABLE t1 (pk INT NOT NULL AUTO_INCREMENT PRIMARY KEY, daynum INT, a CHAR(1), INDEX(daynum), INDEX(a)) ENGINE=MyISAM;
9173INSERT INTO t1 (daynum) VALUES (1),(2),(3),(4),(5),(TO_DAYS(NOW())),(7),(8);
9174INSERT INTO t1 (daynum) SELECT a1.daynum FROM t1 a1, t1 a2, t1 a3, t1 a4, t1 a5;
9175
9176FLUSH TABLES;
9177FLUSH STATUS;
9178
9179SHOW STATUS LIKE '%Handler_read%';
9180UPDATE t1 SET a = '+' WHERE daynum=tdn();
9181SHOW STATUS LIKE '%Handler_read%';
9182
9183drop function tdn;
9184drop table t1;
9185
9186--echo #
9187--echo # lp:1002157 : testing stored function
9188--echo # bug#62125        result for null incorrectly yields 1292 warning.
9189--echo #
9190
9191--disable_warnings
9192DROP FUNCTION IF EXISTS f1;
9193DROP FUNCTION IF EXISTS f2;
9194DROP FUNCTION IF EXISTS f3;
9195DROP FUNCTION IF EXISTS f4;
9196--enable_warnings
9197
9198delimiter /;
9199
9200CREATE FUNCTION f1() RETURNS VARCHAR(1)
9201BEGIN RETURN 'X'; END;/
9202
9203CREATE FUNCTION f2() RETURNS CHAR(1)
9204BEGIN RETURN 'X'; END;/
9205
9206CREATE FUNCTION f3() RETURNS VARCHAR(1)
9207BEGIN RETURN NULL; END;/
9208
9209CREATE FUNCTION f4() RETURNS CHAR(1)
9210BEGIN RETURN NULL; END;/
9211
9212delimiter ;/
9213
9214SELECT f1() IS NULL;
9215SELECT f2() IS NULL;
9216SELECT f3() IS NULL;
9217SELECT f4() IS NULL;
9218
9219DROP FUNCTION f1;
9220DROP FUNCTION f2;
9221DROP FUNCTION f3;
9222DROP FUNCTION f4;
9223
9224--echo
9225--echo Stored procedures and a condition handler in a nested procedure call
9226--echo doesn't suppress the condition from being passed on to the calling
9227--echo procedure
9228--echo
9229
9230--disable_warnings
9231drop procedure if exists p1;
9232drop procedure if exists p0;
9233--enable_warnings
9234
9235create table t1 (id int);
9236delimiter $$;
9237create procedure p1 () begin
9238declare i int default 0;
9239declare continue handler for not found begin
9240select "You should see this message and the warning that generated this" as "message";
9241show warnings;
9242end;
9243select id into i from t1;
9244end$$
9245create procedure p0 () begin
9246declare continue handler for not found begin
9247select "You should NOT see this message" as "message";
9248end;
9249call p1();
9250end$$
9251delimiter ;$$
9252call p0();
9253
9254drop procedure p1;
9255drop procedure p0;
9256drop table t1;
9257
9258--echo
9259--echo Test if stored procedures propagates errors
9260--echo
9261
9262create table t1 (id int primary key);
9263delimiter $$;
9264create procedure p1 () begin
9265insert into t1 values(1);
9266insert into t1 values(2);
9267insert into t1 values(2);
9268insert into t1 values(3);
9269end$$
9270create procedure p2 () begin
9271    declare x int;
9272    select id into x from t1 where id=5;
9273end$$
9274delimiter ;$$
9275--error ER_DUP_ENTRY
9276call p1();
9277show warnings;
9278select * from t1;
9279call p2();
9280
9281drop procedure p1;
9282drop procedure p2;
9283
9284drop table t1;
9285
9286--echo #
9287--echo # MDEV-4978 - Server cursor is broken with blobs in the select list,
9288--echo #             ORDER BY does not work
9289--echo #
9290CREATE TABLE t1(a INT, b BLOB);
9291INSERT INTO t1 VALUES(1,REPEAT('a',4835)),(2,'b'),(3,'c'),(4,'d'),(5,REPEAT('e',805)),(6,'f');
9292
9293DELIMITER |;
9294CREATE PROCEDURE p1()
9295BEGIN
9296  DECLARE done INT DEFAULT 0;
9297  DECLARE v1 INT;
9298  DECLARE v2 BLOB;
9299  DECLARE c1 CURSOR FOR SELECT * FROM t1 ORDER BY a;
9300  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
9301  OPEN c1;
9302  REPEAT
9303    FETCH c1 INTO v1, v2;
9304    IF NOT done THEN
9305      SELECT v1;
9306    END IF;
9307  UNTIL done END REPEAT;
9308  CLOSE c1;
9309END|
9310DELIMITER ;|
9311
9312CALL p1;
9313
9314DROP PROCEDURE p1;
9315DROP TABLE t1;
9316
9317--echo #
9318--echo # MDEV-10713: signal 11 error on multi-table update - crash in
9319--echo # handler::increment_statistics or in make_select or assertion
9320--echo # failure pfs_thread == ((PFS_thread*) pthread_getspecific((THR_PFS)))
9321--echo #
9322
9323CREATE TABLE `t1` (
9324  `CLOSE_YN` varchar(10) COLLATE utf8_bin DEFAULT NULL
9325) DEFAULT CHARSET=utf8 COLLATE=utf8_bin ;
9326
9327
9328CREATE TABLE `t2` (
9329  `ap_close_to` varchar(8) COLLATE utf8_bin DEFAULT NULL
9330) DEFAULT CHARSET=utf8 COLLATE=utf8_bin ;
9331insert t1 values (1);
9332
9333
9334--delimiter $$
9335
9336CREATE  FUNCTION `f1`(`P_DC_CD` VARBINARY(50), `P_SYS_DATE` DATETIME) RETURNS datetime
9337    DETERMINISTIC
9338    SQL SECURITY INVOKER
9339BEGIN
9340  DECLARE V_SYS_DATE DATETIME;
9341  SELECT now() AS LOC_DATE INTO V_SYS_DATE ;
9342  RETURN v_sys_date ;
9343END $$
9344
9345--delimiter ;
9346
9347update t1 S
9348JOIN
9349(
9350  SELECT CASE
9351           WHEN DATE_FORMAT( f1('F01', NOW()) , '%Y%m%d') <= CLOSE_YMD
9352             THEN '99991231'
9353	     ELSE '' END ACCOUNT_APPLY_YYYYMMDD
9354  FROM (
9355        select case
9356                 when 'AP'='AP'
9357                   then ap_close_to
9358                   end AS CLOSE_YMD
9359	from t2
9360       ) A
9361) X
9362SET S.CLOSE_YN = ''
9363where 1=1;
9364
9365drop function if exists f1;
9366drop table t1,t2;
9367
9368--echo #
9369--echo # MDEV-16957: Server crashes in Field_iterator_natural_join::next
9370--echo # upon 2nd execution of SP
9371--echo #
9372
9373CREATE TABLE t1 (a INT, b VARCHAR(32));
9374CREATE PROCEDURE sp() SELECT * FROM t1 AS t1x JOIN t1 AS t1y USING (c);
9375--error ER_BAD_FIELD_ERROR
9376CALL sp;
9377--error ER_BAD_FIELD_ERROR
9378CALL sp;
9379--error ER_BAD_FIELD_ERROR
9380CALL sp;
9381alter table t1 add column c int;
9382CALL sp;
9383
9384# Cleanup
9385DROP PROCEDURE sp;
9386DROP TABLE t1;
9387
9388--echo #
9389--echo # MDEV-17055: Server crashes in find_order_in_list upon
9390--echo # 2nd (3rd) execution of SP with UPDATE
9391--echo #
9392
9393CREATE TABLE t1 (a INT);
9394CREATE VIEW v1 AS SELECT * FROM t1;
9395CREATE TABLE t2 (c INT);
9396
9397CREATE PROCEDURE sp() UPDATE v1 SET a = 1 ORDER BY a, b LIMIT 1;
9398LOCK TABLE t2 READ;
9399--error ER_TABLE_NOT_LOCKED
9400CALL sp;
9401UNLOCK TABLES;
9402--error ER_BAD_FIELD_ERROR
9403CALL sp;
9404--error ER_BAD_FIELD_ERROR
9405CALL sp;
9406--error ER_BAD_FIELD_ERROR
9407CALL sp;
9408
9409# Cleanup
9410DROP PROCEDURE sp;
9411
9412CREATE PROCEDURE sp() UPDATE v1 SET a = 1 WHERE a=1 and b=2;
9413LOCK TABLE t2 READ;
9414--error ER_TABLE_NOT_LOCKED
9415CALL sp;
9416UNLOCK TABLES;
9417--error ER_BAD_FIELD_ERROR
9418CALL sp;
9419--error ER_BAD_FIELD_ERROR
9420CALL sp;
9421--error ER_BAD_FIELD_ERROR
9422CALL sp;
9423
9424# Cleanup
9425DROP PROCEDURE sp;
9426
9427DROP VIEW v1;
9428DROP TABLE t1, t2;
9429
9430--echo # End of 5.5 test
9431
9432--echo #
9433--echo #  MDEV-7040: Crash in field_conv, memcpy_field_possible, part#2
9434--echo #
9435create table t1 (
9436  col1 bigint(20),
9437  col2 char(1),
9438  col3 char(2)
9439);
9440insert into t1 values (1,'a','a'), (2,'b','b');
9441
9442create table t2 as select * from t1;
9443create table t3 as select * from t1;
9444create table t4 as select * from t1;
9445create table t5 as select * from t1;
9446create table t6 as select * from t1;
9447
9448flush tables;
9449
9450DELIMITER |;
9451
9452CREATE PROCEDURE p1()
9453begin
9454   DECLARE _var1 bigint(20) UNSIGNED;
9455   DECLARE _var2 CHAR(1) DEFAULT NULL;
9456   DECLARE _var3 CHAR(1) DEFAULT NULL;
9457
9458   DECLARE _done BOOLEAN DEFAULT 0;
9459
9460   declare cur1 cursor for
9461   select col1, col2, col3
9462   from t1
9463   where
9464     col1 in (select t2.col1 from t2 where t2.col2=t1.col2) or
9465     col2 in (select t3.col3 from t3 where t3.col3=t1.col2) ;
9466
9467   DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1;
9468
9469   OPEN cur1;
9470
9471   set _var1 = (select _var1 from t4 limit 1);
9472   set _var1 = (select _var1 from t5 limit 1);
9473   set _var1 = (select _var1 from t6 limit 1);
9474label1:
9475   LOOP
9476     SET _done = 0;
9477     FETCH cur1 INTO _var1, _var2, _var3;
9478     IF _done THEN
9479         LEAVE label1;
9480     END IF;
9481   END LOOP label1;
9482   CLOSE cur1;
9483end|
9484DELIMITER ;|
9485
9486set @tmp_toc= @@table_open_cache;
9487set @tmp_tdc= @@table_definition_cache;
9488
9489set global table_open_cache=10;
9490set global table_definition_cache=1;
9491call p1();
9492
9493set global table_open_cache= @tmp_toc;
9494set global table_definition_cache= @tmp_tdc;
9495drop procedure p1;
9496
9497drop table t1,t2,t3,t4,t5,t6;
9498
9499--echo #
9500--echo # MDEV-11935: Queries in stored procedures with and
9501--echo # EXISTS(SELECT * FROM VIEW) crashes and closes hte conneciton.
9502--echo #
9503
9504CREATE TABLE ANY_TABLE (
9505  ENTITY_UID    BIGINT NOT NULL
9506);
9507CREATE TABLE SECURITY_PATH(
9508origid BIGINT UNSIGNED NOT NULL,
9509destid BIGINT UNSIGNED NOT NULL,
9510KEY (destid)
9511);
9512CREATE VIEW ENTITY_ACCESS (
9513ENTITY_UID,
9514OWNER_UID
9515) AS
9516SELECT SP1.origid,
9517       SP2.destid
9518FROM SECURITY_PATH SP1
9519JOIN SECURITY_PATH SP2 ON SP1.destid = SP2.origid
9520;
9521--delimiter //
9522CREATE PROCEDURE SP_EXAMPLE_SELECT ()
9523BEGIN
9524   SELECT *
9525   FROM ANY_TABLE AT1
9526   WHERE EXISTS ( SELECT *
9527                  FROM ENTITY_ACCESS EA
9528                  WHERE AT1.ENTITY_UID = EA.ENTITY_UID
9529                  AND   EA.OWNER_UID IS NULL );
9530END
9531//
9532--delimiter ;
9533CALL SP_EXAMPLE_SELECT ();
9534CALL SP_EXAMPLE_SELECT ();
9535
9536drop procedure SP_EXAMPLE_SELECT;
9537drop view ENTITY_ACCESS;
9538drop table ANY_TABLE, SECURITY_PATH;
9539--echo # End of 10.0 test
9540
9541DELIMITER |;
9542CREATE FUNCTION f(f1 VARCHAR(64) COLLATE latin1_german2_ci)
9543  RETURNS VARCHAR(64)
9544BEGIN
9545  RETURN 'str';
9546END|
9547DROP FUNCTION f|
9548
9549CREATE FUNCTION f(f1 VARCHAR(64))
9550  RETURNS VARCHAR(64) COLLATE latin1_german2_ci
9551BEGIN
9552  RETURN 'str';
9553END|
9554DROP FUNCTION f|
9555
9556CREATE FUNCTION f(f1 VARCHAR(64))
9557  RETURNS VARCHAR(64)
9558BEGIN
9559  DECLARE f2 VARCHAR(64) COLLATE latin1_german2_ci;
9560  RETURN 'str';
9561END|
9562DROP FUNCTION f|
9563DELIMITER ;|
9564
9565--echo #
9566--echo # MDEV-7023: Error 2027: Malformed packet and assertion
9567--echo # `field_types == 0 || field_types[field_pos] == MYSQL_TYPE_INT24 ||
9568--echo #field_types[field_pos] == MYSQL_TYPE_LONG' failure in
9569--echo #Protocol_text::store_long
9570--echo #
9571create table t1 (i int);
9572create table t2 (i int);
9573--delimiter |
9574--error ER_SP_NO_RETSET
9575create function f() returns int
9576begin
9577  analyze insert into t1 values (1);
9578  return 1;
9579end |
9580--error ER_SP_NO_RETSET
9581create function f() returns int
9582begin
9583  analyze insert t1 select * from t2;
9584  return 1;
9585end |
9586--error ER_SP_NO_RETSET
9587create function f() returns int
9588begin
9589  analyze delete from t1;
9590  return 1;
9591end |
9592--error ER_SP_NO_RETSET
9593create function f() returns int
9594begin
9595  analyze delete t1 from t1,t2;
9596  return 1;
9597end |
9598--error ER_SP_NO_RETSET
9599create function f() returns int
9600begin
9601  analyze update t1 set i=1;
9602  return 1;
9603end |
9604--error ER_SP_NO_RETSET
9605create function f() returns int
9606begin
9607  analyze update t1,t2 set i=1;
9608  return 1;
9609end |
9610--error ER_SP_NO_RETSET
9611create function f() returns int
9612begin
9613  analyze replace t1 set i=1;
9614  return 1;
9615end |
9616--error ER_SP_NO_RETSET
9617create function f() returns int
9618begin
9619  analyze replace t1 select * from t2;
9620  return 1;
9621end |
9622--delimiter ;
9623
9624drop table t1,t2;
9625
9626--echo #
9627--echo # MDEV-11584: GRANT inside an SP does not work well on 2nd execution
9628--echo #
9629
9630CREATE PROCEDURE sp1()
9631  GRANT ALL PRIVILEGES ON *.* TO 'foo'@'%' IDENTIFIED BY 'pass';
9632CALL sp1();
9633CALL sp1();
9634drop user 'foo'@'%';
9635drop procedure sp1;
9636
9637--echo #
9638--echo # MDEV-10972: Insert from select / view / union --
9639--echo # repeatable crash in 10.1, 10.2 Linux/Mac/Windows
9640--echo #
9641
9642create table t (id int auto_increment primary key);
9643insert into t values (9494),(9495),(9496),(9497),(9498),(9499),(9500),(9501),(9502),(9503);
9644
9645create VIEW v AS
9646select id from t
9647union
9648select id from t
9649;
9650
9651drop procedure if exists p;
9652create procedure p()
9653insert into tmp_t select t.id from (
9654    select id from v
9655    union
9656    select id from v
9657) sq
9658inner join t on (sq.id = t.id);
9659
9660--error ER_NO_SUCH_TABLE
9661CALL p();
9662create table tmp_t (id int null);
9663CALL p();
9664
9665drop procedure p;
9666drop view v;
9667drop table t, tmp_t;
9668
9669
9670--echo #
9671--echo # MDEV-13936: Server crashes in Time_and_counter_tracker::incr_loops
9672--echo #
9673CREATE TABLE t1 (i INT);
9674CREATE VIEW v1 AS SELECT * FROM t1 WHERE RAND() > 0.5;
9675CREATE FUNCTION f1() RETURNS INT RETURN ( SELECT MAX(i) FROM v1 );
9676
9677--error ER_NON_INSERTABLE_TABLE
9678REPLACE INTO v1 VALUES (f1());
9679SET @aux = f1();
9680
9681# Cleanup
9682DROP FUNCTION f1;
9683DROP VIEW v1;
9684DROP TABLE t1;
9685
9686--echo #
9687--echo # MDEV-14857: problem with 10.2.11 server crashing when
9688--echo # executing stored procedure
9689--echo #
9690
9691SET max_sp_recursion_depth=10;
9692
9693CREATE TABLE t1 (a INT);
9694CREATE TABLE t2 (b INT);
9695
9696delimiter ||;
9697
9698CREATE PROCEDURE proc_0()
9699BEGIN
9700  CALL empty_1();
9701  CALL proc_1();
9702END ||
9703
9704CREATE PROCEDURE proc_1()
9705BEGIN
9706  CALL proc_2();
9707  CALL proc_3();
9708  CALL proc_4();
9709  CALL proc_5();
9710END ||
9711
9712CREATE PROCEDURE proc_2()
9713  CALL proc_6();
9714||
9715
9716CREATE PROCEDURE proc_3()
9717BEGIN
9718  CALL empty_2();
9719  CALL empty_3();
9720END ||
9721
9722CREATE PROCEDURE proc_4()
9723  CALL proc_7();
9724||
9725
9726CREATE PROCEDURE proc_5()
9727  CALL proc_select();
9728||
9729
9730CREATE PROCEDURE proc_6()
9731BEGIN
9732  CALL empty_4();
9733  CALL empty_5();
9734  CALL empty_6();
9735  CALL empty_7();
9736  CALL proc_8();
9737END ||
9738
9739CREATE PROCEDURE proc_7()
9740  CALL proc_9('foo');
9741||
9742
9743CREATE PROCEDURE proc_8()
9744  CALL proc_10();
9745||
9746
9747CREATE PROCEDURE proc_9(IN opt VARCHAR(40))
9748  IF LEFT(opt,1) <> '_' THEN
9749    CALL proc_11();
9750  END IF;
9751||
9752
9753CREATE PROCEDURE proc_10()
9754  CALL proc_12();
9755||
9756
9757CREATE PROCEDURE proc_11()
9758BEGIN
9759  CALL empty_8();
9760  CALL empty_9();
9761  CALL empty_10();
9762  CALL proc_13();
9763END ||
9764
9765CREATE PROCEDURE proc_12()
9766BEGIN
9767  CALL empty_11();
9768  CALL empty_12();
9769  CALL empty_13();
9770END ||
9771
9772CREATE PROCEDURE proc_13()
9773BEGIN
9774  CALL proc_9('_bar');
9775  CALL empty_14();
9776END ||
9777
9778delimiter ;||
9779
9780CREATE PROCEDURE empty_1() BEGIN END ;
9781CREATE PROCEDURE empty_2() BEGIN END ;
9782CREATE PROCEDURE empty_3() BEGIN END ;
9783CREATE PROCEDURE empty_4() BEGIN END ;
9784CREATE PROCEDURE empty_5() BEGIN END ;
9785CREATE PROCEDURE empty_6() BEGIN END ;
9786CREATE PROCEDURE empty_7() BEGIN END ;
9787CREATE PROCEDURE empty_8() BEGIN END ;
9788CREATE PROCEDURE empty_9() BEGIN END ;
9789CREATE PROCEDURE empty_10() BEGIN END ;
9790CREATE PROCEDURE empty_11() BEGIN END ;
9791CREATE PROCEDURE empty_12() BEGIN END ;
9792CREATE PROCEDURE empty_13() BEGIN END ;
9793CREATE PROCEDURE empty_14() BEGIN END ;
9794
9795CREATE PROCEDURE proc_select()
9796  SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2)
9797;
9798
9799CALL proc_0();
9800
9801# Cleanup
9802DROP PROCEDURE empty_1;
9803DROP PROCEDURE empty_2;
9804DROP PROCEDURE empty_3;
9805DROP PROCEDURE empty_4;
9806DROP PROCEDURE empty_5;
9807DROP PROCEDURE empty_6;
9808DROP PROCEDURE empty_7;
9809DROP PROCEDURE empty_8;
9810DROP PROCEDURE empty_9;
9811DROP PROCEDURE empty_10;
9812DROP PROCEDURE empty_11;
9813DROP PROCEDURE empty_12;
9814DROP PROCEDURE empty_13;
9815DROP PROCEDURE empty_14;
9816DROP PROCEDURE proc_0;
9817DROP PROCEDURE proc_1;
9818DROP PROCEDURE proc_2;
9819DROP PROCEDURE proc_3;
9820DROP PROCEDURE proc_4;
9821DROP PROCEDURE proc_5;
9822DROP PROCEDURE proc_6;
9823DROP PROCEDURE proc_7;
9824DROP PROCEDURE proc_8;
9825DROP PROCEDURE proc_9;
9826DROP PROCEDURE proc_10;
9827DROP PROCEDURE proc_11;
9828DROP PROCEDURE proc_12;
9829DROP PROCEDURE proc_13;
9830DROP PROCEDURE proc_select;
9831DROP TABLE t1, t2;
9832
9833SET max_sp_recursion_depth=default;
9834
9835--echo #
9836--echo # MDEV-15347: Valgrind or ASAN errors in mysql_make_view on query
9837--echo # from information_schema
9838--echo #
9839
9840CREATE VIEW v AS SELECT 1;
9841CREATE FUNCTION f() RETURNS INT RETURN 1;
9842--disable_result_log
9843SELECT * FROM INFORMATION_SCHEMA.TABLES JOIN INFORMATION_SCHEMA.PARAMETERS
9844UNION
9845SELECT * FROM INFORMATION_SCHEMA.TABLES JOIN INFORMATION_SCHEMA.PARAMETERS;
9846--enable_result_log
9847DROP FUNCTION f;
9848DROP VIEW v;
9849
9850--echo #
9851--echo # MDEV-17963: Assertion `field_pos < field_count' failed in Protocol_text::store,
9852--echo # Assertion `field_handlers == 0 || field_pos < field_count'
9853--echo #
9854
9855CREATE TABLE t1 (ct time);
9856INSERT INTO t1 VALUES ('16:11:28');
9857
9858DELIMITER |;
9859--error ER_SP_NO_RETSET
9860CREATE FUNCTION f1 () RETURNS varchar(100)
9861BEGIN
9862DECLARE xxx varchar(100);
9863ANALYZE SELECT sum(ct) FROM t1 INTO xxx ;
9864RETURN xxx;
9865END|
9866
9867DELIMITER ;|
9868drop table t1;
9869
9870--echo #End of 10.1 tests
9871
9872--echo #
9873--echo # MDEV-11081: CURSOR for query with GROUP BY
9874--echo #
9875
9876CREATE TABLE t1 (name VARCHAR(10), value INT);
9877INSERT INTO t1 VALUES ('b',1);
9878INSERT INTO t1 VALUES ('b',1);
9879INSERT INTO t1 VALUES ('c',1);
9880INSERT INTO t1 VALUES ('a',1);
9881INSERT INTO t1 VALUES ('a',1);
9882INSERT INTO t1 VALUES ('a',1);
9883DELIMITER |;
9884CREATE PROCEDURE p1 ()
9885BEGIN
9886  DECLARE done INT DEFAULT FALSE;
9887  DECLARE v_name VARCHAR(10);
9888  DECLARE v_total INT;
9889  DECLARE c CURSOR FOR
9890    SELECT name, SUM(value) AS total FROM t1 GROUP BY name;
9891  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
9892  OPEN c;
9893read_loop:
9894  LOOP
9895    FETCH c INTO v_name, v_total;
9896    IF done THEN
9897      LEAVE read_loop;
9898    END IF;
9899    SELECT v_name, v_total;
9900  END LOOP;
9901  CLOSE c;
9902END;
9903|
9904DELIMITER ;|
9905CALL p1();
9906DROP PROCEDURE p1;
9907DROP TABLE t1;
9908
9909--echo #
9910--echo # MDEV-13346: CURSOR a query with GROUP BY using derived table
9911--echo #
9912
9913DELIMITER |;
9914CREATE PROCEDURE p1()
9915BEGIN
9916  DECLARE c CURSOR FOR
9917    SELECT
9918      IFNULL(NULL,1) AS col
9919    FROM
9920      ( select 1 as id ) AS t
9921    GROUP BY t.id
9922  ;
9923  OPEN c;
9924END
9925|
9926DELIMITER ;|
9927CALL p1();
9928DROP PROCEDURE p1;
9929
9930--echo #
9931--echo # MDEV-15057 Crash when using an unknown identifier as an SP parameter
9932--echo #
9933
9934CREATE OR REPLACE PROCEDURE p1 (a VARCHAR(10)) SELECT 1;
9935--error ER_BAD_FIELD_ERROR
9936CALL p1(a);
9937drop procedure p1;
9938
9939DELIMITER |;
9940
9941CREATE OR REPLACE PROCEDURE p1 (a VARCHAR(10)) SELECT a|
9942CREATE OR REPLACE PROCEDURE p2 ()
9943BEGIN
9944  DECLARE name VARCHAR(10);
9945  SET name="hello";
9946  call p1(name);
9947END|
9948CREATE OR REPLACE PROCEDURE p3 ()
9949BEGIN
9950  DECLARE name VARCHAR(10);
9951  SET name="hello";
9952  call p1(name2);
9953END|
9954
9955DELIMITER ;|
9956
9957call p2();
9958--error ER_BAD_FIELD_ERROR
9959call p3();
9960drop procedure p1;
9961drop procedure p2;
9962drop procedure p3;
9963
9964--echo #
9965--echo # MDEV-15328: MariaDB 10.2.13 Crashes upon CALL PROCEDURE PARAM
9966--echo # LAST_INSERT_ID ()
9967--echo # (part 1, part 2 is in query_cache.test)
9968--echo #
9969
9970CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END;
9971CALL foo( LAST_INSERT_ID() );
9972DROP PROCEDURE foo;
9973
9974--echo #
9975--echo # MDEV-15870 Using aggregate and window function in unexpected places can crash the server
9976--echo #
9977
9978CREATE PROCEDURE p1 (a TEXT) BEGIN END;
9979--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
9980CALL p1(RANK() OVER (ORDER BY 1));
9981--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
9982CALL p1(ROW_NUMBER() OVER ());
9983--error ER_INVALID_GROUP_FUNC_USE
9984CALL p1(SUM(1));
9985DROP PROCEDURE p1;
9986
9987
9988--echo #
9989--echo # MDEV-16311 Server crash when using a NAME_CONST() with a CURSOR
9990--echo #
9991
9992SET sql_mode=STRICT_ALL_TABLES;
9993CREATE TABLE t1 (a INT);
9994INSERT INTO t1 VALUES (10);
9995DELIMITER $$;
9996--error ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
9997BEGIN NOT ATOMIC
9998  DECLARE a INT;
9999  DECLARE c CURSOR FOR SELECT NAME_CONST('x','y') FROM t1;
10000  OPEN c;
10001  FETCH c INTO a;
10002  CLOSE c;
10003END;
10004$$
10005DELIMITER ;$$
10006DROP TABLE t1;
10007SET sql_mode=DEFAULT;
10008
10009--echo #
10010--echo # MDEV-24220: error when opening a table for the second call of SP
10011--echo #
10012
10013CREATE TABLE t1 (a INT, b INT);
10014INSERT INTO t1 VALUES (1,1),(2,2);
10015CREATE VIEW v1 AS SELECT MAX(a) as f FROM t1;
10016--delimiter $
10017CREATE PROCEDURE p1()
10018BEGIN
10019  SELECT * FROM v1;
10020END $
10021--delimiter ;
10022
10023CALL p1;
10024ALTER TABLE t1 DROP a;
10025-- error ER_VIEW_INVALID
10026CALL p1;
10027
10028DROP PROCEDURE p1;
10029DROP VIEW v1;
10030DROP TABLE t1;
10031
10032
10033--echo #
10034--echo # BUG#30366310: USING A FUNCTION TO ASSIGN DEFAULT VALUES TO
10035--echo # 2 OR MORE VARIABLES CRASHES SERVER
10036--echo #
10037
10038delimiter |;
10039create function f1() returns bigint return now()-1|
10040create procedure p1()
10041begin
10042  declare b, c bigint default f1();
10043  select b-c;
10044end|
10045call p1()|
10046drop procedure p1|
10047drop function f1|
10048delimiter ;|
10049
10050--echo #
10051--echo # MDEV-24827: MariaDB 10.5.5 crash (sig 11) during a SELECT
10052--echo #
10053
10054CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT);
10055CREATE TABLE t2 (c1 INT PRIMARY KEY, c2 INT, KEY idx_c2(c2));
10056
10057INSERT INTO t1 (c1, c2) SELECT seq, seq FROM seq_1_to_10000;
10058INSERT INTO t2 (c1, c2) SELECT seq, seq FROM seq_1_to_20000;
10059
10060--delimiter $
10061
10062CREATE OR REPLACE PROCEDURE p1()
10063begin
10064  DECLARE done INT DEFAULT FALSE;
10065  DECLARE a INT;
10066
10067  DECLARE cur1 CURSOR FOR
10068    SELECT t2.c1 AS c1 FROM t1 LEFT JOIN t2 ON t1.c1 = t2.c1
10069    WHERE EXISTS (SELECT 1 FROM t1 WHERE c2 = -1) ORDER BY c1;
10070
10071  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10072
10073  OPEN cur1;
10074  read_loop: LOOP
10075    FETCH cur1 INTO a;
10076    IF done THEN
10077      LEAVE read_loop;
10078    END IF;
10079  END LOOP;
10080  CLOSE cur1;
10081END $
10082
10083--delimiter ;
10084
10085CALL p1();
10086
10087DROP PROCEDURE p1;
10088DROP TABLE t1;
10089DROP TABLE t2;
10090
10091--echo #End of 10.2 tests
10092
10093--echo #
10094--echo # MDEV-12007 Allow ROW variables as a cursor FETCH target
10095--echo #
10096
10097
10098--echo # The cursor and the ROW variable in FETCH must have the same number of fields
10099DELIMITER $$;
10100CREATE PROCEDURE p1()
10101BEGIN
10102  DECLARE done INT DEFAULT FALSE;
10103  DECLARE rec ROW(aa INT, bb VARCHAR(32), cc INT);
10104  DECLARE cur CURSOR FOR SELECT 10 AS a,'b10' AS b;
10105  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10106  OPEN cur;
10107read_loop:
10108  LOOP
10109    FETCH cur INTO rec;
10110    IF done THEN
10111      LEAVE read_loop;
10112    END IF;
10113  END LOOP;
10114  CLOSE cur;
10115END;
10116$$
10117DELIMITER ;$$
10118--error ER_SP_WRONG_NO_OF_FETCH_ARGS
10119CALL p1();
10120DROP PROCEDURE p1;
10121
10122
10123--echo # Multiple ROW variables in FETCH
10124DELIMITER $$;
10125CREATE PROCEDURE p1()
10126BEGIN
10127  DECLARE done INT DEFAULT FALSE;
10128  DECLARE rec1 ROW(aa INT);
10129  DECLARE rec2 ROW(aa INT);
10130  DECLARE cur CURSOR FOR SELECT 10 AS a, 20 AS b;
10131  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10132  OPEN cur;
10133read_loop:
10134  LOOP
10135    FETCH cur INTO rec1, rec2;
10136    IF done THEN
10137      LEAVE read_loop;
10138    END IF;
10139  END LOOP;
10140  CLOSE cur;
10141END;
10142$$
10143DELIMITER ;$$
10144--error ER_OPERAND_COLUMNS
10145CALL p1();
10146DROP PROCEDURE p1;
10147
10148
10149--echo # A complete working example
10150CREATE TABLE t1 (a INT, b VARCHAR(32));
10151INSERT INTO t1 VALUES (10,'b10');
10152INSERT INTO t1 VALUES (20,'b20');
10153INSERT INTO t1 VALUES (30,'b30');
10154DELIMITER $$;
10155CREATE PROCEDURE p1()
10156BEGIN
10157  DECLARE done INT DEFAULT FALSE;
10158  DECLARE rec ROW(aa INT, bb VARCHAR(32));
10159  DECLARE cur CURSOR FOR SELECT a,b FROM t1;
10160  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10161  OPEN cur;
10162read_loop:
10163  LOOP
10164    FETCH cur INTO rec;
10165    IF done THEN
10166      LEAVE read_loop;
10167    END IF;
10168    SELECT CONCAT('rec=(',rec.aa,',',rec.bb,')') AS c;
10169  END LOOP;
10170  CLOSE cur;
10171END;
10172$$
10173DELIMITER ;$$
10174CALL p1();
10175DROP PROCEDURE p1;
10176DROP TABLE t1;
10177
10178
10179--echo # A ROW variable with a single field
10180DELIMITER $$;
10181CREATE PROCEDURE p1()
10182BEGIN
10183  DECLARE done INT DEFAULT FALSE;
10184  DECLARE rec ROW(aa INT);
10185  DECLARE cur CURSOR FOR SELECT 10 AS a UNION SELECT 20;
10186  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
10187  OPEN cur;
10188read_loop:
10189  LOOP
10190    FETCH cur INTO rec;
10191    IF done THEN
10192      LEAVE read_loop;
10193    END IF;
10194    SELECT CONCAT('rec=(',rec.aa,')') AS c;
10195  END LOOP;
10196  CLOSE cur;
10197END;
10198$$
10199DELIMITER ;$$
10200CALL p1();
10201DROP PROCEDURE p1;
10202
10203--echo #
10204--echo # MDEV-14228 MariaDB crashes with function
10205--echo #
10206
10207CREATE TABLE t1 (c VARCHAR(16), KEY(c));
10208INSERT INTO t1 VALUES ('foo');
10209
10210DELIMITER $$;
10211CREATE FUNCTION f1() RETURNS VARCHAR(16)
10212BEGIN
10213  DECLARE v VARCHAR(16);
10214  FOR v IN (SELECT DISTINCT c FROM t1)
10215  DO
10216    IF (v = 'bar') THEN
10217      SELECT 1 INTO @a;
10218    END IF;
10219  END FOR;
10220  RETURN 'qux';
10221END $$
10222DELIMITER  ;$$
10223--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10224SELECT f1();
10225DROP FUNCTION f1;
10226
10227DELIMITER $$;
10228CREATE FUNCTION f1() RETURNS VARCHAR(16)
10229BEGIN
10230  DECLARE v ROW TYPE OF t1;
10231  IF v = 'bar' THEN
10232    RETURN 'eq';
10233  END IF;
10234  RETURN 'ne';
10235END $$
10236DELIMITER ;$$
10237--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10238SELECT f1();
10239DROP FUNCTION f1;
10240
10241DELIMITER $$;
10242CREATE FUNCTION f1() RETURNS VARCHAR(16)
10243BEGIN
10244  DECLARE v ROW(a INT);
10245  IF v = 'bar' THEN
10246    RETURN 'eq';
10247  END IF;
10248  RETURN 'ne';
10249END $$
10250DELIMITER ;$$
10251--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10252SELECT f1();
10253DROP FUNCTION f1;
10254
10255DROP TABLE t1;
10256
10257
10258DELIMITER $$;
10259--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10260BEGIN NOT ATOMIC
10261  DECLARE v ROW(a INT);
10262  SELECT v IN ('a','b');
10263END $$
10264DELIMITER ;$$
10265
10266DELIMITER $$;
10267--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10268BEGIN NOT ATOMIC
10269DECLARE v ROW(a INT);
10270  SELECT 'a' IN (v,'b');
10271END $$
10272DELIMITER ;$$
10273
10274DELIMITER $$;
10275--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
10276BEGIN NOT ATOMIC
10277  DECLARE v ROW(a INT);
10278  SELECT 'a' IN ('b',v);
10279END $$
10280DELIMITER ;$$
10281
10282--echo #
10283--echo # MDEV-15112 Inconsistent evaluation of spvariable=0 in strict mode
10284--echo #
10285
10286SET sql_mode=STRICT_ALL_TABLES;
10287CREATE OR REPLACE TABLE t1 (e TIMESTAMP(6));
10288INSERT INTO t1 VALUES ('2001-01-01 10:20:30');
10289
10290DELIMITER $$;
10291CREATE FUNCTION f1(a VARBINARY(255))
10292RETURNS INT
10293DETERMINISTIC
10294BEGIN
10295  RETURN a = timestamp'2038-01-19 03:14:07.999999'
10296      OR a = 0;
10297END
10298$$
10299CREATE FUNCTION f2(a VARBINARY(255))
10300RETURNS INT
10301DETERMINISTIC
10302BEGIN
10303  RETURN a = 0;
10304END
10305$$
10306CREATE OR REPLACE FUNCTION f3(a VARBINARY(255))
10307RETURNS INT
10308DETERMINISTIC
10309BEGIN
10310  RETURN a = timestamp'2038-01-19 03:14:07.999999'
10311      OR a = sleep(0);
10312END
10313$$
10314DELIMITER ;$$
10315
10316--error ER_TRUNCATED_WRONG_VALUE
10317SELECT f1(e) FROM t1;
10318--error ER_TRUNCATED_WRONG_VALUE
10319SELECT f2(e) FROM t1;
10320--error ER_TRUNCATED_WRONG_VALUE
10321SELECT f3(e) FROM t1;
10322
10323DROP FUNCTION f1;
10324DROP FUNCTION f2;
10325DROP FUNCTION f3;
10326DROP TABLE t1;
10327
10328--echo # Test affected rows from an sp
10329
10330create table t1 (a int);
10331
10332DELIMITER $$;
10333create procedure p1()
10334begin
10335insert into t1 values(1);
10336insert into t1 values(2);
10337end;
10338$$
10339create procedure p2()
10340begin
10341insert into t1 values(1);
10342call p1();
10343select row_count();
10344insert into t1 values(2);
10345insert into t1 values(2);
10346end;
10347$$
10348DELIMITER ;$$
10349
10350--enable_info
10351CALL p2();
10352--disable_info
10353DROP PROCEDURE p1;
10354DROP PROCEDURE p2;
10355drop table t1;
10356
10357--echo #
10358--echo # MDEV-15957 Unexpected "Data too long" when doing CREATE..SELECT with stored functions
10359--echo #
10360
10361CREATE TABLE t1 (a INT(3));
10362INSERT INTO t1 VALUES (-999);
10363CREATE FUNCTION f1(a INT(3)) RETURNS INT(3) RETURN a;
10364CREATE TABLE t2 AS SELECT CONCAT(a) AS c1, CONCAT(COALESCE(a)) AS c2, CONCAT(f1(a)) AS c3 FROM t1;
10365SHOW CREATE TABLE t2;
10366DROP TABLE t1,t2;
10367DROP FUNCTION f1;
10368
10369
10370CREATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET latin1 RETURN '';
10371CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10372SHOW CREATE TABLE t1;
10373DROP TABLE t1;
10374DROP FUNCTION f1;
10375
10376CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET latin1 RETURN '';
10377CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10378SHOW CREATE TABLE t1;
10379DROP TABLE t1;
10380DROP FUNCTION f1;
10381
10382CREATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET latin1 RETURN '';
10383CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10384SHOW CREATE TABLE t1;
10385DROP TABLE t1;
10386DROP FUNCTION f1;
10387
10388CREATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET latin1 RETURN '';
10389CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10390SHOW CREATE TABLE t1;
10391DROP TABLE t1;
10392DROP FUNCTION f1;
10393
10394CREATE FUNCTION f1() RETURNS TINYTEXT CHARACTER SET utf8 RETURN '';
10395CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10396SHOW CREATE TABLE t1;
10397DROP TABLE t1;
10398DROP FUNCTION f1;
10399
10400CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET utf8 RETURN '';
10401CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10402SHOW CREATE TABLE t1;
10403DROP TABLE t1;
10404DROP FUNCTION f1;
10405
10406CREATE FUNCTION f1() RETURNS MEDIUMTEXT CHARACTER SET utf8 RETURN '';
10407CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10408SHOW CREATE TABLE t1;
10409DROP TABLE t1;
10410DROP FUNCTION f1;
10411
10412CREATE FUNCTION f1() RETURNS LONGTEXT CHARACTER SET utf8 RETURN '';
10413CREATE TABLE t1 AS SELECT f1() AS c1, COALESCE(f1()) AS c2, CONCAT(f1()) AS c3;
10414SHOW CREATE TABLE t1;
10415DROP TABLE t1;
10416DROP FUNCTION f1;
10417
10418--echo #
10419--echo # MDEV-16036: Debug assertion failed in resignal on create
10420--echo # temporary table
10421--echo #
10422
10423set @save_sql_mode= @@sql_mode;
10424set sql_mode='ORACLE';
10425delimiter /;
10426CREATE or replace procedure p4()
10427AS
10428  CONTINUE HANDLER FOR SQLWARNING
10429  BEGIN
10430    NULL;
10431  END;
10432  EXIT HANDLER FOR OTHERS -- SQLEXCEPTION
10433  BEGIN
10434      GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
10435      SELECT @sqlstate, @errno, @text;
10436      SHOW WARNINGS;
10437      RESIGNAL;  -- cause DBG_ASSERT failed
10438  END;
10439BEGIN
10440  CREATE TEMPORARY TABLE IF NOT EXISTS t1(origine VARCHAR2(10) NOT NULL);
10441END
10442/
10443delimiter ;/
10444call p4();
10445call p4();
10446drop procedure p4;
10447drop table t1;
10448set @@sql_mode=@save_sql_mode;
10449set @@global.userstat= @save_userstat;
10450
10451--echo #
10452--echo # MDEV-17363 Compressed columns cannot be restored from dump
10453--echo # COMPRESSED conflicted between data type and SP label,
10454--echo # so it's not allowed as an SP label any more.
10455--echo #
10456
10457DELIMITER $$;
10458CREATE FUNCTION f1() RETURNS TEXT COMPRESSED
10459BEGIN
10460  RETURN '';
10461END;
10462$$
10463DELIMITER ;$$
10464SHOW CREATE FUNCTION f1;
10465DROP FUNCTION f1;
10466
10467DELIMITER $$;
10468--error ER_PARSE_ERROR
10469CREATE FUNCTION f1() RETURNS TEXT
10470COMPRESSED:
10471BEGIN
10472  RETURN '';
10473END;
10474$$
10475DELIMITER ;$$
10476
10477--echo # End of 10.3 tests
10478