Bug #121974 Wrong result: hash join drops equality with a column of an always-NULL (ON FALSE) outer-joined table
Description:
In a LEFT JOIN whose ON condition is `z.a = x.a AND x.a = y.a`, where `y` is outer-joined with `ON FALSE`
(so y.a is always NULL), the condition must be FALSE for every row and z must be NULL-extended.
MySQL 26.10.0 returns a matched z row. EXPLAIN shows the hash join condition `(x.a = z.a), (z.a = x.a)`:
the conjunct `x.a = y.a` was rewritten into `z.a = x.a`, i.e. y.a was replaced by an "equal" field from
the build side, although the multiple equality {x.a, y.a, z.a} only holds when that very ON condition is TRUE.
The optimizer trace (finalizing_table_conditions) still shows the correct condition
`((x.a = y.a) and (z.a = y.a))`; the rewrite happens when the hash join conditions are created.
Correct with: SET optimizer_switch='hypergraph_optimizer=on'; or SET optimizer_switch='block_nested_loop=off';
How to repeat:
CREATE TABLE u(a INT);
INSERT INTO u VALUES (1);
SELECT x.a, y.a, z.a FROM u x LEFT JOIN u y ON FALSE LEFT JOIN u z ON z.a = x.a AND x.a = y.a;
-- expected: 1, NULL, NULL actual (26.10.0): 1, NULL, 1
EXPLAIN FORMAT=TREE SELECT x.a, y.a, z.a FROM u x LEFT JOIN u y ON FALSE LEFT JOIN u z ON z.a = x.a AND x.a = y.a;
-- -> Left hash join (x.a = z.a), (z.a = x.a)
Suggested fix:
In SplitConditions() (sql/sql_executor.cc), Item_func_eq::ensure_multi_equality_fields_are_available(
replace=true) (sql/item_cmpfunc.cc) replaces y.a, whose table is neither in the probe nor the build map
(y is a const NULL-row table), by FindEqualField(). Substituting through the multiple equality is only valid
if the equality is implied by a multiple equality of an enclosing scope, not by the ON condition that is
being split. For a field of a const/NULL-complemented table the equality should be evaluated as is
(it folds to FALSE), not re-targeted.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: In a LEFT JOIN whose ON condition is `z.a = x.a AND x.a = y.a`, where `y` is outer-joined with `ON FALSE` (so y.a is always NULL), the condition must be FALSE for every row and z must be NULL-extended. MySQL 26.10.0 returns a matched z row. EXPLAIN shows the hash join condition `(x.a = z.a), (z.a = x.a)`: the conjunct `x.a = y.a` was rewritten into `z.a = x.a`, i.e. y.a was replaced by an "equal" field from the build side, although the multiple equality {x.a, y.a, z.a} only holds when that very ON condition is TRUE. The optimizer trace (finalizing_table_conditions) still shows the correct condition `((x.a = y.a) and (z.a = y.a))`; the rewrite happens when the hash join conditions are created. Correct with: SET optimizer_switch='hypergraph_optimizer=on'; or SET optimizer_switch='block_nested_loop=off'; How to repeat: CREATE TABLE u(a INT); INSERT INTO u VALUES (1); SELECT x.a, y.a, z.a FROM u x LEFT JOIN u y ON FALSE LEFT JOIN u z ON z.a = x.a AND x.a = y.a; -- expected: 1, NULL, NULL actual (26.10.0): 1, NULL, 1 EXPLAIN FORMAT=TREE SELECT x.a, y.a, z.a FROM u x LEFT JOIN u y ON FALSE LEFT JOIN u z ON z.a = x.a AND x.a = y.a; -- -> Left hash join (x.a = z.a), (z.a = x.a) Suggested fix: In SplitConditions() (sql/sql_executor.cc), Item_func_eq::ensure_multi_equality_fields_are_available( replace=true) (sql/item_cmpfunc.cc) replaces y.a, whose table is neither in the probe nor the build map (y is a const NULL-row table), by FindEqualField(). Substituting through the multiple equality is only valid if the equality is implied by a multiple equality of an enclosing scope, not by the ON condition that is being split. For a field of a const/NULL-complemented table the equality should be evaluated as is (it folds to FALSE), not re-targeted.