Skip to content

[Compatibility]: expression defaults cannot reference other columns in the same row #28247

Closed

Description

@Ariznawlll

问题描述

MySQL 8 支持列的表达式默认值引用同一行中的其他普通列,例如 b INT DEFAULT (a+1)。MatrixOne 的默认值 binder 没有列上下文,建表和 ALTER TABLE ADD COLUMN 都会把该合法引用报为 ambiguous column reference。

这不是普通 INSERT ... VALUES 中的列引用语法:表达式被持久化为列定义,在省略该列或显式使用 DEFAULT 时按当前行的其他列计算。MySQL 还支持多级默认依赖、prepared INSERT、UPDATE ... SET col=DEFAULT、索引、CREATE TABLE LIKE 和 ALTER 回填。

环境

  • MatrixOne latest main, commit e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • MySQL 8.3.0(对照)

最小复现

create database default_colref;
use default_colref;

create table t(
  id int primary key,
  a int,
  b int default (a+1)
);

insert into t(id,a) values(1,10);
select * from t;

MatrixOne 行为

CREATE TABLE 返回:

ERROR 20301: invalid input: ambiguous column reference 'a'

ALTER TABLE 入口存在相同问题:

create table ta(id int primary key,a int);
insert into ta values(1,10),(2,20);
alter table ta add column b int default (a+1);
-- ERROR 20301: invalid input: ambiguous column reference 'a'

MySQL 8.3.0 行为与组合边界

基础和多级依赖:

create table t(
  id int primary key,
  a int,
  b int default (a+1),
  c int default (b+1),
  key idx_b(b)
);

insert into t(id,a) values(1,10),(2,20);
insert into t(id,a,b,c) values(3,30,default,default);

结果分别为 (1,10,11,12)、(2,20,21,22)、(3,30,31,32);idx_b 范围查询返回相同结果。

prepared INSERT:

prepare s from 'insert into t(id,a,b,c) values(?,?,default,default)';
set @id=4,@a=40;
execute s using @id,@a;
-- (4,40,41,42)

UPDATE 的语义是按需重新应用默认表达式,而不是普通列变更时自动重算:

update t set a=100 where id=1;
-- b,c 仍为 11,12

update t set b=default,c=default where id=1;
-- b,c 变为 101,102

CREATE TABLE t2 LIKE t 会保留 b DEFAULT (a+1) 和 c DEFAULT (b+1);向 t2 插入 (id,a)=(10,50) 得到 (10,50,51,52)。

对已有数据增加引用默认值会按每行回填:

create table ta(id int primary key,a int);
insert into ta values(1,10),(2,20);
alter table ta add column b int default (a+1);
-- rows: (1,10,11), (2,20,21)

MySQL 也允许引用定义位置在后的普通列:

create table tf(b int default (a+1),a int);
insert into tf(a) values(10),(20);
-- rows: (11,10), (21,20)

但若后置依赖列本身是生成列或带表达式默认值,则会以 3767 拒绝;自引用同样拒绝。依赖列不能直接删除或重命名(3766)。这些限制可作为实现时的合法性边界。

期望行为

MatrixOne 应在 DEFAULT binder 中提供当前表的列定义上下文,解析并持久化合法的同一行列引用;DML 应按 MySQL 规则计算依赖顺序,ALTER/LIKE/索引/prepared 路径应保留该语义,同时拒绝循环依赖和会破坏依赖的 DDL。

代码线索

pkg/sql/plan/build_util.go 的 buildDefaultExpr 当前使用 NewDefaultBinder(proc.Ctx, nil, nil, typ, nil),列名和列类型上下文均为 nil,因此合法列引用无法绑定。

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. added a commit that references this issue on Sep 10, 2026
    5dc33c1
  6. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新官方 main efb2c5084957ac90273a3f625668fc6a784562c4 验证。

    • 原始场景:列 b DEFAULT (a+1)、c DEFAULT (b+1) 可引用同一行的前置列。
    • Happy path:直接 INSERT、显式 DEFAULT 和 prepared INSERT 分别得到 (10,11,12)、(20,21,22)、(30,31,32)。
    • 边界控制:引用不存在列的默认值被 20301 拒绝且 information_schema 中无残留表;会话保持可用。
    • 自动化:已加入 MOTR PR (aoe)[daily-update]: correct the memory node size #165,issue_28247_28631_ddl;三轮复跑 12/12 case、252/252 statements 一致通过。

    测试通过。

  7. added 3 commits that reference this issue on Sep 17, 2026
    f06c172
    5975a90
    7b6e1d8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions