Skip to content

Commit 101346c

Browse files
MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
We need to ensure that all the unions are marked as UNCACHEABLE_RAND in cases like: select * from t1 where 1 in (select 1 union select 2 union select RAND());
1 parent 06cce62 commit 101346c

File tree

8 files changed

+79
-1
lines changed

8 files changed

+79
-1
lines changed

mysql-test/main/subselect.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7642,3 +7642,13 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e'
76427642
DROP VIEW v1;
76437643
DROP TABLE t1, t2, t3;
76447644
# End of 10.10 tests
7645+
#
7646+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7647+
#
7648+
create table t1 (a int);
7649+
insert into t1 (a) values (1);
7650+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7651+
a
7652+
1
7653+
drop table t1;
7654+
# End of 12.2 tests

mysql-test/main/subselect.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6501,3 +6501,13 @@ DROP VIEW v1;
65016501
DROP TABLE t1, t2, t3;
65026502

65036503
--echo # End of 10.10 tests
6504+
6505+
--echo #
6506+
--echo # MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
6507+
--echo #
6508+
create table t1 (a int);
6509+
insert into t1 (a) values (1);
6510+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
6511+
drop table t1;
6512+
6513+
--echo # End of 12.2 tests

mysql-test/main/subselect_no_exists_to_in.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7644,6 +7644,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e'
76447644
DROP VIEW v1;
76457645
DROP TABLE t1, t2, t3;
76467646
# End of 10.10 tests
7647+
#
7648+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7649+
#
7650+
create table t1 (a int);
7651+
insert into t1 (a) values (1);
7652+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7653+
a
7654+
1
7655+
drop table t1;
7656+
# End of 12.2 tests
76477657
set optimizer_switch=default;
76487658
select @@optimizer_switch like '%exists_to_in=off%';
76497659
@@optimizer_switch like '%exists_to_in=off%'

mysql-test/main/subselect_no_mat.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7639,6 +7639,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e'
76397639
DROP VIEW v1;
76407640
DROP TABLE t1, t2, t3;
76417641
# End of 10.10 tests
7642+
#
7643+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7644+
#
7645+
create table t1 (a int);
7646+
insert into t1 (a) values (1);
7647+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7648+
a
7649+
1
7650+
drop table t1;
7651+
# End of 12.2 tests
76427652
set optimizer_switch=default;
76437653
select @@optimizer_switch like '%materialization=on%';
76447654
@@optimizer_switch like '%materialization=on%'

mysql-test/main/subselect_no_opts.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7637,4 +7637,14 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e'
76377637
DROP VIEW v1;
76387638
DROP TABLE t1, t2, t3;
76397639
# End of 10.10 tests
7640+
#
7641+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7642+
#
7643+
create table t1 (a int);
7644+
insert into t1 (a) values (1);
7645+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7646+
a
7647+
1
7648+
drop table t1;
7649+
# End of 12.2 tests
76407650
set @optimizer_switch_for_subselect_test=null;

mysql-test/main/subselect_no_scache.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7648,6 +7648,16 @@ Warning 1292 Truncated incorrect DECIMAL value: 'e'
76487648
DROP VIEW v1;
76497649
DROP TABLE t1, t2, t3;
76507650
# End of 10.10 tests
7651+
#
7652+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7653+
#
7654+
create table t1 (a int);
7655+
insert into t1 (a) values (1);
7656+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7657+
a
7658+
1
7659+
drop table t1;
7660+
# End of 12.2 tests
76517661
set optimizer_switch=default;
76527662
select @@optimizer_switch like '%subquery_cache=on%';
76537663
@@optimizer_switch like '%subquery_cache=on%'

mysql-test/main/subselect_no_semijoin.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7638,6 +7638,16 @@ DROP VIEW v1;
76387638
DROP TABLE t1, t2, t3;
76397639
# End of 10.10 tests
76407640
#
7641+
# MDEV-37905: UNCACHEABLE_RAND inconsistently set on unions
7642+
#
7643+
create table t1 (a int);
7644+
insert into t1 (a) values (1);
7645+
select * from t1 where 1 in (select 1 union select 2 union select RAND());
7646+
a
7647+
1
7648+
drop table t1;
7649+
# End of 12.2 tests
7650+
#
76417651
# MDEV-19714: JOIN::pseudo_bits_cond is not visible in EXPLAIN FORMAT=JSON
76427652
#
76437653
CREATE TABLE t1 ( a INT );

sql/sql_lex.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5160,13 +5160,20 @@ bool st_select_lex::optimize_unflattened_subqueries(bool const_only)
51605160
bool has_rand= false;
51615161
for (SELECT_LEX *sl= un->first_select(); sl && !has_rand;
51625162
sl= sl->next_select())
5163-
has_rand= sl->uncacheable & UNCACHEABLE_RAND;
5163+
has_rand= (sl->uncacheable & UNCACHEABLE_RAND);
51645164
if (has_rand)
51655165
{
51665166
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
51675167
sl->uncacheable |= UNCACHEABLE_UNITED;
51685168
}
51695169

5170+
/*
5171+
If any SELECT in the unit is marked as UNCACHEABLE_RAND, then the
5172+
unit itself should also be marked as UNCACHEABLE_RAND.
5173+
*/
5174+
DBUG_ASSERT(has_rand ==
5175+
static_cast<bool>(un->uncacheable & UNCACHEABLE_RAND));
5176+
51705177
if (is_correlated_unit)
51715178
{
51725179
/*
@@ -11403,6 +11410,7 @@ SELECT_LEX_UNIT *LEX::parsed_select_expr_cont(SELECT_LEX_UNIT *unit,
1140311410
}
1140411411
last->link_neighbour(sel1);
1140511412
sel1->set_master_unit(unit);
11413+
unit->uncacheable|= sel1->uncacheable;
1140611414
sel1->set_linkage_and_distinct(unit_type, distinct);
1140711415
unit->pre_last_parse= last;
1140811416
return unit;

0 commit comments

Comments
 (0)