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

« back to all changes in this revision

Viewing changes to mysql-test/t/ps_ddl1.test

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Testing the behavior of 'PREPARE', 'DDL', 'EXECUTE' scenarios
 
3
#
 
4
# There are several subtests which are probably "superfluous" because a DDL
 
5
# statement before the EXECUTE <prepared stmt handle> contained a keyword
 
6
# or action (Example: Alter) which causes that all prepared statements using
 
7
# the modified object are reprepared before execution.
 
8
# Please do not delete these subtests if they disturb. Just disable them by
 
9
# if (0)
 
10
# {
 
11
#    <tests to disable>
 
12
# }.
 
13
# There might be future optimisations of the server which decrease the amount
 
14
# of unneeded reprepares or skip unneeded prepare steps and than these subtests
 
15
# might become valuable.
 
16
#    Example:
 
17
#    Every preceding ALTER TABLE seems to cause a reprepare.
 
18
#    But if the ALTER only changed the table comment ...
 
19
#
 
20
# Created: 2008-04-18 mleich
 
21
#
 
22
 
 
23
--disable_warnings
 
24
drop temporary table if exists t1;
 
25
drop table if exists t1, t2;
 
26
drop procedure if exists p_verify_reprepare_count;
 
27
drop procedure if exists p1;
 
28
drop function if exists f1;
 
29
drop view if exists t1;
 
30
drop schema if exists mysqltest;
 
31
--enable_warnings
 
32
 
 
33
delimiter |;
 
34
create procedure p_verify_reprepare_count(expected int)
 
35
begin
 
36
  declare old_reprepare_count int default @reprepare_count;
 
37
 
 
38
  select variable_value from
 
39
  information_schema.session_status where
 
40
  variable_name='com_stmt_reprepare'
 
41
  into @reprepare_count;
 
42
 
 
43
  if old_reprepare_count + expected <> @reprepare_count then
 
44
    select concat("Expected: ", expected,
 
45
                   ", actual: ", @reprepare_count - old_reprepare_count)
 
46
    as "ERROR";
 
47
  else
 
48
    select '' as "SUCCESS";
 
49
  end if;
 
50
end|
 
51
delimiter ;|
 
52
set @reprepare_count= 0;
 
53
flush status;
 
54
 
 
55
--disable_warnings
 
56
drop table if exists t1;
 
57
--disable_warnings
 
58
 
 
59
--echo # Column added or dropped is not within the list of selected columns
 
60
--echo # or table comment has changed.
 
61
--echo # A reprepare is probably not needed.
 
62
create table t1 (a int, b int);
 
63
prepare stmt from "select a from t1";
 
64
execute stmt;
 
65
call p_verify_reprepare_count(0);
 
66
alter table t1 add column c int;
 
67
execute stmt;
 
68
call p_verify_reprepare_count(1);
 
69
execute stmt;
 
70
call p_verify_reprepare_count(0);
 
71
alter table t1 drop column b;
 
72
execute stmt;
 
73
call p_verify_reprepare_count(1);
 
74
execute stmt;
 
75
call p_verify_reprepare_count(0);
 
76
alter table t1 comment "My best table";
 
77
execute stmt;
 
78
call p_verify_reprepare_count(1);
 
79
execute stmt;
 
80
call p_verify_reprepare_count(0);
 
81
drop table t1;
 
82
deallocate prepare stmt;
 
83
 
 
84
--echo # Selects using the table at various positions, inser,update ...
 
85
--echo # + the table disappears
 
86
create table t1 (a int);
 
87
# Attention:
 
88
#   "truncate" must have the first position (= executed as last prepared
 
89
#   statement), because it recreates the table which has leads to reprepare
 
90
#   (is this really needed) of all statements.
 
91
prepare stmt1 from "truncate t1";
 
92
prepare stmt2 from "select 1 as my_column from t1";
 
93
prepare stmt3 from "select 1 as my_column from (select * from t1) as t2";
 
94
prepare stmt4 from
 
95
"select 1 as my_column from (select 1) as t2 where exists (select 1 from t1)";
 
96
prepare stmt5 from "select * from (select 1 as b) as t2, t1";
 
97
prepare stmt6 from "select * from t1 union all select 1.5";
 
98
prepare stmt7 from "select 1 as my_column union all select 1 from t1";
 
99
prepare stmt8 from "insert into t1 values(1),(2)";
 
100
prepare stmt9 from "update t1 set a = 3 where a = 2";
 
101
prepare stmt10 from "delete from t1 where a = 1";
 
102
let ps_stmt_count= 10;
 
103
--echo # Attention: Result logging is disabled.
 
104
# Checks of correct results of statements are not the goal of this test.
 
105
let $num= $ps_stmt_count;
 
106
while ($num)
 
107
{
 
108
   --disable_result_log
 
109
   eval execute stmt$num;
 
110
   --enable_result_log
 
111
   dec $num;
 
112
}
 
113
# There was no reprepare needed, because none of the objects has changed.
 
114
call p_verify_reprepare_count(0);
 
115
drop table t1;
 
116
let $num= $ps_stmt_count;
 
117
while ($num)
 
118
{
 
119
   --error ER_NO_SUCH_TABLE
 
120
   eval execute stmt$num;
 
121
   dec $num;
 
122
}
 
123
# There was no reprepare needed, because the statement is no more applicable.
 
124
call p_verify_reprepare_count(0);
 
125
let $num= $ps_stmt_count;
 
126
while ($num)
 
127
{
 
128
   eval deallocate prepare stmt$num;
 
129
   dec $num;
 
130
}
 
131
 
 
132
--echo # Selects using the table at various positions, inser,update ...
 
133
--echo # + layout change (drop column) which must cause a reprepare
 
134
create table t1 (a int, b int);
 
135
insert into t1 values(1,1),(2,2),(3,3);
 
136
create table t2 like t1;
 
137
insert into t1 values(2,2);
 
138
prepare stmt1 from "select a,b from t1";
 
139
prepare stmt2 from "select a,b from (select * from t1) as t1";
 
140
prepare stmt3 from "select * from t1 where a = 2 and b = 2";
 
141
prepare stmt4 from "select * from t2 where (a,b) in (select * from t1)";
 
142
prepare stmt5 from "select * from t1 union select * from t2";
 
143
prepare stmt6 from "select * from t1 union all select * from t2";
 
144
prepare stmt7 from "insert into t1 set a = 4, b = 4";
 
145
prepare stmt8 from "insert into t1 select * from t2";
 
146
let ps_stmt_count= 8;
 
147
--echo # Attention: Result logging is disabled.
 
148
# Checks of correct results of statements are not the goal of this test.
 
149
let $num= $ps_stmt_count;
 
150
while ($num)
 
151
{
 
152
   --disable_result_log
 
153
   eval execute stmt$num;
 
154
   --enable_result_log
 
155
   dec $num;
 
156
}
 
157
call p_verify_reprepare_count(0);
 
158
alter table t1 drop column b;
 
159
--disable_abort_on_error
 
160
let $num= $ps_stmt_count;
 
161
while ($num)
 
162
{
 
163
   eval execute stmt$num;
 
164
   # A reprepare is needed, because layout change of t1 affects statement.
 
165
   call p_verify_reprepare_count(1);
 
166
   dec $num;
 
167
}
 
168
let $num= $ps_stmt_count;
 
169
while ($num)
 
170
{
 
171
   eval execute stmt$num;
 
172
   call p_verify_reprepare_count(1);
 
173
   dec $num;
 
174
}
 
175
--echo # Why does the INSERT ... SELECT does not get a reprepare or is
 
176
--echo # only the counter not incremented?
 
177
eval execute stmt8;
 
178
call p_verify_reprepare_count(1);
 
179
--enable_abort_on_error
 
180
alter table t2 add column c int;
 
181
--error ER_WRONG_VALUE_COUNT_ON_ROW
 
182
eval execute stmt8;
 
183
call p_verify_reprepare_count(1);
 
184
let $num= $ps_stmt_count;
 
185
while ($num)
 
186
{
 
187
   eval deallocate prepare stmt$num;
 
188
   dec $num;
 
189
}
 
190
drop table t1;
 
191
drop table t2;
 
192
 
 
193
 
 
194
--echo # select AVG(<col>) + optimizer uses index meets loss of the index
 
195
create table t1 (a int, b int, primary key(b),unique index t1_unq_idx(a));
 
196
# We need an index which is not converted to PRIMARY KEY (becomes in
 
197
# case of InnoDB the key used for table clustering).
 
198
insert into t1 set a = 0, b = 0;
 
199
insert into t1 select a + 1, b + 1 from t1;
 
200
insert into t1 select a + 2, b + 2 from t1;
 
201
insert into t1 select a + 4, b + 4 from t1;
 
202
insert into t1 select a + 8, b + 8 from t1;
 
203
# "using index" optimizer strategy is intended
 
204
let $possible_keys=
 
205
    query_get_value(explain select avg(a) from t1, possible_keys, 1);
 
206
let $extra=
 
207
    query_get_value(explain select avg(a) from t1, Extra, 1);
 
208
--echo # Optimizer strategy: Possible keys = $possible_keys , Extra = $extra
 
209
prepare stmt from "select avg(a) from t1";
 
210
execute stmt;
 
211
call p_verify_reprepare_count(0);
 
212
execute stmt;
 
213
call p_verify_reprepare_count(0);
 
214
 
 
215
alter table t1 drop index t1_unq_idx;
 
216
let $possible_keys=
 
217
    query_get_value(explain select avg(a) from t1, possible_keys, 1);
 
218
let $extra=
 
219
    query_get_value(explain select avg(a) from t1, Extra, 1);
 
220
--echo # Optimizer strategy: Possible keys = $possible_keys , Extra = $extra
 
221
execute stmt;
 
222
call p_verify_reprepare_count(1);
 
223
execute stmt;
 
224
call p_verify_reprepare_count(0);
 
225
 
 
226
 
 
227
--echo # select AVG(<col>) + optimizer uses table scan meets a new index
 
228
alter table t1 add unique index t1_unq_idx(a);
 
229
let $possible_keys=
 
230
    query_get_value(explain select avg(a) from t1, possible_keys, 1);
 
231
let $extra=
 
232
    query_get_value(explain select avg(a) from t1, Extra, 1);
 
233
--echo # Optimizer strategy: Possible keys = $possible_keys , Extra = $extra
 
234
execute stmt;
 
235
call p_verify_reprepare_count(1);
 
236
execute stmt;
 
237
call p_verify_reprepare_count(0);
 
238
 
 
239
deallocate prepare stmt;
 
240
drop table t1;
 
241
 
 
242
 
 
243
--echo # table replaced by not updatable view - Insert
 
244
create table t1 (a int);
 
245
prepare stmt from "insert into t1 values(1)";
 
246
execute stmt;
 
247
call p_verify_reprepare_count(0);
 
248
 
 
249
drop table t1;
 
250
create view t1 as select 1;
 
251
--error ER_NON_INSERTABLE_TABLE
 
252
execute stmt;
 
253
call p_verify_reprepare_count(1);
 
254
 
 
255
drop view t1;
 
256
create table t2 (a int);
 
257
create view t1 as select * from t2 with check option;
 
258
execute stmt;
 
259
call p_verify_reprepare_count(1);
 
260
execute stmt;
 
261
call p_verify_reprepare_count(0);
 
262
select * from t1;
 
263
 
 
264
deallocate prepare stmt;
 
265
drop view t1;
 
266
drop table t2;
 
267
 
 
268
 
 
269
--echo =====================================================================
 
270
--echo Some freestyle tests
 
271
--echo =====================================================================
 
272
 
 
273
create temporary table t1 as select 1 as a;
 
274
delimiter |;
 
275
create procedure p1()
 
276
begin
 
277
  drop temporary table t1;
 
278
end|
 
279
create function f1() returns int
 
280
begin
 
281
  call p1();
 
282
  return 1;
 
283
end|
 
284
delimiter ;|
 
285
 
 
286
prepare stmt from "select f1() as my_column, a from t1";
 
287
--error ER_CANT_REOPEN_TABLE
 
288
execute stmt;
 
289
call p_verify_reprepare_count(0);
 
290
select * from t1;
 
291
 
 
292
prepare stmt from "select a, f1() as my_column from t1";
 
293
--error ER_CANT_REOPEN_TABLE
 
294
execute stmt;
 
295
call p_verify_reprepare_count(0);
 
296
select * from t1;
 
297
 
 
298
prepare stmt from "select f1() as my_column, count(*) from t1";
 
299
--error ER_CANT_REOPEN_TABLE
 
300
execute stmt;
 
301
call p_verify_reprepare_count(0);
 
302
select * from t1;
 
303
 
 
304
prepare stmt from "select count(*), f1() as my_column from t1";
 
305
--error ER_CANT_REOPEN_TABLE
 
306
execute stmt;
 
307
call p_verify_reprepare_count(0);
 
308
select * from t1;
 
309
 
 
310
 
 
311
--echo # Execute fails, no drop of temporary table
 
312
prepare stmt from "select 1 as my_column from (select 1) as t2
 
313
                   where exists (select f1() from t1)";
 
314
execute stmt;
 
315
call p_verify_reprepare_count(0);
 
316
execute stmt;
 
317
call p_verify_reprepare_count(0);
 
318
select * from t1;
 
319
 
 
320
--echo # Execute drops temporary table
 
321
prepare stmt from "select f1()";
 
322
execute stmt;
 
323
call p_verify_reprepare_count(0);
 
324
--error ER_BAD_TABLE_ERROR
 
325
execute stmt;
 
326
call p_verify_reprepare_count(0);
 
327
 
 
328
drop function f1;
 
329
drop procedure p1;
 
330
deallocate prepare stmt;
 
331
 
 
332
--echo # Execute fails, temporary table is not replaced by another
 
333
create temporary table t1 as select 1 as a;
 
334
delimiter |;
 
335
create procedure p1()
 
336
begin
 
337
  drop temporary table t1;
 
338
  create temporary table t1 as select 'abc' as a;
 
339
end|
 
340
create function f1() returns int
 
341
begin
 
342
  call p1();
 
343
  return 1;
 
344
end|
 
345
delimiter ;|
 
346
prepare stmt from "select count(*), f1() as my_column from t1";
 
347
--error ER_CANT_REOPEN_TABLE
 
348
execute stmt;
 
349
call p_verify_reprepare_count(0);
 
350
select * from t1;
 
351
deallocate prepare stmt;
 
352
 
 
353
prepare stmt from "call p1";
 
354
execute stmt;
 
355
drop procedure p1;
 
356
create schema mysqltest;
 
357
delimiter |;
 
358
create procedure mysqltest.p1()
 
359
begin
 
360
   drop schema mysqltest;
 
361
   create schema mysqltest;
 
362
end|
 
363
delimiter ;|
 
364
--error ER_SP_DOES_NOT_EXIST
 
365
execute stmt;
 
366
call p_verify_reprepare_count(1);
 
367
--error ER_SP_DOES_NOT_EXIST
 
368
execute stmt;
 
369
call p_verify_reprepare_count(0);
 
370
deallocate prepare stmt;
 
371
drop schema mysqltest;
 
372
drop temporary table t1;
 
373
 
 
374
 
 
375
# Bug#36089 drop temp table in SP called by function, crash
 
376
# Note: A non prepared "select 1 from t1 having count(*) = f1();" is sufficient. 
 
377
if (0)
 
378
{
 
379
create temporary table t1 as select 1 as a;
 
380
prepare stmt from "select 1 from t1 having count(*) = f1()";
 
381
execute stmt;
 
382
call p_verify_reprepare_count(0);
 
383
deallocate prepare stmt;
 
384
drop temporary table t1;
 
385
}
 
386
 
 
387
 
 
388
--echo # Cleanup
 
389
--echo #
 
390
--disable_warnings
 
391
drop temporary table if exists t1;
 
392
drop table if exists t1, t2;
 
393
drop procedure if exists p_verify_reprepare_count;
 
394
drop procedure if exists p1;
 
395
drop function if exists f1;
 
396
drop view if exists t1;
 
397
drop schema if exists mysqltest;
 
398
--enable_warnings