From 2e253efcc8cdfe47246a07d23001da3f86aa6975 Mon Sep 17 00:00:00 2001 From: Dave Gosselin Date: Mon, 12 Jan 2026 14:05:40 -0500 Subject: [PATCH] MDEV-38099: Assertion failed in st_select_lex::optimize_unflattened_subqueries When optimizing unflattened subqueries we assert that, if any SELECT in a unit sets the uncacheable flag with UNCACHEABLE_RAND, then the unit's uncacheable flag itself should be set likewise. Put another way, any SELECT in the unit that is UNCACHEABLE_RAND causes the entire unit to be so marked. However, this marking was not preserved by SELECT_LEX::register_unit when registering a new unit. During that method, the SELECT_LEX's uncacheable value is updated with the registered unit's uncacheable value but, if the UNCACHEABLE_RAND marking is present, it is not propagated to the uncacheable flag of the enclosing unit. If the incoming unit's uncacheable flag has the UNCACHEABLE_RAND bit set and if the master unit exists, then set the UNCACHEABLE_RAND flag on the master unit. --- mysql-test/main/cte_nonrecursive.result | 8 ++++++++ mysql-test/main/cte_nonrecursive.test | 13 +++++++++++++ sql/sql_lex.cc | 2 ++ 3 files changed, 23 insertions(+) diff --git a/mysql-test/main/cte_nonrecursive.result b/mysql-test/main/cte_nonrecursive.result index 992c218a10eed..59365d71be1da 100644 --- a/mysql-test/main/cte_nonrecursive.result +++ b/mysql-test/main/cte_nonrecursive.result @@ -2683,3 +2683,11 @@ cte as (select t1.a as t1a, t2.a as t2a from t as t1, t as t2 where t1.a=t2.a) select * from cte; ERROR 42S02: Table 'test.t' doesn't exist # End of 10.4 tests +# +# Start of 12.2 tests +# +# +# MDEV-38099: Assertion failed in st_select_lex::optimize_unflattened_subqueries +# +SELECT (WITH cte AS (SELECT RAND()) SELECT * FROM cte); +# End of 12.2 tests diff --git a/mysql-test/main/cte_nonrecursive.test b/mysql-test/main/cte_nonrecursive.test index 1816888626398..5950c71399141 100644 --- a/mysql-test/main/cte_nonrecursive.test +++ b/mysql-test/main/cte_nonrecursive.test @@ -2026,3 +2026,16 @@ drop table t,s; eval $q1; --echo # End of 10.4 tests + +--echo # +--echo # Start of 12.2 tests +--echo # + +--echo # +--echo # MDEV-38099: Assertion failed in st_select_lex::optimize_unflattened_subqueries +--echo # +--disable_result_log +SELECT (WITH cte AS (SELECT RAND()) SELECT * FROM cte); +--enable_result_log + +--echo # End of 12.2 tests diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 73d97861325ef..7779411ff8a98 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -11130,6 +11130,8 @@ void st_select_lex::register_unit(SELECT_LEX_UNIT *unit, slave= unit; unit->master= this; uncacheable|= unit->uncacheable; + if (master && (unit->uncacheable & UNCACHEABLE_RAND)) + master->uncacheable|= UNCACHEABLE_RAND; for(SELECT_LEX *sel= unit->first_select();sel; sel= sel->next_select()) {