Bug #121975 Wrong result: hash join extra condition `a.x = b.y` inside OR rewritten to `c.z = c.z`
Description:
For `b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5)` MySQL 26.10.0 matches c rows
for which `a.x = b.y` is FALSE. EXPLAIN shows the extra hash-join condition `((c.z = c.z) or (a.id = 5))`:
both a.x and b.y were remapped to the build-side column c.z. a.x -> c.z is valid (ON-level multiple
equality {a.x, c.z}), but b.y -> c.z uses the multiple equality {b.y, a.x, c.z} that only holds inside the
first OR branch, so the disjunct becomes a tautology. The optimizer trace (finalizing_table_conditions)
still shows the correct condition. Correct with hypergraph_optimizer=on or block_nested_loop=off.
The join order matters (b before a); with a STRAIGHT_JOIN b the result is correct.
Related: Bug #121974 (same family: multiple-equality substitution when hash join conditions are split).
How to repeat:
CREATE TABLE a(id INT, x INT); CREATE TABLE b(y INT); CREATE TABLE c(z INT);
INSERT INTO a VALUES (0, 1); INSERT INTO b VALUES (2); INSERT INTO c VALUES (1);
SELECT a.x, c.z FROM b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5);
-- expected: 1, NULL actual (26.10.0): 1, 1
EXPLAIN FORMAT=TREE SELECT a.x, c.z FROM b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5);
-- -> Left hash join (c.z = a.x), extra conditions: ((c.z = c.z) or (a.id = 5))
(The same happens without STRAIGHT_JOIN when the optimizer picks that order, e.g. a with 2 rows
(0,1),(1,NULL) and b with one NULL row.)
Suggested fix:
SplitConditions() (sql/sql_executor.cc) calls find_and_adjust_equal_fields(cond, right_table_map,
replace=true) on extra conditions that reference only probe-side tables (added for Bug#35749693). It uses each
field's own item_equal, which for b.y is the OR-branch multiple equality, not an ON-level one. The remap must
only use multiple equalities that are valid for the whole ON condition (or skip conditions containing OR /
nested Item_cond), and must not turn an equality into `x = x`.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.050 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: For `b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5)` MySQL 26.10.0 matches c rows for which `a.x = b.y` is FALSE. EXPLAIN shows the extra hash-join condition `((c.z = c.z) or (a.id = 5))`: both a.x and b.y were remapped to the build-side column c.z. a.x -> c.z is valid (ON-level multiple equality {a.x, c.z}), but b.y -> c.z uses the multiple equality {b.y, a.x, c.z} that only holds inside the first OR branch, so the disjunct becomes a tautology. The optimizer trace (finalizing_table_conditions) still shows the correct condition. Correct with hypergraph_optimizer=on or block_nested_loop=off. The join order matters (b before a); with a STRAIGHT_JOIN b the result is correct. Related: Bug #121974 (same family: multiple-equality substitution when hash join conditions are split). How to repeat: CREATE TABLE a(id INT, x INT); CREATE TABLE b(y INT); CREATE TABLE c(z INT); INSERT INTO a VALUES (0, 1); INSERT INTO b VALUES (2); INSERT INTO c VALUES (1); SELECT a.x, c.z FROM b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5); -- expected: 1, NULL actual (26.10.0): 1, 1 EXPLAIN FORMAT=TREE SELECT a.x, c.z FROM b STRAIGHT_JOIN a LEFT JOIN c ON a.x = c.z AND (a.x = b.y OR a.id = 5); -- -> Left hash join (c.z = a.x), extra conditions: ((c.z = c.z) or (a.id = 5)) (The same happens without STRAIGHT_JOIN when the optimizer picks that order, e.g. a with 2 rows (0,1),(1,NULL) and b with one NULL row.) Suggested fix: SplitConditions() (sql/sql_executor.cc) calls find_and_adjust_equal_fields(cond, right_table_map, replace=true) on extra conditions that reference only probe-side tables (added for Bug#35749693). It uses each field's own item_equal, which for b.y is the OR-branch multiple equality, not an ON-level one. The remap must only use multiple equalities that are valid for the whole ON condition (or skip conditions containing OR / nested Item_cond), and must not turn an equality into `x = x`.