diff --git a/mysql-test/main/subselect.result b/mysql-test/main/subselect.result index 3ceba6d6feb5f..82dd5f77d8728 100644 --- a/mysql-test/main/subselect.result +++ b/mysql-test/main/subselect.result @@ -7642,3 +7642,13 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests +# +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests diff --git a/mysql-test/main/subselect.test b/mysql-test/main/subselect.test index 371cf80986311..370b9489309e8 100644 --- a/mysql-test/main/subselect.test +++ b/mysql-test/main/subselect.test @@ -6501,3 +6501,13 @@ DROP VIEW v1; DROP TABLE t1, t2, t3; --echo # End of 10.10 tests + +--echo # +--echo # MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +--echo # +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +drop table t1; + +--echo # End of 12.2 tests diff --git a/mysql-test/main/subselect_no_exists_to_in.result b/mysql-test/main/subselect_no_exists_to_in.result index 7d80763c25a38..bb6260f741576 100644 --- a/mysql-test/main/subselect_no_exists_to_in.result +++ b/mysql-test/main/subselect_no_exists_to_in.result @@ -7644,6 +7644,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests +# +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests set optimizer_switch=default; select @@optimizer_switch like '%exists_to_in=off%'; @@optimizer_switch like '%exists_to_in=off%' diff --git a/mysql-test/main/subselect_no_mat.result b/mysql-test/main/subselect_no_mat.result index 0220a028fcbf4..168a1c64d7389 100644 --- a/mysql-test/main/subselect_no_mat.result +++ b/mysql-test/main/subselect_no_mat.result @@ -7639,6 +7639,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests +# +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests set optimizer_switch=default; select @@optimizer_switch like '%materialization=on%'; @@optimizer_switch like '%materialization=on%' diff --git a/mysql-test/main/subselect_no_opts.result b/mysql-test/main/subselect_no_opts.result index 78eecf8d88f4e..4e3719d2593ca 100644 --- a/mysql-test/main/subselect_no_opts.result +++ b/mysql-test/main/subselect_no_opts.result @@ -7637,4 +7637,14 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests +# +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests set @optimizer_switch_for_subselect_test=null; diff --git a/mysql-test/main/subselect_no_scache.result b/mysql-test/main/subselect_no_scache.result index 91eecfdb0711b..74684b673ef08 100644 --- a/mysql-test/main/subselect_no_scache.result +++ b/mysql-test/main/subselect_no_scache.result @@ -7648,6 +7648,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e' DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests +# +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests set optimizer_switch=default; select @@optimizer_switch like '%subquery_cache=on%'; @@optimizer_switch like '%subquery_cache=on%' diff --git a/mysql-test/main/subselect_no_semijoin.result b/mysql-test/main/subselect_no_semijoin.result index fb0fc0a687451..1a9523a5b9f9e 100644 --- a/mysql-test/main/subselect_no_semijoin.result +++ b/mysql-test/main/subselect_no_semijoin.result @@ -7638,6 +7638,16 @@ DROP VIEW v1; DROP TABLE t1, t2, t3; # End of 10.10 tests # +# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions +# +create table t1 (a int); +insert into t1 (a) values (1); +select * from t1 where 1 in (select 1 union select 2 union select RAND()); +a +1 +drop table t1; +# End of 12.2 tests +# # MDEV-19714: JOIN::pseudo_bits_cond is not visible in EXPLAIN FORMAT=JSON # CREATE TABLE t1 ( a INT ); diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 9b60977f2735d..c358cd913c0dc 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -5160,13 +5160,20 @@ bool st_select_lex::optimize_unflattened_subqueries(bool const_only) bool has_rand= false; for (SELECT_LEX *sl= un->first_select(); sl && !has_rand; sl= sl->next_select()) - has_rand= sl->uncacheable & UNCACHEABLE_RAND; + has_rand= (sl->uncacheable & UNCACHEABLE_RAND); if (has_rand) { for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select()) sl->uncacheable |= UNCACHEABLE_UNITED; } + /* + If any SELECT in the unit is marked as UNCACHEABLE_RAND, then the + unit itself should also be marked as UNCACHEABLE_RAND. + */ + DBUG_ASSERT(has_rand == + static_cast(un->uncacheable & UNCACHEABLE_RAND)); + if (is_correlated_unit) { /* @@ -11403,6 +11410,7 @@ SELECT_LEX_UNIT *LEX::parsed_select_expr_cont(SELECT_LEX_UNIT *unit, } last->link_neighbour(sel1); sel1->set_master_unit(unit); + unit->uncacheable|= sel1->uncacheable; sel1->set_linkage_and_distinct(unit_type, distinct); unit->pre_last_parse= last; return unit;