Repository navigation
fix: drop CHECKs referencing only the dropped column in ALTER TABLE - #31010
Conversation
### Root cause `ALTER TABLE ... DROP COLUMN` failed with 1054 when a CHECK constraint referenced only the dropped column: the COPY-alter planner kept the constraint metadata, and the rebuilt table bound the dead column reference. MySQL implicitly removes such single-column CHECKs; MatrixOne could neither do that nor drop the column. CHECKs referencing surviving columns were collapsed into the same unbound path instead of rejecting with MySQL 3959. Two latent defects sat on the alter-copy expression replay and were proven by counterexamples on this branch: - a same-statement replacement (DROP COLUMN v auto-drops ck(v>0); the same ALTER re-adds v and ck(w>0)) could inherit the old persisted expression because positional expressions compare equal, so COPY accepted rows violating the authored constraint; - a retained CHECK rewritten by RENAME/CHANGE was rebound from the temporary CREATE text under the ALTER session's div_precision_increment, silently changing its division scale and rejecting previously valid rows during COPY. ### Exact change - pkg/sql/plan/build_alter_check.go: classify every CHECK on the source table by column dependency; drop the ones referencing only the dropped column(s) together with the column, reject a surviving CHECK that still references the column with MySQL 3959, and release auto-dropped names for same-statement ADD admission. - pkg/common/moerr/error.go: ErrDependentByCheckConstraint (20483) mapped to MySQL 3959 with MySQL's message text. - pkg/sql/plan/build_alter_add_column.go: DropColumn invokes the CHECK classification (mirrors MySQL's prepare_check_constraints_for_alter). - pkg/sql/plan/ddl_expression_replay.go: the replay distinguishes a retained CHECK from a same-name replacement by column identity - a same-name, expression-equal CHECK replays its preserved bound expression only when every referenced original column still resolves by ColId in the COPY target (RENAME/CHANGE preserve ColIds; a replacement's original columns were dropped). Retained CHECKs keep creation-time bound semantics; replacements bind their own authored expression. - pkg/sql/plan/build_alter_check_test.go: dependency classification with plan-level atomicity, replacement binding through the real execution replay wrapper, and rename division-scale preservation. - test/distributed/cases/ddl/alter_table_check_constraints.sql/.result: issue reproduction, unrelated-column control, 3959 rejection with failure atomicity, re-add/replace enforcement pairs, and rename scale preservation under differing div_precision_increment. ### Proof - RED on assigned base 5fe68a8: the issue repro returns 1054; each later layer red-probed on its reviewed predecessor before fixing; the rename precision probe is GREEN on unmodified base (layers 2/3 exist only inside this branch's earlier revisions). - GREEN: focused planner batch 596 PASS / 0 FAIL; full pkg/sql/plan ok 115.817s; golangci-lint --new-from-rev, gofmt and go vet clean. - Isolated-cluster runtime over the MySQL protocol (binary built from this branch at base 5fe68a8): issue repro succeeds with the row retained; atomic 3819 rejection with full intactness; land/reject insert pairs discriminating replacement and rename-precision semantics. - BVT via mo-tester on the extended case: two consecutive 316/316 runs. Related to matrixorigin#30956.
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? |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 327961a72a1f5d522df846aa67adb3c4beeb5657
Assessment: approve
Reviewed the complete seven-file delta and affected callers. The design keeps CHECK dependency handling in the COPY planner and bound-expression preservation in the existing replay owner. It correctly distinguishes automatically dropped constraints, replacements and retained constraints without changing catalog formats. Boundary, failure-atomicity, compatibility and test-quality review found no actionable defects. Added work is confined to DDL planning and replay.
Model: gpt-6-astra / xhigh
Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1
Validation:
-
Eight focused top-level planner tests passed, including all new regressions and existing replay controls: 131 PASS records, zero failures, count=1, using the supplied test-exec. Receipt: checks/1792806948255722329-3.json; retained output: focused-tests.log.
-
Isolation preflight passed. All 192 tracked native inputs matched the reused build, and the current repository provenance checker accepted it. Receipts: checks/1792806714668975604-3.json, checks/1792806759720362953-3.json and checks/1792806759944225405-30.json.
-
Reused the supplied exact-head successful build, unit-test, static-analysis and BVT results. Inspected workflow selection and confirmed the changed SQL case belongs to the included DDL group.
-
Final audit verified the exact HEAD, merge base, unchanged source, commands, exit receipts and retained logs. Audit receipt: checks/1792807053825010698-3.json. Evidence and assessment are retained under /home/xupeng/.local/state/mo-bot/jobs/b9e8772c5aea1d788e8a9d2b/attempt-1 in review-evidence-audit.json and review-record.json.
Limitations:
-
SQL BVT was not repeated locally. Broader coverage relies on the supplied frozen-head CI snapshot; per-case CI logs and author-reported execution artifacts were unavailable. Author-reported counts were not treated as independently verified executions.
-
No base/head performance benchmark was run. Source tracing found bounded DDL-only overhead and no added per-row SELECT/DML work, I/O, locks or background state.
-
The linked issue's independent body was unavailable; its behavioral contract was assessed from the supplied PR description.
Merge Queue Status
This pull request spent 55 minutes 52 seconds in the queue, including 55 minutes 36 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
Related to #30956
What this PR does / why we need it:
ALTER TABLE ... DROP COLUMNfailed with error 1054 whenever a CHECK constraint referenced only the dropped column: the COPY-alter planner kept the constraint metadata, and the rebuilt table definition tried to bind the dead column reference. MySQL removes such single-column CHECKs implicitly; MatrixOne could neither do that nor drop the column, so a trivial, legalDROP COLUMNwas blocked. CHECKs referencing other columns were collapsed into the same unbound path instead of being rejected with MySQL error 3959.Root causes (three layers, each red-probed on its reviewed predecessor)
invalid input: column 'v' does not exist or cannot be referenced by a generated columnfor any CHECK that referenced only the dropped column.DROP COLUMN vauto-dropsck(v>0), the same ALTER re-addsvandck(w>0)) could inherit the old persisted expression because positional expressions compare equal; the constraint displayedCHECK (w > 0)but enforcedv > 0, and COPY accepted rows violating the authored constraint.RENAME/CHANGE COLUMNas new declarations, so they were rebound from the temporary CREATE text under the ALTER session'sdiv_precision_increment: a division bound at increment 10 (decimal(22,12)) came back asdecimal(16,6)at the default (scale 2 at increment 0), so previously valid rows failed COPY validation and empty-table alters persisted changed constraint semantics.Exact changes
pkg/sql/plan/build_alter_check.go: classify every CHECK on the source table by column dependency: a CHECK whose expression references only the dropped column(s) is dropped together with the column; a CHECK still referencing surviving columns rejects with MySQL 3959 (Check constraint '%s' uses column '%s', hence column cannot be dropped or renamed.); auto-dropped CHECK names are released so a same-statementADD CONSTRAINTof the same name is valid.pkg/common/moerr/error.go: newErrDependentByCheckConstraint(20483) mapped to MySQL 3959, with its constructor; message text byte-identical to MySQL's.pkg/sql/plan/build_alter_add_column.go:DropColumninvokes the CHECK classification before positional handling (mirrors MySQL'sprepare_check_constraints_for_alter).pkg/sql/plan/ddl_expression_replay.go: the persisted-expression replay distinguishes a retained CHECK from a same-name replacement by column identity: a same-name, expression-equal CHECK replays its preserved bound expression only when every referenced original column still resolves byColIdin the COPY target (RENAME/CHANGE preserve ColIds; a replacement's original columns were dropped, so its IDs cannot resolve). Retained CHECKs keep their creation-time bound semantics (division scale preserved through RENAME/CHANGE regardless of the session'sdiv_precision_increment); replacements bind their own authored expression.pkg/sql/plan/build_alter_check_test.go): dependency classification incl. plan-level atomicity (sourceTableDefuntouched), the replacement binding proof through the real execution replay wrapper (WithAlterTableDDLReplay+ replan ofCreateTmpTableSql), and rename precision preservation (decimal(22,12)survives an ALTER planned under default precision;OriginSqlfollows the rename).test/distributed/cases/ddl/alter_table_check_constraints.sql/.result): the issue reproduction with retained rows, an unrelated-column control, multi-column 3959 rejection with full failure atomicity, same-name re-add/replace enforcement (land/reject insert pairs), rename division-scale preservation under differingdiv_precision_increment(row-bearing and empty-table forms), and the pre-existing explicit ADD/DROP CHECK scenarios.Issue-to-test proof
TestAlterRenameRetainedCheckPreservesBoundPrecision: RED on the reviewed predecessor of layer 3 (decimal(16,6)vs requireddecimal(22,12)), PASS on unmodified base (layers 2/3 defects existed only inside this branch's earlier revisions), GREEN after the fix;TestAlterDropColumnReplacementCheckBindsAuthoredColumn(layer 2) likewise red-then-green.go test ./pkg/sql/plan:ok 115.817s;gofmt/go vetclean;golangci-lint --new-from-revclean.5fe68a88e4): issue reproduction succeeds with the row retained; atomic 3819 rejection leaves columns/constraint/row fully intact; replacement enforcement discriminated by the(2,-5,9)reject vs(2,5,-9)accept pair; rename scenario created atdiv_precision_increment=10and altered at 0 succeeds, keeps satisfying rows, and keeps rejecting violating inserts.Residual risks
ColId == 0(synthetic LIKE skeletons) keep the previous name-resolution acceptance; their declarations are unchanged in the reconstructed skeleton.