Skip to content

fix: support ADD INDEX in COPY ALTER TABLE - #29052

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
aptend:fix/28052-alter-copy-add-index
Sep 21, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
aptend:fix/28052-alter-copy-add-index

Conversation

@aptend

@aptend aptend commented Sep 17, 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 #28052

What this PR does / why we need it:

COPY ALTER rejected statements that combined column changes with ADD INDEX clauses. This change:

  • accepts regular, unique, fulltext, and vector indexes in the COPY ALTER path;
  • defers index planning until all copied-table schema changes are available, so an index can reference a column added later in the same statement;
  • records newly added plugin indexes separately and rebuilds them after the copied data is inserted, avoiding duplicate synchronous maintenance;
  • adds planner/compiler unit tests and distributed SQL coverage for clause ordering, anonymous and invalid index definitions, uniqueness failures, fulltext, IVFFLAT, and HNSW indexes;
  • verifies teardown after dropping the test database: no table rows, index rows, active ISCP jobs, or index-update registrations remain.

Validation:

  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=20m ./pkg/sql/plan ./pkg/sql/compile
  • local changed-code coverage with the CI parser: 51/61 blocks (83.61%)
  • go vet ./pkg/sql/plan ./pkg/sql/compile with the repository CGO environment
  • make -j8 build
  • distributed case: 71/71 statements passed twice consecutively on the same instance, with teardown postconditions checked after each run
  • existing generated_column.sql: 325/325 statements passed
  • existing alter_table_algorithm_lock.sql: 21 passed, 5 pre-existing ignored, 0 failed

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

@mergify mergify Bot added the kind/bug Something isn't working label Sep 17, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Sep 17, 2026
@aptend
aptend force-pushed the fix/28052-alter-copy-add-index branch from 59620ff to 37b940e Compare September 21, 2026 06:11
@aptend
aptend force-pushed the fix/28052-alter-copy-add-index branch from 37b940e to 16454ed Compare September 21, 2026 06:17

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

REQUEST_CHANGES at exact head 16454ed.\n\n[P2] Bind newly added indexes before validating dependent self-referencing foreign keys.\n\nIn pkg/sql/plan/build_alter_table.go:495-507, pending indexes are materialized after addAlterCopyForeignKeys runs at line 438. Consequently, finalTableDef lacks UNIQUE indexes introduced by the same COPY ALTER statement.\n\nConcrete input:\n\nCREATE TABLE t (\n id INT PRIMARY KEY,\n parent_code INT,\n ref_code INT\n);\n\nALTER TABLE t\n ADD COLUMN note INT,\n ADD UNIQUE INDEX uk_parent(parent_code),\n ADD CONSTRAINT fk_self\n FOREIGN KEY (ref_code) REFERENCES t(parent_code);\n\nEven with an empty table, addAlterCopyForeignKeys -> checkFkColsAreValid -> selectForeignKeyReferencedIndex cannot find uk_parent and returns a failed-to-add-FK error before index planning. Reordering the SQL clauses does not avoid this internal ordering.\n\nMove pending-index materialization before foreign-key binding, after column mutations. Add planner tests for both clause orders and a public SQL regression proving successful creation and subsequent FK enforcement.\n\nThe previous teardown concern is addressed: exact-head CI ran the case 71/71, and the supplied same-instance rerun/cleanup evidence was reviewed. No additional confirmed blocker was found.\n\nReviewed exact head: 16454ed

@aptend
aptend force-pushed the fix/28052-alter-copy-add-index branch from 16454ed to cfdef0c Compare September 21, 2026 08:37
@aptend

aptend commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the new COPY ALTER review finding in cfdef0c. Pending indexes are now materialized before foreign-key binding, so a self-referencing FK can use a UNIQUE index added by the same ALTER regardless of clause order. Added planner coverage for both orders and a distributed SQL regression that verifies successful creation, valid inserts, FK rejection, and teardown. Local validation: pkg/sql/plan and pkg/sql/compile passed; the exact BVT passed 78/78; go vet and golangci-lint passed with 0 issues.

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

APPROVE at exact head cfdef0c.\n\nThe previous P2 is resolved: pending indexes are materialized before foreign-key binding against the final COPY schema. Planner tests cover both UNIQUE/FK clause orders with newly added columns, and the SQL regression verifies successful creation and subsequent FK enforcement.\n\nReviewed index dispatch, COPY insert versus plugin rebuild ownership, hidden-table handling, rollback/retry, ISCP/CDC and index-update teardown, and generated protobuf consistency. No P0/P1/P2 blockers remain. Exact-head BVT passed 78/78 with no failures, skips, or abnormal results. Coverage percentage was not used as a gate.\n\nReviewed exact head: cfdef0c

@mergify

mergify Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-21 09:50 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-21 09:57 UTC · at a0f7f0b283bc8213edf4d010935ba87b2c3aa1dc

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

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

Pull request #29052 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.

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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants