Repository navigation
Description
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_v3/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 withv=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 butft_txtis absent fromSHOW CREATE TABLEandinformation_schema.statistics; MySQL retainsFULLTEXT 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 NULLsucceeds on two rows with distinctavalues, but MatrixOne silently shortensuq_abtoUNIQUE(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 shareabut differ in oldb, MatrixOne wrongly rejects this ALTER with a duplicateaerror while MySQL succeeds because both replacementbvalues 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 INDEXplusDROP COLUMNbeing rejected. Here noDROP INDEXappears, 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
fix(sql): preserve inherited indexes across column replacement (#30671)
Ariznawlll commentedon Oct 20, 2026
测试版本
main,完整 commit180608ea8c575285a13f78db96d70f618a5161d6;SELECT git_version()在实际服务返回180608ea8c。d378990b297ca96ff5cfd2a2f2a21dd554c7dbcc已核实在该 SHA 祖先链。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 覆盖
白盒验证
pkg/tests/sqlintegration的TestAlterReplacementPreservesIndexes -count=3,终端ok、退出码 0;已逐项核对源测试中对数据值、索引 key parts、强制/忽略索引结果、1062、失败后 catalog 临时关系数量及 FULLTEXT token 的具体断言。回归测试
测试证据
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成功编译实际测试服务,此构建障碍不属于产品测试失败。测试结论
测试通过。