Skip to content

fix: preserve table integrity across ALTER and TRUNCATE - #26893

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
aptend:fix/alter-table-integrity-26853-26839-26838
Aug 10, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
aptend:fix/alter-table-integrity-26853-26839-26838

Conversation

@aptend

@aptend aptend commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #26853
issue #26839
issue #26838

What this PR does / why we need it:

  • Rechecks PRIMARY and UNIQUE keys during ALTER COPY whenever a conversion or generated expression can change key values.
  • Rewrites renamed column references in CHECK expressions and persists the updated structured metadata through CN/TN, including legacy tables whose CHECK definitions only exist in CREATE SQL.
  • Ignores the table ID 0 self-reference marker when TRUNCATE refreshes foreign-key parent tables.
  • Adds unit and BVT coverage for all three regressions.

@aptend
aptend requested a review from a user August 10, 2026 03:32
@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 →

@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Aug 10, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Aug 10, 2026

@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.

发现一个滚动升级下的阻塞性正确性问题:新增 AlterTableRenameCol.checks 字段没有对应的 MORPC 协议版本门禁,当前 MORPCLatestVersion 仍为 13。滚动升级期间新 CN 可能连接旧 TN;旧 TN 会成功解码并跳过未知字段,rename 仍会提交,但 TN 持久化的 SchemaExtra.Checks 保留旧列名,而 CN 保存新列名,导致重启/缓存刷新后元数据分叉,SHOW CREATE 和后续 ALTER 可能失败。

建议:为 CHECK-aware rename 分配新的协议版本(例如 v14),在包含 CHECK(包括 legacy CREATE SQL 恢复出的 CHECK)时,协议低于该版本直接 fail closed;补充 v13 reject/v14 accept 及旧 TN 解码器反例测试。

其余 ALTER COPY key 去重、CHECK AST 重写、TRUNCATE self-FK sentinel 修复未发现新的阻塞问题。常规 CI 通过,但 upgrade compatibility jobs 被跳过,未覆盖该混部场景。

@gouhongshen gouhongshen 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.

Codex automated review

Supported CHANGE COLUMN renames still bypass the new CHECK rewrite and fail during copy; the existing unresolved protocol-gating review was not duplicated.

P1 - CHANGE COLUMN renames do not rewrite CHECK metadata (pkg/sql/plan/build_alter_rename_column.go:116)

The recovery/rewrite is wired only into the RENAME COLUMN path. A different-name CHANGE COLUMN is forced to COPY and dispatched to ChangeColumn, whose updateNewColumnInTableDef path never rewrites checks. Thus CHECK(a < b) remains stale after CHANGE a TO x; ConstructCreateTableSQL emits the old reference for the temporary table, causing the ALTER to fail or leave inconsistent metadata. Add equivalent handling and a regression test for CHANGE COLUMN with CHECK.

@aptend

aptend commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both blocking reviews in dd4973f:

  • Added MORPC v14 and fail-closed planner plus sender gates for CHECK-aware RENAME COLUMN. v13 rejects, v14 accepts, no-CHECK renames remain compatible. The legacy decoder regression proves a v13 request type accepts the unknown field but cannot expose it to the TN alter handler.
  • Recovered and AST-rewrote CHECK metadata for renamed CHANGE COLUMN COPY operations, with unit and BVT coverage.

Decision log: CHANGE COLUMN does not need the v14 gate because it uses the existing COPY path and does not send AlterTableRenameCol.checks. The sender-side RENAME gate is intentionally retained in addition to the planner gate to cover synthesized or cached plans.

Validated focused tests, full pkg/defines, pkg/pb/api, pkg/sql/plan, pkg/sql/compile, pkg/vm/engine/tae/catalog, and pkg/vm/engine/disttae tests; go build/vet; make build; and a local SQL harness for the CHANGE COLUMN CHECK case.

@aptend

aptend commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the latest review finding and rebased onto current main (dcef98c269).

The panic came from CaseExpr.Accept traversing both optional operands unconditionally:

  • searched CASE WHEN ... has no input expression
  • CASE ... END without ELSE has no fallback expression

The visitor now skips absent optional operands while preserving traversal of all present children. Added direct AST coverage, planner coverage through ALTER TABLE ... RENAME COLUMN CHECK rewriting, and distributed SQL coverage for both forms.

Post-rebase validation passed:

  • focused + full pkg/sql/parsers/tree
  • focused + full pkg/sql/plan with the CGo wrapper
  • go build / go vet for both packages
  • make build

@gouhongshen gouhongshen 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.

Codex automated review

Prior blockers are fixed: protocol-v15 CHECK transport gating, CHANGE/COPY CHECK recovery, source-identity key deduplication, and nil-safe CASE traversal. The earlier COPY compatibility concern is non-blocking because gating is now INPLACE-only. A new independent blocking panic remains in generic CHECK rewriting.

P1 - Renaming a column panics on a planner-accepted MATCH CHECK (pkg/sql/plan/build_alter_rename_column.go:300)

This call traverses every persisted CheckDef.OriginSql, even when the renamed column is unrelated. The current DDL binder accepts CHECK (MATCH(body) AGAINST ('foo')): it handles FullTextMatchExpr, registers FULLTEXT_MATCH overloads, and validateCheckExpr does not reject it. Re-parsing the stored SQL yields FullTextMatchExpr, whose Accept implementation unconditionally panics at pkg/sql/parsers/tree/expr.go:1958-1960. Thus ALTER TABLE t RENAME COLUMN body TO body2 can panic in the planner. This is distinct from the resolved CASE finding: CASE's optional operands are now nil-safe, while FullTextMatchExpr.Accept itself still panics. Implement non-panicking traversal for accepted CHECK forms, or reject MATCH-containing CHECKs before persistence with a controlled error.

@aptend

aptend commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the latest AST traversal review on rebased head 86af0dea8e (base 5a753bacb4).

I expanded the audit from the reported node to the complete persisted-CHECK visitor boundary:

  • Implemented FullTextMatchExpr.Accept, including KeyPart.ColName, expression key parts, and the pattern.
  • Implemented leaf traversal for ParamExpr, because MATCH ... AGAINST (?) is a valid prepared FullText AST shape even though parameter markers cannot be persisted in CHECK constraints.
  • Found and fixed a separate reachable omission not mentioned in the review: ComparisonExpr.Accept skipped the ESCAPE expression. A real service reproduced CHECK (s LIKE ... ESCAPE escape_char) retaining the stale old name after rename; it now persists marker correctly.
  • Kept the earlier nil-safe CASE handling.

Reachability note: the review claim that current DDL persists CHECK (MATCH(body) AGAINST (...)) does not reproduce on this head. Both pre-fix and post-fix real-service CREATE fail before persistence with invalid input: ambiguous column reference body. The planner regression still covers legacy or synthesized persisted MATCH metadata, where the reported panic was real.

The remaining unimplemented AST visitors are outside persisted CHECK input: generated-column binding rejects subqueries; validation rejects variables and parameters; binding rejects Interval, ExprList, and StrVal; DEFAULT/UPDATE/MAX/SAMPLE/SET forms are not produced by CHECK grammar. This is the decision boundary for this PR.

Validation after the final rebase/edit: focused and full tree/plan packages, build/vet for both, make build, planner MATCH/ESCAPE counterexamples, and a real-service ESCAPE rename with SHOW CREATE. All passed.

@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 86af0de against base 5a753ba.

All previously blocking paths are closed:

  • ALTER COPY now retains target PK/UNIQUE dedup whenever type conversion, generated-column recomputation, or source-column replacement can change key values; same-name DROP/ADD is covered by source-identity proofs on both planner and compiler paths.
  • RENAME/CHANGE rewrites persisted CHECK SQL, legacy recovery is fail-closed on SQL-mode ambiguity, and INPLACE CHECK transport is gated at MORPC v15 at both planning and sending boundaries. CN rollback and TN schema persistence carry cloned CHECK metadata.
  • The CASE optional operands are nil-safe. The latest traversal change also closes the reachable LIKE ESCAPE child omission. FullTextMatch/Param traversal is bounded and does not regress the existing RETURNING qualifier visitor.
  • TRUNCATE correctly excludes the table-ID-zero self-FK sentinel while retaining real parent refreshes.

I independently checked the prior MATCH report: current CREATE CHECK binding rejects MATCH before persistence with ambiguous column reference, so it was not a current legal catalog path. The new visitor support still makes legacy or synthesized metadata non-panicking.

Non-blocking follow-up: alterCopyKeyColumnValueUnchanged and its compiler counterpart do not compare plan.Type.Charset, unlike the canonical text-type equality contract. Current PK/UNIQUE dedup remains raw-byte based, so this is not a present integrity blocker, but Charset should be included before key equality becomes collation-aware.

Validation on the exact head: full tests and go vet passed for pkg/defines, pkg/pb/api, pkg/sql/parsers/tree, pkg/sql/plan, pkg/sql/compile, pkg/vm/engine/disttae, and pkg/vm/engine/tae/catalog. The branch is based directly on current main and is mergeable. CI was still in progress at review time with no reported failure; this approval does not depend on waiting for it.

@iamlinjunhong iamlinjunhong 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 86af0de against base 5a753ba.\n\nNo P0-P3 findings remain. I verified the ALTER COPY key-value/source-identity dedup gates, CHECK recovery/rewrite and v15 CN/TN transport, the complete persisted-CHECK AST traversal boundary (including CASE and LIKE ESCAPE), rollback ownership, and the TRUNCATE self-FK sentinel path. The final rebase is patch-equivalent for the prior six commits, the AST follow-up closes the reachable traversal omission, and all 26 checks are terminal with no failures.

@gouhongshen gouhongshen 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.

Codex automated review

No remaining findings. Prior blockers are resolved: v15 fail-closed CHECK-rename gating, legacy CHECK recovery/rewrite, source-identity key deduplication, nil-safe CASE/MATCH/parameter/ESCAPE traversal, and self-FK TRUNCATE handling. The COPY gate concern was correctly narrowed to INPLACE and is non-blocking. The remaining Charset comparison note is accepted as non-blocking under current raw-byte PK/UNIQUE encoding.

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

mergify Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-10 14:46 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-10 15:22 UTC · at 25399a5ba4b7c1448dfeac0782cc2f85092bed2e

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

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

Reason

Pull request #26893 has been dequeued

Pull request from fork cannot be queued. This pull request comes 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.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, 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

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/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants