Skip to content

[Bug]: CTAS ignores NULLs synthesized by outer joins and scalar subqueries #26828

Closed

Description

@Ariznawlll

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 COALESCE removes 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

Activity

  1. added this to the MO-202608 milestone on Aug 9, 2026
  2. added
    kind/bug
    on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. changed the title [-][Bug]: CTAS marks outer-join null-extended columns NOT NULL and fails[/-] [+][Bug]: CTAS ignores NULLs synthesized by outer joins and scalar subqueries[/+] on Aug 9, 2026
  5. added
    severity/s0
    and removed on Aug 9, 2026
  6. VioletQwQ-0 commented on Aug 11, 2026

    @VioletQwQ-0
    Collaborator

    Post-merge testing handoff for #26828

    PR and merge:

    QA Decision

    • Required: yes.
    • Reason: this changes user-visible CTAS nullability metadata and acceptance for NULLs synthesized by outer joins and unmatched scalar subqueries.
    • Production entrypoint: SQL CTAS through pkg/sql/plan/build_ddl.go genAsSelectCols, buildCreateTable, and the CTAS follow-up INSERT path.
    • Automated evidence: focused CTAS planner tests, the complete pkg/sql/plan CGo suite, build, and vet passed as recorded by the PR. The current exact-head pull_request_target run 31503375035 had an unrelated Compose PROXY BVT failure in ddl/comprimary_key.sql (expected duplicate-entry output versus actual query interrupted) and a derived coverage-prerequisite failure; these are not tester acceptance.

    Remaining validation

    • Run test/distributed/cases/ddl/create_table_as_select.sql on the current main head and verify LEFT, RIGHT, and FULL join plus unmatched correlated scalar-subquery CTAS outputs are nullable and execute successfully.
    • Verify direct, derived-table, and view CTAS metadata, INNER JOIN and COALESCE non-null controls, prepared CTAS, and explicit NOT NULL rejection with no residual target.

    Expected terminal

    • PASS requires the canonical BVT and all listed deployed SQL scenarios to pass without incorrect NOT NULL metadata or a spurious CTAS failure.
    • FAIL must include the tested MatrixOne version or main head, environment/topology, reproduction SQL, and terminal result; keep this issue open for follow-up.

    Tester selection

    • Tester: Ariznawlll, selected because the current issue author is Ariznawlll.

    The issue remains open with phase/testing until complete tester evidence is received.

  7. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:ca845354162ca2b01feb391d7b5b6e029c609420
    • 部署方式:本地单机 mo-service,1 CN / 1 TN / 1 Log service,MySQL text protocol
    • 测试日期:2026-08-12

    原问题验证

    在最新 main 上执行 LEFT/RIGHT/FULL OUTER JOIN 和 unmatched scalar subquery CTAS。直接、derived-table、view 和 prepared CTAS 均成功,NULL 扩展侧的列被标为 nullable 并保存精确行;连续 3 轮一致。

    Happy Path 覆盖

    1. LEFT JOIN 保存 (2,20,NULL,NULL),RIGHT/FULL JOIN 保存对应无匹配行。
    2. direct 和 view-based scalar subquery 对无匹配外层行保存 NULL。
    3. prepared CTAS 成功保存 2 行,其中 1 行 r_id 为 NULL;INNER JOIN 和 COALESCE 控制保持非 NULL。
    4. 对 8 类目标表逐列核对 information_schema.columns.is_nullable。

    Unhappy Path 覆盖

    1. 将 outer-join NULL 扩展结果写入显式 NOT NULL CTAS 返回 3819。
    2. 拒绝后 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 和拒绝后清理断言。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions