diff options
author | Narayanan V <v.narayanan@sun.com> | 2008-09-16 18:37:59 +0530 |
---|---|---|
committer | Narayanan V <v.narayanan@sun.com> | 2008-09-16 18:37:59 +0530 |
commit | d714d29058fcb6320f385c87a8f0809c73edf7df (patch) | |
tree | 246002d76b2fbd0c0629b2224d0d7fa959d70619 /mysql-test/t/auto_increment.test | |
parent | 4f3d241b28559b7fceebda8524ac86b4004c579c (diff) | |
download | mariadb-git-d714d29058fcb6320f385c87a8f0809c73edf7df.tar.gz |
Bug#38338: REPLACE causes last_insert_id() to return an incorrect value
Fix the write_record function to record auto increment
values in a consistent way.
mysql-test/r/auto_increment.result:
Updated the test result file with the output of the
new test case added to verify this bug.
mysql-test/t/auto_increment.test:
Added a new test case to verify this bug.
sql/sql_insert.cc:
The algorithm for the write_record function
in sql_insert.cc is (more emphasis given to
the parts that deal with the autogenerated values)
1) If a write fails
1.1) save the autogenerated value to avoid
thd->insert_id_for_cur_row to become 0.
1.2) <logic to handle INSERT ON DUPLICATE KEY
UPDATE and REPLACE>
2) record the first successful insert id.
explanation of the failure
--------------------------
As long as 1.1) was executed 2) worked fine.
1.1) was always executed when REPLACE worked
with the last row update optimization, but
in cases where 1.1) was not executed 2)
would fail and would result in the autogenerated
value not being saved.
solution
--------
repeat a check for thd->insert_id_for_cur_row
being zero similar to 1.1) before 2) and ensure
that the correct value is saved.
Diffstat (limited to 'mysql-test/t/auto_increment.test')
-rw-r--r-- | mysql-test/t/auto_increment.test | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/mysql-test/t/auto_increment.test b/mysql-test/t/auto_increment.test index ff92c743960..47458c1f054 100644 --- a/mysql-test/t/auto_increment.test +++ b/mysql-test/t/auto_increment.test @@ -314,5 +314,15 @@ insert into t1 values(null,0,0,null); # this will delete two rows replace into t1 values(null,1,0,null); select last_insert_id(); +drop table t1; +# Test of REPLACE when it does a INSERT+DELETE for all the conflicting rows +# (i.e.) when there are three rows conflicting in unique key columns with +# a row that is being inserted, all the three rows will be deleted and then +# the new rows will be inserted. +create table t1 (a int primary key auto_increment, b int, c int, e int, d timestamp default current_timestamp, unique(b),unique(c),unique(e)); +insert into t1 values(null,1,1,1,now()); +insert into t1 values(null,0,0,0,null); +replace into t1 values(null,1,0,2,null); +select last_insert_id(); drop table t1; |