[BugFix] Extra outer-join ON predicates can be "proven" from unrelated constant groups
Bug Description
Severity: CRITICAL
Component: Materialized View Rewriter
Source file: fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/transformation/materialization/MaterializedViewRewriter.java (line 284)
Root cause: The equivalence-class check at line 284 verifies that each column appears in some equivalence class, but does not verify they belong to the same class. This allows a diff predicate l.a = r.c to be falsely "proven" when l.a and r.c are in separate classes with different constant values.
Concrete failure scenario: A LEFT JOIN query with predicates l.a=1 and r.c=2 is rewritten using an MV whose ON clause contains l.a = r.c. Because the rewriter incorrectly treats the predicate as proven, it selects the MV — whose right-side columns are all NULL (since a=1, c=2 never matches) — causing the outer filter to eliminate every row.
Steps to Reproduce
CREATE TABLE t_left2 (id INT NOT NULL, a INT NOT NULL, b INT NOT NULL)
DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES ('replication_num' = '1');
CREATE TABLE t_right2 (id INT NOT NULL, c INT NOT NULL, d INT NOT NULL)
DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES ('replication_num' = '1');
INSERT INTO t_left2 VALUES (1,1,1), (2,1,1), (3,1,1);
INSERT INTO t_right2 VALUES (1,2,2), (2,2,2), (3,2,2), (4,2,2);
CREATE MATERIALIZED VIEW mv_c4_false_pos
DISTRIBUTED BY HASH(l_id) BUCKETS 4 REFRESH ASYNC AS
SELECT l.id AS l_id, r.id AS r_id, l.a, l.b, r.c, r.d
FROM t_left2 l LEFT JOIN t_right2 r ON l.id = r.id AND l.a = r.c;
REFRESH MATERIALIZED VIEW mv_c4_false_pos WITH SYNC MODE;
SET enable_materialized_view_rewrite = true;
SELECT COUNT(*)
FROM t_left2 l LEFT JOIN t_right2 r ON l.id = r.id
WHERE l.a=1 AND l.b=1 AND r.c=2 AND r.d=2;Actual Behavior
0 rows with MV rewrite enabled. EXPLAIN shows MaterializedView: true, scanning mv_c4_false_pos whose right-side columns are all NULL.
Expected Behavior
3 rows regardless of rewrite setting. The predicate l.a=r.c cannot be proven: l.a is in EC{l.a, l.b} with constant 1, r.c is in EC{r.c, r.d} with constant 2 — different equivalence classes, different constants.
Verification
With MV rewrite enabled, SELECT COUNT(*) from a LEFT JOIN with predicates l.a=1, l.b=1, r.c=2, r.d=2 returns 0 rows; with rewrite disabled it returns 3 rows. The rewrite incorrectly selects MV mv_c4_false_pos whose right-side columns are all NULL (because the MV ON predicate l.a=r.c is never satisfied when a=1, c=2), then the filter c=2, d=2 eliminates every row.
This bug was identified and reproduced by AI-assisted code review.
What type of issue is this:
- BugFix
Does this entail a change in behavior?
- Yes, this issue will result in a change in behavior.
Type of change:
- Miscellaneous: correctness bug fix
Checklist
- Fix needs a regression test
- Fix may need user documentation update
Test Environment
| Tested Version | main-34f64d7 (MySQL protocol 8.0.33) |
| Cluster | 1 FE, 1 BE, shared-nothing mode |
Source: StarRocks/starrocks