1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-27 18:02:13 +03:00

MDEV-27624 Wrong result for nested left join using not_exists optimization

This bug affected queries with nested left joins having the same last inner
table such that not_exists optimization could be applied to the most inner
outer join when optimizer chose to use join buffers. The bug could lead to
producing wrong a result set.
If the WHERE condition a query contains a conjunctive IS NULL predicate
over a non-nullable column of an inner table of a not nested outer join
then not_exists optimization can be applied to tho the outer join. With
this optimization when looking for matches for a certain record from the
outer table of the join the records of the inner table can be ignored
right after the first match satisfying the ON condition is found.
In the case of nested outer joins having the same last inner table this
optimization still can be applied but only if all ON conditions of the
embedding outer joins are satisfied. Such check was missing in the code
that tried to apply not_exists optimization when join buffers were used
for outer join operations.
This problem has been already fixed in the patch for bug MDEV-7992. Yet
there it was resolved only for the cases when join buffers were not used
for outer joins.

Approved by Oleksandr Byelkin <sanja@mariadb.com>
This commit is contained in:
Igor Babaev
2022-10-26 22:08:32 -07:00
parent af0ff8b455
commit b21832ef15
4 changed files with 171 additions and 6 deletions

View File

@ -1999,3 +1999,55 @@ Note 1003 select `test`.`t3`.`pk` AS `pk`,`test`.`t3`.`c1` AS `c1`,`test`.`t3`.`
DROP TABLE t1,t2,t3;
set join_cache_level= @save_join_cache_level;
set optimizer_switch=@save_optimizer_switch;
#
# MDEV-27624: Nested left joins with not_exists optimization
# for most inner left join
#
set @save_join_cache_level= @@join_cache_level;
CREATE TABLE t1 (a INT NOT NULL, b INT, c INT);
INSERT INTO t1 VALUES (1,1,1), (1,2,1), (1,3,1);
CREATE TABLE t2(a INT NOT NULL);
INSERT INTO t2 VALUES (1), (2);
CREATE TABLE t3(a INT not null, b INT);
INSERT INTO t3 VALUES (1, 1), (2, 1), (3, 1);
set join_cache_level = 0;
EXPLAIN SELECT *
FROM t1
LEFT JOIN
( t2 LEFT JOIN t3 ON t2.a = t3.b )
ON t2.a = 1 AND (t3.b = t1.a AND t3.a > t1.b OR t3.a is NULL)
WHERE t1.c = 1 AND t3.a is NULL;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 3 Using where; Not exists
SELECT *
FROM t1
LEFT JOIN
( t2 LEFT JOIN t3 ON t2.a = t3.b )
ON t2.a = 1 AND (t3.b = t1.a AND t3.a > t1.b OR t3.a is NULL)
WHERE t1.c = 1 AND t3.a is NULL;
a b c a a b
1 3 1 NULL NULL NULL
set join_cache_level = 2;
EXPLAIN SELECT *
FROM t1
LEFT JOIN
( t2 LEFT JOIN t3 ON t2.a = t3.b )
ON t2.a = 1 AND (t3.b = t1.a AND t3.a > t1.b OR t3.a is NULL)
WHERE t1.c = 1 AND t3.a is NULL;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t3 ALL NULL NULL NULL NULL 3 Using where; Not exists; Using join buffer (incremental, BNL join)
SELECT *
FROM t1
LEFT JOIN
( t2 LEFT JOIN t3 ON t2.a = t3.b )
ON t2.a = 1 AND (t3.b = t1.a AND t3.a > t1.b OR t3.a is NULL)
WHERE t1.c = 1 AND t3.a is NULL;
a b c a a b
1 3 1 NULL NULL NULL
DROP TABLE t1, t2, t3;
set join_cache_level= @save_join_cache_level;
# end of 10.3 tests