MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ps_query.inc
1 ####################### ps_query.inc #########################
2 # #
3 # Tests for prepared statements: SELECTs #
4 # #
5 ##############################################################
6 
7 #
8 # NOTE: PLEASE SEE ps_1general.test (bottom)
9 # BEFORE ADDING NEW TEST CASES HERE !!!
10 
11 #
12 # Please be aware, that this file will be sourced by several test case files
13 # stored within the subdirectory 't'. So every change here will affect
14 # several test cases.
15 #
16 # Please do not modify (INSERT/UPDATE/DELETE) the content or the
17 # structure (DROP/ALTER..) of the tables
18 # 't1' and 't9'.
19 # Such tests should be done in include/ps_modify.inc .
20 #
21 # But you are encouraged to use these two tables within your SELECT statements
22 # whenever possible.
23 # t1 - very simple table
24 # t9 - table with nearly all available column types
25 #
26 # The structure and the content of these tables can be found in
27 # include/ps_create.inc CREATE TABLE ...
28 # include/ps_renew.inc DELETE all rows and INSERT some rows
29 #
30 # Both tables are managed by the same storage engine.
31 # The type of the storage engine is stored in the variable '$type' .
32 
33 
34 
35 #------------------- Please insert your test cases here -------------------#
36 
37 
38 
39 #-------- Please be very carefull when editing behind this line ----------#
40 
41 ################ simple select tests ################
42 --disable_query_log
43 select '------ simple select tests ------' as test_sequence ;
44 --enable_query_log
45 
46 ##### many column types, but no parameter
47 # heavy modified case derived from client_test.c: test_func_fields()
48 prepare stmt1 from ' select * from t9 order by c1 ' ;
49 --enable_metadata
50 execute stmt1;
51 --disable_metadata
52 
53 ##### parameter used for keyword like SELECT (must fail)
54 set @arg00='SELECT' ;
55 --error 1064
56 @arg00 a from t1 where a=1;
57 --error 1064
58 prepare stmt1 from ' ? a from t1 where a=1 ';
59 
60 ##### parameter in select column list
61 ## parameter is not NULL
62 set @arg00=1 ;
63 select @arg00, b from t1 where a=1 ;
64 prepare stmt1 from ' select ?, b from t1 where a=1 ' ;
65 execute stmt1 using @arg00 ;
66 set @arg00='lion' ;
67 select @arg00, b from t1 where a=1 ;
68 prepare stmt1 from ' select ?, b from t1 where a=1 ' ;
69 execute stmt1 using @arg00 ;
70 ## parameter is NULL
71 set @arg00=NULL ;
72 select @arg00, b from t1 where a=1 ;
73 prepare stmt1 from ' select ?, b from t1 where a=1 ' ;
74 execute stmt1 using @arg00 ;
75 ## parameter within an expression
76 set @arg00=1 ;
77 select b, a - @arg00 from t1 where a=1 ;
78 prepare stmt1 from ' select b, a - ? from t1 where a=1 ' ;
79 execute stmt1 using @arg00 ;
80 # case derived from client_test.c: test_ps_null_param()
81 set @arg00=null ;
82 select @arg00 as my_col ;
83 prepare stmt1 from ' select ? as my_col';
84 execute stmt1 using @arg00 ;
85 select @arg00 + 1 as my_col ;
86 prepare stmt1 from ' select ? + 1 as my_col';
87 execute stmt1 using @arg00 ;
88 select 1 + @arg00 as my_col ;
89 prepare stmt1 from ' select 1 + ? as my_col';
90 execute stmt1 using @arg00 ;
91 ## parameter is within a function
92 # variations on 'substr'
93 set @arg00='MySQL' ;
94 select substr(@arg00,1,2) from t1 where a=1 ;
95 prepare stmt1 from ' select substr(?,1,2) from t1 where a=1 ' ;
96 execute stmt1 using @arg00 ;
97 set @arg00=3 ;
98 select substr('MySQL',@arg00,5) from t1 where a=1 ;
99 prepare stmt1 from ' select substr(''MySQL'',?,5) from t1 where a=1 ' ;
100 execute stmt1 using @arg00 ;
101 select substr('MySQL',1,@arg00) from t1 where a=1 ;
102 prepare stmt1 from ' select substr(''MySQL'',1,?) from t1 where a=1 ' ;
103 execute stmt1 using @arg00 ;
104 # variations on 'concat'
105 set @arg00='MySQL' ;
106 select a , concat(@arg00,b) from t1 order by a;
107 # BUG#3796 Prepared statement, select concat(<parameter>,<column>),wrong result
108 prepare stmt1 from ' select a , concat(?,b) from t1 order by a ' ;
109 execute stmt1 using @arg00;
110 #
111 select a , concat(b,@arg00) from t1 order by a ;
112 prepare stmt1 from ' select a , concat(b,?) from t1 order by a ' ;
113 execute stmt1 using @arg00;
114 
115 # variations on 'group_concat'
116 set @arg00='MySQL' ;
117 select group_concat(@arg00,b order by a) from t1
118 group by 'a' ;
119 prepare stmt1 from ' select group_concat(?,b order by a) from t1
120 group by ''a'' ' ;
121 execute stmt1 using @arg00;
122 #
123 select group_concat(b,@arg00 order by a) from t1
124 group by 'a' ;
125 prepare stmt1 from ' select group_concat(b,? order by a) from t1
126 group by ''a'' ' ;
127 execute stmt1 using @arg00;
128 
129 ## two parameters
130 set @arg00='first' ;
131 set @arg01='second' ;
132 set @arg02=NULL;
133 select @arg00, @arg01 from t1 where a=1 ;
134 prepare stmt1 from ' select ?, ? from t1 where a=1 ' ;
135 execute stmt1 using @arg00, @arg01 ;
136 # NULL as first and/or last parameter
137 execute stmt1 using @arg02, @arg01 ;
138 execute stmt1 using @arg00, @arg02 ;
139 execute stmt1 using @arg02, @arg02 ;
140 # case derived from client_test.c: test_ps_conj_select()
141 # for BUG#3420: select returned all rows of the table
142 --disable_warnings
143 drop table if exists t5 ;
144 --enable_warnings
145 create table t5 (id1 int(11) not null default '0',
146  value2 varchar(100), value1 varchar(100)) ;
147 insert into t5 values (1,'hh','hh'),(2,'hh','hh'),
148  (1,'ii','ii'),(2,'ii','ii') ;
149 prepare stmt1 from ' select id1,value1 from t5 where id1=? or value1=? order by id1,value1 ' ;
150 set @arg00=1 ;
151 set @arg01='hh' ;
152 execute stmt1 using @arg00, @arg01 ;
153 drop table t5 ;
154 # case derived from client_test.c: test_bug1180()
155 # for BUG#1180 optimized away part of WHERE clause
156 --disable_warnings
157 drop table if exists t5 ;
158 --enable_warnings
159 create table t5(session_id char(9) not null) ;
160 insert into t5 values ('abc') ;
161 prepare stmt1 from ' select * from t5
162 where ?=''1111'' and session_id = ''abc'' ' ;
163 set @arg00='abc' ;
164 execute stmt1 using @arg00 ;
165 set @arg00='1111' ;
166 execute stmt1 using @arg00 ;
167 set @arg00='abc' ;
168 execute stmt1 using @arg00 ;
169 drop table t5 ;
170 
171 
172 ##### parameter used for keyword FROM (must fail)
173 set @arg00='FROM' ;
174 --error 1064
175 select a @arg00 t1 where a=1 ;
176 --error 1064
177 prepare stmt1 from ' select a ? t1 where a=1 ' ;
178 ##### parameter used for tablename (must fail)
179 set @arg00='t1' ;
180 --error 1064
181 select a from @arg00 where a=1 ;
182 --error 1064
183 prepare stmt1 from ' select a from ? where a=1 ' ;
184 ##### parameter used for keyword WHERE tablename (must fail)
185 set @arg00='WHERE' ;
186 --error 1064
187 select a from t1 @arg00 a=1 ;
188 --error 1064
189 prepare stmt1 from ' select a from t1 ? a=1 ' ;
190 
191 ##### parameter used in where clause
192 # parameter is not NULL
193 set @arg00=1 ;
194 select a FROM t1 where a=@arg00 ;
195 prepare stmt1 from ' select a FROM t1 where a=? ' ;
196 execute stmt1 using @arg00 ;
197 set @arg00=1000 ;
198 # row not found
199 execute stmt1 using @arg00 ;
200 # parameter is NULL
201 set @arg00=NULL ;
202 select a FROM t1 where a=@arg00 ;
203 prepare stmt1 from ' select a FROM t1 where a=? ' ;
204 execute stmt1 using @arg00 ;
205 # parameter is not NULL within a function
206 set @arg00=4 ;
207 select a FROM t1 where a=sqrt(@arg00) ;
208 prepare stmt1 from ' select a FROM t1 where a=sqrt(?) ' ;
209 execute stmt1 using @arg00 ;
210 # parameter is NULL within a function
211 set @arg00=NULL ;
212 select a FROM t1 where a=sqrt(@arg00) ;
213 prepare stmt1 from ' select a FROM t1 where a=sqrt(?) ' ;
214 execute stmt1 using @arg00 ;
215 # parameter in IN
216 set @arg00=2 ;
217 set @arg01=3 ;
218 select a FROM t1 where a in (@arg00,@arg01) order by a;
219 prepare stmt1 from ' select a FROM t1 where a in (?,?) order by a ';
220 execute stmt1 using @arg00, @arg01;
221 # case derived from client_test.c: test_bug1500()
222 set @arg00= 'one' ;
223 set @arg01= 'two' ;
224 set @arg02= 'five' ;
225 prepare stmt1 from ' select b FROM t1 where b in (?,?,?) order by b ' ;
226 execute stmt1 using @arg00, @arg01, @arg02 ;
227 # parameter in LIKE
228 prepare stmt1 from ' select b FROM t1 where b like ? ';
229 set @arg00='two' ;
230 execute stmt1 using @arg00 ;
231 set @arg00='tw%' ;
232 execute stmt1 using @arg00 ;
233 set @arg00='%wo' ;
234 execute stmt1 using @arg00 ;
235 # case derived from client_test.c: test_ps_null_param():
236 # second part, comparisions with NULL placeholders in prepared
237 # mode
238 set @arg00=null ;
239 insert into t9 set c1= 0, c5 = NULL ;
240 select c5 from t9 where c5 > NULL ;
241 prepare stmt1 from ' select c5 from t9 where c5 > ? ';
242 execute stmt1 using @arg00 ;
243 select c5 from t9 where c5 < NULL ;
244 prepare stmt1 from ' select c5 from t9 where c5 < ? ';
245 execute stmt1 using @arg00 ;
246 select c5 from t9 where c5 = NULL ;
247 prepare stmt1 from ' select c5 from t9 where c5 = ? ';
248 execute stmt1 using @arg00 ;
249 select c5 from t9 where c5 <=> NULL ;
250 prepare stmt1 from ' select c5 from t9 where c5 <=> ? ';
251 execute stmt1 using @arg00 ;
252 delete from t9 where c1= 0 ;
253 
254 ##### parameter used for operator in WHERE clause (must fail)
255 set @arg00='>' ;
256 --error 1064
257 select a FROM t1 where a @arg00 1 ;
258 --error 1064
259 prepare stmt1 from ' select a FROM t1 where a ? 1 ' ;
260 
261 ##### parameter used in group by clause
262 set @arg00=1 ;
263 select a,b FROM t1 where a is not NULL
264 AND b is not NULL group by a - @arg00 ;
265 prepare stmt1 from ' select a,b FROM t1 where a is not NULL
266 AND b is not NULL group by a - ? ' ;
267 execute stmt1 using @arg00 ;
268 
269 ##### parameter used in having clause
270 set @arg00='two' ;
271 select a,b FROM t1 where a is not NULL
272 AND b is not NULL having b <> @arg00 order by a ;
273 prepare stmt1 from ' select a,b FROM t1 where a is not NULL
274 AND b is not NULL having b <> ? order by a ' ;
275 execute stmt1 using @arg00 ;
276 
277 ##### parameter used in order clause
278 set @arg00=1 ;
279 select a,b FROM t1 where a is not NULL
280 AND b is not NULL order by a - @arg00 ;
281 prepare stmt1 from ' select a,b FROM t1 where a is not NULL
282 AND b is not NULL order by a - ? ' ;
283 execute stmt1 using @arg00 ;
284 ## What is the semantic of a single parameter (integer >0)
285 # after order by? column number or constant
286 set @arg00=2 ;
287 select a,b from t1 order by 2 ;
288 prepare stmt1 from ' select a,b from t1
289 order by ? ';
290 execute stmt1 using @arg00;
291 set @arg00=1 ;
292 execute stmt1 using @arg00;
293 set @arg00=0 ;
294 --error 1054
295 execute stmt1 using @arg00;
296 
297 ##### parameter used in limit clause
298 set @arg00=1;
299 prepare stmt1 from ' select a,b from t1 order by a
300 limit 1 ';
301 execute stmt1 ;
302 prepare stmt1 from ' select a,b from t1 order by a limit ? ';
303 execute stmt1 using @arg00;
304 
305 ##### parameter used in many places
306 set @arg00='b' ;
307 set @arg01=0 ;
308 set @arg02=2 ;
309 set @arg03=2 ;
310 select sum(a), @arg00 from t1 where a > @arg01
311 and b is not null group by substr(b,@arg02)
312 having sum(a) <> @arg03 ;
313 prepare stmt1 from ' select sum(a), ? from t1 where a > ?
314 and b is not null group by substr(b,?)
315 having sum(a) <> ? ';
316 execute stmt1 using @arg00, @arg01, @arg02, @arg03;
317 
318 
319 ################ join tests ################
320 --disable_query_log
321 select '------ join tests ------' as test_sequence ;
322 --enable_query_log
323 
324 # no parameter
325 select first.a as a1, second.a as a2
326 from t1 first, t1 second
327 where first.a = second.a order by a1 ;
328 prepare stmt1 from ' select first.a as a1, second.a as a2
329  from t1 first, t1 second
330  where first.a = second.a order by a1 ';
331 execute stmt1 ;
332 
333 # some parameters
334 set @arg00='ABC';
335 set @arg01='two';
336 set @arg02='one';
337 select first.a, @arg00, second.a FROM t1 first, t1 second
338 where @arg01 = first.b or first.a = second.a or second.b = @arg02
339 order by second.a, first.a;
340 prepare stmt1 from ' select first.a, ?, second.a FROM t1 first, t1 second
341  where ? = first.b or first.a = second.a or second.b = ?
342  order by second.a, first.a';
343 execute stmt1 using @arg00, @arg01, @arg02;
344 
345 # test case derived from client_test.c: test_join()
346 --disable_warnings
347 drop table if exists t2 ;
348 --enable_warnings
349 create table t2 as select * from t1 ;
350 set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
351 set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
352 set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
353 set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
354 set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
355 set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
356 set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
357 set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
358 set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
359 let $1= 9 ;
360 while ($1)
361 {
362  --disable_query_log
363  eval select @query$1 as 'the join statement is:' ;
364  --enable_query_log
365  eval prepare stmt1 from @query$1 ;
366  let $2= 3 ;
367  while ($2)
368  {
369  execute stmt1 ;
370  dec $2 ;
371  }
372  dec $1 ;
373 }
374 drop table t2 ;
375 
376 
377 ################ subquery tests ################
378 --disable_query_log
379 select '------ subquery tests ------' as test_sequence ;
380 --enable_query_log
381 
382 # no parameter
383 prepare stmt1 from ' select a, b FROM t1 outer_table where
384  a = (select a from t1 where b = ''two'') ';
385 execute stmt1 ;
386 
387 ###### parameter in the outer part
388 set @arg00='two' ;
389 select a, b FROM t1 outer_table where
390  a = (select a from t1 where b = 'two' ) and b=@arg00 ;
391 prepare stmt1 from ' select a, b FROM t1 outer_table where
392  a = (select a from t1 where b = ''two'') and b=? ';
393 execute stmt1 using @arg00;
394 ###### parameter in the inner part
395 set @arg00='two' ;
396 # Bug#4000 (only BDB tables)
397 select a, b FROM t1 outer_table where
398  a = (select a from t1 where b = @arg00 ) and b='two' ;
399 prepare stmt1 from ' select a, b FROM t1 outer_table where
400  a = (select a from t1 where b = ? ) and b=''two'' ' ;
401 execute stmt1 using @arg00;
402 set @arg00=3 ;
403 set @arg01='three' ;
404 select a,b FROM t1 where (a,b) in (select 3, 'three');
405 select a FROM t1 where (a,b) in (select @arg00,@arg01);
406 prepare stmt1 from ' select a FROM t1 where (a,b) in (select ?, ?) ';
407 execute stmt1 using @arg00, @arg01;
408 
409 ###### parameters in the both parts
410 set @arg00=1 ;
411 set @arg01='two' ;
412 set @arg02=2 ;
413 set @arg03='two' ;
414 # Bug#4000 (only BDB tables)
415 select a, @arg00, b FROM t1 outer_table where
416  b=@arg01 and a = (select @arg02 from t1 where b = @arg03 ) ;
417 prepare stmt1 from ' select a, ?, b FROM t1 outer_table where
418  b=? and a = (select ? from t1 where b = ? ) ' ;
419 execute stmt1 using @arg00, @arg01, @arg02, @arg03 ;
420 
421 # Bug#8807
422 prepare stmt1 from 'select c4 FROM t9 where
423  c13 = (select MAX(b) from t1 where a = ?) and c22 = ? ' ;
424 execute stmt1 using @arg01, @arg02;
425 
426 ######## correlated subquery
427 # no parameter
428 prepare stmt1 from ' select a, b FROM t1 outer_table where
429  a = (select a from t1 where b = outer_table.b ) order by a ';
430 # also Bug#4000 (only BDB tables)
431 # Bug#4106 : ndb table, query with correlated subquery, wrong result
432 execute stmt1 ;
433 # test case derived from client_test.c: test_subqueries_ref
434 let $1= 3 ;
435 while ($1)
436 {
437  prepare stmt1 from ' SELECT a as ccc from t1 outr where a+1=
438  (SELECT 1+outr.a from t1 where outr.a+1=a+1 and a=1) ';
439  execute stmt1 ;
440  deallocate prepare stmt1 ;
441  dec $1 ;
442 }
443 
444 
445 ###### parameter in the outer part
446 set @arg00='two' ;
447 # Bug#4000 (only BDB tables)
448 select a, b FROM t1 outer_table where
449  a = (select a from t1 where b = outer_table.b ) and b=@arg00 ;
450 prepare stmt1 from ' select a, b FROM t1 outer_table where
451  a = (select a from t1 where b = outer_table.b) and b=? ';
452 # also Bug#4000 (only BDB tables)
453 execute stmt1 using @arg00;
454 
455 ###### parameter in the inner part
456 set @arg00=2 ;
457 select a, b FROM t1 outer_table where
458  a = (select a from t1 where a = @arg00 and b = outer_table.b) and b='two' ;
459 prepare stmt1 from ' select a, b FROM t1 outer_table where
460  a = (select a from t1 where a = ? and b = outer_table.b) and b=''two'' ' ;
461 execute stmt1 using @arg00;
462 
463 set @arg00=2 ;
464 select a, b FROM t1 outer_table where
465  a = (select a from t1 where outer_table.a = @arg00 and a=2) and b='two' ;
466 prepare stmt1 from ' select a, b FROM t1 outer_table where
467  a = (select a from t1 where outer_table.a = ? and a=2) and b=''two'' ' ;
468 execute stmt1 using @arg00;
469 
470 ###### parameters in the both parts
471 set @arg00=1 ;
472 set @arg01='two' ;
473 set @arg02=2 ;
474 set @arg03='two' ;
475 # Bug#4000 (only BDB tables)
476 select a, @arg00, b FROM t1 outer_table where
477  b=@arg01 and a = (select @arg02 from t1 where outer_table.b = @arg03
478  and outer_table.a=a ) ;
479 prepare stmt1 from ' select a, ?, b FROM t1 outer_table where
480  b=? and a = (select ? from t1 where outer_table.b = ?
481  and outer_table.a=a ) ' ;
482 # also Bug#4000 (only BDB tables)
483 execute stmt1 using @arg00, @arg01, @arg02, @arg03 ;
484 
485 ###### subquery after from
486 set @arg00=1 ;
487 set @arg01=0 ;
488 select a, @arg00
489 from ( select a - @arg00 as a from t1 where a=@arg00 ) as t2
490 where a=@arg01;
491 prepare stmt1 from ' select a, ?
492  from ( select a - ? as a from t1 where a=? ) as t2
493  where a=? ';
494 execute stmt1 using @arg00, @arg00, @arg00, @arg01 ;
495 
496 ###### subquery in select list
497 # test case derived from client_test.c: test_create_drop
498 --disable_warnings
499 drop table if exists t2 ;
500 --enable_warnings
501 create table t2 as select * from t1;
502 prepare stmt1 from ' select a in (select a from t2) from t1 ' ;
503 execute stmt1 ;
504 # test case derived from client_test.c: test_selecttmp()
505 --disable_warnings
506 drop table if exists t5, t6, t7 ;
507 --enable_warnings
508 create table t5 (a int , b int) ;
509 create table t6 like t5 ;
510 create table t7 like t5 ;
511 insert into t5 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7),
512  (2, -1), (3, 10) ;
513 insert into t6 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1) ;
514 insert into t7 values (3, 3), (2, 2), (1, 1) ;
515 prepare stmt1 from ' select a, (select count(distinct t5.b) as sum from t5, t6
516  where t5.a=t6.a and t6.b > 0 and t5.a <= t7.b
517  group by t5.a order by sum limit 1) from t7 ' ;
518 let $1= 3 ;
519 while ($1)
520 {
521  execute stmt1 ;
522  dec $1 ;
523 }
524 drop table t5, t6, t7 ;
525 
526 
527 ###### heavy modified case derived from client_test.c: test_distinct()
528 --disable_warnings
529 drop table if exists t2 ;
530 --enable_warnings
531 create table t2 as select * from t9;
532 ## unusual and complex SELECT without parameters
533 set @stmt= ' SELECT
534  (SELECT SUM(c1 + c12 + 0.0) FROM t2
535  where (t9.c2 - 0e-3) = t2.c2
536  GROUP BY t9.c15 LIMIT 1) as scalar_s,
537  exists (select 1.0e+0 from t2
538  where t2.c3 * 9.0000000000 = t9.c4) as exists_s,
539  c5 * 4 in (select c6 + 0.3e+1 from t2) as in_s,
540  (c7 - 4, c8 - 4) in (select c9 + 4.0, c10 + 40e-1 from t2) as in_row_s
541 FROM t9,
542 (select c25 x, c32 y from t2) tt WHERE x = c25 ' ;
543 --enable_metadata
544 prepare stmt1 from @stmt ;
545 #
546 # Result log was disabled upon test case failure in the optimized build.
547 #
548 --disable_result_log
549 execute stmt1 ;
550 --disable_metadata
551 execute stmt1 ;
552 ## now expand the terrible SELECT to EXPLAIN SELECT
553 set @stmt= concat('explain ',@stmt);
554 --enable_metadata
555 prepare stmt1 from @stmt ;
556 execute stmt1 ;
557 --disable_metadata
558 # Bug#4271 prepared explain complex select, second executes crashes the server
559 execute stmt1 ;
560 ## many parameters
561 ## replace the constants of the complex SELECT with parameters
562 set @stmt= ' SELECT
563  (SELECT SUM(c1+c12+?) FROM t2 where (t9.c2-?)=t2.c2
564  GROUP BY t9.c15 LIMIT 1) as scalar_s,
565  exists (select ? from t2
566  where t2.c3*?=t9.c4) as exists_s,
567  c5*? in (select c6+? from t2) as in_s,
568  (c7-?, c8-?) in (select c9+?, c10+? from t2) as in_row_s
569 FROM t9,
570 (select c25 x, c32 y from t2) tt WHERE x =c25 ' ;
571 set @arg00= 0.0 ;
572 set @arg01= 0e-3 ;
573 set @arg02= 1.0e+0 ;
574 set @arg03= 9.0000000000 ;
575 set @arg04= 4 ;
576 set @arg05= 0.3e+1 ;
577 set @arg06= 4 ;
578 set @arg07= 4 ;
579 set @arg08= 4.0 ;
580 set @arg09= 40e-1 ;
581 --enable_metadata
582 prepare stmt1 from @stmt ;
583 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04, @arg05, @arg06,
584  @arg07, @arg08, @arg09 ;
585 --disable_metadata
586 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04, @arg05, @arg06,
587  @arg07, @arg08, @arg09 ;
588 ## now expand the terrible SELECT to EXPLAIN SELECT
589 set @stmt= concat('explain ',@stmt);
590 --enable_metadata
591 prepare stmt1 from @stmt ;
592 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04, @arg05, @arg06,
593  @arg07, @arg08, @arg09 ;
594 --disable_metadata
595 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04, @arg05, @arg06,
596  @arg07, @arg08, @arg09 ;
597 --enable_result_log
598 drop table t2 ;
599 
600 
601 ##### test case derived from client_test.c: test_bug4079()
602 --error 1242
603 select 1 < (select a from t1) ;
604 prepare stmt1 from ' select 1 < (select a from t1) ' ;
605 --error 1242
606 execute stmt1 ;
607 # Bug#5066 embedded server, select after failed subquery provides wrong result
608 # (two additional records, all column values NULL)
609 select 1 as my_col ;
610 
611 ################ union tests ################
612 --disable_query_log
613 select '------ union tests ------' as test_sequence ;
614 --enable_query_log
615 
616 # no parameter
617 prepare stmt1 from ' select a FROM t1 where a=1
618  union distinct
619  select a FROM t1 where a=1 ';
620 execute stmt1 ;
621 # Bug#3577: the second execute crashes mysqld
622 execute stmt1 ;
623 prepare stmt1 from ' select a FROM t1 where a=1
624  union all
625  select a FROM t1 where a=1 ';
626 execute stmt1 ;
627 # test case derived from client_test.c: test_bad_union()
628 --error 1222
629 prepare stmt1 from ' SELECT 1, 2 union SELECT 1 ' ;
630 --error 1222
631 prepare stmt1 from ' SELECT 1 union SELECT 1, 2 ' ;
632 --error 1222
633 prepare stmt1 from ' SELECT * from t1 union SELECT 1 ' ;
634 --error 1222
635 prepare stmt1 from ' SELECT 1 union SELECT * from t1 ' ;
636 
637 
638 ##### everything in the first table
639 # one parameter as constant in the first table
640 set @arg00=1 ;
641 select @arg00 FROM t1 where a=1
642 union distinct
643 select 1 FROM t1 where a=1;
644 prepare stmt1 from ' select ? FROM t1 where a=1
645  union distinct
646  select 1 FROM t1 where a=1 ' ;
647 execute stmt1 using @arg00;
648 
649 ##### everything in the second table
650 # one parameter as constant
651 set @arg00=1 ;
652 select 1 FROM t1 where a=1
653 union distinct
654 select @arg00 FROM t1 where a=1;
655 prepare stmt1 from ' select 1 FROM t1 where a=1
656  union distinct
657  select ? FROM t1 where a=1 ' ;
658 execute stmt1 using @arg00;
659 
660 # one parameter in every table
661 set @arg00='a' ;
662 select @arg00 FROM t1 where a=1
663 union distinct
664 select @arg00 FROM t1 where a=1;
665 prepare stmt1 from ' select ? FROM t1 where a=1
666  union distinct
667  select ? FROM t1 where a=1 ';
668 # BUG#3811 wrong result, prepared statement, union,
669 # parameter in result column list
670 execute stmt1 using @arg00, @arg00;
671 prepare stmt1 from ' select ?
672  union distinct
673  select ? ';
674 execute stmt1 using @arg00, @arg00;
675 
676 # many parameters
677 set @arg00='a' ;
678 set @arg01=1 ;
679 set @arg02='a' ;
680 set @arg03=2 ;
681 select @arg00 FROM t1 where a=@arg01
682 union distinct
683 select @arg02 FROM t1 where a=@arg03;
684 prepare stmt1 from ' select ? FROM t1 where a=?
685  union distinct
686  select ? FROM t1 where a=? ' ;
687 execute stmt1 using @arg00, @arg01, @arg02, @arg03;
688 
689 ## increased complexity
690 
691 set @arg00=1 ;
692 # Bug#3686 the wrong server response was 1140 Mixing of GROUP columns ..
693 prepare stmt1 from ' select sum(a) + 200, ? from t1
694 union distinct
695 select sum(a) + 200, 1 from t1
696 group by b ' ;
697 execute stmt1 using @arg00;
698 
699 set @Oporto='Oporto' ;
700 set @Lisboa='Lisboa' ;
701 set @0=0 ;
702 set @1=1 ;
703 set @2=2 ;
704 set @3=3 ;
705 set @4=4 ;
706 select @Oporto,@Lisboa,@0,@1,@2,@3,@4 ;
707 
708 ## union + group by
709 select sum(a) + 200 as the_sum, @Oporto as the_town from t1
710 group by b
711 union distinct
712 select sum(a) + 200, @Lisboa from t1
713 group by b ;
714 
715 prepare stmt1 from ' select sum(a) + 200 as the_sum, ? as the_town from t1
716  group by b
717  union distinct
718  select sum(a) + 200, ? from t1
719  group by b ' ;
720 execute stmt1 using @Oporto, @Lisboa;
721 
722 
723 ## union + where + group by
724 select sum(a) + 200 as the_sum, @Oporto as the_town from t1
725 where a > @1
726 group by b
727 union distinct
728 select sum(a) + 200, @Lisboa from t1
729 where a > @2
730 group by b ;
731 
732 prepare stmt1 from ' select sum(a) + 200 as the_sum, ? as the_town from t1
733  where a > ?
734  group by b
735  union distinct
736  select sum(a) + 200, ? from t1
737  where a > ?
738  group by b ' ;
739 execute stmt1 using @Oporto, @1, @Lisboa, @2;
740 
741 ## union + where + group by + having
742 select sum(a) + 200 as the_sum, @Oporto as the_town from t1
743 where a > @1
744 group by b
745 having avg(a) > @2
746 union distinct
747 select sum(a) + 200, @Lisboa from t1
748 where a > @2
749 group by b
750 having avg(a) > @3;
751 
752 prepare stmt1 from ' select sum(a) + 200 as the_sum, ? as the_town from t1
753  where a > ?
754  group by b
755  having avg(a) > ?
756  union distinct
757  select sum(a) + 200, ? from t1
758  where a > ?
759  group by b
760  having avg(a) > ? ';
761 execute stmt1 using @Oporto, @1, @2, @Lisboa, @2, @3;
762 
763 
764 ################ explain select tests ################
765 --disable_query_log
766 select '------ explain select tests ------' as test_sequence ;
767 --enable_query_log
768 --disable_metadata
769 # table with many column types
770 prepare stmt1 from ' explain select * from t9 ' ;
771 --enable_metadata
772 execute stmt1;
773 --disable_metadata
774 
775