[MDEV-32569] Failure when executing PS for query using IN subquery Created: 2023-10-24  Updated: 2024-01-19  Resolved: 2023-11-01

Status: Closed
Project: MariaDB Server
Component/s: Prepared Statements
Affects Version/s: 10.4
Fix Version/s: 10.4.33, 10.5.24, 10.6.17, 10.11.7, 11.0.5, 11.1.4, 11.2.3

Type: Bug Priority: Critical
Reporter: Igor Babaev Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Blocks
blocks MDEV-30073 Wrong result on 2nd execution of PS f... Stalled
Relates
relates to MDEV-32369 Memory leak when executing PS for que... Closed

 Description   

The following test case fails if the server is built with -DWITH_PROTECT_STATEMENT_MEMROOT:BOOL=ON

CREATE TABLE t1 (a varchar(10)) ENGINE=MYISAM;
CREATE TABLE t2 (b varchar(10) CHARACTER SET utf8) ENGINE=MYISAM;
INSERT INTO t1 VALUES ('b');
INSERT INTO t2 VALUES ('b');
PREPARE stmt FROM 
"SELECT STRAIGHT_JOIN  t1.a FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2)";
EXECUTE stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
DROP TABLE t1,t2;

An assertion failure happens when trying to allocate an Item_direct_ref_to_item at the second execution of the PS.



 Comments   
Comment by Igor Babaev [ 2023-10-24 ]

See also the test case for MDEV-16128 from ps.test that fails in the same way if the server is built with -DWITH_PROTECT_STATEMENT_MEMROOT:BOOL=ON

CREATE TABLE t1 (a varchar(10));
CREATE TABLE t2 (b varchar(10) CHARACTER SET utf8);
INSERT INTO t1 VALUES ('b');
INSERT INTO t2 VALUES ('b');
PREPARE stmt FROM 'SELECT STRAIGHT_JOIN 1 FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a)';
EXECUTE stmt;
EXECUTE stmt;

Comment by Igor Babaev [ 2023-10-30 ]

The following patch fixes this problem.

--- a/sql/item.cc
+++ b/sql/item.cc
@@ -2585,7 +2585,8 @@ bool Type_std_attributes::agg_item_set_converter(const DTCollation &coll,
     if (conv->fix_fields_if_needed(thd, arg))
       return TRUE;
 
-    if (!thd->stmt_arena->is_conventional())
+    if (!thd->stmt_arena->is_conventional() &&
+        thd->lex->current_select->first_cond_optimization)
     {
       Query_arena *arena, backup;
       arena= thd->activate_stmt_arena_if_needed(&backup);

It also fixes all assertion failures in the function Type_std_attributes::agg_item_set_converter() for all test cases from the main test suite.

Comment by Igor Babaev [ 2023-10-31 ]

Please review this patch. You've already checked it.

Comment by Rex Johnston [ 2023-10-31 ]

Looks good to me. OK to push.

Comment by Igor Babaev [ 2023-11-01 ]

A fix for this bug was pushed into 10.4. It has to be merged upstream as it is.

Generated at Thu Feb 08 10:32:20 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.