datafusion
datafusion copied to clipboard
`SanityCheckPlan` error in a sort-merge join when two equivalence classes refer to each other
Describe the bug
Follow-up to #26967 (review). That PR fixes a filter that puts a + b and a in one equivalence class. The same failure remains when two classes refer to each other.
With x.a + x.b = x.c AND x.c + x.b = x.a the classes are {a + b, c} and {c + b, a}. Neither canonical expression contains a member of its own class, but each contains a member of the other, so EquivalenceGroup::normalize_expr is not idempotent:
a -> c + b -> (a + b) + b -> ...
To Reproduce
set datafusion.optimizer.prefer_hash_join = false;
CREATE TABLE t (a BIGINT, b BIGINT, c BIGINT) AS VALUES (1, 0, 1), (2, 3, 5);
SELECT * FROM t x JOIN t y ON x.a = y.a AND x.b = y.b
WHERE x.a + x.b = x.c AND x.c + x.b = x.a;
SanityCheckPlan: ... does not satisfy order requirements: [a@0 ASC, b@1 ASC].
Child-0 order: [[c@2 + b@1 ASC, b@1 ASC]]
Expected behavior
The query returns 1 0 1 1 0 1, as it does with prefer_hash_join = true. In general, no canonical expression should contain a member of any class.
Additional context
@adriangb suggested this shape in the review:
- Pick the canonical expression by cost, like Trino's
EqualityInference: fewest columns, then fewest nodes, then a stable tiebreak. Thenabeatsa + bwithout a promotion pass. - When a class is added or merged, check the canonical expressions across classes. If one contains a member of another class, directly or through a cycle, choose a different canonical expression or skip that rewrite.
- Add
debug_assert_eq!(normalize(normalize(e)), normalize(e))innormalize_expr, so the next case fails in tests instead of inSanityCheckPlan.
Willingness to contribute
I cannot contribute a fix for this bug at this time
take