Commit e47ded81 authored by unknown's avatar unknown
Browse files

A fix and test cases for

Bug#4968 "Stored procedure crash if cursor opened on altered table"
Bug#19733 "Repeated alter, or repeated create/drop, fails"
Bug#19182 "CREATE TABLE bar (m INT) SELECT n FROM foo; doesn't work from 
stored procedure."
Bug#6895 "Prepared Statements: ALTER TABLE DROP COLUMN does nothing"
Bug#22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"

Test cases for bugs 4968, 19733, 6895 will be added in 5.0.

Re-execution of CREATE DATABASE, CREATE TABLE and ALTER TABLE 
statements in stored routines or as prepared statements caused
incorrect results (and crashes in versions prior to 5.0.25).
In 5.1 the problem occured only for CREATE DATABASE, CREATE TABLE
SELECT and CREATE TABLE with INDEX/DATA DIRECTOY options).

The problem of bugs 4968, 19733, 19282 and 6895 was that functions
mysql_prepare_table, mysql_create_table and mysql_alter_table were not
re-execution friendly: during their operation they used to modify contents
of LEX (members create_info, alter_info, key_list, create_list),
thus making the LEX unusable for the next execution.
In particular, these functions removed processed columns and keys from
create_list, key_list and drop_list. Search the code in sql_table.cc 
for drop_it.remove() and similar patterns to find evidence.

The fix is to supply to these functions a usable copy of each of the
above structures at every re-execution of an SQL statement. 

To simplify memory management, LEX::key_list and LEX::create_list
were added to LEX::alter_info, a fresh copy of which is created for
every execution.

The problem of crashing bug 22060 stemmed from the fact that the above 
metnioned functions were not only modifying HA_CREATE_INFO structure in 
LEX, but also were changing it to point to areas in volatile memory of 
the execution memory root.
 
The patch solves this problem by creating and using an on-stack
copy of HA_CREATE_INFO (note that code in 5.1 already creates and
uses a copy of this structure in mysql_create_table()/alter_table(),
but this approach didn't work well for CREATE TABLE SELECT statement).


mysql-test/r/ps.result:
  Update test results (Bug#19182, Bug#22060)
mysql-test/t/ps.test:
  Add a test case for Bug#19182, Bug#22060 (4.1-only parts)
sql/mysql_priv.h:
  LEX::key_list and LEX::create_list were moved to LEX::alter_info.
  Update declarations to use LEX::alter_info instead of these two
  members.
sql/sql_class.h:
  Replace pair<columns, keys> with an instance of Alter_info in
  select_create constructor. We create a new copy of Alter_info
  each time we re-execute SELECT .. CREATE prepared statement.
sql/sql_insert.cc:
  Adjust to a new signature of create_table_from_items.
sql/sql_lex.cc:
  Implement Alter_info::Alter_info that would make a "deep" copy
  of all definition lists (keys, columns).
sql/sql_lex.h:
  Move key_list and create_list to class Alter_info. Implement
  Alter_info::Alter_info that can be used with PS and SP.
sql/sql_list.h:
  Implement a copy constructor of class List that makes a deep copy
  of all list nodes.
sql/sql_parse.cc:
  Adjust to new signatures of mysql_create_table, mysql_alter_table,
  select_create. Functions mysql_create_index and mysql_drop_index has
  become identical after initialization of alter_info was moved to the 
  parser, and were merged. Flag enable_slow_log was not updated for 
  SQLCOM_DROP_INDEX, which is a bug. Just like CREATE INDEX, DROP INDEX
  is currently done via complete table rebuild and is rightfully a slow
  administrative statement.
sql/sql_show.cc:
  Adjust mysqld_show_create_db to a new signature.
sql/sql_table.cc:
  Adjust mysql_alter_table, mysql_recreate_table, mysql_create_table,
  mysql_prepare_table to new signatures.
sql/sql_yacc.yy:
  LEX::key_list and LEX::create_list moved to class Alter_info
parent 73079a24
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
@@ -1035,4 +1035,71 @@ EXECUTE stmt USING @a;
0	0
DEALLOCATE PREPARE stmt;
DROP TABLE t1;
DROP TABLE IF EXISTS t1, t2;
CREATE TABLE t1 (i INT);
PREPARE st_19182
FROM "CREATE TABLE t2 (i INT, j INT, KEY (i), KEY(j)) SELECT i FROM t1";
EXECUTE st_19182;
DESC t2;
Field	Type	Null	Key	Default	Extra
j	int(11)	YES	MUL	NULL	
i	int(11)	YES	MUL	NULL	
DROP TABLE t2;
EXECUTE st_19182;
DESC t2;
Field	Type	Null	Key	Default	Extra
j	int(11)	YES	MUL	NULL	
i	int(11)	YES	MUL	NULL	
DEALLOCATE PREPARE st_19182;
DROP TABLE t2, t1;
drop database if exists mysqltest;
drop table if exists t1, t2;
create database mysqltest character set utf8;
prepare stmt1 from "create table mysqltest.t1 (c char(10))";
prepare stmt2 from "create table mysqltest.t2 select 'test'";
execute stmt1;
execute stmt2;
show create table mysqltest.t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `c` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
show create table mysqltest.t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `test` char(4) character set latin1 NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=utf8
drop table mysqltest.t1;
drop table mysqltest.t2;
alter database mysqltest character set latin1;
execute stmt1;
execute stmt2;
show create table mysqltest.t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `c` char(10) character set utf8 default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
show create table mysqltest.t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `test` char(4) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop database mysqltest;
deallocate prepare stmt1;
deallocate prepare stmt2;
execute stmt;
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `c` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 DATA DIRECTORY='MYSQLTEST_VARDIR/tmp/'
drop table t1;
execute stmt;
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `c` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 DATA DIRECTORY='MYSQLTEST_VARDIR/tmp/'
drop table t1;
deallocate prepare stmt;
End of 4.1 tests.
+72 −0
Original line number Diff line number Diff line
@@ -1068,5 +1068,77 @@ EXECUTE stmt USING @a;
DEALLOCATE PREPARE stmt;
DROP TABLE t1;

#
# Bug#19182: CREATE TABLE bar (m INT) SELECT n FROM foo; doesn't work
# from stored procedure.
#
# The cause of a bug was that cached LEX::create_list was modified,
# and then together with LEX::key_list was reset.
#
--disable_warnings
DROP TABLE IF EXISTS t1, t2;
--enable_warnings

CREATE TABLE t1 (i INT);

PREPARE st_19182
FROM "CREATE TABLE t2 (i INT, j INT, KEY (i), KEY(j)) SELECT i FROM t1";

EXECUTE st_19182;
DESC t2;

DROP TABLE t2;

# Check that on second execution we don't loose 'j' column and the keys
# on 'i' and 'j' columns.
EXECUTE st_19182;
DESC t2;

DEALLOCATE PREPARE st_19182;
DROP TABLE t2, t1;

#
# Bug #22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
#
# Code which implemented CREATE/ALTER TABLE and CREATE DATABASE
# statement modified HA_CREATE_INFO structure in LEX, making these
# statements PS/SP-unsafe (their re-execution might have resulted
# in incorrect results).
#
--disable_warnings
drop database if exists mysqltest;
drop table if exists t1, t2;
--enable_warnings
# CREATE TABLE and CREATE TABLE ... SELECT
create database mysqltest character set utf8;
prepare stmt1 from "create table mysqltest.t1 (c char(10))";
prepare stmt2 from "create table mysqltest.t2 select 'test'";
execute stmt1;
execute stmt2;
show create table mysqltest.t1;
show create table mysqltest.t2;
drop table mysqltest.t1;
drop table mysqltest.t2;
alter database mysqltest character set latin1;
execute stmt1;
execute stmt2;
show create table mysqltest.t1;
show create table mysqltest.t2;
drop database mysqltest;
deallocate prepare stmt1;
deallocate prepare stmt2;
# CREATE TABLE with DATA DIRECTORY option
--disable_query_log
eval prepare stmt from "create table t1 (c char(10)) data directory='$MYSQLTEST_VARDIR/tmp'";
--enable_query_log
execute stmt;
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
show create table t1;
drop table t1;
execute stmt;
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
show create table t1;
drop table t1;
deallocate prepare stmt;
#
--echo End of 4.1 tests.
+7 −12
Original line number Diff line number Diff line
@@ -563,25 +563,22 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info,
		       uint &key_count, int select_field_count);
int mysql_create_table(THD *thd,const char *db, const char *table_name,
		       HA_CREATE_INFO *create_info,
		       List<create_field> &fields, List<Key> &keys,
                       Alter_info *alter_info,
		       bool tmp_table, uint select_field_count);

TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
			       const char *db, const char *name,
			       List<create_field> *extra_fields,
			       List<Key> *keys,
                               Alter_info *alter_info,
			       List<Item> *items,
			       MYSQL_LOCK **lock);
int mysql_alter_table(THD *thd, char *new_db, char *new_name,
		      HA_CREATE_INFO *create_info,
		      TABLE_LIST *table_list,
		      List<create_field> &fields,
		      List<Key> &keys,
                      Alter_info *alter_info,
		      uint order_num, ORDER *order,
		      enum enum_duplicates handle_duplicates,
                      bool ignore,
		      ALTER_INFO *alter_info, bool do_send_ok=1);
int mysql_recreate_table(THD *thd, TABLE_LIST *table_list, bool do_send_ok);
                      bool ignore);
int mysql_recreate_table(THD *thd, TABLE_LIST *table_list);
int mysql_create_like_table(THD *thd, TABLE_LIST *table,
                            HA_CREATE_INFO *create_info,
                            Table_ident *src_table);
@@ -590,9 +587,6 @@ bool mysql_rename_table(enum db_type base,
			const char * old_name,
			const char *new_db,
			const char * new_name);
int mysql_create_index(THD *thd, TABLE_LIST *table_list, List<Key> &keys);
int mysql_drop_index(THD *thd, TABLE_LIST *table_list,
		     ALTER_INFO *alter_info);
int mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
			 TABLE_LIST *update_table_list,
			 Item **conds, uint order_num, ORDER *order);
@@ -679,7 +673,8 @@ int get_quote_char_for_identifier(THD *thd, const char *name, uint length);
void mysqld_list_fields(THD *thd,TABLE_LIST *table, const char *wild);
int mysqld_dump_create_info(THD *thd, TABLE *table, int fd = -1);
int mysqld_show_create(THD *thd, TABLE_LIST *table_list);
int mysqld_show_create_db(THD *thd, char *dbname, HA_CREATE_INFO *create);
int mysqld_show_create_db(THD *thd, char *dbname,
                          const HA_CREATE_INFO *create);

void mysqld_list_processes(THD *thd,const char *user,bool verbose);
int mysqld_show_status(THD *thd);
+10 −9
Original line number Diff line number Diff line
@@ -1293,20 +1293,21 @@ class select_create: public select_insert {
  ORDER *group;
  const char *db;
  const char *name;
  List<create_field> *extra_fields;
  List<Key> *keys;
  HA_CREATE_INFO *create_info;
  Alter_info *alter_info;
  MYSQL_LOCK *lock;
  Field **field;
public:
  select_create(const char *db_name, const char *table_name,
		HA_CREATE_INFO *create_info_par,
		List<create_field> &fields_par,
		List<Key> &keys_par,
		List<Item> &select_fields,enum_duplicates duplic, bool ignore)
    :select_insert (NULL, &select_fields, duplic, ignore), db(db_name),
    name(table_name), extra_fields(&fields_par),keys(&keys_par),
    create_info(create_info_par), lock(0)
                HA_CREATE_INFO *create_info_arg,
                Alter_info *alter_info_arg,
                List<Item> &select_fields,
                enum_duplicates duplic, bool ignore)
    :select_insert(NULL, &select_fields, duplic, ignore),
    db(db_name), name(table_name),
    create_info(create_info_arg),
    alter_info(alter_info_arg),
    lock(0)
    {}
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  void store_values(List<Item> &values);
+1 −1
Original line number Diff line number Diff line
@@ -1808,7 +1808,7 @@ select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)

  unit= u;
  table= create_table_from_items(thd, create_info, db, name,
				 extra_fields, keys, &values, &lock);
                                 alter_info, &values, &lock);
  if (!table)
    DBUG_RETURN(-1);				// abort() deletes table

Loading