From 9e800eda8602a09212f6c35b6f8b32c48c8df8e8 Mon Sep 17 00:00:00 2001 From: Rex Date: Tue, 19 Mar 2024 08:50:19 +1200 Subject: [PATCH] MDEV-32583 UUID() should be treated as stochastic for the purposes of forcing query materialization RAND() and UUID() are treated differently with respect to subquery materialization both should be marked as uncacheable, forcing materialization. Altered Create_func_uuid(_short)::create_builder(). Added comment in header about UNCACHEABLE_RAND meaning also unmergeable. --- mysql-test/main/func_misc.result | 13 +++++++++++++ mysql-test/main/func_misc.test | 15 +++++++++++++++ sql/item_create.cc | 4 ++-- sql/sql_lex.h | 9 +++++++++ sql/sql_priv.h | 5 ++++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/mysql-test/main/func_misc.result b/mysql-test/main/func_misc.result index d4c7c9915ad..a6869296090 100644 --- a/mysql-test/main/func_misc.result +++ b/mysql-test/main/func_misc.result @@ -1732,3 +1732,16 @@ RELEASE_ALL_LOCKS() SELECT LOCK_MODE, LOCK_TYPE, TABLE_SCHEMA FROM information_schema.metadata_lock_info WHERE thread_id>0 ORDER BY TABLE_SCHEMA; LOCK_MODE LOCK_TYPE TABLE_SCHEMA +# +# MDEV-32583 UUID() should be treated as stochastic for the purposes of +# forcing query materialization +# +create table t1 as WITH cte AS (SELECT UUID() as r FROM seq_1_to_10) +SELECT r as r1, r FROM cte; +select count(*) from t1 where r1!=r; +count(*) +0 +drop table t1; +# +# End of 10.5 tests +# diff --git a/mysql-test/main/func_misc.test b/mysql-test/main/func_misc.test index b6edde29890..d0f622421bd 100644 --- a/mysql-test/main/func_misc.test +++ b/mysql-test/main/func_misc.test @@ -1361,3 +1361,18 @@ FROM information_schema.metadata_lock_info WHERE thread_id>0 ORDER BY TABLE_SCHE --enable_ps2_protocol --enable_view_protocol + +--echo # +--echo # MDEV-32583 UUID() should be treated as stochastic for the purposes of +--echo # forcing query materialization +--echo # + +--source include/have_sequence.inc +create table t1 as WITH cte AS (SELECT UUID() as r FROM seq_1_to_10) +SELECT r as r1, r FROM cte; +select count(*) from t1 where r1!=r; +drop table t1; + +--echo # +--echo # End of 10.5 tests +--echo # diff --git a/sql/item_create.cc b/sql/item_create.cc index f9588eaa953..2c54bc9118e 100644 --- a/sql/item_create.cc +++ b/sql/item_create.cc @@ -5301,7 +5301,7 @@ Create_func_uuid::create_builder(THD *thd) { DBUG_ENTER("Create_func_uuid::create"); thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION); - thd->lex->safe_to_cache_query= 0; + thd->lex->uncacheable(UNCACHEABLE_RAND); // disallow cache and query merges DBUG_RETURN(new (thd->mem_root) Item_func_uuid(thd)); } @@ -5313,7 +5313,7 @@ Create_func_uuid_short::create_builder(THD *thd) { DBUG_ENTER("Create_func_uuid_short::create"); thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION); - thd->lex->safe_to_cache_query= 0; + thd->lex->uncacheable(UNCACHEABLE_RAND); // disallow cache and query merges DBUG_RETURN(new (thd->mem_root) Item_func_uuid_short(thd)); } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 3ab50d4aaa8..0a88e92d723 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -3621,6 +3621,15 @@ public: return (context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW); } + /** + Mark all queries in this lex structure as uncacheable for the cause given + + @param cause the reason queries are to be marked as uncacheable + + Note, any cause is sufficient for st_select_lex_unit::can_be_merged() to + disallow query merges. + */ + inline void uncacheable(uint8 cause) { safe_to_cache_query= 0; diff --git a/sql/sql_priv.h b/sql/sql_priv.h index 7c8b604539c..2335c6ba516 100644 --- a/sql/sql_priv.h +++ b/sql/sql_priv.h @@ -316,7 +316,10 @@ */ /* This subquery has fields from outer query (put by user) */ #define UNCACHEABLE_DEPENDENT_GENERATED 1 -/* This subquery contains functions with random result */ +/* + This subquery contains functions with random result. + Something that is uncacheable is by default unmergeable. +*/ #define UNCACHEABLE_RAND 2 /* This subquery contains functions with side effect */ #define UNCACHEABLE_SIDEEFFECT 4