diff --git a/mysql-test/r/explain_non_select.result b/mysql-test/r/explain_non_select.result index bb43fccc114..42ae7bd4ebd 100644 --- a/mysql-test/r/explain_non_select.result +++ b/mysql-test/r/explain_non_select.result @@ -198,3 +198,24 @@ id select_type table type possible_keys key key_len ref rows Extra 3 SUBQUERY t1 index NULL a 5 NULL 3 Using index 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-5122: "Commands out of sync", "Malformed packet" or client hang up on unique key violation +# +drop table if exists t1; +Warnings: +Note 1051 Unknown table 't1' +drop function if exists f1; +create table t1 (a int, unique(a)); +create function f1(x int) +returns int +begin +insert into t1 values(x),(x); +return 10; +end| +select f1(100); +ERROR 23000: Duplicate entry '100' for key 'a' +select 'OK'; +OK +OK +drop function f1; +drop table t1; diff --git a/mysql-test/t/explain_non_select.test b/mysql-test/t/explain_non_select.test index 316cc1bf8d2..f05373c312e 100644 --- a/mysql-test/t/explain_non_select.test +++ b/mysql-test/t/explain_non_select.test @@ -169,3 +169,32 @@ explain insert into t2 values (11, 1+(select max(a+1) from t1)); drop table t1,t2; + +--echo # +--echo # MDEV-5122: "Commands out of sync", "Malformed packet" or client hang up on unique key violation +--echo # +drop table if exists t1; + +--disable_warnings +drop function if exists f1; +--enable_warnings + +create table t1 (a int, unique(a)); + +delimiter |; +create function f1(x int) + returns int +begin + insert into t1 values(x),(x); + return 10; +end| +delimiter ;| + +--error ER_DUP_ENTRY +select f1(100); +select 'OK'; + +drop function f1; +drop table t1; + + diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 10cd8a6a7f6..56882f7798c 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -819,8 +819,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, save_insert_query_plan(thd, table_list); if (thd->lex->describe) { - retval= 0; - goto exit_without_my_ok; + retval= thd->lex->explain->send_explain(thd); + goto free_and_exit; } /* @@ -1170,15 +1170,14 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, DBUG_RETURN(FALSE); abort: -exit_without_my_ok: #ifndef EMBEDDED_LIBRARY if (lock_type == TL_WRITE_DELAYED) end_delayed_insert(thd); #endif if (table != NULL) table->file->ha_release_auto_increment(); - retval= thd->lex->explain->send_explain(thd); +free_and_exit: if (!joins_freed) free_underlaid_joins(thd, &thd->lex->select_lex); thd->abort_on_warning= 0;