From 5a99eb4878b6b3bb00bef0cc552f707d32b91574 Mon Sep 17 00:00:00 2001 From: Rucha Deodhar Date: Sun, 26 Sep 2021 09:54:55 +0530 Subject: MDEV-26681: ERROR_INDEX is not available within compound statement blocks Fixed after patch MDEV-26606 because root cause was same. Analysis: m_current_row_for_warning is reset to 1 during cleanup phase of stored procedure. When we perform a copy because some statement of procedure created warning, this reset value is passed to push _warning(). Fix: Add a parameter in relevant functions to pass correct value of error index and don't use m_current_row_for_warning directly. --- mysql-test/main/get_diagnostics.result | 33 +++++++++++++++++++++++++++++++++ mysql-test/main/get_diagnostics.test | 32 ++++++++++++++++++++++++++++++++ sql/sql_error.cc | 2 +- sql/sql_error.h | 4 ++-- 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/mysql-test/main/get_diagnostics.result b/mysql-test/main/get_diagnostics.result index 7a07bb06516..61dd373f720 100644 --- a/mysql-test/main/get_diagnostics.result +++ b/mysql-test/main/get_diagnostics.result @@ -1595,3 +1595,36 @@ SELECT @num, @msg; 2 Duplicate entry '1' for key 'PRIMARY' DROP PROCEDURE sp; DROP TABLE t1; +# +# MDEV-26681: ERROR_INDEX is not available within compound statement blocks +# +CREATE OR REPLACE TABLE t1 (pk INT PRIMARY KEY, a CHAR(3)); +INSERT IGNORE INTO t1 VALUES (1,'foo'),(1,'bar'),(2,'foobar'); +Warnings: +Warning 1062 Duplicate entry '1' for key 'PRIMARY' +Warning 1265 Data truncated for column 'a' at row 3 +BEGIN NOT ATOMIC +DECLARE i INT DEFAULT 0; +DECLARE rnum INT DEFAULT -1; +DECLARE msg VARCHAR(1024) DEFAULT ''; +DECLARE err INT DEFAULT -1; +WHILE i < @@warning_count +DO +SET i = i + 1; +GET DIAGNOSTICS CONDITION i rnum = ERROR_INDEX, msg = MESSAGE_TEXT, err = MYSQL_ERRNO; +SELECT i, rnum, msg, err; +END WHILE; +END | +i rnum msg err +1 2 Duplicate entry '1' for key 'PRIMARY' 1062 +i rnum msg err +2 3 Data truncated for column 'a' at row 3 1265 +GET DIAGNOSTICS CONDITION 1 @rnum = ERROR_INDEX, @msg = MESSAGE_TEXT, @err = MYSQL_ERRNO; +select @rnum, @msg, @err; +@rnum @msg @err +2 Duplicate entry '1' for key 'PRIMARY' 1062 +GET DIAGNOSTICS CONDITION 2 @rnum = ERROR_INDEX, @msg = MESSAGE_TEXT, @err = MYSQL_ERRNO; +SELECT @rnum, @msg, @err; +@rnum @msg @err +3 Data truncated for column 'a' at row 3 1265 +DROP TABLE t1; diff --git a/mysql-test/main/get_diagnostics.test b/mysql-test/main/get_diagnostics.test index eb8d2a23a45..7f044e4371b 100644 --- a/mysql-test/main/get_diagnostics.test +++ b/mysql-test/main/get_diagnostics.test @@ -1481,3 +1481,35 @@ SELECT @num, @msg; DROP PROCEDURE sp; DROP TABLE t1; + +--echo # +--echo # MDEV-26681: ERROR_INDEX is not available within compound statement blocks +--echo # + +CREATE OR REPLACE TABLE t1 (pk INT PRIMARY KEY, a CHAR(3)); +INSERT IGNORE INTO t1 VALUES (1,'foo'),(1,'bar'),(2,'foobar'); + +DELIMITER |; + +BEGIN NOT ATOMIC + DECLARE i INT DEFAULT 0; + DECLARE rnum INT DEFAULT -1; + DECLARE msg VARCHAR(1024) DEFAULT ''; + DECLARE err INT DEFAULT -1; + WHILE i < @@warning_count + DO + SET i = i + 1; + GET DIAGNOSTICS CONDITION i rnum = ERROR_INDEX, msg = MESSAGE_TEXT, err = MYSQL_ERRNO; + SELECT i, rnum, msg, err; + END WHILE; +END | + +DELIMITER ;| + +GET DIAGNOSTICS CONDITION 1 @rnum = ERROR_INDEX, @msg = MESSAGE_TEXT, @err = MYSQL_ERRNO; +select @rnum, @msg, @err; + +GET DIAGNOSTICS CONDITION 2 @rnum = ERROR_INDEX, @msg = MESSAGE_TEXT, @err = MYSQL_ERRNO; +SELECT @rnum, @msg, @err; + +DROP TABLE t1; diff --git a/sql/sql_error.cc b/sql/sql_error.cc index 2c27fd13532..ca9da3dbf89 100644 --- a/sql/sql_error.cc +++ b/sql/sql_error.cc @@ -664,7 +664,7 @@ void Warning_info::reserve_space(THD *thd, uint count) Sql_condition *Warning_info::push_warning(THD *thd, const Sql_condition_identity *value, const char *msg, - ulonglong current_error_index) + ulong current_error_index) { Sql_condition *cond= NULL; diff --git a/sql/sql_error.h b/sql/sql_error.h index 0b7319a1e82..a3e00e8419d 100644 --- a/sql/sql_error.h +++ b/sql/sql_error.h @@ -748,7 +748,7 @@ private: Sql_condition *push_warning(THD *thd, const Sql_condition_identity *identity, const char* msg, - ulonglong current_error_index); + ulong current_error_index); /** Add a new SQL-condition to the current list and increment the respective @@ -1180,7 +1180,7 @@ public: Sql_condition::enum_warning_level level, const Sql_user_condition_identity &ucid, const char* msg, - ulonglong current_error_index) + ulong current_error_index) { Sql_condition_identity tmp(sql_errno_arg, sqlstate, level, ucid); return get_warning_info()->push_warning(thd, &tmp, msg, -- cgit v1.2.1