Commit 930db43e authored by bell@sanja.is.com.ua's avatar bell@sanja.is.com.ua

correct processing of rand() in subqueries with static tables (BUG#2645)

parent d0d54abc
...@@ -1625,3 +1625,11 @@ PIPPO ...@@ -1625,3 +1625,11 @@ PIPPO
1 1
NULL NULL
DROP TABLE t1, t2; DROP TABLE t1, t2;
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;
...@@ -1075,3 +1075,11 @@ s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM ...@@ -1075,3 +1075,11 @@ s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM
t2 AS cns; t2 AS cns;
DROP TABLE t1, t2; DROP TABLE t1, t2;
#
# 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;
...@@ -952,6 +952,7 @@ void Item_func_rand::fix_length_and_dec() ...@@ -952,6 +952,7 @@ void Item_func_rand::fix_length_and_dec()
{ {
decimals=NOT_FIXED_DEC; decimals=NOT_FIXED_DEC;
max_length=float_length(decimals); max_length=float_length(decimals);
used_tables_cache|= RAND_TABLE_BIT;
if (arg_count) if (arg_count)
{ // Only use argument once in query { // Only use argument once in query
uint32 tmp= (uint32) (args[0]->val_int()); uint32 tmp= (uint32) (args[0]->val_int());
......
...@@ -504,7 +504,6 @@ class Item_func_rand :public Item_real_func ...@@ -504,7 +504,6 @@ class Item_func_rand :public Item_real_func
double val(); double val();
const char *func_name() const { return "rand"; } const char *func_name() const { return "rand"; }
bool const_item() const { return 0; } bool const_item() const { return 0; }
table_map used_tables() const { return RAND_TABLE_BIT; }
void fix_length_and_dec(); void fix_length_and_dec();
}; };
......
...@@ -586,7 +586,9 @@ JOIN::optimize() ...@@ -586,7 +586,9 @@ JOIN::optimize()
DBUG_RETURN(1); // error == -1 DBUG_RETURN(1); // error == -1
} }
if (const_table_map != found_const_table_map && 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"; zero_result_cause= "no matching row in const table";
DBUG_PRINT("error",("Error: %s", zero_result_cause)); DBUG_PRINT("error",("Error: %s", zero_result_cause));
...@@ -3387,7 +3389,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -3387,7 +3389,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
table_map used_tables; table_map used_tables;
if (join->tables > 1) if (join->tables > 1)
cond->update_used_tables(); // Tablenr may have changed 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; join->const_table_map|=RAND_TABLE_BIT;
{ // Check const tables { // Check const tables
COND *const_cond= COND *const_cond=
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment