Bug #28914 Alter table does not handle all error cases in statement commit
Submitted: 6 Jun 2007 5:23 Modified: 15 Jun 2007 8:15
Reporter: Jan Lindström Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.0.41 & 5.1.x OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution

[6 Jun 2007 5:23] Jan Lindström
Description:
In alter table ha_enable_transaction could call a ha_commit_stmt(). However error code returned from the ha_enable_transaction is not handled in copy_data_between_tables. Statement commit could fail to e.g. duplicate key error.

How to repeat:
drop table if exists t0;
create table t0 (a int) engine=soliddb;
insert into t0 values (0),(1),(2),(2),(3),(4);
alter table t0 add unique index abc (a);
show create table t0;
select * from t0 order by a;
drop table t0;

Suggested fix:
--- /home/jan/mysql-5.0.41/sql/sql_table.cc     2007-04-26 18:49:48.000000000 +0300
+++ sql_table.cc        2007-06-06 08:15:22.000000000 +0300
@@ -3969,7 +3984,7 @@
                         ha_rows *deleted,
                          enum enum_enable_or_disable keys_onoff)
 {
-  int error;
+  int error=0;
   Copy_field *copy,*copy_end;
   ulong found_count,delete_count;
   THD *thd= current_thd;
@@ -4122,7 +4137,7 @@
   }
   to->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
-  ha_enable_transaction(thd,TRUE);
+  error = ha_enable_transaction(thd,TRUE);
 
   /*
     Ensure that the new table is saved properly to disk so that we
[6 Jun 2007 11:38] Sveta Smirnova
Thank you for the report.

But error "alter table t0 add unique index abc (a)' failed: 1062: Duplicate entry '2' for key 1" is expected. Or do you want to handle this error inside the storage engine?
[6 Jun 2007 11:47] Jan Lindström
My problem is that ha_enable_transactions calls ha_commit_stmt but does not update error in copy_data_between_tables. Following call to ha_commit_stmt and ha_commit do succeed. Thus, return value of the ha_enable_transactions should be stored. With provided proposed fix I get correct error, without it I don't see any errors.
[15 Jun 2007 8:15] Sveta Smirnova
Thank you for the comment.

This bug has been fixed by patch for Bug #28652. See also http://mysql.bkbits.net:8080/mysql-5.1/sql/sql_table.cc?PAGE=diffs&REV=1.239.55.1