From 7b2e2288e9f784ea12f85dc005503511fe99f796 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Fri, 14 Dec 2018 14:28:30 +0100 Subject: [PATCH] MDEV-16903 Assertion `!auto_increment_field_not_null' failed in TABLE::init after unsuccessful attempt to add CHECK constraint on temporary table if the CHECK constraint failed in copy_data_between_tables(), the loop was aborted prematurely and to->auto_increment_field_not_null wasn't reset. --- mysql-test/r/check.result | 10 ++++++++++ mysql-test/t/check.test | 12 ++++++++++++ sql/sql_table.cc | 16 ++++++++-------- sql/temporary_tables.cc | 11 ++++------- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/mysql-test/r/check.result b/mysql-test/r/check.result index e3dcda773f4..e882a4cdbe6 100644 --- a/mysql-test/r/check.result +++ b/mysql-test/r/check.result @@ -85,3 +85,13 @@ t1 CREATE TABLE `t1` ( `c` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 DROP TABLE t1; +create temporary table t1 ( +id int not null auto_increment primary key, +f int not null default 0 +); +insert into t1 () values (); +alter ignore table t1 add constraint check (f > 0); +Warnings: +Warning 4025 CONSTRAINT `CONSTRAINT_1` failed for `test`.`t1` +alter table t1; +drop table t1; diff --git a/mysql-test/t/check.test b/mysql-test/t/check.test index cce8fd34c9c..475a7996a09 100644 --- a/mysql-test/t/check.test +++ b/mysql-test/t/check.test @@ -103,3 +103,15 @@ CREATE OR REPLACE TABLE t1 (a INT, b INT, c INT, CHECK (a+b>0)) ENGINE=MyISAM; ALTER TABLE t1 DROP COLUMN b, DROP CONSTRAINT `CONSTRAINT_1`; SHOW CREATE TABLE t1; DROP TABLE t1; + +# +# MDEV-16903 Assertion `!auto_increment_field_not_null' failed in TABLE::init after unsuccessful attempt to add CHECK constraint on temporary table +# +create temporary table t1 ( + id int not null auto_increment primary key, + f int not null default 0 +); +insert into t1 () values (); +alter ignore table t1 add constraint check (f > 0); +alter table t1; +drop table t1; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 1765e352b88..9e72278db8d 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -10065,14 +10065,7 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to, error= 1; break; } - if (to->next_number_field) - { - if (auto_increment_field_copied) - to->auto_increment_field_not_null= TRUE; - else - to->next_number_field->reset(); - } - + for (Copy_field *copy_ptr=copy ; copy_ptr != copy_end ; copy_ptr++) { copy_ptr->do_copy(copy_ptr); @@ -10091,6 +10084,13 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to, error= 1; break; } + if (to->next_number_field) + { + if (auto_increment_field_copied) + to->auto_increment_field_not_null= TRUE; + else + to->next_number_field->reset(); + } error=to->file->ha_write_row(to->record[0]); to->auto_increment_field_not_null= FALSE; if (error) diff --git a/sql/temporary_tables.cc b/sql/temporary_tables.cc index 3201b306fad..87c4671ada4 100644 --- a/sql/temporary_tables.cc +++ b/sql/temporary_tables.cc @@ -1140,8 +1140,7 @@ TABLE *THD::open_temporary_table(TMP_TABLE_SHARE *share, @return Success false Failure true */ -bool THD::find_and_use_tmp_table(const TABLE_LIST *tl, - TABLE **out_table) +bool THD::find_and_use_tmp_table(const TABLE_LIST *tl, TABLE **out_table) { DBUG_ENTER("THD::find_and_use_tmp_table"); @@ -1151,11 +1150,9 @@ bool THD::find_and_use_tmp_table(const TABLE_LIST *tl, key_length= create_tmp_table_def_key(key, tl->get_db_name(), tl->get_table_name()); - result= - use_temporary_table(find_temporary_table(key, key_length, - TMP_TABLE_NOT_IN_USE), - out_table); - + result= use_temporary_table(find_temporary_table(key, key_length, + TMP_TABLE_NOT_IN_USE), + out_table); DBUG_RETURN(result); }