Commit b11f1d0c authored by unknown's avatar unknown

5.1 version of a fix and test cases for bugs:

Bug#4968 ""Stored procedure crash if cursor opened on altered table"
Bug#6895 "Prepared Statements: ALTER TABLE DROP COLUMN does nothing"
Bug#19182 "CREATE TABLE bar (m INT) SELECT n FROM foo; doesn't work from 
stored procedure."
Bug#19733 "Repeated alter, or repeated create/drop, fails"
Bug#22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
Bug#24879 "Prepared Statements: CREATE TABLE (UTF8 KEY) produces a 
growing key length" (this bug is not fixed 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 are not
re-execution friendly: during their operation they 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 in mysql_execute_command.

Additionally, this patch splits the part of mysql_alter_table
that analizes and rewrites information from the parser into
a separate function - mysql_prepare_alter_table, in analogy with
mysql_prepare_table, which is renamed to mysql_prepare_create_table.


mysql-test/r/ps.result:
  Update test results (Bug#19182, Bug#22060, Bug#4968, Bug#6895)
mysql-test/r/sp.result:
  Update results (Bug#19733)
mysql-test/t/ps.test:
  Add test cases for Bug#19182, Bug#22060, Bug#4968, Bug#6895
mysql-test/t/sp.test:
  Add a test case for Bug#19733
sql/field.h:
  Implement a deep copy constructor for create_field
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.
  Remove declarations of mysql_add_index, mysql_drop_index.
sql/sql_class.cc:
  Implement deep copy constructors.
sql/sql_class.h:
  Implement (almost) deep copy constructors for key_part_spec, 
  Alter_drop, Alter_column, Key, foreign_key.
  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).
  Move is_partition_management() from sql_partition.cc (feature-based
  file division is evil).
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.
  Get rid of Alter_info::clear() which was an attempt to save on
  matches and always use Alter_info::reset().
  Implement an auxiliary Alter_info::init_for_create_from_alter()
  which is used in mysql_alter_table.
sql/sql_list.cc:
    Implement a copy constructor of class List that makes a deep copy
    of all list nodes.
sql/sql_list.h:
  Implement a way to make 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 was a bug.
  Just like CREATE INDEX, DROP INDEX is currently done via complete 
  table rebuild and is rightfully a slow administrative statement.
sql/sql_partition.cc:
  Move is_partition_management() to sql_lex.cc
  Adjust code to the new Alter_info.
sql/sql_table.cc:
  Adjust mysql_alter_table, mysql_recreate_table, mysql_create_table,
  mysql_prepare_table to new signatures.
  Rename mysql_prepare_table to mysql_prepare_create_table. Make
  sure it follows the convention and returns FALSE for success and
  TRUE for error.
  Move parts of mysql_alter_table to mysql_prepare_alter_table.
  Move the first invokation of mysql_prepare_table from mysql_alter_table
  to compare_tables, as it was needed only for the purpose
  of correct comparison.
  Since now Alter_info itself is created in the runtime mem root,
  adjust mysql_prepare_table to always allocate memory in the
  runtime memory root.
  Remove dead code.
sql/sql_yacc.yy:
  LEX::key_list and LEX::create_list moved to class Alter_info
parent 850eadad
......@@ -1062,6 +1062,87 @@ 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` varchar(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) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
show create table mysqltest.t2;
Table Create Table
t2 CREATE TABLE `t2` (
`test` varchar(4) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop database mysqltest;
deallocate prepare stmt1;
deallocate prepare stmt2;
execute stmt;
show create table t1;
drop table t1;
execute stmt;
show create table t1;
drop table t1;
deallocate prepare stmt;
CREATE TABLE t1(a int);
INSERT INTO t1 VALUES (2), (3), (1);
PREPARE st1 FROM
'(SELECT a FROM t1) UNION (SELECT a+10 FROM t1) ORDER BY RAND()*0+a';
EXECUTE st1;
a
1
2
3
11
12
13
EXECUTE st1;
a
1
2
3
11
12
13
DEALLOCATE PREPARE st1;
DROP TABLE t1;
End of 4.1 tests.
create table t1 (a varchar(20));
insert into t1 values ('foo');
......@@ -1544,6 +1625,72 @@ a
2
DEALLOCATE PREPARE stmt;
DROP TABLE t1,t2;
drop table if exists t1;
create table t1 (s1 char(20));
prepare stmt from "alter table t1 modify s1 int";
execute stmt;
execute stmt;
drop table t1;
deallocate prepare stmt;
drop table if exists t1;
create table t1 (a int, b int);
prepare s_6895 from "alter table t1 drop column b";
execute s_6895;
show columns from t1;
Field Type Null Key Default Extra
a int(11) YES NULL
drop table t1;
create table t1 (a int, b int);
execute s_6895;
show columns from t1;
Field Type Null Key Default Extra
a int(11) YES NULL
drop table t1;
create table t1 (a int, b int);
execute s_6895;
show columns from t1;
Field Type Null Key Default Extra
a int(11) YES NULL
deallocate prepare s_6895;
drop table t1;
create table t1 (i int primary key auto_increment) comment='comment for table t1';
create table t2 (i int, j int, k int);
prepare stmt from "alter table t1 auto_increment=100";
execute stmt;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`i`)
) ENGINE=MyISAM AUTO_INCREMENT=100 DEFAULT CHARSET=latin1 COMMENT='comment for table t1'
flush tables;
select * from t2;
i j k
execute stmt;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`i`)
) ENGINE=MyISAM AUTO_INCREMENT=100 DEFAULT CHARSET=latin1 COMMENT='comment for table t1'
deallocate prepare stmt;
drop table t1, t2;
set @old_character_set_server= @@character_set_server;
set @@character_set_server= latin1;
prepare stmt from "create database mysqltest_1";
execute stmt;
show create database mysqltest_1;
Database Create Database
mysqltest_1 CREATE DATABASE `mysqltest_1` /*!40100 DEFAULT CHARACTER SET latin1 */
drop database mysqltest_1;
set @@character_set_server= utf8;
execute stmt;
show create database mysqltest_1;
Database Create Database
mysqltest_1 CREATE DATABASE `mysqltest_1` /*!40100 DEFAULT CHARACTER SET utf8 */
drop database mysqltest_1;
deallocate prepare stmt;
set @@character_set_server= @old_character_set_server;
drop tables if exists t1;
create table t1 (id int primary key auto_increment, value varchar(10));
insert into t1 (id, value) values (1, 'FIRST'), (2, 'SECOND'), (3, 'THIRD');
......@@ -2524,4 +2671,25 @@ i j
4 5
3 NULL
DROP TABLE t1, t2;
drop table if exists t1;
Warnings:
Note 1051 Unknown table 't1'
prepare stmt
from "create table t1 (c char(100) character set utf8, key (c(10)))";
execute stmt;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c` char(100) CHARACTER SET utf8 DEFAULT NULL,
KEY `c` (`c`(10))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
execute stmt;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c` char(100) CHARACTER SET utf8 DEFAULT NULL,
KEY `c` (`c`(10))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
End of 5.1 tests.
......@@ -5617,6 +5617,23 @@ Called B
Called B
drop procedure proc_21462_a|
drop procedure proc_21462_b|
drop table if exists t3|
drop procedure if exists proc_bug19733|
create table t3 (s1 int)|
create procedure proc_bug19733()
begin
declare v int default 0;
while v < 100 do
create index i on t3 (s1);
drop index i on t3;
set v = v + 1;
end while;
end|
call proc_bug19733()|
call proc_bug19733()|
call proc_bug19733()|
drop procedure proc_bug19733|
drop table t3|
DROP PROCEDURE IF EXISTS p1|
DROP VIEW IF EXISTS v1, v2|
DROP TABLE IF EXISTS t3, t4|
......
......@@ -1118,6 +1118,113 @@ 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
#
# Protect ourselves from data left in tmp/ by a previos possibly failed
# test
--system rm -f $MYSQLTEST_VARDIR/tmp/t1.*
--disable_warnings
--disable_query_log
eval prepare stmt from "create table t1 (c char(10)) data directory='$MYSQLTEST_VARDIR/tmp'";
--enable_query_log
execute stmt;
#
# DATA DIRECTORY option does not always work: if the operating
# system does not support symlinks, have_symlinks option is automatically
# disabled.
# In this case DATA DIRECTORY is silently ignored when
# creating a table, and is not output by SHOW CREATE TABLE.
#
--disable_result_log
show create table t1;
--enable_result_log
drop table t1;
execute stmt;
--disable_result_log
show create table t1;
--enable_result_log
--enable_warnings
drop table t1;
deallocate prepare stmt;
#
#
# Bug #27937: crash on the second execution for prepared statement
# from UNION with ORDER BY an expression containing RAND()
#
CREATE TABLE t1(a int);
INSERT INTO t1 VALUES (2), (3), (1);
PREPARE st1 FROM
'(SELECT a FROM t1) UNION (SELECT a+10 FROM t1) ORDER BY RAND()*0+a';
EXECUTE st1;
EXECUTE st1;
DEALLOCATE PREPARE st1;
DROP TABLE t1;
--echo End of 4.1 tests.
############################# 5.0 tests start ################################
......@@ -1596,6 +1703,77 @@ EXECUTE stmt USING @arg;
DEALLOCATE PREPARE stmt;
DROP TABLE t1,t2;
#
# Bug#4968 "Stored procedure crash if cursor opened on altered table"
# The bug is not repeatable any more after the fix for
# Bug#15217 "Bug #15217 Using a SP cursor on a table created with PREPARE
# fails with weird error", however ALTER TABLE is not re-execution friendly
# and that caused a valgrind warning. Check that the warning is gone.
#
--disable_warnings
drop table if exists t1;
--enable_warnings
create table t1 (s1 char(20));
prepare stmt from "alter table t1 modify s1 int";
execute stmt;
execute stmt;
drop table t1;
deallocate prepare stmt;
#
# Bug#6895 "Prepared Statements: ALTER TABLE DROP COLUMN does nothing"
#
--disable_warnings
drop table if exists t1;
--enable_warnings
create table t1 (a int, b int);
prepare s_6895 from "alter table t1 drop column b";
execute s_6895;
show columns from t1;
drop table t1;
create table t1 (a int, b int);
execute s_6895;
show columns from t1;
drop table t1;
create table t1 (a int, b int);
execute s_6895;
show columns from t1;
deallocate prepare s_6895;
drop table t1;
#
# Bug #22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
#
# 5.0 part of the test.
#
# ALTER TABLE
create table t1 (i int primary key auto_increment) comment='comment for table t1';
create table t2 (i int, j int, k int);
prepare stmt from "alter table t1 auto_increment=100";
execute stmt;
show create table t1;
# Let us trash table-cache's memory
flush tables;
select * from t2;
execute stmt;
show create table t1;
deallocate prepare stmt;
drop table t1, t2;
# 5.1 part of the test.
# CREATE DATABASE
set @old_character_set_server= @@character_set_server;
set @@character_set_server= latin1;
prepare stmt from "create database mysqltest_1";
execute stmt;
show create database mysqltest_1;
drop database mysqltest_1;
set @@character_set_server= utf8;
execute stmt;
show create database mysqltest_1;
drop database mysqltest_1;
deallocate prepare stmt;
set @@character_set_server= @old_character_set_server;
#
......@@ -2570,5 +2748,21 @@ connection default;
DROP TABLE t1, t2;
#
# Bug #24879 Prepared Statements: CREATE TABLE (UTF8 KEY) produces a growing
# key length
#
# Test that parse information is not altered by subsequent executions of a
# prepared statement
#
drop table if exists t1;
prepare stmt
from "create table t1 (c char(100) character set utf8, key (c(10)))";
execute stmt;
show create table t1;
drop table t1;
execute stmt;
show create table t1;
drop table t1;
--echo End of 5.1 tests.
......@@ -6569,6 +6569,34 @@ drop procedure proc_21462_a|
drop procedure proc_21462_b|
#
# Bug#19733 "Repeated alter, or repeated create/drop, fails"
# Check that CREATE/DROP INDEX is re-execution friendly.
#
--disable_warnings
drop table if exists t3|
drop procedure if exists proc_bug19733|
--enable_warnings
create table t3 (s1 int)|
create procedure proc_bug19733()
begin
declare v int default 0;
while v < 100 do
create index i on t3 (s1);
drop index i on t3;
set v = v + 1;
end while;
end|
call proc_bug19733()|
call proc_bug19733()|
call proc_bug19733()|
drop procedure proc_bug19733|
drop table t3|
#
# BUG#20492: Subsequent calls to stored procedure yeild incorrect
# result if join is used
......
......@@ -1622,6 +1622,9 @@ class create_field :public Sql_alloc
uint offset,pack_flag;
create_field() :after(0) {}
create_field(Field *field, Field *orig_field);
/* Used to make a clone of this object for ALTER/CREATE TABLE */
create_field *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) create_field(*this); }
void create_length_to_internal_length(void);
/* Init for a tmp table field. To be extended if need be. */
......
......@@ -960,32 +960,26 @@ int prepare_create_field(create_field *sql_field,
longlong table_flags);
bool mysql_create_table(THD *thd,const char *db, const char *table_name,
HA_CREATE_INFO *create_info,
List<create_field> &fields, List<Key> &keys,
bool tmp_table, uint select_field_count,
bool use_copy_create_info);
Alter_info *alter_info,
bool tmp_table, uint select_field_count);
bool mysql_create_table_no_lock(THD *thd, const char *db,
const char *table_name,
HA_CREATE_INFO *create_info,
List<create_field> &fields, List<Key> &keys,
bool tmp_table, uint select_field_count,
bool use_copy_create_info);
Alter_info *alter_info,
bool tmp_table, uint select_field_count);
bool 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,
uint order_num, ORDER *order, bool ignore,
ALTER_INFO *alter_info, bool do_send_ok);
bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list, bool do_send_ok);
bool mysql_create_like_table(THD *thd, TABLE_LIST *table, TABLE_LIST *src_table,
Alter_info *alter_info,
uint order_num, ORDER *order, bool ignore);
bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list);
bool mysql_create_like_table(THD *thd, TABLE_LIST *table,
TABLE_LIST *src_table,
HA_CREATE_INFO *create_info);
bool mysql_rename_table(handlerton *base, const char *old_db,
const char * old_name, const char *new_db,
const char * new_name, uint flags);
bool mysql_create_index(THD *thd, TABLE_LIST *table_list, List<Key> &keys);
bool mysql_drop_index(THD *thd, TABLE_LIST *table_list,
ALTER_INFO *alter_info);
bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
Item **conds, uint order_num, ORDER *order);
int mysql_update(THD *thd,TABLE_LIST *tables,List<Item> &fields,
......@@ -1309,14 +1303,13 @@ char *make_default_log_name(char *buff,const char* log_ext);
#ifdef WITH_PARTITION_STORAGE_ENGINE
uint fast_alter_partition_table(THD *thd, TABLE *table,
ALTER_INFO *alter_info,
Alter_info *alter_info,
HA_CREATE_INFO *create_info,
TABLE_LIST *table_list,
List<create_field> *create_list,
List<Key> *key_list, char *db,
char *db,
const char *table_name,
uint fast_alter_partition);
uint prep_alter_part_table(THD *thd, TABLE *table, ALTER_INFO *alter_info,
uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info,
HA_CREATE_INFO *create_info,
handlerton *old_db_type,
bool *partition_changed,
......@@ -1348,11 +1341,7 @@ typedef struct st_lock_param_type
ulonglong deleted;
THD *thd;
HA_CREATE_INFO *create_info;
ALTER_INFO *alter_info;
List<create_field> *create_list;
List<create_field> new_create_list;
List<Key> *key_list;
List<Key> new_key_list;
Alter_info *alter_info;
TABLE *table;
KEY *key_info_buffer;
const char *db;
......
......@@ -91,6 +91,40 @@ bool key_part_spec::operator==(const key_part_spec& other) const
return length == other.length && !strcmp(field_name, other.field_name);
}
/**
Construct an (almost) deep copy of this key. Only those
elements that are known to never change are not copied.
If out of memory, a partial copy is returned and an error is set
in THD.
*/
Key::Key(const Key &rhs, MEM_ROOT *mem_root)
:type(rhs.type),
key_create_info(rhs.key_create_info),
columns(rhs.columns, mem_root),
name(rhs.name),
generated(rhs.generated)
{
list_copy_and_replace_each_value(columns, mem_root);
}
/**
Construct an (almost) deep copy of this foreign key. Only those
elements that are known to never change are not copied.
If out of memory, a partial copy is returned and an error is set
in THD.
*/
foreign_key::foreign_key(const foreign_key &rhs, MEM_ROOT *mem_root)
:Key(rhs),
ref_table(rhs.ref_table),
ref_columns(rhs.ref_columns),
delete_opt(rhs.delete_opt),
update_opt(rhs.update_opt),
match_opt(rhs.match_opt)
{
list_copy_and_replace_each_value(ref_columns, mem_root);
}
/*
Test if a foreign key (= generated key) is a prefix of the given key
......
......@@ -90,6 +90,17 @@ class key_part_spec :public Sql_alloc {
uint length;
key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {}
bool operator==(const key_part_spec& other) const;
/**
Construct a copy of this key_part_spec. field_name is copied
by-pointer as it is known to never change. At the same time
'length' may be reset in mysql_prepare_create_table, and this
is why we supply it with a copy.
@return If out of memory, 0 is returned and an error is set in
THD.
*/
key_part_spec *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) key_part_spec(*this); }
};
......@@ -100,6 +111,12 @@ class Alter_drop :public Sql_alloc {
enum drop_type type;
Alter_drop(enum drop_type par_type,const char *par_name)
:name(par_name), type(par_type) {}
/**
Used to make a clone of this object for ALTER/CREATE TABLE
@sa comment for key_part_spec::clone
*/
Alter_drop *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) Alter_drop(*this); }
};
......@@ -109,6 +126,12 @@ class Alter_column :public Sql_alloc {
Item *def;
Alter_column(const char *par_name,Item *literal)
:name(par_name), def(literal) {}
/**
Used to make a clone of this object for ALTER/CREATE TABLE
@sa comment for key_part_spec::clone
*/
Alter_column *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) Alter_column(*this); }
};
......@@ -127,9 +150,16 @@ class Key :public Sql_alloc {
:type(type_par), key_create_info(*key_info_arg), columns(cols),
name(name_arg), generated(generated_arg)
{}
~Key() {}
Key(const Key &rhs, MEM_ROOT *mem_root);
virtual ~Key() {}
/* Equality comparison of keys (ignoring name) */
friend bool foreign_key_prefix(Key *a, Key *b);
/**
Used to make a clone of this object for ALTER/CREATE TABLE
@sa comment for key_part_spec::clone
*/
virtual Key *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) Key(*this, mem_root); }
};
class Table_ident;
......@@ -152,6 +182,13 @@ class foreign_key: public Key {
delete_opt(delete_opt_arg), update_opt(update_opt_arg),
match_opt(match_opt_arg)
{}
foreign_key(const foreign_key &rhs, MEM_ROOT *mem_root);
/**
Used to make a clone of this object for ALTER/CREATE TABLE
@sa comment for key_part_spec::clone
*/
virtual Key *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) foreign_key(*this, mem_root); }
};
typedef struct st_mysql_lock
......@@ -1944,19 +1981,19 @@ class select_insert :public select_result_interceptor {
class select_create: public select_insert {
ORDER *group;
TABLE_LIST *create_table;
List<create_field> *extra_fields;
List<Key> *keys;
HA_CREATE_INFO *create_info;
Alter_info *alter_info;
Field **field;
public:
select_create (TABLE_LIST *table_arg,
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, NULL, &select_fields, 0, 0, duplic, ignore),
create_table(table_arg), extra_fields(&fields_par),keys(&keys_par),
create_info(create_info_par)
select_create(TABLE_LIST *table_arg,
HA_CREATE_INFO *create_info_arg,
Alter_info *alter_info_arg,
List<Item> &select_fields,
enum_duplicates duplic, bool ignore)
:select_insert(NULL, NULL, &select_fields, 0, 0, duplic, ignore),
create_table(table_arg),
create_info(create_info_arg),
alter_info(alter_info_arg)
{}
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
......
......@@ -3138,11 +3138,11 @@ bool select_insert::send_eof()
temporary table flag)
create_table in Pointer to TABLE_LIST object providing database
and name for table to be created or to be open
extra_fields in/out Initial list of fields for table to be created
keys in List of keys for table to be created
alter_info in/out Initial list of columns and indexes for the table
to be created
items in List of items which should be used to produce rest
of fields for the table (corresponding fields will
be added to the end of 'extra_fields' list)
be added to the end of alter_info->create_list)
lock out Pointer to the MYSQL_LOCK object for table created
(or open temporary table) will be returned in this
parameter. Since this table is not included in
......@@ -3171,8 +3171,7 @@ bool select_insert::send_eof()
static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
TABLE_LIST *create_table,
List<create_field> *extra_fields,
List<Key> *keys,
Alter_info *alter_info,
List<Item> *items,
MYSQL_LOCK **lock,
TABLEOP_HOOKS *hooks)
......@@ -3236,7 +3235,7 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
DBUG_RETURN(0);
if (item->maybe_null)
cr_field->flags &= ~NOT_NULL_FLAG;
extra_fields->push_back(cr_field);
alter_info->create_list.push_back(cr_field);
}
DBUG_EXECUTE_IF("sleep_create_select_before_create", my_sleep(6000000););
......@@ -3261,8 +3260,8 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
tmp_disable_binlog(thd);
if (!mysql_create_table_no_lock(thd, create_table->db,
create_table->table_name,
create_info, *extra_fields, *keys, 0,
select_field_count, 0))
create_info, alter_info, 0,
select_field_count))
{
if (create_info->table_existed &&
......@@ -3388,7 +3387,7 @@ select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
}
if (!(table= create_table_from_items(thd, create_info, create_table,
extra_fields, keys, &values,
alter_info, &values,
&thd->extra_lock, hook_ptr)))
DBUG_RETURN(-1); // abort() deletes table
......
......@@ -1118,6 +1118,34 @@ int MYSQLlex(void *arg, void *yythd)
}
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
:drop_list(rhs.drop_list, mem_root),
alter_list(rhs.alter_list, mem_root),
key_list(rhs.key_list, mem_root),
create_list(rhs.create_list, mem_root),
flags(rhs.flags),
keys_onoff(rhs.keys_onoff),
tablespace_op(rhs.tablespace_op),
partition_names(rhs.partition_names, mem_root),
no_parts(rhs.no_parts)
{
/*
Make deep copies of used objects.
This is not a fully deep copy - clone() implementations
of Alter_drop, Alter_column, Key, foreign_key, key_part_spec
do not copy string constants. At the same length the only
reason we make a copy currently is that ALTER/CREATE TABLE
code changes input Alter_info definitions, but string
constants never change.
*/
list_copy_and_replace_each_value(drop_list, mem_root);
list_copy_and_replace_each_value(alter_list, mem_root);
list_copy_and_replace_each_value(key_list, mem_root);
list_copy_and_replace_each_value(create_list, mem_root);
/* partition_names are not deeply copied currently */
}
/*
Skip comment in the end of statement.
......@@ -2379,3 +2407,22 @@ bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
current_index_hint_clause,
str, length));
}
/**
A routine used by the parser to decide whether we are specifying a full
partitioning or if only partitions to add or to split.
@note This needs to be outside of WITH_PARTITION_STORAGE_ENGINE since it
is used from the sql parser that doesn't have any #ifdef's
@retval TRUE Yes, it is part of a management partition command
@retval FALSE No, not a management partition command
*/
bool st_lex::is_partition_management() const
{
return (sql_command == SQLCOM_ALTER_TABLE &&
(alter_info.flags == ALTER_ADD_PARTITION ||
alter_info.flags == ALTER_REORGANIZE_PARTITION));
}
......@@ -830,26 +830,60 @@ inline bool st_select_lex_unit::is_union ()
#define ALTER_REMOVE_PARTITIONING (1L << 25)
#define ALTER_FOREIGN_KEY (1L << 26)
typedef struct st_alter_info
/**
@brief Parsing data for CREATE or ALTER TABLE.
This structure contains a list of columns or indexes to be created,
altered or dropped.
*/
class Alter_info
{
public:
List<Alter_drop> drop_list;
List<Alter_column> alter_list;
List<Key> key_list;
List<create_field> create_list;
uint flags;
enum enum_enable_or_disable keys_onoff;
enum tablespace_op_type tablespace_op;
List<char> partition_names;
uint no_parts;
st_alter_info(){clear();}
void clear()
Alter_info() :
flags(0),
keys_onoff(LEAVE_AS_IS),
tablespace_op(NO_TABLESPACE_OP),
no_parts(0)
{}
void reset()
{
drop_list.empty();
alter_list.empty();
key_list.empty();
create_list.empty();
flags= 0;
keys_onoff= LEAVE_AS_IS;
tablespace_op= NO_TABLESPACE_OP;
no_parts= 0;
partition_names.empty();
}
void reset(){drop_list.empty();alter_list.empty();clear();}
} ALTER_INFO;
/**
Construct a copy of this object to be used for mysql_alter_table
and mysql_create_table. Historically, these two functions modify
their Alter_info arguments. This behaviour breaks re-execution of
prepared statements and stored procedures and is compensated by
always supplying a copy of Alter_info to these functions.
@return You need to use check the error in THD for out
of memory condition after calling this function.
*/
Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
private:
Alter_info &operator=(const Alter_info &rhs); // not implemented
Alter_info(const Alter_info &rhs); // not implemented
};
struct st_sp_chistics
{
......@@ -1103,8 +1137,6 @@ typedef struct st_lex : public Query_tables_list
List<String> interval_list;
List<LEX_USER> users_list;
List<LEX_COLUMN> columns;
List<Key> key_list;
List<create_field> create_list;
List<Item> *insert_list,field_list,value_list,update_list;
List<List_item> many_values;
List<set_var_base> var_list;
......@@ -1202,7 +1234,7 @@ typedef struct st_lex : public Query_tables_list
bool safe_to_cache_query;
bool subqueries, ignore;
st_parsing_options parsing_options;
ALTER_INFO alter_info;
Alter_info alter_info;
/* Prepared statements SQL syntax:*/
LEX_STRING prepared_stmt_name; /* Statement name (in all queries) */
/*
......@@ -1361,6 +1393,7 @@ typedef struct st_lex : public Query_tables_list
void restore_backup_query_tables_list(Query_tables_list *backup);
bool table_or_sp_used();
bool is_partition_management() const;
} LEX;
struct st_lex_local: public st_lex
......
......@@ -36,3 +36,37 @@ void free_list(I_List <i_string> *list)
while ((tmp= list->get()))
delete tmp;
}
base_list::base_list(const base_list &rhs, MEM_ROOT *mem_root)
{
if (rhs.elements)
{
/*
It's okay to allocate an array of nodes at once: we never
call a destructor for list_node objects anyway.
*/
first= (list_node*) alloc_root(mem_root,
sizeof(list_node) * rhs.elements);
if (first)
{
elements= rhs.elements;
list_node *dst= first;
list_node *src= rhs.first;
for (; dst < first + elements - 1; dst++, src= src->next)
{
dst->info= src->info;
dst->next= dst + 1;
}
/* Copy the last node */
dst->info= src->info;
dst->next= &end_of_list;
/* Setup 'last' member */
last= &dst->next;
return;
}
}
elements= 0;
first= &end_of_list;
last= &first;
}
#ifndef INCLUDES_MYSQL_SQL_LIST_H
#define INCLUDES_MYSQL_SQL_LIST_H
/* Copyright (C) 2000-2003 MySQL AB
This program is free software; you can redistribute it and/or modify
......@@ -63,9 +65,14 @@ class Sql_alloc
pointer.
*/
class list_node :public Sql_alloc
/**
list_node - a node of a single-linked list.
@note We never call a destructor for instances of this class.
*/
struct list_node :public Sql_alloc
{
public:
list_node *next;
void *info;
list_node(void *info_par,list_node *next_par)
......@@ -73,11 +80,9 @@ class list_node :public Sql_alloc
{}
list_node() /* For end_of_list */
{
info=0;
info= 0;
next= this;
}
friend class base_list;
friend class base_list_iterator;
};
......@@ -93,12 +98,28 @@ class base_list :public Sql_alloc
inline void empty() { elements=0; first= &end_of_list; last=&first;}
inline base_list() { empty(); }
/**
This is a shallow copy constructor that implicitly passes the ownership
from the source list to the new instance. The old instance is not
updated, so both objects end up sharing the same nodes. If one of
the instances then adds or removes a node, the other becomes out of
sync ('last' pointer), while still operational. Some old code uses and
relies on this behaviour. This logic is quite tricky: please do not use
it in any new code.
*/
inline base_list(const base_list &tmp) :Sql_alloc()
{
elements= tmp.elements;
first= tmp.first;
last= elements ? tmp.last : &first;
}
/**
Construct a deep copy of the argument in memory root mem_root.
The elements themselves are copied by pointer. If you also
need to copy elements by value, you should employ
list_copy_and_replace_each_value after creating a copy.
*/
base_list(const base_list &rhs, MEM_ROOT *mem_root);
inline base_list(bool error) { }
inline bool push_back(void *info)
{
......@@ -185,6 +206,15 @@ class base_list :public Sql_alloc
elements+= list->elements;
}
}
/**
Swap two lists.
*/
inline void swap(base_list &rhs)
{
swap_variables(list_node *, first, rhs.first);
swap_variables(list_node **, last, rhs.last);
swap_variables(uint, elements, rhs.elements);
}
inline list_node* last_node() { return *last; }
inline list_node* first_node() { return first;}
inline void *head() { return first->info; }
......@@ -349,6 +379,8 @@ template <class T> class List :public base_list
public:
inline List() :base_list() {}
inline List(const List<T> &tmp) :base_list(tmp) {}
inline List(const List<T> &tmp, MEM_ROOT *mem_root) :
base_list(tmp, mem_root) {}
inline bool push_back(T *a) { return base_list::push_back(a); }
inline bool push_back(T *a, MEM_ROOT *mem_root)
{ return base_list::push_back(a, mem_root); }
......@@ -547,3 +579,32 @@ template <class T> class I_List_iterator :public base_ilist_iterator
I_List_iterator(I_List<T> &a) : base_ilist_iterator(a) {}
inline T* operator++(int) { return (T*) base_ilist_iterator::next(); }
};
/**
Make a deep copy of each list element.
@note A template function and not a template method of class List
is employed because of explicit template instantiation:
in server code there are explicit instantiations of List<T> and
an explicit instantiation of a template requires that any method
of the instantiated class used in the template can be resolved.
Evidently not all template arguments have clone() method with
the right signature.
@return You must query the error state in THD for out-of-memory
situation after calling this function.
*/
template <typename T>
inline
void
list_copy_and_replace_each_value(List<T> &list, MEM_ROOT *mem_root)
{
/* Make a deep copy of each element */
List_iterator<T> it(list);
T *el;
while ((el= it++))
it.replace(el->clone(mem_root));
}
#endif // INCLUDES_MYSQL_SQL_LIST_H
This diff is collapsed.
......@@ -141,30 +141,6 @@ int get_part_iter_for_interval_via_walking(partition_info *part_info,
PARTITION_ITERATOR *part_iter);
static void set_up_range_analysis_info(partition_info *part_info);
/*
A routine used by the parser to decide whether we are specifying a full
partitioning or if only partitions to add or to split.
SYNOPSIS
is_partition_management()
lex Reference to the lex object
RETURN VALUE
TRUE Yes, it is part of a management partition command
FALSE No, not a management partition command
DESCRIPTION
This needs to be outside of WITH_PARTITION_STORAGE_ENGINE since it is
used from the sql parser that doesn't have any #ifdef's
*/
my_bool is_partition_management(LEX *lex)
{
return (lex->sql_command == SQLCOM_ALTER_TABLE &&
(lex->alter_info.flags == ALTER_ADD_PARTITION ||
lex->alter_info.flags == ALTER_REORGANIZE_PARTITION));
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
/*
A support function to check if a name is in a list of strings
......@@ -4125,7 +4101,7 @@ static bool check_native_partitioned(HA_CREATE_INFO *create_info,bool *ret_val,
change patterns.
*/
uint prep_alter_part_table(THD *thd, TABLE *table, ALTER_INFO *alter_info,
uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info,
HA_CREATE_INFO *create_info,
handlerton *old_db_type,
bool *partition_changed,
......@@ -5991,8 +5967,6 @@ void handle_alter_part_error(ALTER_PARTITION_PARAM_TYPE *lpt,
alter_info ALTER TABLE info
create_info Create info for CREATE TABLE
table_list List of the table involved
create_list The fields in the resulting table
key_list The keys in the resulting table
db Database name of new table
table_name Table name of new table
......@@ -6006,11 +5980,10 @@ void handle_alter_part_error(ALTER_PARTITION_PARAM_TYPE *lpt,
*/
uint fast_alter_partition_table(THD *thd, TABLE *table,
ALTER_INFO *alter_info,
Alter_info *alter_info,
HA_CREATE_INFO *create_info,
TABLE_LIST *table_list,
List<create_field> *create_list,
List<Key> *key_list, char *db,
char *db,
const char *table_name,
uint fast_alter_partition)
{
......@@ -6027,8 +6000,6 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
lpt->part_info= part_info;
lpt->alter_info= alter_info;
lpt->create_info= create_info;
lpt->create_list= create_list;
lpt->key_list= key_list;
lpt->db_options= create_info->table_options;
if (create_info->row_type == ROW_TYPE_DYNAMIC)
lpt->db_options|= HA_OPTION_PACK_RECORD;
......@@ -6106,7 +6077,7 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
The first approach here was to downgrade locks. Now a different approach
is decided upon. The idea is that the handler will have access to the
ALTER_INFO when store_lock arrives with TL_WRITE_ALLOW_READ. So if the
Alter_info when store_lock arrives with TL_WRITE_ALLOW_READ. So if the
handler knows that this functionality can be handled with a lower lock
level it will set the lock level to TL_WRITE_ALLOW_WRITE immediately.
Thus the need to downgrade the lock disappears.
......@@ -6379,7 +6350,7 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
user
*/
DBUG_RETURN(fast_end_partition(thd, lpt->copied, lpt->deleted,
table, table_list, FALSE, lpt,
table, table_list, FALSE, NULL,
written_bin_log));
}
#endif
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -491,7 +491,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%pure_parser /* We have threads */
/*
Currently there is 287 shift/reduce conflict. We should not introduce
Currently there is 286 shift/reduce conflict. We should not introduce
new conflicts any more.
*/
%expect 286
......@@ -1569,8 +1569,7 @@ create:
TL_OPTION_UPDATING,
TL_WRITE))
MYSQL_YYABORT;
lex->create_list.empty();
lex->key_list.empty();
lex->alter_info.reset();
lex->col_list.empty();
lex->change=NullS;
bzero((char*) &lex->create_info,sizeof(lex->create_info));
......@@ -1603,21 +1602,23 @@ create:
NULL,
TL_OPTION_UPDATING))
MYSQL_YYABORT;
lex->create_list.empty();
lex->key_list.empty();
lex->alter_info.reset();
lex->alter_info.flags= ALTER_ADD_INDEX;
lex->col_list.empty();
lex->change=NullS;
}
'(' key_list ')' key_options
{
LEX *lex=Lex;
Key *key;
if ($2 != Key::FULLTEXT && lex->key_create_info.parser_name.str)
{
my_parse_error(ER(ER_SYNTAX_ERROR));
MYSQL_YYABORT;
}
lex->key_list.push_back(new Key($2, $4.str, &lex->key_create_info, 0,
lex->col_list));
key= new Key($2, $4.str, &lex->key_create_info, 0,
lex->col_list);
lex->alter_info.key_list.push_back(key);
lex->col_list.empty();
}
| CREATE DATABASE opt_if_not_exists ident
......@@ -3931,7 +3932,7 @@ opt_part_values:
/* empty */
{
LEX *lex= Lex;
if (!is_partition_management(lex))
if (! lex->is_partition_management())
{
if (lex->part_info->part_type == RANGE_PARTITION)
{
......@@ -3952,7 +3953,7 @@ opt_part_values:
| VALUES LESS_SYM THAN_SYM part_func_max
{
LEX *lex= Lex;
if (!is_partition_management(lex))
if (! lex->is_partition_management())
{
if (Lex->part_info->part_type != RANGE_PARTITION)
{
......@@ -3967,7 +3968,7 @@ opt_part_values:
| VALUES IN_SYM '(' part_list_func ')'
{
LEX *lex= Lex;
if (!is_partition_management(lex))
if (! lex->is_partition_management())
{
if (Lex->part_info->part_type != LIST_PARTITION)
{
......@@ -4478,8 +4479,9 @@ key_def:
my_parse_error(ER(ER_SYNTAX_ERROR));
MYSQL_YYABORT;
}
lex->key_list.push_back(new Key($1,$2, &lex->key_create_info, 0,
lex->col_list));
Key *key= new Key($1, $2, &lex->key_create_info, 0,
lex->col_list);
lex->alter_info.key_list.push_back(key);
lex->col_list.empty(); /* Alloced by sql_alloc */
}
| opt_constraint constraint_key_type opt_ident key_alg
......@@ -4487,24 +4489,27 @@ key_def:
{
LEX *lex=Lex;
const char *key_name= $3 ? $3 : $1;
lex->key_list.push_back(new Key($2, key_name, &lex->key_create_info, 0,
lex->col_list));
Key *key= new Key($2, key_name, &lex->key_create_info, 0,
lex->col_list);
lex->alter_info.key_list.push_back(key);
lex->col_list.empty(); /* Alloced by sql_alloc */
}
| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
{
LEX *lex=Lex;
lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
const char *key_name= $4 ? $4 : $1;
Key *key= new foreign_key(key_name, lex->col_list,
$8,
lex->ref_list,
lex->fk_delete_opt,
lex->fk_update_opt,
lex->fk_match_option));
lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
lex->fk_match_option);
lex->alter_info.key_list.push_back(key);
key= new Key(Key::MULTIPLE, key_name,
&default_key_create_info, 1,
lex->col_list));
lex->col_list);
lex->alter_info.key_list.push_back(key);
lex->col_list.empty(); /* Alloced by sql_alloc */
/* Only used for ALTER TABLE. Ignored otherwise. */
lex->alter_info.flags|= ALTER_FOREIGN_KEY;
}
......@@ -5095,8 +5100,7 @@ alter:
if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
TL_OPTION_UPDATING))
MYSQL_YYABORT;
lex->create_list.empty();
lex->key_list.empty();
lex->alter_info.reset();
lex->col_list.empty();
lex->select_lex.init_order();
lex->select_lex.db=
......@@ -5106,7 +5110,6 @@ alter:
lex->create_info.default_table_charset= NULL;
lex->create_info.row_type= ROW_TYPE_NOT_USED;
lex->alter_info.reset();
lex->alter_info.flags= 0;
lex->no_write_to_binlog= 0;
lex->create_info.storage_media= HA_SM_DEFAULT;
}
......@@ -8141,7 +8144,8 @@ drop:
{
LEX *lex=Lex;
lex->sql_command= SQLCOM_DROP_INDEX;
lex->alter_info.drop_list.empty();
lex->alter_info.reset();
lex->alter_info.flags= ALTER_DROP_INDEX;
lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
$3.str));
if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment