Skip to content

Commit

Permalink
MDEV-19580 Unrelated JOINs corrupt usage of 'WHERE function() IN (sub…
Browse files Browse the repository at this point in the history
…query)'

Handling of top level conjuncts in WHERE whose used_tables() contained
RAND_TABLE_BIT in the function make_join_select() was incorrect.
As a result if such a conjunct referred to fields non of which belonged
to the last joined table  it was pushed twice. (This could be seen
for a test case from subselect.test whose output was changed after this
patch had been applied. In 10.1 when running EXPLAIN FORMAT=JSON for
the query from this test case we clearly see that one of the conjuncts
is pushed twice.) This fact by itself was not good. Besides, if such a
conjunct was pushed to a table that was the result of materialization
of a semi-join the query could return a wrong result set. In particular
we could watch it for queries with semi-join subqueries whose left parts
used stored functions without "deterministic' specifier.
  • Loading branch information
igorbabaev committed Jun 9, 2019
1 parent 6660c07 commit 6db2ebb
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 19 deletions.
2 changes: 1 addition & 1 deletion mysql-test/r/subselect.result
Original file line number Diff line number Diff line change
Expand Up @@ -6939,7 +6939,7 @@ WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
Expand Down
89 changes: 89 additions & 0 deletions mysql-test/r/subselect_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -2453,6 +2453,95 @@ SELECT 1 FROM t1 where t1.id IN (SELECT v1.i1 from v1);
1
drop table t1,t2;
drop view v1;
#
# MDEV-19580: function invocation in the left part of IN subquery
#
create table t1 (id int, a varchar(50), b int);
insert into t1 values
(1,'mrs',2), (2,'joe',2), (3,'paul',1), (4,'art',1);
create table t2 (id int, a varchar(50), x int);
insert into t2 values
(1,'grand',1),(2,'average',1),(3,'serf',0);
create table t3 (d1 date, d2 date, t1_id int, t2_id int );
insert into t3 values
('1972-01-01','1988-12-31',3,1), ('1972-01-01','1988-12-31',4,1),
('1972-01-01','1988-12-31',1,2), ('1972-01-01','1988-12-31',2,3);
create table t4 ( id int, a varchar(50) );
insert into t4 values
(1,'songwriter'),(2,'song character');
create function f1(who int, dt date) returns int
deterministic
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$
create function f2(who int, dt date) returns int
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$
# Deterministic function in left part of IN subquery: semi-join is OK
select * from t1
left join t4 on t1.b = t4.id
where f1(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a
3 paul 1 1 songwriter
4 art 1 1 songwriter
1 mrs 2 2 song character
explain extended select * from t1
left join t4 on t1.b = t4.id
where f1(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f1`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
# Non-deterministic function in left part of IN subq: semi-join is OK
select * from t1
left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a
3 paul 1 1 songwriter
4 art 1 1 songwriter
1 mrs 2 2 song character
explain extended select * from t1
left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f2`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
select t1.*, t4.*,
(select max(t4.id) from t4 where t4.id=t1.b and sleep(0) = 0) as s
from t1 left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a s
3 paul 1 1 songwriter 1
4 art 1 1 songwriter 1
1 mrs 2 2 song character 2
explain extended select t1.*, t4.*,
(select max(t4.id) from t4 where t4.id=t1.b and sleep(0) = 0) as s
from t1 left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
3 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a`,(select max(`test`.`t4`.`id`) from `test`.`t4` where ((`test`.`t4`.`id` = `test`.`t1`.`b`) and (sleep(0) = 0))) AS `s` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f2`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
drop function f1;
drop function f2;
drop table t1,t2,t3,t4;
# End of 5.5 tests
set @subselect_mat_test_optimizer_switch_value=null;
set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_no_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -6937,7 +6937,7 @@ WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_no_opts.result
Original file line number Diff line number Diff line change
Expand Up @@ -6934,7 +6934,7 @@ WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_no_scache.result
Original file line number Diff line number Diff line change
Expand Up @@ -6945,7 +6945,7 @@ WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_no_semijoin.result
Original file line number Diff line number Diff line change
Expand Up @@ -6934,7 +6934,7 @@ WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
Expand Down
89 changes: 89 additions & 0 deletions mysql-test/r/subselect_sj_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -2493,4 +2493,93 @@ SELECT 1 FROM t1 where t1.id IN (SELECT v1.i1 from v1);
1
drop table t1,t2;
drop view v1;
#
# MDEV-19580: function invocation in the left part of IN subquery
#
create table t1 (id int, a varchar(50), b int);
insert into t1 values
(1,'mrs',2), (2,'joe',2), (3,'paul',1), (4,'art',1);
create table t2 (id int, a varchar(50), x int);
insert into t2 values
(1,'grand',1),(2,'average',1),(3,'serf',0);
create table t3 (d1 date, d2 date, t1_id int, t2_id int );
insert into t3 values
('1972-01-01','1988-12-31',3,1), ('1972-01-01','1988-12-31',4,1),
('1972-01-01','1988-12-31',1,2), ('1972-01-01','1988-12-31',2,3);
create table t4 ( id int, a varchar(50) );
insert into t4 values
(1,'songwriter'),(2,'song character');
create function f1(who int, dt date) returns int
deterministic
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$
create function f2(who int, dt date) returns int
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$
# Deterministic function in left part of IN subquery: semi-join is OK
select * from t1
left join t4 on t1.b = t4.id
where f1(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a
3 paul 1 1 songwriter
4 art 1 1 songwriter
1 mrs 2 2 song character
explain extended select * from t1
left join t4 on t1.b = t4.id
where f1(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f1`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
# Non-deterministic function in left part of IN subq: semi-join is OK
select * from t1
left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a
3 paul 1 1 songwriter
4 art 1 1 songwriter
1 mrs 2 2 song character
explain extended select * from t1
left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f2`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
select t1.*, t4.*,
(select max(t4.id) from t4 where t4.id=t1.b and sleep(0) = 0) as s
from t1 left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id a b id a s
3 paul 1 1 songwriter 1
4 art 1 1 songwriter 1
1 mrs 2 2 song character 2
explain extended select t1.*, t4.*,
(select max(t4.id) from t4 where t4.id=t1.b and sleep(0) = 0) as s
from t1 left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 100.00 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
3 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t4`.`id` AS `id`,`test`.`t4`.`a` AS `a`,(select max(`test`.`t4`.`id`) from `test`.`t4` where ((`test`.`t4`.`id` = `test`.`t1`.`b`) and (sleep(0) = 0))) AS `s` from `test`.`t1` semi join (`test`.`t2`) left join `test`.`t4` on((`test`.`t4`.`id` = `test`.`t1`.`b`)) where ((`test`.`t2`.`x` = 1) and (`f2`(`test`.`t1`.`id`,'1980-01-01') = `test`.`t2`.`id`))
drop function f1;
drop function f2;
drop table t1,t2,t3,t4;
# End of 5.5 tests
75 changes: 75 additions & 0 deletions mysql-test/t/subselect_sj_mat.test
Original file line number Diff line number Diff line change
Expand Up @@ -2230,4 +2230,79 @@ explain SELECT 1 FROM t1 where t1.id IN (SELECT v1.i1 from v1);
SELECT 1 FROM t1 where t1.id IN (SELECT v1.i1 from v1);
drop table t1,t2;
drop view v1;


--echo #
--echo # MDEV-19580: function invocation in the left part of IN subquery
--echo #

create table t1 (id int, a varchar(50), b int);
insert into t1 values
(1,'mrs',2), (2,'joe',2), (3,'paul',1), (4,'art',1);

create table t2 (id int, a varchar(50), x int);
insert into t2 values
(1,'grand',1),(2,'average',1),(3,'serf',0);

create table t3 (d1 date, d2 date, t1_id int, t2_id int );
insert into t3 values
('1972-01-01','1988-12-31',3,1), ('1972-01-01','1988-12-31',4,1),
('1972-01-01','1988-12-31',1,2), ('1972-01-01','1988-12-31',2,3);

create table t4 ( id int, a varchar(50) );
insert into t4 values
(1,'songwriter'),(2,'song character');

delimiter $$;

create function f1(who int, dt date) returns int
deterministic
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$

create function f2(who int, dt date) returns int
begin
declare result int;
select t2_id into result from t3 where dt>=d1 and dt<=d2 and t1_id=who;
return result;
end$$

delimiter ;$$

--echo # Deterministic function in left part of IN subquery: semi-join is OK

let $q1=
select * from t1
left join t4 on t1.b = t4.id
where f1(t1.id, '1980-01-01') in (select id from t2 where x=1);

eval $q1;
eval explain extended $q1;

--echo # Non-deterministic function in left part of IN subq: semi-join is OK

let $q2=
select * from t1
left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);

eval $q2;
eval explain extended $q2;

let $q3=
select t1.*, t4.*,
(select max(t4.id) from t4 where t4.id=t1.b and sleep(0) = 0) as s
from t1 left join t4 on t1.b = t4.id
where f2(t1.id, '1980-01-01') in (select id from t2 where x=1);

eval $q3;
eval explain extended $q3;

drop function f1;
drop function f2;
drop table t1,t2,t3,t4;

--echo # End of 5.5 tests
Loading

0 comments on commit 6db2ebb

Please sign in to comment.