Skip to content

[Bug]: ENUM/SET DDL accepts invalid DEFAULT values and duplicate members #31892

Open

Description

@MatrixAdventurer

问题描述

ENUM/SET 列的常量 DEFAULT 不在成员列表里时,MO 在建表/改表时直接接受,等到之后每一条依赖这个默认值的 INSERT 才失败(严格模式报 1265,非严格模式存成空串)。MySQL 在 DDL 阶段就拒绝,报 ERROR 1067 (42000): Invalid default value,并且与 sql_mode 无关。

环境

  • 分支:main(a5485098,2026-10-30)
  • 部署:单机 mo-service -launch etc/launch/launch.toml
  • 对照:MySQL 8.4.9(默认 sql_mode)

复现步骤

create table e1(id int, e enum('a','b') default 'zz');
create table e2(id int, s set('a','b') default 'c');
create table e4(id int, e enum('a','b') default 3);
create table e6(id int, s set('a','b') default 4);
create table e8(id int, e enum('a','b') default '');
create table e3(id int, e enum('a','b') default 'a');
alter table e3 alter column e set default 'zz';
alter table e3 modify column e enum('a','b') default 'q';
alter table e3 add column e4 enum('x','y') default 'z';
insert into e1(id) values (1);
show create table e1;

实际结果(MO)

所有 CREATE / ALTER 都返回 OK,SHOW CREATE TABLE e1 显示 DEFAULT 'zz',然后:

insert into e1(id) values (1);
ERROR 1265 (01000): Data truncated for ENUM value: 'zz'
insert into e2(id) values (1);
ERROR 1265 (01000): Data truncated for SET value: 'c'
insert into e4(id) values (1);
ERROR 1265 (01000): Data truncated for ENUM value: '3'
set sql_mode=''; insert into e1(id) values (2);   -- 存成 ''

期望结果(MySQL 8.4.9)

每一条 DDL 都报:

ERROR 1067 (42000): Invalid default value for 'e'   (SET 列为 's',ADD COLUMN 为 'e4')

合法默认值(成员本身、大小写不同、尾部空格、NULL、SET 的 'b,a' / ''、ENUM('','a') 的 '')以及括号表达式默认值 DEFAULT ('zz') 在 MySQL 里都能建表。

分析

ENUM/SET 的默认值被绑定成 cast_value_to_index / cast_set_value_to_index 等赋值函数。这些函数的截断行为取决于插入时的 sql_mode,所以 DDL 阶段的常量折叠不会对它们求值,成员校验就被推迟到了每一次 INSERT。

本操作由 LW 授权的 Grok Bot 智能体执行。

Activity

  1. added
    kind/bug
    on Oct 30, 2026
  2. changed the title [-][Bug]: ENUM/SET DEFAULT that is not a member is accepted at DDL, then every INSERT fails with 1265 (MySQL: 1067)[/-] [+][Bug]: ENUM/SET DDL accepts invalid DEFAULT values and duplicate members[/+] on Nov 20, 2026
  3. XuPeng-SH commented on Nov 20, 2026

    @XuPeng-SH
    Contributor

    Administrative consolidation of #31893 into this issue. The title is broadened to cover both DDL validation contracts; this is not a fix claim.

    Additional acceptance coverage retained from #31893:

    • Reject duplicate ENUM members with MySQL-compatible ERROR 1291 behavior under the documented strict/non-strict modes and default case-insensitive collation.
    • SET duplicate members must report MySQL-compatible 1291, not internal error 20301.
    • Preserve the existing invalid-DEFAULT cases. Binary-collation member identity remains separate under [Bug]: ENUM/SET must preserve label identity and text-versus-ordinal semantics #30771.

    Assignee retained: matrix-meow.

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions