Skip to content

[Bug]: DELETE cannot maintain ENUM/SET keys and either skips rows or returns internal type errors #29188

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

57c280e

Actual Behavior

DELETE cannot correctly maintain keys whose storage type is ENUM or SET:

  • with a regular secondary index on an ENUM or SET column, a matching DELETE silently reports 0 affected rows and leaves the base row unchanged;
  • with an ENUM primary key, DELETE returns an internal panic stack because a VARCHAR vector is consumed as []types.Enum;
  • with an ENUM unique key, DELETE fails with invalid argument cast to uint16.

The secondary-index case reproduces 3/3 on CN1, 3/3 on CN2, and 3/3 through Proxy. It is independent of JDBC: the same behavior occurs in a minimal SQL session.

This also makes ordinary Spring Data JPA entity deletion fail as an optimistic-lock conflict. Hibernate issues DELETE ... WHERE id=? AND version=?, receives affected row count 0, and raises ObjectOptimisticLockingFailureException even though the row and version both exist.

Steps to Reproduce

Secondary ENUM index silently prevents DELETE

create database enum_delete_repro;
use enum_delete_repro;

create table t (
  id int primary key,
  e enum('A','B','C'),
  index ix(e)
);
insert into t values (1,'A'),(2,'B'),(3,'C');

delete from t where id=3;
select row_count(), count(*) from t;

Actual result:

row_count()  count(*)
0            3

Replacing ENUM with SET('A','B','C') produces the same result.

ENUM primary key returns an internal panic

create table pk_t (
  e enum('A','B') primary key,
  v int
);
insert into pk_t values ('A',1),('B',2);
delete from pk_t where e='B';

Actual error:

internal error: panic type mismatch: casting VARCHAR vector to []types.Enum

ENUM unique key returns a cast error

create table uk_t (
  id int primary key,
  e enum('A','B','C') unique
);
insert into uk_t values (1,'A'),(2,'B'),(3,'C');
delete from uk_t where id=3;

Actual error:

invalid argument cast to uint16, bad value C

Expected Behavior

All three DELETE statements should remove exactly one row and return affected row count 1. Index maintenance must use the same typed representation that INSERT used to create the index key.

Code and physical-key analysis

For an inline regular index on ENUM, INSERT stores the enum ordinal in the hidden serialized key. For example, C is stored using its numeric ordinal. During DELETE, pkg/sql/plan/bind_delete.go builds serial_full directly from the scan projection, whose ENUM/SET value is the display VARCHAR. The generated label-based key does not equal the ordinal-based hidden key, so the INNER JOIN used by Multi Update removes zero rows.

The current UPDATE binder already contains an explicit fix for this representation boundary: pkg/sql/plan/bind_update.go casts old ENUM/SET display values back to their typed storage representation before index maintenance. The equivalent conversion is absent from DELETE.

Physical-key control:

  • an existing row backfilled by ALTER TABLE ... ADD INDEX(e) has a label-based key and can initially be deleted;
  • a new row inserted after that ALTER receives an ordinal-based key and again cannot be deleted;
  • ordinary VARCHAR/DECIMAL secondary-index controls delete correctly.

This issue is separate from #28994: that issue concerns IVFFLAT hidden-table schema metadata during vector-index construction; this one affects ordinary DELETE planning and regular primary/unique/secondary index maintenance.

Activity

  1. self-assigned this
    on Sep 21, 2026
  2. added a commit that references this issue on Sep 22, 2026
    1a24bdb
  3. Ariznawlll commented on Sep 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:3727c5c5436ca136d7b0312f75bc223a11692a85
    • 部署方式:macOS arm64,本地单机拓扑(1 Log Service、1 TN、1 CN),MOTR/mo-tester 通过 MySQL 协议连接
    • 测试日期:2026-09-22

    原问题验证

    按 issue 原始场景创建带 ENUM/SET 主键、唯一索引和普通二级索引的表后执行 DELETE。原始路径重复 3 次,目标行均被正确删除,row_count() 为 1,剩余行和索引查询结果一致;已删除的 ENUM 唯一键可以重新插入。未再出现 affected rows 为 0、类型错误或 panic。

    Happy Path 覆盖

    1. ENUM 普通二级索引删除:索引项同步移除,剩余 2 行正确。
    2. ENUM 主键删除:目标行删除成功。
    3. ENUM 唯一索引删除并复用旧键:复用成功,无残留唯一键。
    4. SET 普通二级索引删除:位图值与索引项同步移除。
    5. 复合 ENUM 二级索引、带 ORDER BY/LIMIT 的 SET 删除、多表 DELETE 均通过。

    Unhappy Path 覆盖

    1. 重复删除不存在的行返回 affected rows 0,已有数据不变。
    2. ENUM 唯一键冲突更新被正确拒绝,冲突前后的主表和索引状态不变。
    3. 显式事务内删除后 ROLLBACK,3 行数据及 ENUM 唯一键均恢复。

    白盒验证

    修复 PR #29198 增加了 DELETE 规划器的 ENUM/SET 类型保持测试;本轮以公开 SQL 和 MOTR 黑盒回归为主。

    回归测试

    回归已随修复合入 test/distributed/cases/dtype/enum_index_update.sql。目标文件连续 3 轮均为 107/107;dtype suite 为 7354/7354(32 条既有 ignore),FAILED 0、ABNORMAL 0。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions