1-- source include/have_log_bin.inc
2
3call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
4#
5# SQL Syntax for Prepared Statements test
6#
7--disable_warnings
8drop table if exists t1,t2,t3,t4;
9
10# Avoid wrong warnings if mysql_client_test fails
11drop database if exists client_test_db;
12--enable_warnings
13
14create table t1
15(
16  a int primary key,
17  b char(10)
18);
19insert into t1 values (1,'one');
20insert into t1 values (2,'two');
21insert into t1 values (3,'three');
22insert into t1 values (4,'four');
23
24# basic functionality
25set @a=2;
26prepare stmt1 from 'select * from t1 where a <= ?';
27execute stmt1 using @a;
28set @a=3;
29execute stmt1 using @a;
30
31# non-existant statement
32--error 1243
33deallocate prepare no_such_statement;
34
35--error 1210
36execute stmt1;
37
38# Nesting ps commands is not allowed:
39--error ER_UNSUPPORTED_PS
40prepare stmt2 from 'prepare nested_stmt from "select 1"';
41
42--error ER_UNSUPPORTED_PS
43prepare stmt2 from 'execute stmt1';
44
45--error ER_UNSUPPORTED_PS
46prepare stmt2 from 'deallocate prepare z';
47
48# PS insert
49prepare stmt3 from 'insert into t1 values (?,?)';
50set @arg1=5, @arg2='five';
51execute stmt3 using @arg1, @arg2;
52select * from t1 where a>3;
53
54# PS update
55prepare stmt4 from 'update t1 set a=? where b=?';
56set @arg1=55, @arg2='five';
57execute stmt4 using @arg1, @arg2;
58select * from t1 where a>3;
59
60# PS create/delete
61prepare stmt4 from 'create table t2 (a int)';
62execute stmt4;
63prepare stmt4 from 'drop table t2';
64execute stmt4;
65
66# Do something that will cause error
67--error 1051
68execute stmt4;
69
70# placeholders in result field names.
71prepare stmt5 from 'select ? + a from t1';
72set @a=1;
73execute stmt5 using @a;
74
75execute stmt5 using @no_such_var;
76
77set @nullvar=1;
78set @nullvar=NULL;
79execute stmt5 using @nullvar;
80
81set @nullvar2=NULL;
82execute stmt5 using @nullvar2;
83
84# Check that multiple SQL statements are disabled inside PREPARE
85--error 1064
86prepare stmt6 from 'select 1; select2';
87
88--error 1064
89prepare stmt6 from 'insert into t1 values (5,"five"); select2';
90
91# This shouldn't parse
92--error 1064
93explain prepare stmt6 from 'insert into t1 values (5,"five"); select2';
94
95create table t2
96(
97  a int
98);
99
100insert into t2 values (0);
101
102# parameter is NULL
103set @arg00=NULL ;
104prepare stmt1 from 'select 1 FROM t2 where a=?' ;
105execute stmt1 using @arg00 ;
106
107# prepare using variables:
108--error 1064
109prepare stmt1 from @nosuchvar;
110
111set @ivar= 1234;
112--error 1064
113prepare stmt1 from @ivar;
114
115set @fvar= 123.4567;
116--error 1064
117prepare stmt1 from @fvar;
118
119drop table t1,t2;
120deallocate prepare stmt3;
121deallocate prepare stmt4;
122deallocate prepare stmt5;
123
124#
125# Bug #4105: Server crash on attempt to prepare a statement with character
126# set introducer
127#
128PREPARE stmt1 FROM "select _utf8 'A' collate utf8_bin = ?";
129set @var='A';
130EXECUTE stmt1 USING @var;
131DEALLOCATE PREPARE stmt1;
132
133#
134# BUG#3486:  FOUND_ROWS() fails inside stored procedure [and prepared statement]
135#
136create table t1 (id int);
137prepare stmt1 from "select FOUND_ROWS()";
138select SQL_CALC_FOUND_ROWS * from t1;
139# Expect 0
140execute stmt1;
141insert into t1 values (1);
142select SQL_CALC_FOUND_ROWS * from t1;
143# Expect 1
144execute stmt1;
145# Expect 0
146execute stmt1;
147deallocate prepare stmt1;
148drop table t1;
149
150#
151# prepared EXPLAIN
152#
153create table t1
154(
155  c1  tinyint, c2  smallint, c3  mediumint, c4  int,
156  c5  integer, c6  bigint, c7  float, c8  double,
157  c9  double precision, c10 real, c11 decimal(7, 4), c12 numeric(8, 4),
158  c13 date, c14 datetime, c15 timestamp, c16 time,
159  c17 year, c18 bit, c19 bool, c20 char,
160  c21 char(10), c22 varchar(30), c23 tinyblob, c24 tinytext,
161  c25 blob, c26 text, c27 mediumblob, c28 mediumtext,
162  c29 longblob, c30 longtext, c31 enum('one', 'two', 'three'),
163  c32 set('monday', 'tuesday', 'wednesday')
164) engine = MYISAM ;
165create table t2 like t1;
166
167set @stmt= ' explain SELECT (SELECT SUM(c1 + c12 + 0.0) FROM t2 where (t1.c2 - 0e-3) = t2.c2 GROUP BY t1.c15 LIMIT 1) as scalar_s, exists (select 1.0e+0 from t2 where t2.c3 * 9.0000000000 = t1.c4) as exists_s, c5 * 4 in (select c6 + 0.3e+1 from t2) as in_s, (c7 - 4, c8 - 4) in (select c9 + 4.0, c10 + 40e-1 from t2) as in_row_s FROM t1, (select c25 x, c32 y from t2) tt WHERE x * 1 = c25 ' ;
168prepare stmt1 from @stmt ;
169execute stmt1 ;
170execute stmt1 ;
171explain SELECT (SELECT SUM(c1 + c12 + 0.0) FROM t2 where (t1.c2 - 0e-3) = t2.c2 GROUP BY t1.c15 LIMIT 1) as scalar_s, exists (select 1.0e+0 from t2 where t2.c3 * 9.0000000000 = t1.c4) as exists_s, c5 * 4 in (select c6 + 0.3e+1 from t2) as in_s, (c7 - 4, c8 - 4) in (select c9 + 4.0, c10 + 40e-1 from t2) as in_row_s FROM t1, (select c25 x, c32 y from t2) tt WHERE x * 1 = c25;
172deallocate prepare stmt1;
173drop tables t1,t2;
174
175#
176# parameters from variables (for field creation)
177#
178set @arg00=1;
179prepare stmt1 from ' create table t1 (m int) as select 1 as m ' ;
180execute stmt1 ;
181select m from t1;
182drop table t1;
183prepare stmt1 from ' create table t1 (m int) as select ? as m ' ;
184execute stmt1 using @arg00;
185select m from t1;
186deallocate prepare stmt1;
187drop table t1;
188
189#
190# eq() for parameters
191#
192create table t1 (id int(10) unsigned NOT NULL default '0',
193                 name varchar(64) NOT NULL default '',
194                 PRIMARY KEY  (id), UNIQUE KEY `name` (`name`));
195insert into t1 values (1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5'),(6,'6'),(7,'7');
196prepare stmt1 from 'select name from t1 where id=? or id=?';
197set @id1=1,@id2=6;
198execute stmt1 using @id1, @id2;
199select name from t1 where id=1 or id=6;
200deallocate prepare stmt1;
201drop table t1;
202
203#
204# SHOW TABLE STATUS test
205#
206create table t1 ( a int primary key, b varchar(30)) engine = MYISAM ;
207analyze table t1;
208prepare stmt1 from ' show table status from test like ''t1%'' ';
209--replace_column 8 4294967295 12 # 13 # 14 #
210execute stmt1;
211--replace_column 8 4294967295 12 # 13 # 14 #
212show table status from test like 't1%' ;
213deallocate prepare stmt1 ;
214drop table t1;
215
216#
217# Bug#4912 "mysqld crashs in case a statement is executed a second time":
218# negation elimination should work once and not break prepared statements
219#
220
221create table t1(a varchar(2), b varchar(3));
222prepare stmt1 from "select a, b from t1 where (not (a='aa' and b < 'zzz'))";
223execute stmt1;
224execute stmt1;
225deallocate prepare stmt1;
226drop table t1;
227
228#
229# Bug#5034 "prepared "select 1 into @arg15", second execute crashes
230# server".
231# Check that descendands of select_result can be reused in prepared
232# statements or are correctly created and deleted on each execute
233#
234
235--let $outfile=$MYSQLTEST_VARDIR/tmp/f1.txt
236--error 0,1
237--remove_file $outfile
238
239prepare stmt1 from "select 1 into @var";
240execute stmt1;
241execute stmt1;
242prepare stmt1 from "create table t1 select 1 as i";
243execute stmt1;
244drop table t1;
245execute stmt1;
246prepare stmt1 from "insert into t1 select i from t1";
247execute stmt1;
248execute stmt1;
249--replace_result $MYSQLTEST_VARDIR <MYSQLTEST_VARDIR>
250eval prepare stmt1 from "select * from t1 into outfile '$outfile'";
251execute stmt1;
252deallocate prepare stmt1;
253drop table t1;
254
255--remove_file $outfile
256
257#
258# BUG#5242 "Prepared statement names are case sensitive"
259#
260prepare stmt1 from 'select 1';
261prepare STMT1 from 'select 2';
262execute sTmT1;
263deallocate prepare StMt1;
264
265--error 1243
266deallocate prepare Stmt1;
267
268# also check that statement names are in right charset.
269set names utf8;
270prepare `ü` from 'select 1234';
271execute `ü` ;
272set names latin1;
273--character_set latin1
274execute `�`;
275deallocate prepare `�`;
276set names default;
277--character_set utf8mb4
278
279
280#
281# BUG#4368 "select * from t1 where a like ?" crashes server if a is in utf8
282# and ? is in latin1
283# Check that Item converting latin1 to utf8 (for LIKE function) is created
284# in memory of prepared statement.
285#
286
287create table t1 (a varchar(10)) charset=utf8;
288insert into t1 (a) values ('yahoo');
289set character_set_connection=latin1;
290prepare stmt from 'select a from t1 where a like ?';
291set @var='google';
292execute stmt using @var;
293execute stmt using @var;
294deallocate prepare stmt;
295drop table t1;
296
297#
298# BUG#5510 "inserting Null in AutoIncrement primary key Column Fails"
299# (prepared statements)
300# The cause: misuse of internal MySQL 'Field' API.
301#
302
303create table t1 (a bigint(20) not null primary key auto_increment);
304insert into t1 (a) values (null);
305select * from t1;
306prepare stmt from "insert into t1 (a) values (?)";
307set @var=null;
308execute stmt using @var;
309select * from t1;
310drop table t1;
311#
312# check the same for timestamps
313#
314create table t1 (a timestamp not null);
315prepare stmt from "insert into t1 (a) values (CURRENT_TIMESTAMP)";
316execute stmt;
317--disable_result_log ONCE
318select * from t1;
319deallocate prepare stmt;
320drop table t1;
321
322#
323# BUG#5688 "Upgraded 4.1.5 Server seg faults" # (prepared statements)
324# The test case speaks for itself.
325# Just another place where we used wrong memory root for Items created
326# during statement prepare.
327#
328prepare stmt from "select 'abc' like convert('abc' using utf8)";
329execute stmt;
330execute stmt;
331deallocate prepare stmt;
332
333#
334# BUG#5748 "Prepared statement with BETWEEN and bigint values crashes
335# mysqld". Just another place where an item tree modification must be
336# rolled back.
337#
338create table t1 ( a bigint );
339prepare stmt from 'select a from t1 where a between ? and ?';
340set @a=1;
341execute stmt using @a, @a;
342execute stmt using @a, @a;
343execute stmt using @a, @a;
344drop table t1;
345deallocate prepare stmt;
346
347#
348# Bug #5987 subselect in bool function crashes server (prepared statements):
349# don't overwrite transformed subselects with old arguments of a bool
350# function.
351#
352create table t1 (a int);
353prepare stmt from "select * from t1 where 1 > (1 in (SELECT * FROM t1))";
354execute stmt;
355execute stmt;
356execute stmt;
357drop table t1;
358deallocate prepare stmt;
359
360#
361# Test case for Bug#6042 "constants propogation works only once (prepared
362# statements): check that the query plan changes whenever we change
363# placeholder value.
364#
365create table t1 (a int, b int) engine = myisam;
366insert into t1 (a, b) values (1,1), (1,2), (2,1), (2,2);
367prepare stmt from
368"explain select * from t1 where t1.a=2 and t1.a=t1.b and t1.b > 1 + ?";
369set @v=5;
370--replace_column 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 -
371execute stmt using @v;
372set @v=0;
373--replace_column 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 -
374execute stmt using @v;
375set @v=5;
376--replace_column 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 -
377execute stmt using @v;
378drop table t1;
379deallocate prepare stmt;
380
381#
382# A test case for Bug#5985 prepare stmt from "select rand(?)" crashes
383# server. Check that Item_func_rand is prepared-statements friendly.
384#
385create table t1 (a int);
386insert into t1 (a) values (1), (2), (3), (4);
387set @precision=10000000000;
388--replace_column 1 - 3 -
389select rand(),
390       cast(rand(10)*@precision as unsigned integer) from t1;
391prepare stmt from
392"select rand(),
393        cast(rand(10)*@precision as unsigned integer),
394        cast(rand(?)*@precision as unsigned integer) from t1";
395set @var=1;
396--replace_column 1 - 3 -
397execute stmt using @var;
398set @var=2;
399--replace_column 1 -
400execute stmt using @var;
401set @var=3;
402--replace_column 1 -
403execute stmt using @var;
404drop table t1;
405deallocate prepare stmt;
406
407#
408# A test case for Bug#6050 "EXECUTE stmt reports ambiguous fieldnames with
409# identical tables from different schemata"
410# Check that field name resolving in prepared statements works OK.
411#
412create database mysqltest1;
413create table t1 (a int);
414create table mysqltest1.t1 (a int);
415select * from t1, mysqltest1.t1;
416prepare stmt from "select * from t1, mysqltest1.t1";
417execute stmt;
418execute stmt;
419execute stmt;
420drop table t1;
421drop table mysqltest1.t1;
422drop database mysqltest1;
423deallocate prepare stmt;
424select '1.1' as a, '1.2' as a UNION SELECT '2.1', '2.2';
425prepare stmt from
426"select '1.1' as a, '1.2' as a UNION SELECT '2.1', '2.2'";
427execute stmt;
428execute stmt;
429execute stmt;
430deallocate prepare stmt;
431
432#
433# Test CREATE TABLE ... SELECT (Bug #6094)
434#
435create table t1 (a int);
436insert into t1 values (1),(2),(3);
437create table t2 select * from t1;
438--disable_warnings
439prepare stmt FROM 'create table t2 select * from t1';
440drop table t2;
441execute stmt;
442drop table t2;
443execute stmt;
444--error 1050
445execute stmt;
446drop table t2;
447execute stmt;
448--enable_warnings
449drop table t1,t2;
450deallocate prepare stmt;
451
452#
453# Bug#6088 "FOUND_ROWS returns wrong values for prepared statements when
454# LIMIT is used"
455#
456create table t1 (a int);
457insert into t1 (a) values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
458prepare stmt from "select sql_calc_found_rows * from t1 limit 2";
459execute stmt;
460select found_rows();
461execute stmt;
462select found_rows();
463execute stmt;
464select found_rows();
465deallocate prepare stmt;
466drop table t1;
467
468#
469# Bug#6047 "permission problem when executing mysql_stmt_execute with derived
470# table"
471#
472
473CREATE TABLE t1 (N int, M tinyint);
474INSERT INTO t1 VALUES (1,0),(1,0),(2,0),(2,0),(3,0);
475PREPARE stmt FROM 'UPDATE t1 AS P1 INNER JOIN (SELECT N FROM t1 GROUP BY N HAVING COUNT(M) > 1) AS P2 ON P1.N = P2.N SET P1.M = 2';
476EXECUTE stmt;
477DEALLOCATE PREPARE stmt;
478DROP TABLE t1;
479
480#
481# Bug#6297 "prepared statement, wrong handling of <parameter> IS NULL"
482# Test that placeholders work with IS NULL/IS NOT NULL clauses.
483#
484prepare stmt from "select ? is null, ? is not null, ?";
485select @no_such_var is null, @no_such_var is not null, @no_such_var;
486execute stmt using @no_such_var, @no_such_var, @no_such_var;
487set @var='abc';
488select @var is null, @var is not null, @var;
489execute stmt using @var, @var, @var;
490set @var=null;
491select @var is null, @var is not null, @var;
492execute stmt using @var, @var, @var;
493
494#
495# Bug#6873 "PS, having with subquery, crash during execute"
496# check that if we modify having subtree, we update JOIN->having pointer
497#
498create table t1 (pnum char(3));
499create table t2 (pnum char(3));
500prepare stmt from "select pnum from t2 having pnum in (select 'p1' from t1)";
501execute stmt;
502execute stmt;
503execute stmt;
504deallocate prepare stmt;
505drop table t1, t2;
506
507#
508#
509# Bug#19399 "Stored Procedures 'Lost Connection' when dropping/creating
510#            tables"
511# Check that multi-delete tables are also cleaned up before re-execution.
512#
513--disable_warnings
514drop table if exists t1;
515create temporary table if not exists t1 (a1 int);
516--enable_warnings
517# exact delete syntax is essential
518prepare stmt from "delete t1 from t1 where (cast(a1/3 as unsigned) * 3) = a1";
519drop temporary table t1;
520create temporary table if not exists t1 (a1 int);
521# the server crashed on  the next statement without the fix
522execute stmt;
523drop temporary table t1;
524create temporary table if not exists t1 (a1 int);
525# the problem was in memory corruption: repeat the test just in case
526execute stmt;
527drop temporary table t1;
528create temporary table if not exists t1 (a1 int);
529execute stmt;
530drop temporary table t1;
531deallocate prepare stmt;
532
533# Bug#6102 "Server crash with prepared statement and blank after
534# function name"
535# ensure that stored functions are cached when preparing a statement
536# before we open tables
537#
538create table t1 (a varchar(20));
539insert into t1 values ('foo');
540prepare stmt FROM 'SELECT char_length (a) FROM t1';
541-- error ER_SP_DOES_NOT_EXIST
542prepare stmt2 FROM 'SELECT not_a_function (a) FROM t1';
543drop table t1;
544
545#
546# Bug #6089: FOUND_ROWS returns wrong values when no table/view is used
547#
548
549prepare stmt from "SELECT SQL_CALC_FOUND_ROWS 'foo' UNION SELECT 'bar' LIMIT 0";
550execute stmt;
551SELECT FOUND_ROWS();
552execute stmt;
553SELECT FOUND_ROWS();
554deallocate prepare stmt;
555
556#
557# Bug#9096 "select doesn't return all matched records if prepared statements
558# is used"
559# The bug was is bad co-operation of the optimizer's algorithm which determines
560# which keys can be used to execute a query, constants propagation
561# part of the optimizer and parameter markers used by prepared statements.
562
563drop table if exists t1;
564create table t1 (c1 int(11) not null, c2 int(11) not null,
565             primary key  (c1,c2), key c2 (c2), key c1 (c1));
566
567insert into t1 values (200887, 860);
568insert into t1 values (200887, 200887);
569
570select * from t1 where (c1=200887 and c2=200887) or c2=860;
571
572prepare stmt from
573"select * from t1 where (c1=200887 and c2=200887) or c2=860";
574execute stmt;
575prepare stmt from
576"select * from t1 where (c1=200887 and c2=?) or c2=?";
577set @a=200887, @b=860;
578# this query did not return all matching rows
579execute stmt using @a, @b;
580deallocate prepare stmt;
581
582drop table t1;
583
584#
585# Bug#9777 - another occurrence of the problem stated in Bug#9096:
586# we can not compare basic constants by their names, because a placeholder
587# is a basic constant while his name is always '?'
588#
589
590create table t1 (
591   id bigint(20) not null auto_increment,
592   code varchar(20) character set utf8 collate utf8_bin not null default '',
593   company_name varchar(250) character set utf8 collate utf8_bin default null,
594   setup_mode tinyint(4) default null,
595   start_date datetime default null,
596   primary key  (id), unique key code (code)
597);
598
599create table t2 (
600   id bigint(20) not null auto_increment,
601   email varchar(250) character set utf8 collate utf8_bin default null,
602   name varchar(250) character set utf8 collate utf8_bin default null,
603   t1_id bigint(20) default null,
604   password varchar(250) character set utf8 collate utf8_bin default null,
605   primary_contact tinyint(4) not null default '0',
606   email_opt_in tinyint(4) not null default '1',
607   primary key  (id), unique key email (email), key t1_id (t1_id),
608   constraint t2_fk1 foreign key (t1_id) references t1 (id)
609);
610
611insert into t1 values
612(1, 'demo', 'demo s', 0, current_date()),
613(2, 'code2', 'name 2', 0, current_date()),
614(3, 'code3', 'name 3', 0, current_date());
615
616insert into t2 values
617(2, 'email1', 'name1', 3, 'password1', 0, 0),
618(3, 'email2', 'name1', 1, 'password2', 1, 0),
619(5, 'email3', 'name3', 2, 'password3', 0, 0);
620
621prepare stmt from 'select t2.id from t2, t1 where (t1.id=? and t2.t1_id=t1.id)';
622set @a=1;
623execute stmt using @a;
624
625select t2.id from t2, t1 where (t1.id=1 and t2.t1_id=t1.id);
626
627deallocate prepare stmt;
628drop table t2, t1;
629
630#
631# Bug#11060 "Server crashes on calling stored procedure with INSERT SELECT
632# UNION SELECT" aka "Server crashes on re-execution of prepared INSERT ...
633# SELECT with UNION".
634#
635create table t1 (id int);
636prepare stmt from "insert into t1 (id) select id from t1 union select id from t1";
637execute stmt;
638execute stmt;
639deallocate prepare stmt;
640drop table t1;
641#
642# Bug#11458 "Prepared statement with subselects return random data":
643# drop PARAM_TABLE_BIT from the list of tables used by a subquery
644#
645create table t1 (
646  id int(11) unsigned not null primary key auto_increment,
647  partner_id varchar(35) not null,
648  t1_status_id int(10) unsigned
649);
650
651insert into t1 values ("1", "partner1", "10"), ("2", "partner2", "10"),
652                      ("3", "partner3", "10"), ("4", "partner4", "10");
653
654create table t2 (
655  id int(11) unsigned not null default '0',
656  t1_line_id int(11) unsigned not null default '0',
657  article_id varchar(20),
658  sequence int(11) not null default '0',
659  primary key  (id,t1_line_id)
660);
661
662insert into t2 values ("1", "1", "sup", "0"), ("2", "1", "sup", "1"),
663                      ("2", "2", "sup", "2"), ("2", "3", "sup", "3"),
664                      ("2", "4", "imp", "4"), ("3", "1", "sup", "0"),
665                      ("4", "1", "sup", "0");
666
667create table t3 (
668  id int(11) not null default '0',
669  preceding_id int(11) not null default '0',
670  primary key  (id,preceding_id)
671);
672
673create table t4 (
674  user_id varchar(50) not null,
675  article_id varchar(20) not null,
676  primary key  (user_id,article_id)
677);
678
679insert into t4 values("nicke", "imp");
680
681prepare stmt from
682'select distinct t1.partner_id
683from t1 left join t3 on t1.id = t3.id
684     left join t1 pp on pp.id = t3.preceding_id
685where
686  exists (
687    select *
688    from t2 as pl_inner
689    where pl_inner.id = t1.id
690    and pl_inner.sequence <= (
691      select min(sequence) from t2 pl_seqnr
692      where pl_seqnr.id = t1.id
693    )
694    and exists (
695      select * from t4
696      where t4.article_id = pl_inner.article_id
697      and t4.user_id = ?
698    )
699  )
700  and t1.id = ?
701group by t1.id
702having count(pp.id) = 0';
703set @user_id = 'nicke';
704set @id = '2';
705execute stmt using @user_id, @id;
706execute stmt using @user_id, @id;
707deallocate prepare stmt;
708drop table t1, t2, t3, t4;
709#
710# Bug#9379: make sure that Item::collation is reset when one sets
711# a parameter marker from a string variable.
712#
713prepare stmt from 'select ?=?';
714set @a='CHRISTINE           ';
715set @b='CHRISTINE';
716execute stmt using @a, @b;
717execute stmt using @a, @b;
718set @a=1, @b=2;
719execute stmt using @a, @b;
720set @a='CHRISTINE           ';
721set @b='CHRISTINE';
722execute stmt using @a, @b;
723deallocate prepare stmt;
724#
725# Bug#11299 "prepared statement makes wrong SQL syntax in binlog which stops
726# replication": check that errouneous queries with placeholders are not
727# allowed
728#
729create table t1 (a int);
730--error 1064
731prepare stmt from "select ??";
732--error 1064
733prepare stmt from "select ?FROM t1";
734--error 1064
735prepare stmt from "select FROM t1 WHERE?=1";
736--error 1064
737prepare stmt from "update t1 set a=a+?WHERE 1";
738--disable_ps_protocol
739--error 1064
740select ?;
741--error 1064
742select ??;
743--error 1064
744select ? from t1;
745--enable_ps_protocol
746drop table t1;
747#
748# Bug#9359 "Prepared statements take snapshot of system vars at PREPARE
749# time"
750#
751prepare stmt from "select @@time_zone";
752execute stmt;
753set @@time_zone:='Japan';
754execute stmt;
755prepare stmt from "select @@transaction_isolation";
756execute stmt;
757set transaction isolation level read committed;
758execute stmt;
759set transaction isolation level serializable;
760execute stmt;
761set @@transaction_isolation=default;
762execute stmt;
763deallocate prepare stmt;
764
765#
766# Bug#14410 "Crash in Enum or Set type in CREATE TABLE and PS/SP"
767#
768# Part I. Make sure the typelib for ENUM is created in the statement memory
769# root.
770prepare stmt from "create temporary table t1 (letter enum('','a','b','c')
771not null)";
772execute stmt;
773drop table t1;
774execute stmt;
775drop table t1;
776execute stmt;
777drop table t1;
778# Part II. Make sure that when the default value is converted to UTF-8,
779# the new item is # created in the statement memory root.
780set names latin1;
781prepare stmt from "create table t1 (a enum('test') default 'test')
782 character set utf8";
783execute stmt;
784drop table t1;
785execute stmt;
786drop table t1;
787execute stmt;
788drop table t1;
789# Cleanup
790set names default;
791deallocate prepare stmt;
792
793#
794# A test case for Bug#12734 "prepared statement may return incorrect result
795# set for a select SQL request": test that canDoTurboBM is reset for each
796# execute of a prepared statement.
797#
798create table t1 (
799  word_id mediumint(8) unsigned not null default '0',
800  formatted varchar(20) not null default ''
801);
802
803insert into t1 values
804  (80,'pendant'), (475,'pretendants'), (989,'tendances'),
805  (1019,'cependant'),(1022,'abondance'),(1205,'independants'),
806  (13,'lessiver'),(25,'lambiner'),(46,'situer'),(71,'terminer'),
807  (82,'decrocher');
808
809select count(*) from t1 where formatted like '%NDAN%';
810select count(*) from t1 where formatted like '%ER';
811prepare stmt from "select count(*) from t1 where formatted like ?";
812set @like="%NDAN%";
813execute stmt using @like;
814set @like="%ER";
815execute stmt using @like;
816set @like="%NDAN%";
817execute stmt using @like;
818set @like="%ER";
819execute stmt using @like;
820deallocate prepare stmt;
821drop table t1;
822
823#
824# Bug#13134 "Length of VARCHAR() utf8 column is increasing when table is
825# recreated with PS/SP"
826#
827SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
828prepare stmt from 'create table t1 (a varchar(10) character set utf8)';
829execute stmt;
830--disable_warnings WARN_DATA_TRUNCATED ONCE
831insert into t1 (a) values (repeat('a', 20));
832select length(a) from t1;
833drop table t1;
834execute stmt;
835--disable_warnings WARN_DATA_TRUNCATED ONCE
836insert into t1 (a) values (repeat('a', 20));
837# Check that the data is truncated to the same length
838select length(a) from t1;
839drop table t1;
840deallocate prepare stmt;
841SET sql_mode = default;
842#
843# Bug#16248 "WHERE (col1,col2) IN ((?,?)) gives wrong results":
844# check that ROW implementation is reexecution-friendly.
845#
846create table t1 (col1 integer, col2 integer);
847insert into t1 values(100,100),(101,101),(102,102),(103,103);
848prepare stmt from 'select col1, col2 from t1 where (col1, col2) in ((?,?))';
849set @a=100, @b=100;
850execute stmt using @a,@b;
851set @a=101, @b=101;
852execute stmt using @a,@b;
853set @a=102, @b=102;
854execute stmt using @a,@b;
855set @a=102, @b=103;
856execute stmt using @a,@b;
857deallocate prepare stmt;
858drop table t1;
859
860#
861# Bug#16365 Prepared Statements: DoS with too many open statements
862# Check that the limit @@max_prpeared_stmt_count works.
863#
864# This is also the test for bug#23159 prepared_stmt_count should be
865# status variable.
866#
867# Save the old value
868set @old_max_prepared_stmt_count= @@max_prepared_stmt_count;
869#
870# Disable prepared statement protocol: in this test we set
871# @@max_prepared_stmt_count to 0 or 1 and would like to test the limit
872# manually.
873#
874--disable_ps_protocol
875#
876# A. Check that the new variables are present in SHOW VARIABLES and
877# SHOW STATUS lists.
878#
879show variables like 'max_prepared_stmt_count';
880show status like 'prepared_stmt_count';
881#
882# B. Check that the new system variable is selectable.
883#
884select @@max_prepared_stmt_count;
885#
886# C. Check that max_prepared_stmt_count is settable (global only).
887#
888set global max_prepared_stmt_count=-1;
889select @@max_prepared_stmt_count;
890set global max_prepared_stmt_count=10000000000000000;
891select @@max_prepared_stmt_count;
892set global max_prepared_stmt_count=default;
893select @@max_prepared_stmt_count;
894--error ER_GLOBAL_VARIABLE
895set @@max_prepared_stmt_count=1;
896--error ER_GLOBAL_VARIABLE
897set max_prepared_stmt_count=1;
898--error ER_GLOBAL_VARIABLE
899set local max_prepared_stmt_count=1;
900# set to a reasonable limit works
901set global max_prepared_stmt_count=1;
902select @@max_prepared_stmt_count;
903#
904# D. Check that the variables actually work.
905#
906set global max_prepared_stmt_count=0;
907select @@max_prepared_stmt_count;
908show status like 'prepared_stmt_count';
909--error ER_MAX_PREPARED_STMT_COUNT_REACHED
910prepare stmt from "select 1";
911show status like 'prepared_stmt_count';
912set global max_prepared_stmt_count=1;
913prepare stmt from "select 1";
914show status like 'prepared_stmt_count';
915--error ER_MAX_PREPARED_STMT_COUNT_REACHED
916prepare stmt1 from "select 1";
917show status like 'prepared_stmt_count';
918deallocate prepare stmt;
919show status like 'prepared_stmt_count';
920#
921# E. Check that we can prepare a statement with the same name
922# successfully, without hitting the limit.
923#
924prepare stmt from "select 1";
925show status like 'prepared_stmt_count';
926prepare stmt from "select 2";
927show status like 'prepared_stmt_count';
928#
929# F. We can set the max below the current count. In this case no new
930# statements should be allowed to prepare.
931#
932show status like 'prepared_stmt_count';
933select @@max_prepared_stmt_count;
934set global max_prepared_stmt_count=0;
935--error ER_MAX_PREPARED_STMT_COUNT_REACHED
936prepare stmt from "select 1";
937# Result: the old statement is deallocated, the new is not created.
938--error ER_UNKNOWN_STMT_HANDLER
939execute stmt;
940show status like 'prepared_stmt_count';
941--error ER_MAX_PREPARED_STMT_COUNT_REACHED
942prepare stmt from "select 1";
943show status like 'prepared_stmt_count';
944#
945# G. Show that the variables are up to date even after a connection with all
946# statements in it was terminated.
947#
948set global max_prepared_stmt_count=3;
949select @@max_prepared_stmt_count;
950show status like 'prepared_stmt_count';
951prepare stmt from "select 1";
952
953connect (con1,localhost,root,,);
954
955# Switch to connection con1
956connection con1;
957let $con1_id=`SELECT CONNECTION_ID()`;
958
959prepare stmt from "select 2";
960prepare stmt1 from "select 3";
961--error ER_MAX_PREPARED_STMT_COUNT_REACHED
962prepare stmt2 from "select 4";
963connection default;
964--error ER_MAX_PREPARED_STMT_COUNT_REACHED
965prepare stmt2 from "select 4";
966select @@max_prepared_stmt_count;
967show status like 'prepared_stmt_count';
968
969# Disconnect connection con1 and switch to default connection
970disconnect con1;
971connection default;
972
973# Wait for the connection con1 to die
974let $wait_condition=SELECT COUNT(*)=0 FROM information_schema.processlist WHERE id=$con1_id;
975--source include/wait_condition.inc
976
977deallocate prepare stmt;
978
979select @@max_prepared_stmt_count;
980show status like 'prepared_stmt_count';
981#
982# Restore the old value.
983#
984set global max_prepared_stmt_count= @old_max_prepared_stmt_count;
985--enable_ps_protocol
986
987
988#
989# Bug#19399 "Stored Procedures 'Lost Connection' when dropping/creating
990#            tables"
991# Check that multi-delete tables are also cleaned up before re-execution.
992#
993--disable_warnings
994drop table if exists t1;
995create temporary table if not exists t1 (a1 int);
996--enable_warnings
997# exact delete syntax is essential
998prepare stmt from "delete t1 from t1 where (cast(a1/3 as unsigned) * 3) = a1";
999drop temporary table t1;
1000create temporary table if not exists t1 (a1 int);
1001# the server crashed on  the next statement without the fix
1002execute stmt;
1003drop temporary table t1;
1004create temporary table if not exists t1 (a1 int);
1005# the problem was in memory corruption: repeat the test just in case
1006execute stmt;
1007drop temporary table t1;
1008create temporary table if not exists t1 (a1 int);
1009execute stmt;
1010drop temporary table t1;
1011deallocate prepare stmt;
1012
1013
1014#
1015# BUG#22085: Crash on the execution of a prepared statement that
1016#            uses an IN subquery with aggregate functions in HAVING
1017#
1018
1019CREATE TABLE t1(
1020  ID int(10) unsigned NOT NULL auto_increment,
1021  Member_ID varchar(15) NOT NULL default '',
1022  Action varchar(12) NOT NULL,
1023  Action_Date datetime NOT NULL,
1024  Track varchar(15) default NULL,
1025  User varchar(12) default NULL,
1026  Date_Updated timestamp NOT NULL default CURRENT_TIMESTAMP on update
1027    CURRENT_TIMESTAMP,
1028  PRIMARY KEY (ID),
1029  KEY Action (Action),
1030  KEY Action_Date (Action_Date)
1031);
1032
1033INSERT INTO t1(Member_ID, Action, Action_Date, Track) VALUES
1034  ('111111', 'Disenrolled', '2006-03-01', 'CAD' ),
1035  ('111111', 'Enrolled', '2006-03-01', 'CAD' ),
1036  ('111111', 'Disenrolled', '2006-07-03', 'CAD' ),
1037  ('222222', 'Enrolled', '2006-03-07', 'CAD' ),
1038  ('222222', 'Enrolled', '2006-03-07', 'CHF' ),
1039  ('222222', 'Disenrolled', '2006-08-02', 'CHF' ),
1040  ('333333', 'Enrolled', '2006-03-01', 'CAD' ),
1041  ('333333', 'Disenrolled', '2006-03-01', 'CAD' ),
1042  ('444444', 'Enrolled', '2006-03-01', 'CAD' ),
1043  ('555555', 'Disenrolled', '2006-03-01', 'CAD' ),
1044  ('555555', 'Enrolled', '2006-07-21', 'CAD' ),
1045  ('555555', 'Disenrolled', '2006-03-01', 'CHF' ),
1046  ('666666', 'Enrolled', '2006-02-09', 'CAD' ),
1047  ('666666', 'Enrolled', '2006-05-12', 'CHF' ),
1048  ('666666', 'Disenrolled', '2006-06-01', 'CAD' );
1049
1050PREPARE STMT FROM
1051"SELECT GROUP_CONCAT(Track SEPARATOR ', ') FROM t1
1052  WHERE Member_ID=? AND Action='Enrolled' AND
1053        (Track,Action_Date) IN (SELECT Track, MAX(Action_Date) FROM t1
1054                                  WHERE Member_ID=?
1055                                    GROUP BY Track
1056                                      HAVING Track>='CAD' AND
1057                                             MAX(Action_Date)>'2006-03-01')";
1058SET @id='111111';
1059EXECUTE STMT USING @id,@id;
1060SET @id='222222';
1061EXECUTE STMT USING @id,@id;
1062
1063DEALLOCATE PREPARE STMT;
1064DROP TABLE t1;
1065
1066#
1067# BUG#21354: (COUNT(*) = 1) not working in SELECT inside prepared
1068# statement
1069#
1070--disable_warnings
1071DROP TABLE IF EXISTS t1;
1072--enable_warnings
1073
1074CREATE TABLE t1 (i INT, INDEX(i));
1075INSERT INTO t1 VALUES (1);
1076
1077PREPARE stmt FROM "SELECT (COUNT(i) = 1), COUNT(i) FROM t1 WHERE i = ?";
1078SET @a = 0;
1079EXECUTE stmt USING @a;
1080SET @a = 1;
1081EXECUTE stmt USING @a;
1082SET @a = 0;
1083EXECUTE stmt USING @a;
1084
1085PREPARE stmt FROM "SELECT (AVG(i) = 1), AVG(i) FROM t1 WHERE i = ?";
1086SET @a = 0;
1087EXECUTE stmt USING @a;
1088SET @a = 1;
1089EXECUTE stmt USING @a;
1090SET @a = 0;
1091EXECUTE stmt USING @a;
1092
1093PREPARE stmt FROM "SELECT (VARIANCE(i) = 1), VARIANCE(i) FROM t1 WHERE i = ?";
1094SET @a = 0;
1095EXECUTE stmt USING @a;
1096SET @a = 1;
1097EXECUTE stmt USING @a;
1098SET @a = 0;
1099EXECUTE stmt USING @a;
1100
1101PREPARE stmt FROM "SELECT (STDDEV(i) = 1), STDDEV(i) FROM t1 WHERE i = ?";
1102SET @a = 0;
1103EXECUTE stmt USING @a;
1104SET @a = 1;
1105EXECUTE stmt USING @a;
1106SET @a = 0;
1107EXECUTE stmt USING @a;
1108
1109PREPARE stmt FROM "SELECT (BIT_OR(i) = 1), BIT_OR(i) FROM t1 WHERE i = ?";
1110SET @a = 0;
1111EXECUTE stmt USING @a;
1112SET @a = 1;
1113EXECUTE stmt USING @a;
1114SET @a = 0;
1115EXECUTE stmt USING @a;
1116
1117PREPARE stmt FROM "SELECT (BIT_AND(i) = 1), BIT_AND(i) FROM t1 WHERE i = ?";
1118SET @a = 0;
1119EXECUTE stmt USING @a;
1120SET @a = 1;
1121EXECUTE stmt USING @a;
1122SET @a = 0;
1123EXECUTE stmt USING @a;
1124
1125PREPARE stmt FROM "SELECT (BIT_XOR(i) = 1), BIT_XOR(i) FROM t1 WHERE i = ?";
1126SET @a = 0;
1127EXECUTE stmt USING @a;
1128SET @a = 1;
1129EXECUTE stmt USING @a;
1130SET @a = 0;
1131EXECUTE stmt USING @a;
1132
1133DEALLOCATE PREPARE stmt;
1134DROP TABLE t1;
1135
1136#
1137# Bug#19182: CREATE TABLE bar (m INT) SELECT n FROM foo; doesn't work
1138# from stored procedure.
1139#
1140# The cause of a bug was that cached LEX::create_list was modified,
1141# and then together with LEX::key_list was reset.
1142#
1143
1144CREATE TABLE t1 (i INT);
1145
1146PREPARE st_19182
1147FROM "CREATE TABLE t2 (i INT, j INT, KEY (i), KEY(j)) SELECT i FROM t1";
1148
1149EXECUTE st_19182;
1150DESC t2;
1151
1152DROP TABLE t2;
1153
1154# Check that on second execution we don't loose 'j' column and the keys
1155# on 'i' and 'j' columns.
1156EXECUTE st_19182;
1157DESC t2;
1158
1159DEALLOCATE PREPARE st_19182;
1160DROP TABLE t2, t1;
1161
1162#
1163# Bug #22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
1164#
1165# Code which implemented CREATE/ALTER TABLE and CREATE DATABASE
1166# statement modified HA_CREATE_INFO structure in LEX, making these
1167# statements PS/SP-unsafe (their re-execution might have resulted
1168# in incorrect results).
1169#
1170--disable_warnings
1171drop database if exists mysqltest;
1172drop table if exists t1, t2;
1173--enable_warnings
1174# CREATE TABLE and CREATE TABLE ... SELECT
1175create database mysqltest character set utf8;
1176prepare stmt1 from "create table mysqltest.t1 (c char(10))";
1177prepare stmt2 from "create table mysqltest.t2 select 'test'";
1178execute stmt1;
1179execute stmt2;
1180show create table mysqltest.t1;
1181show create table mysqltest.t2;
1182drop table mysqltest.t1;
1183drop table mysqltest.t2;
1184alter database mysqltest character set latin1;
1185execute stmt1;
1186execute stmt2;
1187show create table mysqltest.t1;
1188show create table mysqltest.t2;
1189drop database mysqltest;
1190deallocate prepare stmt1;
1191deallocate prepare stmt2;
1192#
1193# CREATE TABLE with DATA DIRECTORY option
1194#
1195--disable_warnings
1196--disable_query_log ONCE
1197eval prepare stmt from "create table t1 (c char(10)) data directory='$MYSQLTEST_VARDIR/tmp'";
1198execute stmt;
1199#
1200# DATA DIRECTORY option does not always work: if the operating
1201# system does not support symlinks, have_symlinks option is automatically
1202# disabled.
1203# In this case DATA DIRECTORY is silently ignored when
1204# creating a table, and is not output by SHOW CREATE TABLE.
1205#
1206--disable_result_log ONCE
1207show create table t1;
1208drop table t1;
1209execute stmt;
1210--disable_result_log ONCE
1211show create table t1;
1212drop table t1;
1213deallocate prepare stmt;
1214#
1215
1216#
1217# Bug #27937: crash on the second execution for prepared statement
1218#             from UNION with ORDER BY an expression containing RAND()
1219#
1220
1221CREATE TABLE t1(a int);
1222INSERT INTO t1 VALUES (2), (3), (1);
1223
1224PREPARE st1 FROM
1225  '(SELECT a FROM t1) UNION (SELECT a+10 FROM t1) ORDER BY RAND()*0+a';
1226
1227EXECUTE st1;
1228EXECUTE st1;
1229
1230DEALLOCATE PREPARE st1;
1231DROP TABLE t1;
1232
1233
1234#
1235# Bug #32137: prepared statement crash with str_to_date in update clause
1236#
1237create table t1 (a int, b tinyint);
1238prepare st1 from 'update t1 set b= (str_to_date(a, a))';
1239execute st1;
1240deallocate prepare st1;
1241drop table t1;
1242
1243--echo End of 4.1 tests.
1244
1245############################# 5.0 tests start ################################
1246#
1247#
1248# Bug#6102 "Server crash with prepared statement and blank after
1249# function name"
1250# ensure that stored functions are cached when preparing a statement
1251# before we open tables
1252#
1253create table t1 (a varchar(20));
1254insert into t1 values ('foo');
1255prepare stmt FROM 'SELECT char_length (a) FROM t1';
1256-- error ER_SP_DOES_NOT_EXIST
1257prepare stmt2 FROM 'SELECT not_a_function (a) FROM t1';
1258drop table t1;
1259
1260#
1261# Bug#8115: equality propagation and prepared statements
1262#
1263
1264create table t1 (a char(3) not null, b char(3) not null,
1265                 c char(3) not null, primary key  (a, b, c));
1266create table t2 like t1;
1267
1268# reduced query
1269prepare stmt from
1270  "select t1.a from (t1 left outer join t2 on t2.a=1 and t1.b=t2.b)
1271  where t1.a=1";
1272execute stmt;
1273execute stmt;
1274execute stmt;
1275
1276# original query
1277prepare stmt from
1278"select t1.a, t1.b, t1.c, t2.a, t2.b, t2.c from
1279(t1 left outer join t2 on t2.a=? and t1.b=t2.b)
1280left outer join t2 t3 on t3.a=? where t1.a=?";
1281
1282set @a:=1, @b:=1, @c:=1;
1283
1284execute stmt using @a, @b, @c;
1285execute stmt using @a, @b, @c;
1286execute stmt using @a, @b, @c;
1287
1288deallocate prepare stmt;
1289
1290drop table t1,t2;
1291
1292
1293#
1294# Bug#9383: INFORMATION_SCHEMA.COLUMNS, JOIN, Crash, prepared statement
1295#
1296
1297eval SET @aux= "SELECT COUNT(*)
1298                FROM INFORMATION_SCHEMA.COLUMNS A,
1299                INFORMATION_SCHEMA.COLUMNS B
1300                WHERE A.TABLE_SCHEMA = B.TABLE_SCHEMA
1301                AND A.TABLE_NAME = B.TABLE_NAME
1302                AND A.COLUMN_NAME = B.COLUMN_NAME AND
1303                A.TABLE_NAME = 'user'";
1304
1305let $exec_loop_count= 3;
1306eval prepare my_stmt from @aux;
1307while ($exec_loop_count)
1308{
1309  eval execute my_stmt;
1310  dec $exec_loop_count;
1311}
1312deallocate prepare my_stmt;
1313
1314# Test CALL in prepared mode
1315delimiter |;
1316--disable_warnings
1317drop procedure if exists p1|
1318drop table if exists t1|
1319--enable_warnings
1320create table t1 (id int)|
1321insert into t1 values(1)|
1322create procedure p1(a int, b int)
1323begin
1324  declare c int;
1325  select max(id)+1 into c from t1;
1326  insert into t1 select a+b;
1327  insert into t1 select a-b;
1328  insert into t1 select a-c;
1329end|
1330set @a= 3, @b= 4|
1331prepare stmt from "call p1(?, ?)"|
1332execute stmt using @a, @b|
1333execute stmt using @a, @b|
1334select * from t1|
1335deallocate prepare stmt|
1336drop procedure p1|
1337drop table t1|
1338delimiter ;|
1339
1340
1341#
1342# Bug#7306 LIMIT ?, ? and also WL#1785 " Prepared statements: implement
1343# support for placeholders in LIMIT clause."
1344# Add basic test coverage for the feature.
1345#
1346create table t1 (a int);
1347insert into t1 (a) values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
1348prepare stmt from "select * from t1 limit ?, ?";
1349set @offset=0, @limit=1;
1350execute stmt using @offset, @limit;
1351select * from t1 limit 0, 1;
1352set @offset=3, @limit=2;
1353execute stmt using @offset, @limit;
1354select * from t1 limit 3, 2;
1355prepare stmt from "select * from t1 limit ?";
1356execute stmt using @limit;
1357--error 1235
1358prepare stmt from "select * from t1 where a in (select a from t1 limit ?)";
1359prepare stmt from "select * from t1 union all select * from t1 limit ?, ?";
1360set @offset=9;
1361set @limit=2;
1362execute stmt using @offset, @limit;
1363prepare stmt from "(select * from t1 limit ?, ?) union all
1364                   (select * from t1 limit ?, ?) order by a limit ?";
1365execute stmt using @offset, @limit, @offset, @limit, @limit;
1366
1367drop table t1;
1368deallocate prepare stmt;
1369
1370#
1371# Bug#12651
1372# (Crash on a PS including a subquery which is a select from a simple view)
1373#
1374CREATE TABLE b12651_T1(a int) ENGINE=MYISAM;
1375CREATE TABLE b12651_T2(b int) ENGINE=MYISAM;
1376CREATE VIEW  b12651_V1 as SELECT b FROM b12651_T2;
1377
1378PREPARE b12651 FROM 'SELECT 1 FROM b12651_T1 WHERE a IN (SELECT b FROM b12651_V1)';
1379EXECUTE b12651;
1380
1381DROP VIEW b12651_V1;
1382DROP TABLE b12651_T1, b12651_T2;
1383DEALLOCATE PREPARE b12651;
1384
1385
1386
1387#
1388# Bug #14956: ROW_COUNT() returns incorrect result after EXECUTE of prepared
1389# statement
1390#
1391create table t1 (id int);
1392prepare ins_call from "insert into t1 (id) values (1)";
1393execute ins_call;
1394select row_count();
1395drop table t1;
1396
1397#
1398# BUG#16474: SP crashed MySQL
1399# (when using "order by localvar", where 'localvar' is just that.
1400# The actual bug test is in sp.test, this is just testing that we get the
1401# expected result for prepared statements too, i.e. place holders work as
1402# textual substitution. If it's a single integer, it works as the (deprecated)
1403# "order by column#", otherwise it's an expression.
1404#
1405create table t1 (a int, b int);
1406insert into t1 (a,b) values (2,8),(1,9),(3,7);
1407
1408# Will order by index
1409prepare stmt from "select * from t1 order by ?";
1410set @a=NULL;
1411execute stmt using @a;
1412set @a=1;
1413execute stmt using @a;
1414set @a=2;
1415execute stmt using @a;
1416deallocate prepare stmt;
1417# For reference:
1418select * from t1 order by 1;
1419
1420# Will not order by index.
1421prepare stmt from "select * from t1 order by ?+1";
1422set @a=0;
1423execute stmt using @a;
1424set @a=1;
1425execute stmt using @a;
1426deallocate prepare stmt;
1427# For reference:
1428select * from t1 order by 1+1;
1429
1430drop table t1;
1431
1432#
1433# Bug#19308 "REPAIR/OPTIMIZE/ANALYZE supported in SP but not in PS".
1434# Add test coverage for the added commands.
1435#
1436create table t1 (a int) engine=myisam;
1437create table t2 like t1;
1438create table t3 like t2;
1439prepare stmt from "repair table t1";
1440execute stmt;
1441execute stmt;
1442prepare stmt from "optimize table t1";
1443execute stmt;
1444execute stmt;
1445prepare stmt from "analyze table t1";
1446execute stmt;
1447execute stmt;
1448prepare stmt from "repair table t1, t2, t3";
1449execute stmt;
1450execute stmt;
1451prepare stmt from "optimize table t1, t2, t3";
1452execute stmt;
1453execute stmt;
1454prepare stmt from "analyze table t1, t2, t3";
1455execute stmt;
1456execute stmt;
1457prepare stmt from "repair table t1, t4, t3";
1458execute stmt;
1459execute stmt;
1460prepare stmt from "optimize table t1, t3, t4";
1461execute stmt;
1462execute stmt;
1463prepare stmt from "analyze table t4, t1";
1464execute stmt;
1465execute stmt;
1466deallocate prepare stmt;
1467drop table t1, t2, t3;
1468
1469#
1470# Bug#17199 "Table not found" error occurs if the query contains a call
1471#            to a function from another database.
1472#            Test prepared statements- related behaviour.
1473#
1474#
1475# ALTER TABLE RENAME and Prepared Statements: wrong DB name buffer was used
1476# in ALTER ... RENAME which caused memory corruption in prepared statements.
1477# No need to fix this problem in 4.1 as ALTER TABLE is not allowed in
1478# Prepared Statements in 4.1.
1479#
1480create database mysqltest_long_database_name_to_thrash_heap;
1481use test;
1482create table t1 (i int);
1483prepare stmt from "alter table test.t1 rename t1";
1484use mysqltest_long_database_name_to_thrash_heap;
1485execute stmt;
1486show tables like 't1';
1487prepare stmt from "alter table test.t1 rename t1";
1488use test;
1489execute stmt;
1490show tables like 't1';
1491use mysqltest_long_database_name_to_thrash_heap;
1492show tables like 't1';
1493deallocate prepare stmt;
1494#
1495# Check that a prepared statement initializes its current database at
1496# PREPARE, and then works correctly even if the current database has been
1497# changed.
1498#
1499use mysqltest_long_database_name_to_thrash_heap;
1500# Necessary for preparation of INSERT/UPDATE/DELETE to succeed
1501prepare stmt_create from "create table t1 (i int)";
1502prepare stmt_insert from "insert into t1 (i) values (1)";
1503prepare stmt_update from "update t1 set i=2";
1504prepare stmt_delete from "delete from t1 where i=2";
1505prepare stmt_select from "select * from t1";
1506prepare stmt_alter from "alter table t1 add column (b int)";
1507prepare stmt_alter1 from "alter table t1 drop column b";
1508prepare stmt_analyze from "analyze table t1";
1509prepare stmt_optimize from "optimize table t1";
1510prepare stmt_show from "show tables like 't1'";
1511prepare stmt_truncate from "truncate table t1";
1512prepare stmt_drop from "drop table t1";
1513# Drop the table that was used to prepare INSERT/UPDATE/DELETE: we will
1514# create a new one by executing stmt_create
1515drop table t1;
1516# Switch the current database
1517use test;
1518# Check that all prepared statements operate on the database that was
1519# active at PREPARE
1520execute stmt_create;
1521# should return empty set
1522show tables like 't1';
1523use mysqltest_long_database_name_to_thrash_heap;
1524show tables like 't1';
1525use test;
1526execute stmt_insert;
1527select * from mysqltest_long_database_name_to_thrash_heap.t1;
1528execute stmt_update;
1529select * from mysqltest_long_database_name_to_thrash_heap.t1;
1530execute stmt_delete;
1531execute stmt_select;
1532execute stmt_alter;
1533show columns from mysqltest_long_database_name_to_thrash_heap.t1;
1534execute stmt_alter1;
1535show columns from mysqltest_long_database_name_to_thrash_heap.t1;
1536execute stmt_analyze;
1537execute stmt_optimize;
1538execute stmt_show;
1539execute stmt_truncate;
1540execute stmt_drop;
1541show tables like 't1';
1542use mysqltest_long_database_name_to_thrash_heap;
1543show tables like 't1';
1544#
1545# Attempt a statement PREPARE when there is no current database:
1546# is expected to return an error.
1547#
1548drop database mysqltest_long_database_name_to_thrash_heap;
1549--error ER_NO_DB_ERROR
1550prepare stmt_create from "create table t1 (i int)";
1551--error ER_NO_DB_ERROR
1552prepare stmt_insert from "insert into t1 (i) values (1)";
1553--error ER_NO_DB_ERROR
1554prepare stmt_update from "update t1 set i=2";
1555--error ER_NO_DB_ERROR
1556prepare stmt_delete from "delete from t1 where i=2";
1557--error ER_NO_DB_ERROR
1558prepare stmt_select from "select * from t1";
1559--error ER_NO_DB_ERROR
1560prepare stmt_alter from "alter table t1 add column (b int)";
1561--error ER_NO_DB_ERROR
1562prepare stmt_alter1 from "alter table t1 drop column b";
1563--error ER_NO_DB_ERROR
1564prepare stmt_analyze from "analyze table t1";
1565--error ER_NO_DB_ERROR
1566prepare stmt_optimize from "optimize table t1";
1567--error ER_NO_DB_ERROR
1568prepare stmt_show from "show tables like 't1'";
1569--error ER_NO_DB_ERROR
1570prepare stmt_truncate from "truncate table t1";
1571--error ER_NO_DB_ERROR
1572prepare stmt_drop from "drop table t1";
1573#
1574# The above has automatically deallocated all our statements.
1575#
1576# Attempt to CREATE a temporary table when no DB used: it should fail
1577# This proves that no table can be used without explicit specification of
1578# its database if there is no current database.
1579#
1580--error ER_NO_DB_ERROR
1581create temporary table t1 (i int);
1582#
1583# Restore the old environemnt
1584#
1585use test;
1586
1587
1588#
1589# BUG#21166: Prepared statement causes signal 11 on second execution
1590#
1591# Changes in an item tree done by optimizer weren't properly
1592# registered and went unnoticed, which resulted in preliminary freeing
1593# of used memory.
1594#
1595
1596CREATE TABLE t1 (i BIGINT, j BIGINT);
1597CREATE TABLE t2 (i BIGINT);
1598CREATE TABLE t3 (i BIGINT, j BIGINT);
1599
1600PREPARE stmt FROM "SELECT * FROM t1 JOIN t2 ON (t2.i = t1.i)
1601                   LEFT JOIN t3 ON ((t3.i, t3.j) = (t1.i, t1.j))
1602                   WHERE t1.i = ?";
1603
1604SET @a= 1;
1605EXECUTE stmt USING @a;
1606EXECUTE stmt USING @a;
1607
1608DEALLOCATE PREPARE stmt;
1609DROP TABLE IF EXISTS t1, t2, t3;
1610
1611
1612#
1613# BUG#21081: SELECT inside stored procedure returns wrong results
1614#
1615
1616CREATE TABLE t1 (i INT KEY);
1617CREATE TABLE t2 (i INT);
1618
1619INSERT INTO t1 VALUES (1), (2);
1620INSERT INTO t2 VALUES (1);
1621
1622PREPARE stmt FROM "SELECT t2.i FROM t1 LEFT JOIN t2 ON t2.i = t1.i
1623                   WHERE t1.i = ?";
1624
1625SET @arg= 1;
1626EXECUTE stmt USING @arg;
1627SET @arg= 2;
1628EXECUTE stmt USING @arg;
1629SET @arg= 1;
1630EXECUTE stmt USING @arg;
1631
1632DEALLOCATE PREPARE stmt;
1633DROP TABLE t1, t2;
1634
1635
1636#
1637# BUG#20327: Marking of a wrong field leads to a wrong result on select with
1638#            view, prepared statement and subquery.
1639#
1640CREATE TABLE t1 (i INT);
1641CREATE VIEW v1 AS SELECT * FROM t1;
1642
1643INSERT INTO t1 VALUES (1), (2);
1644
1645let $query = SELECT t1.i FROM t1 JOIN v1 ON t1.i = v1.i
1646             WHERE EXISTS (SELECT * FROM t1 WHERE v1.i = 1);
1647eval $query;
1648eval PREPARE stmt FROM "$query";
1649# Statement execution should return '1'.
1650EXECUTE stmt;
1651# Check re-execution.
1652EXECUTE stmt;
1653
1654DEALLOCATE PREPARE stmt;
1655DROP VIEW v1;
1656DROP TABLE t1;
1657
1658
1659#
1660# BUG#21856: Prepared Statments: crash if bad create
1661#
1662
1663let $iterations= 100;
1664--disable_query_log
1665--disable_result_log
1666while ($iterations)
1667{
1668  --error ER_PARSE_ERROR
1669  PREPARE stmt FROM "CREATE PROCEDURE p1()";
1670  dec $iterations;
1671}
1672--enable_query_log
1673--enable_result_log
1674
1675#
1676# Bug 25027: query with a single-row non-correlated subquery
1677#            and IS NULL predicate
1678#
1679
1680CREATE TABLE t1 (a int);
1681INSERT INTO t1 VALUES (1), (2);
1682CREATE TABLE t2 (b int);
1683INSERT INTO t2 VALUES (NULL);
1684
1685SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL;
1686PREPARE stmt FROM 'SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL';
1687
1688EXECUTE stmt;
1689DEALLOCATE PREPARE stmt;
1690
1691PREPARE stmt FROM 'SELECT a FROM t1 WHERE (SELECT b FROM t2 limit ?) IS NULL';
1692SET @arg=1;
1693EXECUTE stmt USING @arg;
1694DEALLOCATE PREPARE stmt;
1695
1696DROP TABLE t1,t2;
1697#
1698# Bug#4968 "Stored procedure crash if cursor opened on altered table"
1699# The bug is not repeatable any more after the fix for
1700# Bug#15217 "Bug #15217   Using a SP cursor on a table created with PREPARE
1701# fails with weird error", however ALTER TABLE is not re-execution friendly
1702# and that caused a valgrind warning. Check that the warning is gone.
1703#
1704create table t1 (s1 char(20));
1705prepare stmt from "alter table t1 modify s1 int";
1706execute stmt;
1707execute stmt;
1708drop table t1;
1709deallocate prepare stmt;
1710
1711#
1712# Bug#6895 "Prepared Statements: ALTER TABLE DROP COLUMN does nothing"
1713#
1714create table t1 (a int, b int);
1715prepare s_6895 from "alter table t1 drop column b";
1716execute s_6895;
1717show columns from t1;
1718drop table t1;
1719create table t1 (a int, b int);
1720execute s_6895;
1721show columns from t1;
1722drop table t1;
1723create table t1 (a int, b int);
1724execute s_6895;
1725show columns from t1;
1726deallocate prepare s_6895;
1727drop table t1;
1728
1729#
1730# Bug #22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
1731#
1732# 5.0 part of the test.
1733#
1734
1735# ALTER TABLE
1736create table t1 (i int primary key auto_increment) comment='comment for table t1';
1737create table t2 (i int, j int, k int);
1738prepare stmt from "alter table t1 auto_increment=100";
1739execute stmt;
1740show create table t1;
1741# Let us trash table-cache's memory
1742flush tables;
1743select * from t2;
1744execute stmt;
1745show create table t1;
1746deallocate prepare stmt;
1747drop table t1, t2;
1748# 5.1 part of the test.
1749# CREATE DATABASE
1750set @old_character_set_server= @@character_set_server;
1751set @@character_set_server= latin1;
1752prepare stmt from "create database mysqltest_1";
1753execute stmt;
1754show create database mysqltest_1;
1755drop database mysqltest_1;
1756set @@character_set_server= utf8;
1757execute stmt;
1758show create database mysqltest_1;
1759drop database mysqltest_1;
1760deallocate prepare stmt;
1761set @@character_set_server= @old_character_set_server;
1762
1763
1764#
1765# BUG#24491 "using alias from source table in insert ... on duplicate key"
1766#
1767create table t1 (id int primary key auto_increment, value varchar(10));
1768insert into t1 (id, value) values (1, 'FIRST'), (2, 'SECOND'), (3, 'THIRD');
1769# Prepare INSERT ... SELECT ... ON DUPLICATE KEY UPDATE statement
1770# which in its ON DUPLICATE KEY clause erroneously tries to assign value
1771# to a column which is mentioned only in SELECT part. This is now caught
1772# during preparation.
1773--error ER_BAD_FIELD_ERROR
1774prepare stmt from "insert into t1 (id, value) select * from (select 4 as i, 'FOURTH' as v) as y on duplicate key update v = 'DUP'";
1775#
1776# And now the same test for more complex case which is more close
1777# to the one that was reported originally.
1778--error ER_BAD_FIELD_ERROR
1779prepare stmt from "insert into t1 (id, value) select * from (select 4 as id, 'FOURTH' as value) as y on duplicate key update y.value = 'DUP'";
1780drop tables t1;
1781
1782#
1783# Bug #28509: strange behaviour: passing a decimal value to PS
1784#
1785prepare stmt from "create table t1 select ?";
1786set @a=1.0;
1787execute stmt using @a;
1788show create table t1;
1789drop table t1;
1790
1791#
1792# Bug#33798: prepared statements improperly handle large unsigned ints
1793#
1794create table t1 (a bigint unsigned, b bigint(20) unsigned);
1795prepare stmt from "insert into t1 values (?,?)";
1796set @a= 9999999999999999;
1797set @b= 14632475938453979136;
1798insert into t1 values (@a, @b);
1799select * from t1 where a = @a and b = @b;
1800execute stmt using @a, @b;
1801select * from t1 where a = @a and b = @b;
1802deallocate prepare stmt;
1803drop table t1;
1804
1805#
1806# Bug#32890 Crash after repeated create and drop of tables and views
1807#
1808
1809--disable_warnings
1810drop view if exists v1;
1811drop table if exists t1;
1812--enable_warnings
1813
1814create table t1 (a int, b int);
1815insert into t1 values (1,1), (2,2), (3,3);
1816insert into t1 values (3,1), (1,2), (2,3);
1817
1818prepare stmt from "create view v1 as select * from t1";
1819execute stmt;
1820drop table t1;
1821create table t1 (a int, b int);
1822drop view v1;
1823execute stmt;
1824show create view v1;
1825drop view v1;
1826
1827prepare stmt from "create view v1 (c,d) as select a,b from t1";
1828execute stmt;
1829show create view v1;
1830select * from v1;
1831drop view v1;
1832execute stmt;
1833deallocate prepare stmt;
1834show create view v1;
1835select * from v1;
1836drop view v1;
1837
1838prepare stmt from "create view v1 (c) as select b+1 from t1";
1839execute stmt;
1840show create view v1;
1841select * from v1;
1842drop view v1;
1843execute stmt;
1844deallocate prepare stmt;
1845show create view v1;
1846select * from v1;
1847drop view v1;
1848
1849prepare stmt from "create view v1 (c,d,e,f) as select a,b,a in (select a+2 from t1), a = all (select a from t1) from t1";
1850execute stmt;
1851show create view v1;
1852select * from v1;
1853drop view v1;
1854execute stmt;
1855deallocate prepare stmt;
1856show create view v1;
1857select * from v1;
1858drop view v1;
1859
1860prepare stmt from "create or replace view v1 as select 1";
1861execute stmt;
1862show create view v1;
1863select * from v1;
1864execute stmt;
1865show create view v1;
1866deallocate prepare stmt;
1867show create view v1;
1868select * from v1;
1869drop view v1;
1870
1871prepare stmt from "create view v1 as select 1, 1";
1872execute stmt;
1873show create view v1;
1874select * from v1;
1875drop view v1;
1876execute stmt;
1877deallocate prepare stmt;
1878show create view v1;
1879select * from v1;
1880drop view v1;
1881
1882prepare stmt from "create view v1 (x) as select a from t1 where a > 1";
1883execute stmt;
1884show create view v1;
1885select * from v1;
1886drop view v1;
1887execute stmt;
1888deallocate prepare stmt;
1889show create view v1;
1890select * from v1;
1891drop view v1;
1892
1893prepare stmt from "create view v1 as select * from `t1` `b`";
1894execute stmt;
1895show create view v1;
1896select * from v1;
1897drop view v1;
1898execute stmt;
1899deallocate prepare stmt;
1900show create view v1;
1901select * from v1;
1902drop view v1;
1903
1904prepare stmt from "create view v1 (a,b,c) as select * from t1";
1905--error ER_VIEW_WRONG_LIST
1906execute stmt;
1907--error ER_VIEW_WRONG_LIST
1908execute stmt;
1909deallocate prepare stmt;
1910
1911drop table t1;
1912create temporary table t1 (a int, b int);
1913
1914prepare stmt from "create view v1 as select * from t1";
1915--error ER_VIEW_SELECT_TMPTABLE
1916execute stmt;
1917--error ER_VIEW_SELECT_TMPTABLE
1918execute stmt;
1919deallocate prepare stmt;
1920
1921drop table t1;
1922
1923--error ER_NO_SUCH_TABLE
1924prepare stmt from "create view v1 as select * from t1";
1925--error ER_NO_SUCH_TABLE
1926prepare stmt from "create view v1 as select * from `t1` `b`";
1927
1928#
1929# Bug#33851: Passing UNSIGNED param to EXECUTE returns ERROR 1210
1930#
1931
1932prepare stmt from "select ?";
1933set @arg= 123456789.987654321;
1934select @arg;
1935execute stmt using @arg;
1936set @arg= "string";
1937select @arg;
1938execute stmt using @arg;
1939set @arg= 123456;
1940select @arg;
1941execute stmt using @arg;
1942set @arg= cast(-12345.54321 as decimal(20, 10));
1943select @arg;
1944execute stmt using @arg;
1945deallocate prepare stmt;
1946
1947--echo #
1948--echo # Bug#48508: Crash on prepared statement re-execution.
1949--echo #
1950create table t1(b int);
1951insert into t1 values (0);
1952create view v1 AS select 1 as a from t1 where b;
1953prepare stmt from "select * from v1 where a";
1954execute stmt;
1955execute stmt;
1956deallocate prepare stmt;
1957drop table t1;
1958drop view v1;
1959
1960create table t1(a bigint);
1961create table t2(b tinyint);
1962insert into t2 values (null);
1963prepare stmt from "select 1 from t1 join  t2 on a xor b where b > 1  and a =1";
1964execute stmt;
1965execute stmt;
1966deallocate prepare stmt;
1967drop table t1,t2;
1968--echo #
1969
1970
1971--echo #
1972--echo # Bug #49570: Assertion failed: !(order->used & map)
1973--echo # on re-execution of prepared statement
1974--echo #
1975CREATE TABLE t1(a INT PRIMARY KEY);
1976INSERT INTO t1 VALUES(0), (1);
1977PREPARE stmt FROM
1978  "SELECT 1 FROM t1 JOIN t1 t2 USING(a) GROUP BY t2.a, t1.a";
1979EXECUTE stmt;
1980EXECUTE stmt;
1981EXECUTE stmt;
1982DEALLOCATE PREPARE stmt;
1983DROP TABLE t1;
1984
1985
1986--echo End of 5.0 tests.
1987
1988#
1989# Bug #20665: All commands supported in Stored Procedures should work in
1990# Prepared Statements
1991#
1992create procedure proc_1() reset master;
1993delimiter |;
1994--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
1995create function func_1() returns int begin reset master; return 1; end|
1996create function func_1() returns int begin call proc_1(); return 1; end|
1997delimiter ;|
1998--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
1999select func_1(), func_1(), func_1() from dual;
2000drop function func_1;
2001drop procedure proc_1;
2002prepare abc from "reset master";
2003execute abc;
2004execute abc;
2005execute abc;
2006deallocate prepare abc;
2007
2008
2009create procedure proc_1() reset slave;
2010call proc_1();
2011call proc_1();
2012call proc_1();
2013delimiter |;
2014--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2015create function func_1() returns int begin reset slave; return 1; end|
2016create function func_1() returns int begin call proc_1(); return 1; end|
2017delimiter ;|
2018--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2019select func_1(), func_1(), func_1() from dual;
2020drop function func_1;
2021drop procedure proc_1;
2022prepare abc from "reset slave";
2023execute abc;
2024execute abc;
2025execute abc;
2026deallocate prepare abc;
2027
2028
2029create procedure proc_1(a integer) kill a;
2030--error ER_NO_SUCH_THREAD
2031call proc_1(0);
2032--error ER_NO_SUCH_THREAD
2033call proc_1(0);
2034--error ER_NO_SUCH_THREAD
2035call proc_1(0);
2036drop procedure proc_1;
2037delimiter |;
2038create function func_1() returns int begin kill 0; return 1; end|
2039delimiter ;|
2040--error ER_NO_SUCH_THREAD
2041select func_1() from dual;
2042--error ER_NO_SUCH_THREAD
2043select func_1() from dual;
2044--error ER_NO_SUCH_THREAD
2045select func_1() from dual;
2046drop function func_1;
2047prepare abc from "kill 0";
2048--error ER_NO_SUCH_THREAD
2049execute abc;
2050--error ER_NO_SUCH_THREAD
2051execute abc;
2052--error ER_NO_SUCH_THREAD
2053execute abc;
2054deallocate prepare abc;
2055
2056
2057create procedure proc_1() flush hosts;
2058call proc_1();
2059call proc_1();
2060call proc_1();
2061call proc_1();
2062delimiter |;
2063--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2064create function func_1() returns int begin flush hosts; return 1; end|
2065create function func_1() returns int begin call proc_1(); return 1; end|
2066delimiter ;|
2067--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2068select func_1(), func_1(), func_1() from dual;
2069drop function func_1;
2070drop procedure proc_1;
2071prepare abc from "flush hosts";
2072execute abc;
2073execute abc;
2074execute abc;
2075deallocate prepare abc;
2076
2077
2078create procedure proc_1() flush privileges;
2079call proc_1();
2080call proc_1();
2081call proc_1();
2082delimiter |;
2083--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2084create function func_1() returns int begin flush privileges; return 1; end|
2085create function func_1() returns int begin call proc_1(); return 1; end|
2086delimiter ;|
2087--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2088select func_1(), func_1(), func_1() from dual;
2089drop function func_1;
2090drop procedure proc_1;
2091prepare abc from "flush privileges";
2092deallocate prepare abc;
2093
2094
2095create procedure proc_1() flush tables with read lock;
2096call proc_1();
2097unlock tables;
2098call proc_1();
2099unlock tables;
2100call proc_1();
2101unlock tables;
2102delimiter |;
2103--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2104create function func_1() returns int begin flush tables with read lock; return 1; end|
2105create function func_1() returns int begin call proc_1(); return 1; end|
2106delimiter ;|
2107--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2108select func_1(), func_1(), func_1() from dual;
2109drop function func_1;
2110drop procedure proc_1;
2111prepare abc from "flush tables with read lock";
2112execute abc;
2113execute abc;
2114execute abc;
2115deallocate prepare abc;
2116unlock tables;
2117
2118
2119create procedure proc_1() flush tables;
2120call proc_1();
2121call proc_1();
2122call proc_1();
2123delimiter |;
2124--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2125create function func_1() returns int begin flush tables; return 1; end|
2126create function func_1() returns int begin call proc_1(); return 1; end|
2127delimiter ;|
2128--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2129select func_1(), func_1(), func_1() from dual;
2130drop function func_1;
2131drop procedure proc_1;
2132prepare abc from "flush tables";
2133execute abc;
2134execute abc;
2135execute abc;
2136deallocate prepare abc;
2137
2138
2139create procedure proc_1() flush tables;
2140flush tables;
2141--sorted_result
2142show open tables from mysql;
2143select Host, User from mysql.user limit 0;
2144select Host, Db from mysql.db limit 0;
2145--sorted_result
2146show open tables from mysql;
2147call proc_1();
2148--sorted_result
2149show open tables from mysql;
2150select Host, User from mysql.user limit 0;
2151select Host, Db from mysql.db limit 0;
2152--sorted_result
2153show open tables from mysql;
2154call proc_1();
2155--sorted_result
2156show open tables from mysql;
2157select Host, User from mysql.user limit 0;
2158select Host, Db from mysql.db limit 0;
2159--sorted_result
2160show open tables from mysql;
2161call proc_1();
2162--sorted_result
2163show open tables from mysql;
2164select Host, User from mysql.user limit 0;
2165select Host, Db from mysql.db limit 0;
2166--sorted_result
2167show open tables from mysql;
2168flush tables;
2169delimiter |;
2170--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2171create function func_1() returns int begin flush tables; return 1; end|
2172create function func_1() returns int begin call proc_1(); return 1; end|
2173delimiter ;|
2174--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2175select func_1(), func_1(), func_1() from dual;
2176drop function func_1;
2177drop procedure proc_1;
2178
2179# make the output deterministic:
2180# the order used in SHOW OPEN TABLES
2181# is too much implementation dependent
2182--disable_ps_protocol
2183flush tables;
2184select Host, User from mysql.user limit 0;
2185select Host, Db from mysql.db limit 0;
2186--sorted_result
2187show open tables from mysql;
2188--enable_ps_protocol
2189
2190prepare abc from "flush tables";
2191execute abc;
2192--sorted_result
2193show open tables from mysql;
2194select Host, User from mysql.user limit 0;
2195select Host, Db from mysql.db limit 0;
2196--sorted_result
2197show open tables from mysql;
2198execute abc;
2199--sorted_result
2200show open tables from mysql;
2201select Host, User from mysql.user limit 0;
2202select Host, Db from mysql.db limit 0;
2203--sorted_result
2204show open tables from mysql;
2205execute abc;
2206--sorted_result
2207show open tables from mysql;
2208select Host, User from mysql.user limit 0;
2209select Host, Db from mysql.db limit 0;
2210--sorted_result
2211show open tables from mysql;
2212flush tables;
2213deallocate prepare abc;
2214
2215
2216create procedure proc_1() flush logs;
2217call proc_1();
2218call proc_1();
2219call proc_1();
2220delimiter |;
2221--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2222create function func_1() returns int begin flush logs; return 1; end|
2223create function func_1() returns int begin call proc_1(); return 1; end|
2224delimiter ;|
2225--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2226select func_1(), func_1(), func_1() from dual;
2227drop function func_1;
2228drop procedure proc_1;
2229prepare abc from "flush logs";
2230execute abc;
2231execute abc;
2232execute abc;
2233deallocate prepare abc;
2234
2235
2236create procedure proc_1() flush status;
2237call proc_1();
2238call proc_1();
2239call proc_1();
2240delimiter |;
2241--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2242create function func_1() returns int begin flush status; return 1; end|
2243create function func_1() returns int begin call proc_1(); return 1; end|
2244delimiter ;|
2245--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2246select func_1(), func_1(), func_1() from dual;
2247drop function func_1;
2248drop procedure proc_1;
2249prepare abc from "flush status";
2250execute abc;
2251execute abc;
2252execute abc;
2253deallocate prepare abc;
2254
2255
2256create procedure proc_1() flush user_resources;
2257call proc_1();
2258call proc_1();
2259call proc_1();
2260delimiter |;
2261--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2262create function func_1() returns int begin flush user_resources; return 1; end|
2263create function func_1() returns int begin call proc_1(); return 1; end|
2264delimiter ;|
2265--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
2266select func_1(), func_1(), func_1() from dual;
2267drop function func_1;
2268drop procedure proc_1;
2269prepare abc from "flush user_resources";
2270execute abc;
2271execute abc;
2272execute abc;
2273deallocate prepare abc;
2274
2275
2276create procedure proc_1() start slave;
2277drop procedure proc_1;
2278delimiter |;
2279--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2280create function func_1() returns int begin start slave; return 1; end|
2281delimiter ;|
2282
2283prepare abc from "start slave";
2284deallocate prepare abc;
2285
2286
2287create procedure proc_1() stop slave;
2288drop procedure proc_1;
2289delimiter |;
2290--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2291create function func_1() returns int begin stop slave; return 1; end|
2292delimiter ;|
2293
2294prepare abc from "stop slave";
2295deallocate prepare abc;
2296
2297
2298create procedure proc_1() show binlog events;
2299drop procedure proc_1;
2300delimiter |;
2301--error ER_SP_NO_RETSET
2302create function func_1() returns int begin show binlog events; return 1; end|
2303delimiter ;|
2304--error ER_SP_DOES_NOT_EXIST
2305select func_1(), func_1(), func_1() from dual;
2306--error ER_SP_DOES_NOT_EXIST
2307drop function func_1;
2308prepare abc from "show binlog events";
2309deallocate prepare abc;
2310
2311
2312create procedure proc_1() show slave status;
2313drop procedure proc_1;
2314delimiter |;
2315--error ER_SP_NO_RETSET
2316create function func_1() returns int begin show slave status; return 1; end|
2317delimiter ;|
2318--error ER_SP_DOES_NOT_EXIST
2319select func_1(), func_1(), func_1() from dual;
2320--error ER_SP_DOES_NOT_EXIST
2321drop function func_1;
2322prepare abc from "show slave status";
2323deallocate prepare abc;
2324
2325
2326create procedure proc_1() show master status;
2327drop procedure proc_1;
2328delimiter |;
2329--error ER_SP_NO_RETSET
2330create function func_1() returns int begin show master status; return 1; end|
2331delimiter ;|
2332--error ER_SP_DOES_NOT_EXIST
2333select func_1(), func_1(), func_1() from dual;
2334--error ER_SP_DOES_NOT_EXIST
2335drop function func_1;
2336prepare abc from "show master status";
2337deallocate prepare abc;
2338
2339
2340create procedure proc_1() show master logs;
2341drop procedure proc_1;
2342delimiter |;
2343--error ER_SP_NO_RETSET
2344create function func_1() returns int begin show master logs; return 1; end|
2345delimiter ;|
2346--error ER_SP_DOES_NOT_EXIST
2347select func_1(), func_1(), func_1() from dual;
2348--error ER_SP_DOES_NOT_EXIST
2349drop function func_1;
2350prepare abc from "show master logs";
2351deallocate prepare abc;
2352
2353
2354create procedure proc_1() show events;
2355call proc_1();
2356call proc_1();
2357call proc_1();
2358drop procedure proc_1;
2359delimiter |;
2360--error ER_SP_NO_RETSET
2361create function func_1() returns int begin show events; return 1; end|
2362delimiter ;|
2363--error ER_SP_DOES_NOT_EXIST
2364select func_1(), func_1(), func_1() from dual;
2365--error ER_SP_DOES_NOT_EXIST
2366drop function func_1;
2367prepare abc from "show events";
2368execute abc;
2369execute abc;
2370execute abc;
2371deallocate prepare abc;
2372
2373
2374create procedure a() select 42;
2375create procedure proc_1(a char(2)) show create procedure a;
2376call proc_1("bb");
2377call proc_1("bb");
2378call proc_1("bb");
2379drop procedure proc_1;
2380delimiter |;
2381--error ER_SP_NO_RETSET
2382create function func_1() returns int begin show create procedure a; return 1; end|
2383delimiter ;|
2384--error ER_SP_DOES_NOT_EXIST
2385select func_1(), func_1(), func_1() from dual;
2386--error ER_SP_DOES_NOT_EXIST
2387drop function func_1;
2388prepare abc from "show create procedure a";
2389execute abc;
2390execute abc;
2391execute abc;
2392deallocate prepare abc;
2393drop procedure a;
2394
2395
2396create function a() returns int return 42+13;
2397create procedure proc_1(a char(2)) show create function a;
2398call proc_1("bb");
2399call proc_1("bb");
2400call proc_1("bb");
2401drop procedure proc_1;
2402delimiter |;
2403--error ER_SP_NO_RETSET
2404create function func_1() returns int begin show create function a; return 1; end|
2405delimiter ;|
2406--error ER_SP_DOES_NOT_EXIST
2407select func_1(), func_1(), func_1() from dual;
2408--error ER_SP_DOES_NOT_EXIST
2409drop function func_1;
2410prepare abc from "show create function a";
2411execute abc;
2412execute abc;
2413execute abc;
2414deallocate prepare abc;
2415drop function a;
2416
2417
2418create table tab1(a int, b char(1), primary key(a,b));
2419create procedure proc_1() show create table tab1;
2420call proc_1();
2421call proc_1();
2422call proc_1();
2423drop procedure proc_1;
2424delimiter |;
2425--error ER_SP_NO_RETSET
2426create function func_1() returns int begin show create table tab1; return 1; end|
2427delimiter ;|
2428--error ER_SP_DOES_NOT_EXIST
2429select func_1(), func_1(), func_1() from dual;
2430--error ER_SP_DOES_NOT_EXIST
2431drop function func_1;
2432prepare abc from "show create table tab1";
2433execute abc;
2434execute abc;
2435execute abc;
2436deallocate prepare abc;
2437drop table tab1;
2438
2439
2440--disable_warnings
2441drop view if exists v1;
2442drop table if exists t1;
2443--enable_warnings
2444create table t1(a int, b char(5));
2445insert into t1 values (1, "one"), (1, "edno"), (2, "two"), (2, "dve");
2446create view v1 as
2447    (select a, count(*) from t1 group by a)
2448    union all
2449    (select b, count(*) from t1 group by b);
2450create procedure proc_1() show create view v1;
2451call proc_1();
2452call proc_1();
2453call proc_1();
2454drop procedure proc_1;
2455delimiter |;
2456--error ER_SP_NO_RETSET
2457create function func_1() returns int begin show create view v1; return 1; end|
2458delimiter ;|
2459--error ER_SP_DOES_NOT_EXIST
2460select func_1(), func_1(), func_1() from dual;
2461--error ER_SP_DOES_NOT_EXIST
2462drop function func_1;
2463prepare abc from "show create view v1";
2464execute abc;
2465execute abc;
2466execute abc;
2467deallocate prepare abc;
2468drop view v1;
2469drop table t1;
2470
2471
2472create procedure proc_1() install plugin my_plug soname 'some_plugin.so';
2473--replace_regex /(Can\'t open shared library).*$/\1/
2474--error ER_CANT_OPEN_LIBRARY,ER_FEATURE_DISABLED
2475call proc_1();
2476--replace_regex /(Can\'t open shared library).*$/\1/
2477--error ER_CANT_OPEN_LIBRARY,ER_FEATURE_DISABLED
2478call proc_1();
2479--replace_regex /(Can\'t open shared library).*$/\1/
2480--error ER_CANT_OPEN_LIBRARY,ER_FEATURE_DISABLED
2481call proc_1();
2482drop procedure proc_1;
2483delimiter |;
2484--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2485create function func_1() returns int begin install plugin my_plug soname '/tmp/plugin'; return 1; end|
2486delimiter ;|
2487--error ER_SP_DOES_NOT_EXIST
2488select func_1(), func_1(), func_1() from dual;
2489--error ER_SP_DOES_NOT_EXIST
2490drop function func_1;
2491prepare abc from "install plugin my_plug soname 'some_plugin.so'";
2492deallocate prepare abc;
2493
2494
2495create procedure proc_1() uninstall plugin my_plug;
2496--error ER_SP_DOES_NOT_EXIST
2497call proc_1();
2498--error ER_SP_DOES_NOT_EXIST
2499call proc_1();
2500--error ER_SP_DOES_NOT_EXIST
2501call proc_1();
2502drop procedure proc_1;
2503delimiter |;
2504--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2505create function func_1() returns int begin uninstall plugin my_plug; return 1; end|
2506delimiter ;|
2507--error ER_SP_DOES_NOT_EXIST
2508select func_1(), func_1(), func_1() from dual;
2509--error ER_SP_DOES_NOT_EXIST
2510drop function func_1;
2511prepare abc from "uninstall plugin my_plug";
2512--error ER_SP_DOES_NOT_EXIST
2513execute abc;
2514--error ER_SP_DOES_NOT_EXIST
2515execute abc;
2516--error ER_SP_DOES_NOT_EXIST
2517execute abc;
2518deallocate prepare abc;
2519
2520
2521create procedure proc_1() create database mysqltest_xyz;
2522call proc_1();
2523drop database if exists mysqltest_xyz;
2524call proc_1();
2525--error ER_DB_CREATE_EXISTS
2526call proc_1();
2527drop database if exists mysqltest_xyz;
2528call proc_1();
2529drop database if exists mysqltest_xyz;
2530drop procedure proc_1;
2531delimiter |;
2532--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2533create function func_1() returns int begin create database mysqltest_xyz; return 1; end|
2534delimiter ;|
2535--error ER_SP_DOES_NOT_EXIST
2536select func_1(), func_1(), func_1() from dual;
2537--error ER_SP_DOES_NOT_EXIST
2538drop function func_1;
2539prepare abc from "create database mysqltest_xyz";
2540execute abc;
2541drop database if exists mysqltest_xyz;
2542execute abc;
2543--error ER_DB_CREATE_EXISTS
2544execute abc;
2545drop database if exists mysqltest_xyz;
2546execute abc;
2547drop database if exists mysqltest_xyz;
2548deallocate prepare abc;
2549
2550
2551create table t1 (a int, b char(5));
2552insert into t1 values (1, "one"), (2, "two"), (3, "three");
2553create procedure proc_1() checksum table xyz;
2554call proc_1();
2555call proc_1();
2556call proc_1();
2557drop procedure proc_1;
2558delimiter |;
2559--error ER_SP_NO_RETSET
2560create function func_1() returns int begin checksum table t1; return 1; end|
2561delimiter ;|
2562--error ER_SP_DOES_NOT_EXIST
2563select func_1(), func_1(), func_1() from dual;
2564--error ER_SP_DOES_NOT_EXIST
2565drop function func_1;
2566prepare abc from "checksum table t1";
2567execute abc;
2568execute abc;
2569execute abc;
2570deallocate prepare abc;
2571
2572
2573create procedure proc_1() create user pstest_xyz@localhost;
2574call proc_1();
2575drop user pstest_xyz@localhost;
2576call proc_1();
2577--error ER_CANNOT_USER
2578call proc_1();
2579drop user pstest_xyz@localhost;
2580call proc_1();
2581drop user pstest_xyz@localhost;
2582drop procedure proc_1;
2583delimiter |;
2584--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2585create function func_1() returns int begin create user pstest_xyz@localhost; return 1; end|
2586delimiter ;|
2587--error ER_SP_DOES_NOT_EXIST
2588select func_1(), func_1(), func_1() from dual;
2589--error ER_SP_DOES_NOT_EXIST
2590drop function func_1;
2591prepare abc from "create user pstest_xyz@localhost";
2592execute abc;
2593drop user pstest_xyz@localhost;
2594execute abc;
2595--error ER_CANNOT_USER
2596execute abc;
2597drop user pstest_xyz@localhost;
2598execute abc;
2599drop user pstest_xyz@localhost;
2600deallocate prepare abc;
2601
2602
2603#create procedure proc_1() create event xyz on schedule every 5 minute disable do select 123;
2604#call proc_1();
2605#drop event xyz;
2606#call proc_1();
2607#--error ER_EVENT_ALREADY_EXISTS
2608#call proc_1();
2609#drop event xyz;
2610#call proc_1();
2611#drop event xyz;
2612#drop procedure proc_1;
2613delimiter |;
2614--error ER_EVENT_RECURSION_FORBIDDEN
2615create function func_1() returns int begin create event xyz on schedule at now() do select 123; return 1; end|
2616delimiter ;|
2617--error ER_SP_DOES_NOT_EXIST
2618select func_1(), func_1(), func_1() from dual;
2619--error ER_SP_DOES_NOT_EXIST
2620drop function func_1;
2621--error ER_UNSUPPORTED_PS
2622prepare abc from "create event xyz on schedule at now() do select 123";
2623--error ER_UNKNOWN_STMT_HANDLER
2624deallocate prepare abc;
2625
2626
2627--disable_warnings
2628drop event if exists xyz;
2629create event xyz on schedule every 5 minute disable do select 123;
2630--enable_warnings
2631create procedure proc_1() alter event xyz comment 'xyz';
2632call proc_1();
2633drop event xyz;
2634create event xyz on schedule every 5 minute disable do select 123;
2635call proc_1();
2636drop event xyz;
2637create event xyz on schedule every 5 minute disable do select 123;
2638call proc_1();
2639drop event xyz;
2640drop procedure proc_1;
2641delimiter |;
2642--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2643create function func_1() returns int begin alter event xyz comment 'xyz'; return 1; end|
2644delimiter ;|
2645--error ER_UNSUPPORTED_PS
2646prepare abc from "alter event xyz comment 'xyz'";
2647--error ER_UNKNOWN_STMT_HANDLER
2648deallocate prepare abc;
2649
2650
2651--disable_warnings
2652drop event if exists xyz;
2653create event xyz on schedule every 5 minute disable do select 123;
2654--enable_warnings
2655create procedure proc_1() drop event xyz;
2656call proc_1();
2657create event xyz on schedule every 5 minute disable do select 123;
2658call proc_1();
2659--error ER_EVENT_DOES_NOT_EXIST
2660call proc_1();
2661drop procedure proc_1;
2662delimiter |;
2663--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2664create function func_1() returns int begin drop event xyz; return 1; end|
2665delimiter ;|
2666--error ER_UNSUPPORTED_PS
2667prepare abc from "drop event xyz";
2668--error ER_UNKNOWN_STMT_HANDLER
2669deallocate prepare abc;
2670
2671
2672--disable_warnings
2673drop table if exists t1;
2674create table t1 (a int, b char(5)) engine=myisam;
2675insert into t1 values (1, "one"), (2, "two"), (3, "three");
2676--enable_warnings
2677SET GLOBAL new_cache.key_buffer_size=128*1024;
2678create procedure proc_1() cache index t1 in new_cache;
2679call proc_1();
2680call proc_1();
2681call proc_1();
2682drop procedure proc_1;
2683SET GLOBAL second_cache.key_buffer_size=128*1024;
2684prepare abc from "cache index t1 in second_cache";
2685execute abc;
2686execute abc;
2687execute abc;
2688deallocate prepare abc;
2689drop table t1;
2690
2691--disable_warnings
2692drop table if exists t1;
2693drop table if exists t2;
2694create table t1 (a int, b char(5)) engine=myisam;
2695insert into t1 values (1, "one"), (2, "two"), (3, "three");
2696create table t2 (a int, b char(5)) engine=myisam;
2697insert into t2 values (1, "one"), (2, "two"), (3, "three");
2698--enable_warnings
2699create procedure proc_1() load index into cache t1 ignore leaves;
2700call proc_1();
2701call proc_1();
2702call proc_1();
2703drop procedure proc_1;
2704delimiter |;
2705--error ER_SP_NO_RETSET
2706create function func_1() returns int begin load index into cache t1 ignore leaves; return 1; end|
2707delimiter ;|
2708prepare abc from "load index into cache t2 ignore leaves";
2709execute abc;
2710execute abc;
2711execute abc;
2712deallocate prepare abc;
2713drop table t1, t2;
2714
2715#
2716# Bug #21422: GRANT/REVOKE possible inside stored function, probably in a trigger
2717# This is disabled for now till it is resolved in 5.0
2718#
2719
2720#create procedure proc_1() grant all on *.* to abc@host;
2721#drop procedure proc_1;
2722#delimiter |;
2723#--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2724#create function func_1() returns int begin grant all on *.* to abc@host; return 1; end|
2725#delimiter ;|
2726#prepare abc from "grant all on *.* to abc@host";
2727#
2728#create procedure proc_1() revoke all on *.* from abc@host;
2729#drop procedure proc_1;
2730#delimiter |;#--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2731#create function func_1() returns int begin revoke all on *.* from abc@host; return 1; end|
2732#delimiter ;|
2733#prepare abc from "revoke all on *.* from abc@host";
2734
2735create procedure proc_1() show errors;
2736call proc_1();
2737call proc_1();
2738call proc_1();
2739drop procedure proc_1;
2740delimiter |;
2741--error ER_SP_NO_RETSET
2742create function func_1() returns int begin show errors; return 1; end|
2743delimiter ;|
2744# WL#5928: prepare of diagnostics statements fails now, cos the standard says it should.
2745--error ER_UNSUPPORTED_PS
2746prepare abc from "show errors";
2747# deallocate prepare abc;
2748
2749--disable_warnings
2750drop table if exists t1;
2751drop table if exists t2;
2752--enable_warnings
2753create procedure proc_1() show warnings;
2754drop table if exists t1;
2755call proc_1();
2756drop table if exists t2;
2757call proc_1();
2758drop table if exists t1, t2;
2759call proc_1();
2760drop procedure proc_1;
2761delimiter |;
2762--error ER_SP_NO_RETSET
2763create function func_1() returns int begin show warnings; return 1; end|
2764delimiter ;|
2765# WL#5928: prepare of diagnostics statements fails now, cos the standard says it should.
2766--error ER_UNSUPPORTED_PS
2767prepare abc from "show warnings";
2768
2769#
2770# Bug#22684: The Functions ENCODE, DECODE and FORMAT are not real functions
2771#
2772
2773set @to_format="123456789.123456789";
2774set @dec=0;
2775
2776prepare stmt2 from 'select format(?, ?)';
2777execute stmt2 using @to_format, @dec;
2778set @dec=4;
2779execute stmt2 using @to_format, @dec;
2780set @dec=6;
2781execute stmt2 using @to_format, @dec;
2782set @dec=2;
2783execute stmt2 using @to_format, @dec;
2784set @to_format="100";
2785execute stmt2 using @to_format, @dec;
2786set @to_format="1000000";
2787execute stmt2 using @to_format, @dec;
2788set @to_format="10000";
2789execute stmt2 using @to_format, @dec;
2790deallocate prepare stmt2;
2791
2792
2793#
2794# BUG#18326: Do not lock table for writing during prepare of statement
2795#
2796
2797CREATE TABLE t1 (i INT);
2798INSERT INTO t1 VALUES (1);
2799CREATE TABLE t2 (i INT);
2800INSERT INTO t2 VALUES (2);
2801
2802LOCK TABLE t1 READ, t2 WRITE;
2803
2804connect (conn1, localhost, root, , );
2805
2806# Prepare never acquires the lock, and thus should not block.
2807PREPARE stmt1 FROM "SELECT i FROM t1";
2808PREPARE stmt2 FROM "INSERT INTO t2 (i) VALUES (3)";
2809
2810# This should not block because READ lock on t1 is shared.
2811EXECUTE stmt1;
2812
2813# This should block because WRITE lock on t2 is exclusive.
2814send EXECUTE stmt2;
2815
2816connection default;
2817
2818SELECT * FROM t2;
2819UNLOCK TABLES;
2820let $wait_condition= SELECT COUNT(*) = 2 FROM t2;
2821--source include/wait_condition.inc
2822SELECT * FROM t2;
2823
2824# DDL and DML works even if some client have a prepared statement
2825# referencing the table.
2826ALTER TABLE t1 ADD COLUMN j INT;
2827ALTER TABLE t2 ADD COLUMN j INT;
2828INSERT INTO t1 VALUES (4, 5);
2829INSERT INTO t2 VALUES (4, 5);
2830
2831connection conn1;
2832
2833reap;
2834EXECUTE stmt1;
2835EXECUTE stmt2;
2836SELECT * FROM t2;
2837
2838disconnect conn1;
2839
2840connection default;
2841
2842DROP TABLE t1, t2;
2843
2844#
2845# Bug #24879 Prepared Statements: CREATE TABLE (UTF8 KEY) produces a growing
2846# key length
2847#
2848# Test that parse information is not altered by subsequent executions of a
2849# prepared statement
2850#
2851drop table if exists t1;
2852prepare stmt
2853from "create table t1 (c char(100) character set utf8, key (c(10)))";
2854execute stmt;
2855show create table t1;
2856drop table t1;
2857execute stmt;
2858show create table t1;
2859drop table t1;
2860
2861#
2862# Bug #32030 DELETE does not return an error and deletes rows if error
2863# evaluating WHERE
2864#
2865# Test that there is an error for prepared delete just like for the normal
2866# one.
2867#
2868create table t1 (a int, b int);
2869create table t2 like t1;
2870
2871insert into t1 (a, b) values (1,1), (1,2), (1,3), (1,4), (1,5),
2872       (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
2873
2874insert into t2 select a, max(b) from t1 group by a;
2875
2876prepare stmt from "delete from t2 where (select (select max(b) from t1 group
2877by a having a < 2) x from t1) > 10000";
2878
2879--error ER_SUBQUERY_NO_1_ROW
2880delete from t2 where (select (select max(b) from t1 group
2881by a having a < 2) x from t1) > 10000;
2882--error ER_SUBQUERY_NO_1_ROW
2883execute stmt;
2884--error ER_SUBQUERY_NO_1_ROW
2885execute stmt;
2886
2887deallocate prepare stmt;
2888drop table t1, t2;
2889
2890--echo #
2891--echo # Bug#52124 memory leaks like a sieve in datetime, timestamp, time, date fields + warnings
2892--echo #
2893CREATE TABLE t1 (a TIME NOT NULL, b TINYINT);
2894INSERT IGNORE INTO t1 VALUES (0, 0),(0, 0);
2895PREPARE stmt FROM "SELECT 1 FROM t1 WHERE
2896ROW(a, b) >= ROW('1', (SELECT 1 FROM t1 WHERE a > '1234abc'))";
2897EXECUTE stmt;
2898EXECUTE stmt;
2899DEALLOCATE PREPARE stmt;
2900DROP TABLE t1;
2901
2902--echo #
2903--echo # Bug#54494 crash with explain and prepared statements
2904--echo #
2905CREATE TABLE t1(a INT);
2906INSERT INTO t1 VALUES (1),(2);
2907PREPARE stmt FROM 'EXPLAIN SELECT 1 FROM t1 RIGHT JOIN t1 t2 ON 1';
2908EXECUTE stmt;
2909EXECUTE stmt;
2910DEALLOCATE PREPARE stmt;
2911DROP TABLE t1;
2912
2913--echo #
2914--echo # Bug#54488 crash when using explain and prepared statements with subqueries
2915--echo #
2916CREATE TABLE t1(f1 INT);
2917INSERT INTO t1 VALUES (1),(1);
2918PREPARE stmt FROM 'EXPLAIN SELECT 1 FROM t1 WHERE (SELECT (SELECT 1 FROM t1 GROUP BY f1))';
2919EXECUTE stmt;
2920EXECUTE stmt;
2921DEALLOCATE PREPARE stmt;
2922DROP TABLE t1;
2923
2924--echo
2925--echo End of 5.1 tests.
2926
2927###########################################################################
2928
2929--echo
2930--echo #
2931--echo # WL#4435: Support OUT-parameters in prepared statements.
2932--echo #
2933--echo
2934
2935# The idea of this test case is to check that
2936#   - OUT-parameters of four allowed types (string, double, int, decimal) work
2937#     properly;
2938#   - INOUT and OUT parameters work properly;
2939#   - A mix of IN and OUT parameters work properly;
2940
2941--disable_warnings
2942DROP PROCEDURE IF EXISTS p_string;
2943DROP PROCEDURE IF EXISTS p_double;
2944DROP PROCEDURE IF EXISTS p_int;
2945DROP PROCEDURE IF EXISTS p_decimal;
2946--enable_warnings
2947
2948delimiter |;
2949
2950--echo
2951CREATE PROCEDURE p_string(
2952  IN v0 INT,
2953  OUT v1 CHAR(32),
2954  IN v2 CHAR(32),
2955  INOUT v3 CHAR(32))
2956BEGIN
2957  SET v0 = -1;
2958  SET v1 = 'test_v1';
2959  SET v2 = 'n/a';
2960  SET v3 = 'test_v3';
2961END|
2962
2963--echo
2964CREATE PROCEDURE p_double(
2965  IN v0 INT,
2966  OUT v1 DOUBLE(4, 2),
2967  IN v2 DOUBLE(4, 2),
2968  INOUT v3 DOUBLE(4, 2))
2969BEGIN
2970  SET v0 = -1;
2971  SET v1 = 12.34;
2972  SET v2 = 98.67;
2973  SET v3 = 56.78;
2974END|
2975
2976--echo
2977CREATE PROCEDURE p_int(
2978  IN v0 CHAR(10),
2979  OUT v1 INT,
2980  IN v2 INT,
2981  INOUT v3 INT)
2982BEGIN
2983  SET v0 = 'n/a';
2984  SET v1 = 1234;
2985  SET v2 = 9876;
2986  SET v3 = 5678;
2987END|
2988
2989--echo
2990CREATE PROCEDURE p_decimal(
2991  IN v0 INT,
2992  OUT v1 DECIMAL(4, 2),
2993  IN v2 DECIMAL(4, 2),
2994  INOUT v3 DECIMAL(4, 2))
2995BEGIN
2996  SET v0 = -1;
2997  SET v1 = 12.34;
2998  SET v2 = 98.67;
2999  SET v3 = 56.78;
3000END|
3001
3002delimiter ;|
3003
3004--echo
3005PREPARE stmt_str FROM 'CALL p_string(?, ?, ?, ?)';
3006PREPARE stmt_dbl FROM 'CALL p_double(?, ?, ?, ?)';
3007PREPARE stmt_int FROM 'CALL p_int(?, ?, ?, ?)';
3008PREPARE stmt_dec FROM 'CALL p_decimal(?, ?, ?, ?)';
3009
3010--echo
3011SET @x_str_1 = NULL;
3012SET @x_str_2 = NULL;
3013SET @x_str_3 = NULL;
3014SET @x_dbl_1 = NULL;
3015SET @x_dbl_2 = NULL;
3016SET @x_dbl_3 = NULL;
3017SET @x_int_1 = NULL;
3018SET @x_int_2 = NULL;
3019SET @x_int_3 = NULL;
3020SET @x_dec_1 = NULL;
3021SET @x_dec_2 = NULL;
3022SET @x_dec_3 = NULL;
3023
3024--echo
3025--echo -- Testing strings...
3026
3027--echo
3028EXECUTE stmt_str USING @x_int_1, @x_str_1, @x_str_2, @x_str_3;
3029SELECT @x_int_1, @x_str_1, @x_str_2, @x_str_3;
3030
3031--echo
3032EXECUTE stmt_str USING @x_int_1, @x_str_1, @x_str_2, @x_str_3;
3033SELECT @x_int_1, @x_str_1, @x_str_2, @x_str_3;
3034
3035--echo
3036--echo -- Testing doubles...
3037
3038--echo
3039EXECUTE stmt_dbl USING @x_int_1, @x_dbl_1, @x_dbl_2, @x_dbl_3;
3040SELECT @x_int_1, @x_dbl_1, @x_dbl_2, @x_dbl_3;
3041
3042--echo
3043EXECUTE stmt_dbl USING @x_int_1, @x_dbl_1, @x_dbl_2, @x_dbl_3;
3044SELECT @x_int_1, @x_dbl_1, @x_dbl_2, @x_dbl_3;
3045
3046--echo
3047--echo -- Testing ints...
3048
3049--echo
3050EXECUTE stmt_int USING @x_str_1, @x_int_1, @x_int_2, @x_int_3;
3051SELECT @x_str_1, @x_int_1, @x_int_2, @x_int_3;
3052
3053--echo
3054EXECUTE stmt_int USING @x_str_1, @x_int_1, @x_int_2, @x_int_3;
3055SELECT @x_str_1, @x_int_1, @x_int_2, @x_int_3;
3056
3057--echo
3058--echo -- Testing decs...
3059
3060--echo
3061EXECUTE stmt_dec USING @x_int_1, @x_dec_1, @x_dec_2, @x_dec_3;
3062SELECT @x_int_1, @x_dec_1, @x_dec_2, @x_dec_3;
3063
3064--echo
3065EXECUTE stmt_dec USING @x_int_1, @x_dec_1, @x_dec_2, @x_dec_3;
3066SELECT @x_int_1, @x_dec_1, @x_dec_2, @x_dec_3;
3067
3068--echo
3069DEALLOCATE PREPARE stmt_str;
3070DEALLOCATE PREPARE stmt_dbl;
3071DEALLOCATE PREPARE stmt_int;
3072DEALLOCATE PREPARE stmt_dec;
3073
3074--echo
3075DROP PROCEDURE p_string;
3076DROP PROCEDURE p_double;
3077DROP PROCEDURE p_int;
3078DROP PROCEDURE p_decimal;
3079
3080#
3081# Another test case for WL#4435: check out parameters in Dynamic SQL.
3082#
3083
3084--echo
3085--disable_warnings
3086DROP PROCEDURE IF EXISTS p1;
3087DROP PROCEDURE IF EXISTS p2;
3088--enable_warnings
3089
3090--echo
3091
3092CREATE PROCEDURE p1(OUT v1 CHAR(10))
3093  SET v1 = 'test1';
3094
3095--echo
3096
3097delimiter |;
3098CREATE PROCEDURE p2(OUT v2 CHAR(10))
3099BEGIN
3100  SET @query = 'CALL p1(?)';
3101  PREPARE stmt1 FROM @query;
3102  EXECUTE stmt1 USING @u1;
3103  DEALLOCATE PREPARE stmt1;
3104
3105  SET v2 = @u1;
3106END|
3107delimiter ;|
3108
3109--echo
3110
3111CALL p2(@a);
3112SELECT @a;
3113
3114--echo
3115
3116DROP PROCEDURE p1;
3117DROP PROCEDURE p2;
3118
3119###########################################################################
3120
3121--source include/ps_out_params_generated.inc
3122
3123###########################################################################
3124
3125--echo
3126--echo # End of WL#4435.
3127
3128###########################################################################
3129
3130
3131--echo #
3132--echo # WL#4284: Transactional DDL locking
3133--echo #
3134
3135CREATE TABLE t1 (a INT);
3136BEGIN;
3137SELECT * FROM t1;
3138--echo # Test that preparing a CREATE TABLE does not take a exclusive metdata lock.
3139PREPARE stmt1 FROM "CREATE TABLE t1 AS SELECT 1";
3140--error ER_TABLE_EXISTS_ERROR
3141EXECUTE stmt1;
3142DEALLOCATE PREPARE stmt1;
3143DROP TABLE t1;
3144
3145--echo #
3146--echo # WL#4284: Transactional DDL locking
3147--echo #
3148--echo # Test that metadata locks taken during prepare are released.
3149--echo #
3150
3151connect(con1,localhost,root,,);
3152connection default;
3153CREATE TABLE t1 (a INT);
3154connection con1;
3155BEGIN;
3156PREPARE stmt1 FROM "SELECT * FROM t1";
3157connection default;
3158DROP TABLE t1;
3159disconnect con1;
3160
3161--echo
3162--echo #
3163--echo # Bug#56115: invalid memory reads when PS selecting from
3164--echo #            information_schema tables
3165--echo # Bug#58701: crash in Field::make_field, cursor-protocol
3166--echo #
3167--echo # NOTE: MTR should be run both with --ps-protocol and --cursor-protocol.
3168--echo #
3169--echo
3170
3171SELECT *
3172FROM (SELECT 1 UNION SELECT 2) t;
3173
3174--echo
3175--echo # Bug#13805127: Stored program cache produces wrong result in same THD
3176--echo
3177
3178PREPARE s1 FROM
3179"
3180SELECT c1, t2.c2, count(c3)
3181FROM
3182  (
3183  SELECT 3 as c2 FROM dual WHERE @x = 1
3184  UNION
3185  SELECT 2       FROM dual WHERE @x = 1 OR @x = 2
3186  ) AS t1,
3187  (
3188  SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
3189  UNION
3190  SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
3191  UNION
3192  SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
3193  ) AS t2
3194WHERE t2.c2 = t1.c2
3195GROUP BY c1,c2
3196ORDER BY c1,c2
3197";
3198
3199--echo
3200SET @x = 1;
3201SELECT c1, t2.c2, count(c3)
3202FROM
3203  (
3204  SELECT 3 as c2 FROM dual WHERE @x = 1
3205  UNION
3206  SELECT 2       FROM dual WHERE @x = 1 OR @x = 2
3207  ) AS t1,
3208  (
3209  SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
3210  UNION
3211  SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
3212  UNION
3213  SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
3214  ) AS t2
3215WHERE t2.c2 = t1.c2
3216GROUP BY c1, c2
3217ORDER BY c1, c2;
3218--echo
3219EXECUTE s1;
3220
3221--echo
3222SET @x = 2;
3223SELECT c1, t2.c2, count(c3)
3224FROM
3225  (
3226  SELECT 3 as c2 FROM dual WHERE @x = 1
3227  UNION
3228  SELECT 2       FROM dual WHERE @x = 1 OR @x = 2
3229  ) AS t1,
3230  (
3231  SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
3232  UNION
3233  SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
3234  UNION
3235  SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
3236  ) AS t2
3237WHERE t2.c2 = t1.c2
3238GROUP BY c1, c2
3239ORDER BY c1, c2;
3240--echo
3241EXECUTE s1;
3242
3243--echo
3244SET @x = 1;
3245SELECT c1, t2.c2, count(c3)
3246FROM
3247  (
3248  SELECT 3 as c2 FROM dual WHERE @x = 1
3249  UNION
3250  SELECT 2       FROM dual WHERE @x = 1 OR @x = 2
3251  ) AS t1,
3252  (
3253  SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
3254  UNION
3255  SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
3256  UNION
3257  SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
3258  ) AS t2
3259WHERE t2.c2 = t1.c2
3260GROUP BY c1, c2
3261ORDER BY c1, c2;
3262--echo
3263EXECUTE s1;
3264
3265DEALLOCATE PREPARE s1;
3266
3267--echo #
3268--echo # End of 5.5 tests.
3269
3270--echo #
3271--echo # Bug#12603141: JOIN::flatten_subqueries asrt/simplify_joins sig11/...
3272--echo # Bug#12603457: SEGFAULT IN REINIT_STMT_BEFORE_USE
3273--echo #
3274
3275CREATE TABLE t1(a INTEGER);
3276CREATE TABLE t2(a INTEGER);
3277
3278PREPARE stmt FROM '
3279SELECT (SELECT 1 FROM t2 WHERE ot.a) AS d
3280FROM t1 AS ot
3281GROUP BY d';
3282
3283EXECUTE stmt;
3284EXECUTE stmt;
3285
3286INSERT INTO t1 VALUES (0),(1),(2);
3287INSERT INTO t2 VALUES (1);
3288
3289EXECUTE stmt;
3290EXECUTE stmt;
3291
3292DEALLOCATE PREPARE stmt;
3293DROP TABLE t1, t2;
3294
3295--echo #
3296--echo # Bug#12582849
3297--echo # ASSERTION FAILURE IN __CXA_PURE_VIRTUAL/ITEM_COND::FIX_FIELDS
3298--echo #
3299
3300CREATE TABLE t1 (
3301  pk INTEGER AUTO_INCREMENT,
3302  col_int_nokey INTEGER,
3303  col_int_key INTEGER,
3304
3305  col_varchar_key VARCHAR(1),
3306  col_varchar_nokey VARCHAR(1),
3307
3308  PRIMARY KEY (pk),
3309  KEY (col_int_key),
3310  KEY (col_varchar_key, col_int_key)
3311);
3312
3313INSERT INTO t1 (
3314  col_int_key, col_int_nokey,
3315  col_varchar_key, col_varchar_nokey
3316) VALUES
3317(4,    2, 'v', 'v'),
3318(62, 150, 'v', 'v');
3319
3320CREATE TABLE t2 (
3321  pk INTEGER AUTO_INCREMENT,
3322  col_int_nokey INTEGER,
3323  col_int_key INTEGER,
3324
3325  col_varchar_key VARCHAR(1),
3326  col_varchar_nokey VARCHAR(1),
3327
3328  PRIMARY KEY (pk),
3329  KEY (col_int_key),
3330  KEY (col_varchar_key, col_int_key)
3331);
3332
3333INSERT INTO t2 (
3334  col_int_key, col_int_nokey,
3335  col_varchar_key, col_varchar_nokey
3336) VALUES
3337(8, NULL, 'x', 'x'),
3338(7, 8,    'd', 'd');
3339
3340PREPARE stmt FROM '
3341SELECT
3342  ( SELECT MAX( SQ1_alias2 .col_int_nokey ) AS SQ1_field1
3343    FROM ( t2 AS SQ1_alias1 RIGHT JOIN t1 AS SQ1_alias2
3344           ON ( SQ1_alias2.col_varchar_key = SQ1_alias1.col_varchar_nokey )
3345         )
3346    WHERE SQ1_alias2.pk < alias1.col_int_nokey OR alias1.pk
3347  ) AS field1
3348FROM ( t1 AS alias1 JOIN t2 AS alias2 ON alias2.pk )
3349GROUP BY field1
3350';
3351
3352EXECUTE stmt;
3353EXECUTE stmt;
3354
3355DEALLOCATE PREPARE stmt;
3356
3357DROP TABLE t1, t2;
3358
3359--echo #
3360--echo # Bug#16820562: Bad column names are not rejected at 'prepare'
3361--echo #
3362
3363--echo Resolver errors should be given at prepare time in insert select
3364
3365CREATE TABLE t1 (a INTEGER);
3366CREATE TABLE t2 (b INTEGER);
3367
3368--error ER_BAD_FIELD_ERROR
3369PREPARE s FROM "INSERT INTO t1 VALUES(1) ON DUPLICATE KEY UPDATE absent=2";
3370--error ER_BAD_FIELD_ERROR
3371PREPARE s FROM "INSERT INTO t1 VALUES(1) ON DUPLICATE KEY UPDATE a=absent";
3372--error ER_BAD_FIELD_ERROR
3373PREPARE s FROM "INSERT INTO t1 SELECT 1 ON DUPLICATE KEY UPDATE absent=2";
3374--error ER_BAD_FIELD_ERROR
3375PREPARE s FROM "INSERT INTO t1 SELECT 1 ON DUPLICATE KEY UPDATE a=absent";
3376
3377--error ER_BAD_FIELD_ERROR
3378PREPARE s FROM "INSERT INTO t1(absent) VALUES(1) ON DUPLICATE KEY UPDATE a=1";
3379--error ER_BAD_FIELD_ERROR
3380PREPARE s FROM "INSERT INTO t1(absent) SELECT 1 ON DUPLICATE KEY UPDATE a=1";
3381
3382--echo Resolver errors should be given at prepare time in multi-table update
3383
3384--error ER_BAD_FIELD_ERROR
3385PREPARE s FROM "UPDATE t1 JOIN t2 ON t1.a=t2.b SET t1.a=absent";
3386--error ER_BAD_FIELD_ERROR
3387PREPARE s FROM "UPDATE t1 JOIN t2 ON t1.a=t2.b SET t1.a=t1.absent";
3388--error ER_BAD_FIELD_ERROR
3389PREPARE s FROM "UPDATE t1 JOIN t2 ON t1.a=t2.b SET t1.a=t2.absent";
3390--error ER_BAD_FIELD_ERROR
3391PREPARE s FROM "UPDATE t1 JOIN t2 ON t1.a=t2.b SET t1.a=absent.absent";
3392
3393DROP TABLE t1, t2;
3394
3395--echo #
3396--echo # Bug#19894382 - SERVER SIDE PREPARED STATEMENTS LEADS TO POTENTIAL OFF-BY-SECOND
3397--echo #                TIMESTAMP ON SLAVE.
3398--echo #
3399
3400CREATE TABLE bug19894382(f1 CHAR(64) DEFAULT 'slave',
3401                         f2 TIME, f3 TIMESTAMP NULL, f4 DATETIME,
3402                         f5 TIME(3), f6 TIMESTAMP(3) NULL, f7 DATETIME(3));
3403
3404--echo # Execute prepared statements from mysql_client_test.
3405--exec echo "$MYSQL_CLIENT_TEST" > $MYSQLTEST_VARDIR/log/bug19894382.out.log 2>&1
3406--exec $MYSQL_CLIENT_TEST -d -u root test_bug19894382 >> $MYSQLTEST_VARDIR/log/bug19894382.out.log 2>&1
3407
3408--echo # Insert tuples from the client_test_db.bug19894382 to the test.bug19894382.
3409--echo # Tuples in the client_test_db.bug19894382 are inserted from the mysql_client_test.
3410INSERT INTO bug19894382 SELECT * FROM client_test_db.bug19894382;
3411
3412--echo # Replay binlog events
3413let $MYSQLD_DATADIR= `select @@datadir`;
3414--exec $MYSQL_BINLOG --force-if-open -d client_test_db $MYSQLD_DATADIR/binlog.000001 > $MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug19894382.binlog
3415--exec $MYSQL -e "source $MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug19894382.binlog"
3416
3417--echo # Insert tuples from the client_test_db.bug19894382 to the test.bug19894382.
3418--echo # Tuples in the client_test_db.bug19894382 are inserted from the binlog.
3419INSERT INTO bug19894382(f2, f3, f4, f5, f6, f7)
3420  SELECT f2, f3, f4, f5, f6, f7 FROM client_test_db.bug19894382;
3421--echo # With fix, tuples of "master" and "slave" will be same. There will not be any difference
3422--echo # in values inserted for time, timestamp and datetime type columns.
3423SELECT * FROM bug19894382 ORDER BY f2;
3424
3425--echo # Cleanup
3426DROP DATABASE client_test_db;
3427DROP TABLE bug19894382;
3428--remove_file $MYSQLTEST_VARDIR/log/bug19894382.out.log
3429--remove_file $MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug19894382.binlog
3430--rmdir $MYSQLTEST_VARDIR/tmp/test
3431
3432--echo #
3433--echo # Bug#30438038: MAIN.LOCK_MULTI_BUG38499 FAILS ON PB2
3434--echo #
3435CREATE TABLE t(a INT, b INT);
3436INSERT INTO t VALUES (1, 1), (2, 2), (3, 3), (4, 4);
3437PREPARE ps FROM
3438  'UPDATE t, (SELECT 1 FROM t UNION SELECT 2 FROM t) e SET a = 0 WHERE FALSE';
3439EXECUTE ps;
3440EXECUTE ps;  # The second execution used to fail.
3441DROP PREPARE ps;
3442DROP TABLE t;
3443