Skip to content

Commit dfd42ed

Browse files
committed
ALTER TABLE ... DROP COLUMN sys_start should rename a "dropped" column
1 parent c847089 commit dfd42ed

File tree

6 files changed

+54
-62
lines changed

6 files changed

+54
-62
lines changed

mysql-test/suite/versioning/r/alter.result

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ t CREATE TABLE `t` (
6767
alter table t drop system versioning;
6868
ERROR HY000: System versioning field `trx_start` exists
6969
alter table t drop column trx_start, drop column trx_end;
70+
select row_start from t;
71+
row_start
7072
alter table t drop system versioning;
7173
show create table t;
7274
Table Create Table
@@ -133,12 +135,17 @@ with system versioning;
133135
select * from t for system_time all;
134136
a
135137
alter table t drop column row_start;
136-
ERROR HY000: Wrong parameters for `t`: missing 'DROP COLUMN row_end'
138+
ERROR HY000: Wrong parameters for `t`: missing 'DROP COLUMN `row_end`'
137139
alter table t drop column row_end;
138-
ERROR HY000: Wrong parameters for `t`: missing 'DROP COLUMN row_start'
140+
ERROR HY000: Wrong parameters for `t`: missing 'DROP COLUMN `row_start`'
139141
alter table t drop column row_start, drop column row_end;
140142
select * from t for system_time all;
141143
a
144+
show create table t;
145+
Table Create Table
146+
t CREATE TABLE `t` (
147+
`a` int(11) DEFAULT NULL
148+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
142149
alter table t drop column row_start;
143150
ERROR 42000: Can't DROP COLUMN `row_start`; check that it exists
144151
alter table t drop column row_end;

mysql-test/suite/versioning/r/trx_id.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ period for system_time (sys_start, sys_end)
6565
) engine innodb with system versioning;
6666
insert into t1 values (1);
6767
alter table t1 drop column sys_start, drop column sys_end;
68-
select sys_end = 18446744073709551615 as transaction_based from t1 for system_time all;
68+
select row_end = 18446744073709551615 as transaction_based from t1 for system_time all;
6969
transaction_based
7070
1
7171
drop table t1;

mysql-test/suite/versioning/t/alter.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ show create table t;
5757
alter table t drop system versioning;
5858

5959
alter table t drop column trx_start, drop column trx_end;
60+
select row_start from t;
6061
alter table t drop system versioning;
6162
show create table t;
6263

@@ -95,6 +96,7 @@ alter table t drop column row_start;
9596
alter table t drop column row_end;
9697
alter table t drop column row_start, drop column row_end;
9798
select * from t for system_time all;
99+
show create table t;
98100

99101
--error ER_CANT_DROP_FIELD_OR_KEY
100102
alter table t drop column row_start;

mysql-test/suite/versioning/t/trx_id.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ create or replace table t1 (
6969
) engine innodb with system versioning;
7070
insert into t1 values (1);
7171
alter table t1 drop column sys_start, drop column sys_end;
72-
select sys_end = 18446744073709551615 as transaction_based from t1 for system_time all;
72+
select row_end = 18446744073709551615 as transaction_based from t1 for system_time all;
7373

7474
drop table t1;
7575
drop function check_result;

sql/handler.cc

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,60 +7283,6 @@ bool Vers_parse_info::fix_alter_info(THD *thd, Alter_info *alter_info,
72837283
}
72847284
}
72857285

7286-
if (alter_info->drop_list.elements)
7287-
{
7288-
bool done_start= false;
7289-
bool done_end= false;
7290-
List_iterator<Alter_drop> it(alter_info->drop_list);
7291-
while (Alter_drop *d= it++)
7292-
{
7293-
const char *name= d->name;
7294-
Field *f= NULL;
7295-
if (!done_start && is_start(name))
7296-
{
7297-
f= share->vers_start_field();
7298-
done_start= true;
7299-
}
7300-
else if (!done_end && is_end(name))
7301-
{
7302-
f= share->vers_end_field();
7303-
done_end= true;
7304-
}
7305-
else
7306-
continue;
7307-
if (f->invisible > INVISIBLE_USER)
7308-
{
7309-
my_error(ER_CANT_DROP_FIELD_OR_KEY, MYF(0), d->type_name(), name);
7310-
return true;
7311-
}
7312-
7313-
bool integer= table->vers_start_field()->type() == MYSQL_TYPE_LONGLONG;
7314-
Create_field *field= vers_init_sys_field(thd, name, f->flags & VERS_SYSTEM_FIELD, integer);
7315-
if (!field)
7316-
return true;
7317-
7318-
field->change= f->field_name;
7319-
7320-
alter_info->flags|= Alter_info::ALTER_CHANGE_COLUMN;
7321-
alter_info->create_list.push_back(field);
7322-
7323-
it.remove();
7324-
7325-
if (done_start && done_end)
7326-
break;
7327-
}
7328-
7329-
if ((done_start || done_end) && done_start != done_end)
7330-
{
7331-
String tmp;
7332-
tmp.append("DROP COLUMN ");
7333-
tmp.append(done_start ? &table->vers_end_field()->field_name
7334-
: &table->vers_start_field()->field_name);
7335-
my_error(ER_MISSING, MYF(0), table_name, tmp.c_ptr());
7336-
return true;
7337-
}
7338-
}
7339-
73407286
return false;
73417287
}
73427288

@@ -7369,7 +7315,7 @@ Vers_parse_info::fix_create_like(Alter_info &alter_info, HA_CREATE_INFO &create_
73697315
int remove= 2;
73707316
while (remove && (f= it++))
73717317
{
7372-
if (f->flags & (VERS_SYS_START_FLAG|VERS_SYS_END_FLAG))
7318+
if (f->flags & VERS_SYSTEM_FIELD)
73737319
{
73747320
it.remove();
73757321
remove--;

sql/sql_table.cc

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7868,7 +7868,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
78687868
List<Virtual_column_info> new_constraint_list;
78697869
uint db_create_options= (table->s->db_create_options
78707870
& ~(HA_OPTION_PACK_RECORD));
7871-
uint used_fields;
7871+
uint used_fields, dropped_sys_vers_fields= 0;
78727872
KEY *key_info=table->key_info;
78737873
bool rc= TRUE;
78747874
bool modified_primary_key= FALSE;
@@ -7950,7 +7950,15 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
79507950
!my_strcasecmp(system_charset_info,field->field_name.str, drop->name))
79517951
break;
79527952
}
7953-
if (drop && field->invisible < INVISIBLE_SYSTEM)
7953+
/*
7954+
DROP COLULMN xxx
7955+
1. it does not see INVISIBLE_SYSTEM columns
7956+
2. otherwise, normally a column is dropped
7957+
3. unless it's a system versioning column (but see below).
7958+
*/
7959+
if (drop && field->invisible < INVISIBLE_SYSTEM &&
7960+
!(field->flags & VERS_SYSTEM_FIELD &&
7961+
!(alter_info->flags & Alter_info::ALTER_DROP_SYSTEM_VERSIONING)))
79547962
{
79557963
/* Reset auto_increment value if it was dropped */
79567964
if (MTYP_TYPENR(field->unireg_check) == Field::NEXT_NUMBER &&
@@ -7966,7 +7974,8 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
79667974
bitmap_set_bit(dropped_fields, field->field_index);
79677975
continue;
79687976
}
7969-
if (!drop && field->invisible == INVISIBLE_SYSTEM &&
7977+
/* invisible versioning column is dropped automatically on DROP SYSTEM VERSIONING */
7978+
if (!drop && field->invisible >= INVISIBLE_SYSTEM &&
79707979
field->flags & VERS_SYSTEM_FIELD &&
79717980
alter_info->flags & Alter_info::ALTER_DROP_SYSTEM_VERSIONING)
79727981
{
@@ -8016,6 +8025,22 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
80168025
my_error(ER_VERS_SYS_FIELD_EXISTS, MYF(0), field->field_name.str);
80178026
goto err;
80188027
}
8028+
else if (drop && field->invisible < INVISIBLE_SYSTEM &&
8029+
field->flags & VERS_SYSTEM_FIELD &&
8030+
!(alter_info->flags & Alter_info::ALTER_DROP_SYSTEM_VERSIONING))
8031+
{
8032+
/* "dropping" a versioning field only hides it from the user */
8033+
def= new (thd->mem_root) Create_field(thd, field, field);
8034+
def->invisible= INVISIBLE_SYSTEM;
8035+
dropped_sys_vers_fields|= field->flags;
8036+
alter_info->flags|= Alter_info::ALTER_CHANGE_COLUMN;
8037+
if (field->flags & VERS_SYS_START_FLAG)
8038+
create_info->vers_info.as_row.start= def->field_name= Vers_parse_info::default_start;
8039+
else
8040+
create_info->vers_info.as_row.end= def->field_name= Vers_parse_info::default_end;
8041+
new_create_list.push_back(def, thd->mem_root);
8042+
drop_it.remove();
8043+
}
80198044
else
80208045
{
80218046
/*
@@ -8042,6 +8067,18 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
80428067
}
80438068
}
80448069
}
8070+
if (dropped_sys_vers_fields &&
8071+
((dropped_sys_vers_fields & VERS_SYSTEM_FIELD) != VERS_SYSTEM_FIELD))
8072+
{
8073+
StringBuffer<NAME_LEN*2> tmp;
8074+
tmp.append(STRING_WITH_LEN("DROP COLUMN "));
8075+
if (dropped_sys_vers_fields & VERS_SYS_START_FLAG)
8076+
append_identifier(thd, &tmp, &table->vers_end_field()->field_name);
8077+
else
8078+
append_identifier(thd, &tmp, &table->vers_start_field()->field_name);
8079+
my_error(ER_MISSING, MYF(0), table->s->table_name.str, tmp.c_ptr());
8080+
goto err;
8081+
}
80458082
def_it.rewind();
80468083
while ((def=def_it++)) // Add new columns
80478084
{

0 commit comments

Comments
 (0)