Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#70936·starrocks

[BugFix] View-delta lossless-join check incorrectly treats nullable FK columns as non-null

Author: andyziyeCreated Mar 30, 2026Updated Sep 28, 2026
Labelsno-issue-activitysource:ai-detected

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 1064)

Root cause: The view-delta lossless-join check at line 1064 skips the NOT NULL verification when hasForeignKeyConstraintInMv() returns true. The mere existence of an FK declaration is used as proof that FK columns are non-null, but FK columns can still be declared NULL in the schema.

Concrete failure scenario: A query on t_emp (which has nullable dept_id) is rewritten using an MV built on an INNER JOIN between t_emp and t_dept. Rows with dept_id=NULL are silently dropped because the rewriter treats the inner join as lossless despite the nullable FK.


Steps to Reproduce

sql
CREATE TABLE t_dept (
  dept_id INT NOT NULL,
  dept_name VARCHAR(50)
) DUPLICATE KEY(dept_id) DISTRIBUTED BY HASH(dept_id) BUCKETS 4
  PROPERTIES ('replication_num' = '1');

CREATE TABLE t_emp (
  emp_id INT NOT NULL,
  dept_id INT NULL,
  emp_name VARCHAR(50)
) DUPLICATE KEY(emp_id) DISTRIBUTED BY HASH(emp_id) BUCKETS 4
  PROPERTIES ('replication_num' = '1');

INSERT INTO t_dept VALUES (1,'Engineering'),(2,'Marketing'),(3,'Sales');
INSERT INTO t_emp VALUES (1,1,'Alice'),(2,2,'Bob'),(3,NULL,'Charlie'),(4,3,'Diana'),(5,NULL,'Eve');

CREATE MATERIALIZED VIEW mv_emp_dept
  DISTRIBUTED BY HASH(emp_id) BUCKETS 4 REFRESH ASYNC AS
  SELECT e.emp_id, e.dept_id, e.emp_name, d.dept_name
  FROM t_emp e INNER JOIN t_dept d ON e.dept_id = d.dept_id;

ALTER MATERIALIZED VIEW mv_emp_dept SET (
  'foreign_key_constraints' = 'default_catalog.test_db.t_emp(dept_id) REFERENCES default_catalog.test_db.t_dept(dept_id)'
);

REFRESH MATERIALIZED VIEW mv_emp_dept WITH SYNC MODE;

SET enable_materialized_view_rewrite = true;

SELECT emp_id, dept_id, emp_name FROM t_emp ORDER BY emp_id;

Actual Behavior

3 rows: Alice, Bob, Diana. Charlie (emp_id=3, dept_id=NULL) and Eve (emp_id=5, dept_id=NULL) are silently missing. COUNT(*)=3.

Expected Behavior

5 rows including Charlie and Eve. COUNT(*)=5.


Verification

SELECT emp_id, dept_id, emp_name FROM t_emp returns 5 rows without MV rewrite but only 3 rows with MV rewrite enabled. Charlie (emp_id=3, dept_id=NULL) and Eve (emp_id=5, dept_id=NULL) are silently dropped because the view-delta lossless-join check at MaterializedViewRewriter.java:1064 skips the NOT NULL verification when hasForeignKeyConstraintInMv() returns true, allowing the optimizer to incorrectly treat the inner join as lossless despite the nullable FK column.

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
Cluster 1 FE, 3 BE, shared-nothing mode

Source: StarRocks/starrocks

View original on GitHubView discussion on GitHub