Repository navigation
Description
Description
CTAS ignores NULLs synthesized by LEFT/RIGHT/FULL OUTER JOIN and by unmatched scalar subqueries, retaining the underlying source columns' NOT NULL type. Valid rows containing synthesized NULL are rejected with error 3819, so the target table is not created.
Environment
- Branch:
main - Commit:
45a917d7f8f7b2f32c28ca0a734318875722a00e - Deployment: local standalone
mo-service, 1 CN / 1 TN / 1 Log service - Test date: 2026-08-09
Steps to reproduce
CREATE TABLE l (id INT PRIMARY KEY, lv INT NOT NULL);
CREATE TABLE r (id INT PRIMARY KEY, rv INT NOT NULL);
INSERT INTO l VALUES (1,10),(2,20);
INSERT INTO r VALUES (1,100),(3,300);
SELECT l.id AS l_id,l.lv,r.id AS r_id,r.rv
FROM l LEFT JOIN r ON l.id=r.id;
CREATE TABLE dst AS
SELECT l.id AS l_id,l.lv,r.id AS r_id,r.rv
FROM l LEFT JOIN r ON l.id=r.id;Actual behavior
The SELECT correctly returns (2,20,NULL,NULL) for the unmatched left row. CTAS returns:
ERROR 3819 (HY000): constraint violation: Column 'r_id' cannot be null
dst is not created.
Expected behavior
CTAS must create dst, mark r_id and rv nullable, and store both joined rows.
The same applies to a non-null inner column projected by a scalar correlated subquery: an unmatched outer row must make the CTAS result column nullable.
Stability and controls
- Direct LEFT JOIN CTAS: 3/3 failed and created no target.
- The same LEFT JOIN through a derived table and through a view: each 3/3 failed.
- RIGHT JOIN and FULL OUTER JOIN view CTAS: each 3/3 failed on the null-extended side.
- Direct and view-based scalar correlated subquery CTAS: each 3/3 failed when one outer row had no match.
- LEFT/RIGHT/FULL view catalog metadata: 3/3 correctly marked only the null-extended columns nullable, and view rows were correct.
- Scalar-subquery view metadata: 3/3 correctly marked the result nullable and returned
(100,NULL). - INSERT of the LEFT JOIN into an explicitly nullable target: 3/3 stored both rows including one NULL-extended row.
- CTAS after
COALESCEremoves the synthetic NULLs: 3/3 succeeded with two rows. - No panic, restart, or partial target was observed.
Code analysis
genAsSelectCols in pkg/sql/plan/build_ddl.go calls builder.bindSelect and derives CTAS nullability immediately through ctasExprCanBeNull(expr), which trusts expr.Typ.NotNullable. At that point the projected expression still carries the underlying non-null column type and does not include NULL synthesis from outer joins or no-match scalar subqueries. In contrast, view schema generation uses the optimized query path and exposes correct nullability for both. CTAS needs the same operator-aware output nullability before creating its target schema.
Regression coverage
Add BVT coverage for direct, derived, and view-based LEFT/RIGHT/FULL JOIN CTAS plus matched/unmatched scalar-subquery CTAS. Assert target nullability, exact rows, COALESCE controls, and absence of a residual target on rejected NOT NULL controls.
Related
- [Bug]: tracker for remaining CTAS schema inconsistencies with MySQL #24436 tracks general CTAS schema compatibility, but not outer-join NULL expansion or this execution failure.
- No matching open/closed issue or fix PR was found for CTAS operator-synthesized nullability.
Activity
[-][Bug]: CTAS marks outer-join null-extended columns NOT NULL and fails[/-][+][Bug]: CTAS ignores NULLs synthesized by outer joins and scalar subqueries[/+]fix: preserve CTAS nullability after query optimization (#26901)
VioletQwQ-0 commentedon Aug 11, 2026
Post-merge testing handoff for #26828
PR and merge:
QA Decision
Remaining validation
Expected terminal
Tester selection
The issue remains open with phase/testing until complete tester evidence is received.
Ariznawlll commentedon Aug 12, 2026
测试版本
mainca845354162ca2b01feb391d7b5b6e029c609420mo-service,1 CN / 1 TN / 1 Log service,MySQL text protocol原问题验证
在最新 main 上执行 LEFT/RIGHT/FULL OUTER JOIN 和 unmatched scalar subquery CTAS。直接、derived-table、view 和 prepared CTAS 均成功,NULL 扩展侧的列被标为 nullable 并保存精确行;连续 3 轮一致。
Happy Path 覆盖
(2,20,NULL,NULL),RIGHT/FULL JOIN 保存对应无匹配行。r_id为 NULL;INNER JOIN 和 COALESCE 控制保持非 NULL。information_schema.columns.is_nullable。Unhappy Path 覆盖
NOT NULLCTAS 返回 3819。information_schema.tables中目标表数量为 0,LEFT 源表仍为 2 行,连接可继续执行。白盒验证
未做故障注入;公开 SQL 已覆盖 direct/derived/view/prepared CTAS 入口。本轮以 catalog 元数据、精确行和失败原子性为准。
回归测试
已加入 motr PR #106 的 CTAS nullability 用例,连续 3 轮稳定通过。
测试证据
motr PR #106;result 包含 LEFT/RIGHT/FULL、scalar、prepared、INNER/COALESCE 和拒绝后清理断言。
测试结论
测试通过。
fix: preserve CTAS nullability after query optimization (matrixorigin…
fix: preserve CTAS nullability after query optimization (matrixorigin…
fix: preserve CTAS nullability after query optimization (matrixorigin…