Yaoxuan Wu created FLINK-41751:
----------------------------------
Summary: Outer join loses unmatched rows: a NULL-tolerant filter
on the nullable side's join key (COALESCE(a, 'n') <= 'o') is pushed into the
preserved side
Key: FLINK-41751
URL: https://issues.apache.org/jira/browse/FLINK-41751
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner
Affects Versions: 2.3.0
Reporter: Yaoxuan Wu
A WHERE condition that compares a NULL-tolerant expression of a LEFT/RIGHT
join's nullable-side key with a literal is pushed into the preserved side and
removes unmatched rows there. No error is raised; the rows are silently missing.
{code:sql}
-- l(a STRING) = {('x')}, r(b STRING, f BOOLEAN) = {('y', TRUE)}, both table
sources
SELECT * FROM r LEFT JOIN l ON b = a WHERE COALESCE(a, 'n') <= 'o' AND f;
-- returns no rows, expected ('y', TRUE, NULL)
SELECT * FROM l FULL JOIN r ON a = b WHERE COALESCE(a, 'n') <= 'o' AND f;
-- returns no rows, expected (NULL, 'y', TRUE)
SELECT * FROM r LEFT JOIN l ON b = a WHERE COALESCE(a, 'n') <= 'o';
-- ('y', TRUE, NULL), correct
{code}
For the unmatched row of r, a is NULL, so COALESCE(a, 'n') <= 'o' is TRUE and
the row must be returned. IF(a IS NULL, 'n', a) <= 'o' behaves the same way.
Reproduced in batch and streaming mode on 2.0.1, 2.2.1, 2.3.0 and master
(2.4-SNAPSHOT).
The optimized plan (first query) shows the condition rewritten to the preserved
side's key and pushed below the join:
{noformat}
Calc(select=[b, f, a], where=[<=(COALESCE(a, 'n'), 'o')])
+- HashJoin(joinType=[LeftOuterJoin], where=[=(b, a)], select=[b, f, a],
build=[left])
:- Exchange(distribution=[hash[b]])
: +- Calc(select=[b, f], where=[AND(f, <=(COALESCE(b, 'n'), 'o'))])
: +- TableSourceScan(fields=[b, f])
+- Exchange(distribution=[hash[a]])
+- TableSourceScan(fields=[a])
{noformat}
Cause: FlinkFilterJoinRule#pushFiltersToAnotherSide remaps a filter above the
join that references only the join key of one side to the other side's key
(through the equi-join condition), also for LEFT and RIGHT joins. For an outer
join this is only correct if the filter can never be true when the nullable
side's key is NULL, because the null-padded rows are then rejected anyway.
isSuitableFilterToPush only checks that the filter is a comparison (=, <>, <,
<=, >, >=) with a literal operand, not that the other operand is NULL when the
key is NULL. COALESCE(a, 'n') is not, so the pushed copy tests b instead of
NULL and removes the unmatched row. The rule only gets there if another
conjunct was pushed or the join type was simplified (the "AND f" above, or a
FULL join that becomes LEFT/RIGHT), which is why the third query is correct.
FLINK-30369 / FLINK-31273 excluded IS NULL / IS NOT NULL from this push for the
same reason. A more general fix is to push only conjuncts that are strong in
the remapped key columns (for example with Strong.isNull), instead of any
comparison with a literal.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)