From a5504f05f135ca0f4ce900620fe2874ae57b04f5 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/gis.result | 10 ++++++++++ mysql-test/main/gis.test | 11 +++++++++++ sql/sql_lex.cc | 2 ++ 3 files changed, 23 insertions(+) diff --git a/mysql-test/main/gis.result b/mysql-test/main/gis.result index 5190e4ff4fd1c..00506d1b0d636 100644 --- a/mysql-test/main/gis.result +++ b/mysql-test/main/gis.result @@ -5622,3 +5622,13 @@ t # # End of 11.6 tests # +# +# Start of 12.2 tests +# +# +# MDEV-38099: Assertion failed in st_select_lex::optimize_unflattened_subqueries +# +SELECT (WITH cte AS (SELECT st_astext(ST_GeomFromText('POLYGON((1 1,2 2,1 1))'))) SELECT * FROM cte); +(WITH cte AS (SELECT st_astext(ST_GeomFromText('POLYGON((1 1,2 2,1 1))'))) SELECT * FROM cte) +POLYGON((1 1,2 2,1 1)) +# End of 12.2 tests diff --git a/mysql-test/main/gis.test b/mysql-test/main/gis.test index fd604a363a68b..d3ae097a237b8 100644 --- a/mysql-test/main/gis.test +++ b/mysql-test/main/gis.test @@ -3623,3 +3623,14 @@ SELECT ST_ISVALID(ST_GEOMFROMTEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (15 15 --echo # --echo # End of 11.6 tests --echo # + +--echo # +--echo # Start of 12.2 tests +--echo # + +--echo # +--echo # MDEV-38099: Assertion failed in st_select_lex::optimize_unflattened_subqueries +--echo # +SELECT (WITH cte AS (SELECT st_astext(ST_GeomFromText('POLYGON((1 1,2 2,1 1))'))) SELECT * FROM cte); + +--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()) {