Repository navigation
fix: preserve CTAS nullability after query optimization - #26901
Conversation
6795f8b to
25fc7db
Compare
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 1 hour 23 minutes 12 seconds in the queue, with no time running CI. ReasonThe pull request can't be updated
HintYou should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again. Tick the box to put this pull request back in the merge queue (same as
|
…#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>
…#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>
…#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>
What type of PR is this?
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 NULLtarget 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
COALESCEcontrols remain non-null when their final output cannot synthesize NULL.NOT NULLCTAS target still rejects synthesized NULL and leaves no residual table.Non-goals
QA decision
pkg/sql/plan/build_ddl.gogenAsSelectCols, consumed bybuildCreateTableand the CTAS follow-up INSERT path.test/distributed/cases/ddl/create_table_as_select.sqland.result.NullAbility=falsefor NULL-extended LEFT/RIGHT/FULL join and scalar-subquery outputs../pkg/sql/planCGo test package pass.mo-testerrunner was available; this PR does not claim a local BVT pass.Validation
GOWORK=off go build -mod=readonly ./pkg/sql/plan— PASSGOWORK=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— PASSgit diff --check upstream/main...HEAD— PASS