Commit 934b8312 authored by Igor Babaev's avatar Igor Babaev

Fixed the bug mdev-7791.

When an IN subquery predicate was converted to a semi-join that were
materialized and the result of the materialization happened to be
the last in the execution plan then any conjunctive condition with RAND()
turned out to be lost.

Fixed by attaching this condition to the last top base table.
parent 2e1428c0
...@@ -1556,3 +1556,26 @@ i1 ...@@ -1556,3 +1556,26 @@ i1
DROP TABLE t1,t2,t3; DROP TABLE t1,t2,t3;
set join_cache_level= @save_join_cache_level; set join_cache_level= @save_join_cache_level;
set optimizer_switch=@save_optimizer_switch; set optimizer_switch=@save_optimizer_switch;
#
# mdev-7791: materialization of a semi-join subquery +
# RAND() in WHERE
# (materialized table is accessed last)
#
set @save_optimizer_switch=@@optimizer_switch;
set optimizer_switch='materialization=on';
create table t1(i int);
insert into t1 values (1), (2), (3), (7), (9), (10);
create table t2(i int);
insert into t2 values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
select * from t1 where (rand() < 0) and i in (select i from t2);
i
explain extended
select * from t1 where (rand() < 0) and i in (select i from t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 10 100.00
Warnings:
Note 1003 select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t2`) where ((rand() < 0))
drop table t1,t2;
set optimizer_switch=@save_optimizer_switch;
...@@ -263,3 +263,23 @@ DROP TABLE t1,t2,t3; ...@@ -263,3 +263,23 @@ DROP TABLE t1,t2,t3;
set join_cache_level= @save_join_cache_level; set join_cache_level= @save_join_cache_level;
set optimizer_switch=@save_optimizer_switch; set optimizer_switch=@save_optimizer_switch;
--echo #
--echo # mdev-7791: materialization of a semi-join subquery +
--echo # RAND() in WHERE
--echo # (materialized table is accessed last)
--echo #
set @save_optimizer_switch=@@optimizer_switch;
set optimizer_switch='materialization=on';
create table t1(i int);
insert into t1 values (1), (2), (3), (7), (9), (10);
create table t2(i int);
insert into t2 values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
select * from t1 where (rand() < 0) and i in (select i from t2);
explain extended
select * from t1 where (rand() < 0) and i in (select i from t2);
drop table t1,t2;
set optimizer_switch=@save_optimizer_switch;
...@@ -8761,12 +8761,20 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -8761,12 +8761,20 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
/* /*
Step #2: Extract WHERE/ON parts Step #2: Extract WHERE/ON parts
*/ */
uint i;
for (i= join->top_join_tab_count - 1; i >= join->const_tables; i--)
{
if (!join->join_tab[i].bush_children)
break;
}
uint last_top_base_tab_idx= i;
table_map save_used_tables= 0; table_map save_used_tables= 0;
used_tables=((select->const_tables=join->const_table_map) | used_tables=((select->const_tables=join->const_table_map) |
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT); OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
JOIN_TAB *tab; JOIN_TAB *tab;
table_map current_map; table_map current_map;
uint i= join->const_tables; i= join->const_tables;
for (tab= first_depth_first_tab(join); tab; for (tab= first_depth_first_tab(join); tab;
tab= next_depth_first_tab(join, tab), i++) tab= next_depth_first_tab(join, tab), i++)
{ {
...@@ -8804,7 +8812,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) ...@@ -8804,7 +8812,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
Following force including random expression in last table condition. Following force including random expression in last table condition.
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5 It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
*/ */
if (tab == join->join_tab + join->top_join_tab_count - 1) if (tab == join->join_tab + last_top_base_tab_idx)
current_map|= RAND_TABLE_BIT; current_map|= RAND_TABLE_BIT;
used_tables|=current_map; used_tables|=current_map;
......
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