summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/subselect.result8
-rw-r--r--mysql-test/t/subselect.test8
-rw-r--r--sql/item_func.cc1
-rw-r--r--sql/item_func.h1
-rw-r--r--sql/sql_select.cc8
5 files changed, 23 insertions, 3 deletions
diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 1b96faa04af..7d2a4343f3f 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -1630,3 +1630,11 @@ select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where
count(*)
0
drop table if exists t1;
+create table t1 (a int);
+insert into t1 values (1);
+explain select benchmark(1000, (select a from t1 where a=sha(rand())));
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1
+drop table t1;
+
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index 191e12bef5d..05d04af2a7a 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -1073,3 +1073,11 @@ create table t1(val varchar(10));
insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');
drop table if exists t1;
+
+#
+# Static tables & rund() in subqueries
+#
+create table t1 (a int);
+insert into t1 values (1);
+explain select benchmark(1000, (select a from t1 where a=sha(rand())));
+drop table t1;
diff --git a/sql/item_func.cc b/sql/item_func.cc
index 878c4d2ea5f..7ed19d7506c 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -954,6 +954,7 @@ void Item_func_rand::fix_length_and_dec()
{
decimals=NOT_FIXED_DEC;
max_length=float_length(decimals);
+ used_tables_cache|= RAND_TABLE_BIT;
if (arg_count)
{ // Only use argument once in query
uint32 tmp= (uint32) (args[0]->val_int());
diff --git a/sql/item_func.h b/sql/item_func.h
index 8fbe5a124da..0071b3c5495 100644
--- a/sql/item_func.h
+++ b/sql/item_func.h
@@ -512,7 +512,6 @@ public:
double val();
const char *func_name() const { return "rand"; }
bool const_item() const { return 0; }
- table_map used_tables() const { return RAND_TABLE_BIT; }
void fix_length_and_dec();
};
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 467c1295517..bb9223aef22 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -584,7 +584,9 @@ JOIN::optimize()
DBUG_RETURN(1); // error == -1
}
if (const_table_map != found_const_table_map &&
- !(select_options & SELECT_DESCRIBE))
+ !(select_options & SELECT_DESCRIBE) &&
+ !((conds->used_tables() & RAND_TABLE_BIT) &&
+ select_lex->master_unit() != &thd->lex->unit))// not upper level SELECT
{
zero_result_cause= "no matching row in const table";
DBUG_PRINT("error",("Error: %s", zero_result_cause));
@@ -3407,7 +3409,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
table_map used_tables;
if (join->tables > 1)
cond->update_used_tables(); // Tablenr may have changed
- if (join->const_tables == join->tables)
+ if (join->const_tables == join->tables &&
+ join->thd->lex->current_select->master_unit() ==
+ &join->thd->lex->unit) // not upper level SELECT
join->const_table_map|=RAND_TABLE_BIT;
{ // Check const tables
COND *const_cond=