Skip to content

Commit

Permalink
MDEV-14779: using left join causes incorrect results with materializa…
Browse files Browse the repository at this point in the history
…tion and derived tables

Conversion of a subquery to a semi-join is blocked when we have an
IN subquery predicate in the on_expr of an outer join. Currently this
scenario is handled but the cases when an IN subquery predicate is wrapped
inside a Item_in_optimizer item then this blocking is not done.
  • Loading branch information
varunraiko committed Mar 21, 2018
1 parent f3994b7 commit ddc5c65
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
18 changes: 17 additions & 1 deletion mysql-test/r/join_outer.result
Original file line number Diff line number Diff line change
Expand Up @@ -2346,11 +2346,27 @@ CREATE TABLE t1 (b1 BIT NOT NULL);
INSERT INTO t1 VALUES (0),(1);
CREATE TABLE t2 (b2 BIT NOT NULL);
INSERT INTO t2 VALUES (0),(1);
SET SESSION JOIN_CACHE_LEVEL = 3;
set @save_join_cache_level= @@join_cache_level;
SET @@join_cache_level = 3;
SELECT t1.b1+'0' , t2.b2 + '0' FROM t1 LEFT JOIN t2 ON b1 = b2;
t1.b1+'0' t2.b2 + '0'
0 0
1 1
DROP TABLE t1, t2;
set @join_cache_level= @save_join_cache_level;
#
# MDEV-14779: using left join causes incorrect results with materialization and derived tables
#
create table t1(id int);
insert into t1 values (1),(2);
create table t2(sid int, id int);
insert into t2 values (1,1),(2,2);
select * from t1 t
left join (select * from t2 where sid in (select max(sid) from t2 where 0=1 group by id)) r
on t.id=r.id ;
id sid id
1 NULL NULL
2 NULL NULL
drop table t1, t2;
# end of 5.5 tests
SET optimizer_switch=@save_optimizer_switch;
18 changes: 17 additions & 1 deletion mysql-test/r/join_outer_jcl6.result
Original file line number Diff line number Diff line change
Expand Up @@ -2357,12 +2357,28 @@ CREATE TABLE t1 (b1 BIT NOT NULL);
INSERT INTO t1 VALUES (0),(1);
CREATE TABLE t2 (b2 BIT NOT NULL);
INSERT INTO t2 VALUES (0),(1);
SET SESSION JOIN_CACHE_LEVEL = 3;
set @save_join_cache_level= @@join_cache_level;
SET @@join_cache_level = 3;
SELECT t1.b1+'0' , t2.b2 + '0' FROM t1 LEFT JOIN t2 ON b1 = b2;
t1.b1+'0' t2.b2 + '0'
0 0
1 1
DROP TABLE t1, t2;
set @join_cache_level= @save_join_cache_level;
#
# MDEV-14779: using left join causes incorrect results with materialization and derived tables
#
create table t1(id int);
insert into t1 values (1),(2);
create table t2(sid int, id int);
insert into t2 values (1,1),(2,2);
select * from t1 t
left join (select * from t2 where sid in (select max(sid) from t2 where 0=1 group by id)) r
on t.id=r.id ;
id sid id
1 NULL NULL
2 NULL NULL
drop table t1, t2;
# end of 5.5 tests
SET optimizer_switch=@save_optimizer_switch;
set join_cache_level=default;
Expand Down
18 changes: 17 additions & 1 deletion mysql-test/t/join_outer.test
Original file line number Diff line number Diff line change
Expand Up @@ -1891,9 +1891,25 @@ INSERT INTO t1 VALUES (0),(1);
CREATE TABLE t2 (b2 BIT NOT NULL);
INSERT INTO t2 VALUES (0),(1);

SET SESSION JOIN_CACHE_LEVEL = 3;
set @save_join_cache_level= @@join_cache_level;
SET @@join_cache_level = 3;
SELECT t1.b1+'0' , t2.b2 + '0' FROM t1 LEFT JOIN t2 ON b1 = b2;
DROP TABLE t1, t2;
set @join_cache_level= @save_join_cache_level;

--echo #
--echo # MDEV-14779: using left join causes incorrect results with materialization and derived tables
--echo #

create table t1(id int);
insert into t1 values (1),(2);
create table t2(sid int, id int);
insert into t2 values (1,1),(2,2);

select * from t1 t
left join (select * from t2 where sid in (select max(sid) from t2 where 0=1 group by id)) r
on t.id=r.id ;
drop table t1, t2;

--echo # end of 5.5 tests

Expand Down
5 changes: 5 additions & 0 deletions sql/item_cmpfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class Item_in_optimizer: public Item_bool_func
bool is_null();
longlong val_int();
void cleanup();
enum Functype functype() const { return IN_OPTIMIZER_FUNC; }
const char *func_name() const { return "<in_optimizer>"; }
Item_cache **get_cache() { return &cache; }
void keep_top_level_cache();
Expand All @@ -270,6 +271,10 @@ class Item_in_optimizer: public Item_bool_func
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
virtual void print(String *str, enum_query_type query_type);
void restore_first_argument();
Item* get_wrapped_in_subselect_item()
{
return args[1];
}
};

class Comp_creator
Expand Down
2 changes: 1 addition & 1 deletion sql/item_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Item_func :public Item_result_field
NOW_FUNC, TRIG_COND_FUNC,
SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
NEG_FUNC, GSYSVAR_FUNC };
NEG_FUNC, GSYSVAR_FUNC, IN_OPTIMIZER_FUNC };
enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
OPTIMIZE_EQUAL };
enum Type type() const { return FUNC_ITEM; }
Expand Down
4 changes: 4 additions & 0 deletions sql/opt_subselect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,10 @@ bool check_for_outer_joins(List<TABLE_LIST> *join_list)
void find_and_block_conversion_to_sj(Item *to_find,
List_iterator_fast<Item_in_subselect> &li)
{
if (to_find->type() == Item::FUNC_ITEM &&
((Item_func*)to_find)->functype() == Item_func::IN_OPTIMIZER_FUNC)
to_find= ((Item_in_optimizer*)to_find)->get_wrapped_in_subselect_item();

if (to_find->type() != Item::SUBSELECT_ITEM ||
((Item_subselect *) to_find)->substype() != Item_subselect::IN_SUBS)
return;
Expand Down

0 comments on commit ddc5c65

Please sign in to comment.