Skip to content

Commit 9e800ed

Browse files
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.
1 parent 5979dcf commit 9e800ed

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

mysql-test/main/func_misc.result

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,3 +1732,16 @@ RELEASE_ALL_LOCKS()
17321732
SELECT LOCK_MODE, LOCK_TYPE, TABLE_SCHEMA
17331733
FROM information_schema.metadata_lock_info WHERE thread_id>0 ORDER BY TABLE_SCHEMA;
17341734
LOCK_MODE LOCK_TYPE TABLE_SCHEMA
1735+
#
1736+
# MDEV-32583 UUID() should be treated as stochastic for the purposes of
1737+
# forcing query materialization
1738+
#
1739+
create table t1 as WITH cte AS (SELECT UUID() as r FROM seq_1_to_10)
1740+
SELECT r as r1, r FROM cte;
1741+
select count(*) from t1 where r1!=r;
1742+
count(*)
1743+
0
1744+
drop table t1;
1745+
#
1746+
# End of 10.5 tests
1747+
#

mysql-test/main/func_misc.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,18 @@ FROM information_schema.metadata_lock_info WHERE thread_id>0 ORDER BY TABLE_SCHE
13611361

13621362
--enable_ps2_protocol
13631363
--enable_view_protocol
1364+
1365+
--echo #
1366+
--echo # MDEV-32583 UUID() should be treated as stochastic for the purposes of
1367+
--echo # forcing query materialization
1368+
--echo #
1369+
1370+
--source include/have_sequence.inc
1371+
create table t1 as WITH cte AS (SELECT UUID() as r FROM seq_1_to_10)
1372+
SELECT r as r1, r FROM cte;
1373+
select count(*) from t1 where r1!=r;
1374+
drop table t1;
1375+
1376+
--echo #
1377+
--echo # End of 10.5 tests
1378+
--echo #

sql/item_create.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5301,7 +5301,7 @@ Create_func_uuid::create_builder(THD *thd)
53015301
{
53025302
DBUG_ENTER("Create_func_uuid::create");
53035303
thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
5304-
thd->lex->safe_to_cache_query= 0;
5304+
thd->lex->uncacheable(UNCACHEABLE_RAND); // disallow cache and query merges
53055305
DBUG_RETURN(new (thd->mem_root) Item_func_uuid(thd));
53065306
}
53075307

@@ -5313,7 +5313,7 @@ Create_func_uuid_short::create_builder(THD *thd)
53135313
{
53145314
DBUG_ENTER("Create_func_uuid_short::create");
53155315
thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
5316-
thd->lex->safe_to_cache_query= 0;
5316+
thd->lex->uncacheable(UNCACHEABLE_RAND); // disallow cache and query merges
53175317
DBUG_RETURN(new (thd->mem_root) Item_func_uuid_short(thd));
53185318
}
53195319

sql/sql_lex.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,15 @@ struct LEX: public Query_tables_list
36213621
return (context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW);
36223622
}
36233623

3624+
/**
3625+
Mark all queries in this lex structure as uncacheable for the cause given
3626+
3627+
@param cause the reason queries are to be marked as uncacheable
3628+
3629+
Note, any cause is sufficient for st_select_lex_unit::can_be_merged() to
3630+
disallow query merges.
3631+
*/
3632+
36243633
inline void uncacheable(uint8 cause)
36253634
{
36263635
safe_to_cache_query= 0;

sql/sql_priv.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@
316316
*/
317317
/* This subquery has fields from outer query (put by user) */
318318
#define UNCACHEABLE_DEPENDENT_GENERATED 1
319-
/* This subquery contains functions with random result */
319+
/*
320+
This subquery contains functions with random result.
321+
Something that is uncacheable is by default unmergeable.
322+
*/
320323
#define UNCACHEABLE_RAND 2
321324
/* This subquery contains functions with side effect */
322325
#define UNCACHEABLE_SIDEEFFECT 4

0 commit comments

Comments
 (0)