Skip to content

fix: preserve CTAS nullability after query optimization - #26901

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-26828-ctas-nullability
Aug 11, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-26828-ctas-nullability

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26828

What this PR does / why we need it:

CTAS previously derived target-column nullability from the pre-optimization SELECT projection. That misses NULLs synthesized by outer joins and unmatched correlated scalar subqueries, so CTAS can create an incorrect NOT NULL target schema and reject valid result rows.

The planner now derives CTAS column types and nullability from the final optimized query output. It snapshots output provenance before optimization so existing source-default, view-default, and MySQL ENUM/SET metadata handling remains unchanged.

Covered behavior

  • CTAS nullability follows optimized output for LEFT, RIGHT, and FULL joins and unmatched correlated scalar subqueries.
  • INNER JOIN and COALESCE controls remain non-null when their final output cannot synthesize NULL.
  • Direct, derived-table, and view CTAS paths have canonical SQL coverage for rows and catalog nullability.
  • An explicitly NOT NULL CTAS target still rejects synthesized NULL and leaves no residual table.

Non-goals

  • No change to join execution or result values.
  • No change to normal CTAS defaults, ENUM/SET type preservation, explicit target-column semantics, or CTAS transaction/cleanup behavior.

QA decision

  • QA required: yes.
  • Reason: this fixes user-visible CTAS schema metadata and execution behavior.
  • Production entrypoint: pkg/sql/plan/build_ddl.go genAsSelectCols, consumed by buildCreateTable and the CTAS follow-up INSERT path.
  • Canonical BVT: test/distributed/cases/ddl/create_table_as_select.sql and .result.
  • Base FAIL: before this change, the new planner regression case observed NullAbility=false for NULL-extended LEFT/RIGHT/FULL join and scalar-subquery outputs.
  • PR-head PASS: focused CTAS planner tests and the complete ./pkg/sql/plan CGo test package pass.
  • BVT status: pending. The canonical BVT pair is added, but no local mo-tester runner was available; this PR does not claim a local BVT pass.

Validation

  • GOWORK=off go build -mod=readonly ./pkg/sql/plan — PASS
  • GOWORK=off go vet -mod=readonly ./pkg/sql/plan — PASS
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s -run '^(TestCreateTableAsSelectPropagatesNullExtension|TestCreateTableAsSelectKeepsNonTemporalLiteralNotNull|TestBuildCTASFromViewUsesIndependentExecutableDefault|TestBuildCTASPreservesMySQLSpecialColumnTypes|TestBuildCTASConsumesOutputColumnProvenance|TestPrepareCreateTableAsSelectWithParams)$' ./pkg/sql/plan — PASS
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=240s ./pkg/sql/plan — PASS
  • git diff --check upstream/main...HEAD — PASS

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 36a66c6.

CTAS now derives nullable metadata from the optimized final projection while retaining pre-optimization provenance only for enum and set attributes. That closes optimizer-introduced null extension for outer joins and scalar subqueries without discarding source type metadata. The returned query is the same optimized query used for metadata, and prepared CTAS plus direct, derived, view, aggregate, join, and explicit NOT NULL cases are covered.

I found no correctness, compatibility, unhappy-path, or material performance blocker. The full plan package tests pass locally, and diff validation is clean.

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 36a66c6e7622618f10594787059fea168afe5c28; all checks are terminal and successful.

CTAS metadata now comes from the final optimized output, closing NULL extension introduced by outer joins and unmatched scalar subqueries. Pre-optimization provenance is retained only for source defaults and MySQL special types, with optimized nullability reapplied, and the optimized query is also the executable prepared CTAS query. Direct, derived, view, join, scalar-subquery, aggregate, prepared, and explicit NOT NULL controls cover the ownership boundary. I found no blocker.

@mergify mergify Bot added the queued label Aug 11, 2026
@mergify

mergify Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-11 13:24 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-11 14:47 UTC · at a60b911a246030b839f8a49325f88021d4836a6c

This pull request spent 1 hour 23 minutes 12 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@mergify
mergify Bot requested review from LeftHandCold and aptend as code owners August 11, 2026 14:46
@XuPeng-SH
XuPeng-SH merged commit a6f8b91 into matrixorigin:main Aug 11, 2026
24 of 26 checks passed
@mergify mergify Bot added dequeued and removed queued labels Aug 11, 2026
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26901)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26828

## What this PR does / why we need it:

CTAS previously derived target-column nullability from the
pre-optimization SELECT projection. That misses NULLs synthesized by
outer joins and unmatched correlated scalar subqueries, so CTAS can
create an incorrect `NOT NULL` target schema and reject valid result
rows.

The planner now derives CTAS column types and nullability from the final
optimized query output. It snapshots output provenance before
optimization so existing source-default, view-default, and MySQL
ENUM/SET metadata handling remains unchanged.

### Covered behavior

- CTAS nullability follows optimized output for LEFT, RIGHT, and FULL
joins and unmatched correlated scalar subqueries.
- INNER JOIN and `COALESCE` controls remain non-null when their final
output cannot synthesize NULL.
- Direct, derived-table, and view CTAS paths have canonical SQL coverage
for rows and catalog nullability.
- An explicitly `NOT NULL` CTAS target still rejects synthesized NULL
and leaves no residual table.

### Non-goals

- No change to join execution or result values.
- No change to normal CTAS defaults, ENUM/SET type preservation,
explicit target-column semantics, or CTAS transaction/cleanup behavior.

### QA decision

- QA required: yes.
- Reason: this fixes user-visible CTAS schema metadata and execution
behavior.
- Production entrypoint: `pkg/sql/plan/build_ddl.go` `genAsSelectCols`,
consumed by `buildCreateTable` and the CTAS follow-up INSERT path.
- Canonical BVT: `test/distributed/cases/ddl/create_table_as_select.sql`
and `.result`.
- Base FAIL: before this change, the new planner regression case
observed `NullAbility=false` for NULL-extended LEFT/RIGHT/FULL join and
scalar-subquery outputs.
- PR-head PASS: focused CTAS planner tests and the complete
`./pkg/sql/plan` CGo test package pass.
- BVT status: pending. The canonical BVT pair is added, but no local
`mo-tester` runner was available; this PR does not claim a local BVT
pass.

### Validation

- `GOWORK=off go build -mod=readonly ./pkg/sql/plan` — PASS
- `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s -run
'^(TestCreateTableAsSelectPropagatesNullExtension|TestCreateTableAsSelectKeepsNonTemporalLiteralNotNull|TestBuildCTASFromViewUsesIndependentExecutableDefault|TestBuildCTASPreservesMySQLSpecialColumnTypes|TestBuildCTASConsumesOutputColumnProvenance|TestPrepareCreateTableAsSelectWithParams)$'
./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=240s
./pkg/sql/plan` — PASS
- `git diff --check upstream/main...HEAD` — PASS

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26901)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26828

## What this PR does / why we need it:

CTAS previously derived target-column nullability from the
pre-optimization SELECT projection. That misses NULLs synthesized by
outer joins and unmatched correlated scalar subqueries, so CTAS can
create an incorrect `NOT NULL` target schema and reject valid result
rows.

The planner now derives CTAS column types and nullability from the final
optimized query output. It snapshots output provenance before
optimization so existing source-default, view-default, and MySQL
ENUM/SET metadata handling remains unchanged.

### Covered behavior

- CTAS nullability follows optimized output for LEFT, RIGHT, and FULL
joins and unmatched correlated scalar subqueries.
- INNER JOIN and `COALESCE` controls remain non-null when their final
output cannot synthesize NULL.
- Direct, derived-table, and view CTAS paths have canonical SQL coverage
for rows and catalog nullability.
- An explicitly `NOT NULL` CTAS target still rejects synthesized NULL
and leaves no residual table.

### Non-goals

- No change to join execution or result values.
- No change to normal CTAS defaults, ENUM/SET type preservation,
explicit target-column semantics, or CTAS transaction/cleanup behavior.

### QA decision

- QA required: yes.
- Reason: this fixes user-visible CTAS schema metadata and execution
behavior.
- Production entrypoint: `pkg/sql/plan/build_ddl.go` `genAsSelectCols`,
consumed by `buildCreateTable` and the CTAS follow-up INSERT path.
- Canonical BVT: `test/distributed/cases/ddl/create_table_as_select.sql`
and `.result`.
- Base FAIL: before this change, the new planner regression case
observed `NullAbility=false` for NULL-extended LEFT/RIGHT/FULL join and
scalar-subquery outputs.
- PR-head PASS: focused CTAS planner tests and the complete
`./pkg/sql/plan` CGo test package pass.
- BVT status: pending. The canonical BVT pair is added, but no local
`mo-tester` runner was available; this PR does not claim a local BVT
pass.

### Validation

- `GOWORK=off go build -mod=readonly ./pkg/sql/plan` — PASS
- `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s -run
'^(TestCreateTableAsSelectPropagatesNullExtension|TestCreateTableAsSelectKeepsNonTemporalLiteralNotNull|TestBuildCTASFromViewUsesIndependentExecutableDefault|TestBuildCTASPreservesMySQLSpecialColumnTypes|TestBuildCTASConsumesOutputColumnProvenance|TestPrepareCreateTableAsSelectWithParams)$'
./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=240s
./pkg/sql/plan` — PASS
- `git diff --check upstream/main...HEAD` — PASS

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…#26901)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26828

## What this PR does / why we need it:

CTAS previously derived target-column nullability from the
pre-optimization SELECT projection. That misses NULLs synthesized by
outer joins and unmatched correlated scalar subqueries, so CTAS can
create an incorrect `NOT NULL` target schema and reject valid result
rows.

The planner now derives CTAS column types and nullability from the final
optimized query output. It snapshots output provenance before
optimization so existing source-default, view-default, and MySQL
ENUM/SET metadata handling remains unchanged.

### Covered behavior

- CTAS nullability follows optimized output for LEFT, RIGHT, and FULL
joins and unmatched correlated scalar subqueries.
- INNER JOIN and `COALESCE` controls remain non-null when their final
output cannot synthesize NULL.
- Direct, derived-table, and view CTAS paths have canonical SQL coverage
for rows and catalog nullability.
- An explicitly `NOT NULL` CTAS target still rejects synthesized NULL
and leaves no residual table.

### Non-goals

- No change to join execution or result values.
- No change to normal CTAS defaults, ENUM/SET type preservation,
explicit target-column semantics, or CTAS transaction/cleanup behavior.

### QA decision

- QA required: yes.
- Reason: this fixes user-visible CTAS schema metadata and execution
behavior.
- Production entrypoint: `pkg/sql/plan/build_ddl.go` `genAsSelectCols`,
consumed by `buildCreateTable` and the CTAS follow-up INSERT path.
- Canonical BVT: `test/distributed/cases/ddl/create_table_as_select.sql`
and `.result`.
- Base FAIL: before this change, the new planner regression case
observed `NullAbility=false` for NULL-extended LEFT/RIGHT/FULL join and
scalar-subquery outputs.
- PR-head PASS: focused CTAS planner tests and the complete
`./pkg/sql/plan` CGo test package pass.
- BVT status: pending. The canonical BVT pair is added, but no local
`mo-tester` runner was available; this PR does not claim a local BVT
pass.

### Validation

- `GOWORK=off go build -mod=readonly ./pkg/sql/plan` — PASS
- `GOWORK=off go vet -mod=readonly ./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s -run
'^(TestCreateTableAsSelectPropagatesNullExtension|TestCreateTableAsSelectKeepsNonTemporalLiteralNotNull|TestBuildCTASFromViewUsesIndependentExecutableDefault|TestBuildCTASPreservesMySQLSpecialColumnTypes|TestBuildCTASConsumesOutputColumnProvenance|TestPrepareCreateTableAsSelectWithParams)$'
./pkg/sql/plan` — PASS
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=240s
./pkg/sql/plan` — PASS
- `git diff --check upstream/main...HEAD` — PASS

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@VioletQwQ-0
VioletQwQ-0 deleted the codex/issue-26828-ctas-nullability branch December 1, 2026 03:47

This branch was previously deployed

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

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants