Skip to content

[Bug]: same-statement DROP/ADD of an indexed column silently removes its index #30270

Closed

Description

@Ariznawlll

Description

When a single ALTER TABLE drops and re-adds a column under the same name, MatrixOne reports success but silently removes an existing index on that column. For a UNIQUE index, the resulting table can contain duplicate values and accepts later duplicates without any explicit DROP INDEX. MySQL retains the index and rejects the ALTER if the replacement values violate uniqueness.

Environment

  • MatrixOne official main: bdbbd177921067ab1b0956ea45f378c41ba5e2b8 (git_version() = bdbbd17792), locally built, 8.0.30-MatrixOne-v1.3.0
  • Isolated local 1 LogService / 1 TN / 1 CN
  • Comparator: MySQL 8.0.45, InnoDB
  • Date: 2026-10-15

Steps to reproduce

Run in a fresh database:

CREATE TABLE t (id INT PRIMARY KEY, v INT, UNIQUE KEY uq_v(v));
INSERT INTO t VALUES (1,10),(2,20);
ALTER TABLE t DROP COLUMN v, ADD COLUMN v INT DEFAULT 7;
SELECT id,v FROM t ORDER BY id;
SELECT COUNT(*) AS uq_count
FROM information_schema.statistics
WHERE table_schema = DATABASE() AND table_name = 't' AND index_name = 'uq_v';
SHOW CREATE TABLE t;

Actual behavior

MatrixOne executes the ALTER successfully. Both rows become (1,7),(2,7) and uq_count=0; SHOW CREATE TABLE no longer includes uq_v. No DROP INDEX was requested.

Expected behavior

Preserve the existing UNIQUE index on the same-named final column, validate the replacement data against it, and reject this ALTER atomically because both historical rows would become v=7. MySQL 8.0.45 returns ERROR 1062: Duplicate entry '7' for key 't.uq_v'; the original (1,10),(2,20) and uq_v remain unchanged.

Stability and controls

  • The same UNIQUE-index case was reproduced with three fresh tables: MatrixOne succeeded 3/3 and lost uq_v 3/3; MySQL rejected 3/3 and preserved data/index 3/3.
  • With no index on v, both engines successfully perform the same DROP/ADD and produce two rows with v=7.
  • With an ordinary secondary index on v, both engines complete the ALTER, but MatrixOne drops that index while MySQL retains it. The dependent view in that case still reads the replacement values correctly; the defect is index metadata/integrity, not stale view rows.
  • The same loss occurs for a classic FULLTEXT index: after ALTER TABLE ftswap DROP COLUMN txt, ADD COLUMN txt TEXT, MatrixOne reports success but ft_txt is absent from SHOW CREATE TABLE and information_schema.statistics; MySQL retains FULLTEXT KEY ft_txt(txt). Both engines correctly replace old text values with NULL, so the discrepancy is index preservation.
  • The same planner path also changes composite UNIQUE semantics. With UNIQUE uq_ab(a,b), ALTER TABLE t DROP COLUMN b, ADD COLUMN b INT DEFAULT NULL succeeds on two rows with distinct a values, but MatrixOne silently shortens uq_ab to UNIQUE(a); MySQL retains both key parts. A later (a=1,b=99) insert is wrongly rejected by MatrixOne and accepted by MySQL. If two existing rows share a but differ in old b, MatrixOne wrongly rejects this ALTER with a duplicate a error while MySQL succeeds because both replacement b values are NULL. The rejected MatrixOne operation leaves old data/index intact.
  • This differs from [Bug]: Combined ALTER TABLE planning does not consistently honor the final table definition #30256: that issue concerns an explicit combined DROP INDEX plus DROP COLUMN being rejected. Here no DROP INDEX appears, the ALTER succeeds, and the index disappears silently.

Code analysis

pkg/sql/plan/build_alter_table.go applies COPY ALTER clauses in order. DropColumn calls handleDropColumnWithIndex in pkg/sql/plan/build_alter_add_column.go, which removes the existing index definition once its only key part is dropped. The later same-name AddColumn adds the column but does not restore or revalidate the original index. This matches the observed metadata and duplicate rows; the planner should reconcile indexes against the final schema before copying data.

Regression coverage

After a fix, cover same-statement DROP/ADD of an indexed column with ordinary, single-column UNIQUE, composite UNIQUE, and FULLTEXT indexes. Include a duplicate replacement default (atomic rejection), nullable replacement key part, and non-conflicting replacement value. Assert final index key parts, indexed reads, subsequent uniqueness enforcement, data, and dependent-view results. No repository test case was added during this exploration.

Activity

  1. added this to the MO-202610 milestone on Oct 15, 2026
  2. added theissue type on Oct 15, 2026
  3. added a commit that references this issue on Oct 20, 2026
    d378990
  4. Ariznawlll commented on Oct 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:执行时最新官方 main,完整 commit 180608ea8c575285a13f78db96d70f618a5161d6;SELECT git_version() 在实际服务返回 180608ea8c。
    • 修复:MatrixOne PR #30671,merge commit d378990b297ca96ff5cfd2a2f2a21dd554c7dbcc 已核实在该 SHA 祖先链。
    • 部署:macOS arm64 隔离工作树,真实 mo-service(1 CN / 1 TN / 1 LogService)、MySQL TCP 6001;2026-10-21 北京时间。原有主工作区改动未触碰,服务已正常停止。

    原问题验证

    使用 issue 原 SQL 独立建库运行 3 次:t(id PRIMARY KEY,v,UNIQUE uq_v(v)) 含 (1,10),(2,20),执行 ALTER TABLE t DROP COLUMN v, ADD COLUMN v INT DEFAULT 7。三次均收到 ERROR 1062 Duplicate entry '7',不再成功并静默丢弃 UNIQUE 索引。每次失败后读取实际数据仍为 (1,10),(2,20),information_schema.statistics 中 uq_v 计数仍为 1;随后尝试插入 (3,10) 均再次收到 1062,证明约束继续生效。每轮测试库均已删除。

    Happy Path 覆盖

    • 修复自带的 TestAlterReplacementPreservesIndexes 在上述完整 main SHA 上由本轮独立执行 -count=3 通过。它通过真实前端 SQL/COPY ALTER 路径断言:无冲突的单列 UNIQUE 替换后索引读取及后续重复写拒绝;复合 UNIQUE (a,b) 在替换 b DEFAULT NULL 后保留两列与 NULL 语义;普通索引保留、索引查询与表扫描及 View 结果一致、更新后索引可读;经典 FULLTEXT 索引保留、旧 token 消失、新 token 可查。
    • 同一集成测试还覆盖重命名/再次替换、真正删除列后的索引清理、约束名称释放、前缀索引重建,均在三轮测试内完成断言。

    Unhappy Path 覆盖

    • 原始 UNIQUE 冲突 ALTER:独立真实客户端 3/3 返回 1062,表数据及索引保持原值;后续重复值 INSERT 3/3 继续返回 1062。
    • 集成测试 3/3 还断言失败的 COPY ALTER 没有遗留临时表、旧数据与索引不变;不支持的 JSON 替换索引列被拒绝后,旧字符串、前缀索引和后续正常替换路径仍可用。这些是错误拒绝和失败原子性,而非仅检查命令退出码。

    白盒验证

    • 本轮实际执行 pkg/tests/sqlintegration 的 TestAlterReplacementPreservesIndexes -count=3,终端 ok、退出码 0;已逐项核对源测试中对数据值、索引 key parts、强制/忽略索引结果、1062、失败后 catalog 临时关系数量及 FULLTEXT token 的具体断言。
    • PR 的 planner 及 race 定向测试结果见 PR 描述;本轮未独立重跑整个 planner/race suite,不把 PR 作者自报结果写成本地验收结果。

    回归测试

    测试证据

    • 三轮真实客户端输出一致:ALTER → ERROR 1062 Duplicate entry '7';失败后 SELECT id,v → (1,10),(2,20);uq_v 元数据行数 → 1;再次插入旧重复值 → ERROR 1062 Duplicate entry '10'。
    • 本轮 TestAlterReplacementPreservesIndexes -count=3 ./pkg/tests/sqlintegration → ok,耗时 7.247 秒。修复 PR、测试源码与 CI 链接如上。普通 make build 一度因隔离工作树的本地 native provenance marker 不匹配而中止;核对两次 SHA 间 Makefile/CGo/thirdparties 输入未变化后,使用现成 native 构件的官方 build-with-prebuilt-native 成功编译实际测试服务,此构建障碍不属于产品测试失败。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions