From 25b1c3505fd4e1d2f39627fdd77f2920ace6323d Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Mon, 27 Jan 2025 16:17:48 +0400 Subject: [PATCH] MDEV-35677 Assertion `thd->is_error()' failed in virtual bool Field::sp_prepare_and_store_item(THD*, Item**) The Count_handler (derived from Internal_error_handler) used in Type_handler_datetime_common::convert_item_for_comparison() suppressed all error codes. This caused a DBUG_ASSERT(thd->is_error()) in Field::sp_prepare_and_store_item() when the error happened while evaluating a subselect in a scenario like this: CREATE FUNCTION f() RETURNS INT DETERMINISTIC RETURN (SELECT a FROM t); CREATE TABLE t (c TIMESTAMP); SELECT * FROM t WHERE c=f(); Notice there is no such column 'a' in in the table 't'. Fix: Handle only DATETIME->TIMESTAMP conversion related errors in Count_handler: - ER_TRUNCATED_WRONG_VALUE - ER_DATETIME_FUNCTION_OVERFLOW thus let other error kinds be processed in their usual way. The reported scenario now returns this (expected) error: ERROR 1054 (42S22): Unknown column 'a' in 'SELECT' --- mysql-test/main/type_timestamp.result | 11 +++++++++++ mysql-test/main/type_timestamp.test | 18 ++++++++++++++++++ sql/sql_type.cc | 9 +++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/mysql-test/main/type_timestamp.result b/mysql-test/main/type_timestamp.result index 1950410c7c4..e00e6d8bacd 100644 --- a/mysql-test/main/type_timestamp.result +++ b/mysql-test/main/type_timestamp.result @@ -2335,3 +2335,14 @@ DROP TABLE t1; # # End of 11.3 tests # +# Start of 11.4 tests +# +# MDEV-35677 Assertion `thd->is_error()' failed in virtual bool Field::sp_prepare_and_store_item(THD*, Item**) +# +CREATE FUNCTION f() RETURNS INT DETERMINISTIC RETURN (SELECT a FROM t); +CREATE TABLE t (c TIMESTAMP); +SELECT * FROM t WHERE c=f(); +ERROR 42S22: Unknown column 'a' in 'SELECT' +DROP TABLE t; +DROP FUNCTION f; +# End of 11.4 tests diff --git a/mysql-test/main/type_timestamp.test b/mysql-test/main/type_timestamp.test index 8b45bd1590c..2b0e772d54b 100644 --- a/mysql-test/main/type_timestamp.test +++ b/mysql-test/main/type_timestamp.test @@ -1370,3 +1370,21 @@ DROP TABLE t1; --echo # --echo # End of 11.3 tests --echo # + +--echo # Start of 11.4 tests + +--echo # +--echo # MDEV-35677 Assertion `thd->is_error()' failed in virtual bool Field::sp_prepare_and_store_item(THD*, Item**) +--echo # + +CREATE FUNCTION f() RETURNS INT DETERMINISTIC RETURN (SELECT a FROM t); +CREATE TABLE t (c TIMESTAMP); +# with view protocol it raises ER_VIEW_INVALID +--disable_view_protocol +--error ER_BAD_FIELD_ERROR +SELECT * FROM t WHERE c=f(); +--enable_view_protocol +DROP TABLE t; +DROP FUNCTION f; + +--echo # End of 11.4 tests diff --git a/sql/sql_type.cc b/sql/sql_type.cc index 12c971b3bd7..2c74c42b1be 100644 --- a/sql/sql_type.cc +++ b/sql/sql_type.cc @@ -7959,8 +7959,13 @@ Type_handler_datetime_common::convert_item_for_comparison( const char *msg, Sql_condition **cond_hdl) override { - hit++; - return *level >= Sql_condition::WARN_LEVEL_WARN; + if (sql_errno == ER_TRUNCATED_WRONG_VALUE || + sql_errno == ER_DATETIME_FUNCTION_OVERFLOW) + { + hit++; + return *level >= Sql_condition::WARN_LEVEL_WARN; + } + return false; } } cnt_handler;