From fdd5a63fe6d1e81fe80c9d2e610041a3b0bc171c Mon Sep 17 00:00:00 2001 From: Martin Hansson Date: Mon, 4 May 2009 14:45:36 +0200 Subject: [PATCH] Bug#44306: Assertion fail on duplicate key error in 'INSERT ... SELECT' statements The code that produces result rows expected that a duplicate row error could not occur in INSERT ... SELECT statements with unfulfilled WHERE conditions. This may happen, however, if the SELECT list contains only aggregate functions. Fixed by checking if an error occured before trying to send EOF to the client. mysql-test/r/insert_select.result: Bug#44306: Test result mysql-test/t/insert_select.test: Bug#44306: Test case sql/sql_select.cc: Bug#44306: Fix --- mysql-test/r/insert_select.result | 5 +++++ mysql-test/t/insert_select.test | 10 ++++++++++ sql/sql_select.cc | 6 ++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/insert_select.result b/mysql-test/r/insert_select.result index 780e91ea73f..2f2cc6334a9 100644 --- a/mysql-test/r/insert_select.result +++ b/mysql-test/r/insert_select.result @@ -765,6 +765,11 @@ f1 f2 2 2 10 10 DROP TABLE t1, t2; +CREATE TABLE t1 ( a INT KEY, b INT ); +INSERT INTO t1 VALUES ( 0, 1 ); +INSERT INTO t1 ( b ) SELECT MAX( b ) FROM t1 WHERE b = 2; +ERROR 23000: Duplicate entry '0' for key 'PRIMARY' +DROP TABLE t1; SET SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; CREATE TABLE t1 (c VARCHAR(30), INDEX ix_c (c(10))); CREATE TABLE t2 (d VARCHAR(10)); diff --git a/mysql-test/t/insert_select.test b/mysql-test/t/insert_select.test index 499db086877..f8023fcfc60 100644 --- a/mysql-test/t/insert_select.test +++ b/mysql-test/t/insert_select.test @@ -323,6 +323,16 @@ INSERT INTO t2 (f1, f2) SELECT * FROM t2; DROP TABLE t1, t2; +# +# Bug#44306: Assertion fail on duplicate key error in 'INSERT ... SELECT' +# statements +# +CREATE TABLE t1 ( a INT KEY, b INT ); +INSERT INTO t1 VALUES ( 0, 1 ); +--error ER_DUP_ENTRY +INSERT INTO t1 ( b ) SELECT MAX( b ) FROM t1 WHERE b = 2; +DROP TABLE t1; + # # Bug #26207: inserts don't work with shortened index # diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 901e058d934..625608362a6 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -7084,15 +7084,17 @@ return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables, if (!(result->send_fields(fields, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))) { + bool send_error= FALSE; if (send_row) { List_iterator_fast it(fields); Item *item; while ((item= it++)) item->no_rows_in_result(); - result->send_data(fields); + send_error= result->send_data(fields); } - result->send_eof(); // Should be safe + if (!send_error) + result->send_eof(); // Should be safe } /* Update results for FOUND_ROWS */ join->thd->limit_found_rows= join->thd->examined_row_count= 0;