Skip to content

[Bug] REPLACE has multiple MySQL compatibility gaps #24918

Closed
7 of 7 issues completed
Closed

Description

@Ariznawlll

Summary

While testing REPLACE compatibility against MySQL, I found three behavior gaps in MatrixOne:

  1. REPLACE ... SET col = col + 1 does not treat the RHS column reference as DEFAULT(col).
  2. CHECK constraints are not enforced during REPLACE.
  3. Foreign key checks/actions are not applied with MySQL delete+insert semantics during REPLACE.

Tested with:

  • MatrixOne: main@6c0d12dfc, select version() returns 8.0.30-MatrixOne-v
  • MySQL comparison: 8.0.45

According to MySQL docs, REPLACE works like INSERT, except that duplicate PRIMARY KEY or UNIQUE rows are deleted before inserting the new row. Missing columns use default values, and in REPLACE ... SET col = col + 1, the RHS col is treated as DEFAULT(col), not the current row value.

MySQL reference: https://dev.mysql.com/doc/refman/8.4/en/replace.html

1. REPLACE ... SET col = col + 1 should use DEFAULT(col)

Reproduce

drop database if exists replace_expr_cmp;
create database replace_expr_cmp;
use replace_expr_cmp;

create table t_set(
  id int primary key,
  v int default 5,
  note varchar(20) default 'd'
);

insert into t_set values (1,10,'old');
replace into t_set set id = 1, v = v + 1;
select * from t_set;

Expected, same as MySQL

id | v | note
1  | 6 | d

v + 1 should be evaluated as DEFAULT(v) + 1, so the value should be 5 + 1 = 6. Columns not provided by the REPLACE statement should use defaults.

Actual in MatrixOne

ERROR 20301 (HY000): invalid input: ambiguous column reference 'v'

The old row remains unchanged.

2. CHECK constraint is not enforced during REPLACE

Reproduce

drop database if exists replace_check_cmp;
create database replace_check_cmp;
use replace_check_cmp;

create table t_ck(
  id int primary key,
  v int check (v > 0)
);

insert into t_ck values (1,10);
replace into t_ck values (1,-1);
select * from t_ck order by id;

Expected, same as MySQL

The REPLACE should fail because v = -1 violates check (v > 0), and the original row should remain:

id | v
1  | 10

MySQL reports:

ERROR 3819 (HY000): Check constraint 't_ck_chk_1' is violated.

Actual in MatrixOne

The invalid row is inserted:

id | v
1  | -1

If MatrixOne intentionally parses but does not enforce CHECK, this is a known capability gap. Otherwise, REPLACE should be routed through the same constraint validation as INSERT.

3. Foreign key checks/actions are not applied with REPLACE delete+insert semantics

3.1 Child-side FK check is skipped

Reproduce

drop database if exists replace_fk_child_cmp;
create database replace_fk_child_cmp;
use replace_fk_child_cmp;

create table p(id int primary key);
create table c(
  id int primary key,
  pid int,
  foreign key(pid) references p(id)
);

insert into p values (1);
replace into c values (10,1);
select * from c order by id;

replace into c values (10,2);
select * from c order by id;

replace into c values (11,99);
select * from c order by id;

Expected, same as MySQL

Both pid = 2 and pid = 99 should fail because there is no parent row:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

The child table should remain:

id | pid
10 | 1

Actual in MatrixOne

Both invalid child rows are accepted:

id | pid
10 | 2
11 | 99

3.2 Parent-side FK actions are not applied

RESTRICT reproduce

drop database if exists replace_fk_parent_restrict_cmp;
create database replace_fk_parent_restrict_cmp;
use replace_fk_parent_restrict_cmp;

create table fk_p(id int primary key, v varchar(20));
create table fk_c(
  id int primary key,
  pid int,
  foreign key(pid) references fk_p(id) on delete restrict
);

insert into fk_p values (1,'p1');
insert into fk_c values (10,1);
replace into fk_p values (1,'p1_new');
select * from fk_p order by id;
select * from fk_c order by id;

Expected, same as MySQL

Because REPLACE should delete the old parent row before inserting the new one, ON DELETE RESTRICT should reject it:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

The parent row should remain unchanged:

id | v
1  | p1

Actual in MatrixOne

The parent row is replaced successfully and the child remains:

fk_p:
id | v
1  | p1_new

fk_c:
id | pid
10 | 1

CASCADE reproduce

drop database if exists replace_fk_parent_cascade_cmp;
create database replace_fk_parent_cascade_cmp;
use replace_fk_parent_cascade_cmp;

create table fk_cp(id int primary key, v varchar(20));
create table fk_cc(
  id int primary key,
  pid int,
  foreign key(pid) references fk_cp(id) on delete cascade
);

insert into fk_cp values (1,'p1');
insert into fk_cc values (10,1);
replace into fk_cp values (1,'p1_new');
select * from fk_cp order by id;
select * from fk_cc order by id;

Expected, same as MySQL

The old parent row should be deleted, cascading deletion to child rows, then the new parent row should be inserted:

fk_cp:
id | v
1  | p1_new

fk_cc:
-- empty

Actual in MatrixOne

The child row remains:

fk_cp:
id | v
1  | p1_new

fk_cc:
id | pid
10 | 1

Notes

Other REPLACE behavior looks compatible in local testing:

  • Primary key conflict replaces the old row.
  • Unique key conflict replaces the old row.
  • One new row can fan out and delete multiple old rows through multiple unique indexes.
  • Tables without primary/unique keys behave like insert-only tables.
  • Multiple NULL values in a unique key do not conflict.
  • AUTO_INCREMENT with unique-key replacement allocates a new id.
  • Duplicate source keys in one REPLACE statement keep the last row.
  • NOT NULL violations fail and preserve the original row.
  • MatrixOne-specific types also worked with basic PK/UK REPLACE: uuid, vecf32, vecf64, datalink, geometry, geometry32, point, and point32.

Sub-issues

Sub-issues

7 of 7 Issues completed
Loading 7 sub-issues

Activity

  1. changed the title [-]REPLACE has MySQL compatibility gaps for SET expressions, CHECK, and foreign keys[/-] [+][Bug] REPLACE has MySQL compatibility gaps for SET expressions, CHECK, and foreign keys[/+] on Jun 10, 2026
  2. added
    kind/bug
    on Jun 10, 2026
  3. added theissue type on Jun 10, 2026
  4. added this to the MO-202606 milestone on Jun 10, 2026
  5. changed the title [-][Bug] REPLACE has MySQL compatibility gaps for SET expressions, CHECK, and foreign keys[/-] [+][Bug] REPLACE has multiple MySQL compatibility gaps[/+] on Jun 10, 2026
  6. Ariznawlll commented on Jun 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional edge coverage against local MO main (6c0d12dfc, select version() = 8.0.30-MatrixOne-v) and MySQL 8.0.45 found more compatibility gaps.

    4. Affected rows / ROW_COUNT() are not MySQL-compatible

    MySQL counts a replacement as delete + insert:

    drop database if exists replace_rowcount_edge;
    create database replace_rowcount_edge;
    use replace_rowcount_edge;
    create table t(id int primary key, v int);
    insert into t values(1,10);
    
    replace into t values(1,20);
    select row_count(); -- MySQL: 2, MO: 0; client affected rows: MySQL 2, MO 1
    
    replace into t values(2,30);
    select row_count(); -- MySQL: 1, MO: 0; client affected rows: MySQL 1, MO 1
    
    replace into t values(1,40),(3,50);
    select row_count(); -- MySQL: 3, MO: 0; client affected rows: MySQL 3, MO 2

    5. DELETE privilege is not required by MO when replacing an existing row

    MySQL requires DELETE privilege when REPLACE deletes an existing conflicting row. MO allows the replacement with only INSERT + SELECT.

    -- as admin
    create database replace_priv_edge;
    use replace_priv_edge;
    create table t_priv(id int primary key, v int);
    insert into t_priv values(1,10);
    
    create role r_replace_only_insert;
    grant connect on account * to r_replace_only_insert;
    grant show databases on account * to r_replace_only_insert;
    grant show tables on database replace_priv_edge to r_replace_only_insert;
    grant select on table replace_priv_edge.t_priv to r_replace_only_insert;
    grant insert on table replace_priv_edge.t_priv to r_replace_only_insert;
    create user replace_only_insert identified by '111' default role r_replace_only_insert;
    
    -- as replace_only_insert
    use replace_priv_edge;
    replace into t_priv values(1,20);

    Expected MySQL behavior: fails with DELETE command denied.
    Actual MO behavior: succeeds and changes the row to (1,20).

    6. Explicit PARTITION(...) restriction is ignored

    drop database if exists replace_partition_edge;
    create database replace_partition_edge;
    use replace_partition_edge;
    create table t_pr(id int primary key, v int)
    partition by range(id)(
      partition p0 values less than (10),
      partition p1 values less than (20)
    );
    insert into t_pr values (1,10),(11,110);
    
    replace into t_pr partition(p0) values (15,150);

    Expected MySQL behavior: ERROR 1748 (HY000): Found a row not matching the given partition set.
    Actual MO behavior: the row is inserted.

    7. MySQL-compatible REPLACE syntax variants are missing

    REPLACE ... TABLE src works in MySQL but fails in MO parser:

    create table t_table_src(id int primary key, v int);
    create table t_table_dst(id int primary key, v int);
    insert into t_table_src values (1,20),(2,30);
    insert into t_table_dst values (1,10);
    replace into t_table_dst table t_table_src;

    MySQL result: t_table_dst becomes (1,20),(2,30).
    MO result: syntax error near table t_table_src.

    LOW_PRIORITY and DELAYED modifiers are also accepted by MySQL 8.0 (DELAYED is converted to plain REPLACE with a warning), but rejected by MO parser:

    replace low_priority into t_mod values(1,20);
    replace delayed into t_mod values(1,30);

    8. Updatable view case: broader MO view DML gap

    This is probably broader than REPLACE, but it is visible through REPLACE compatibility testing:

    create table base(id int primary key, v int);
    create view v_base as select id,v from base;
    insert into base values(1,10);
    replace into v_base values(1,20);

    MySQL updates the base table to (1,20). MO returns invalid input: cannot insert/update/delete from view.

    Edge cases that matched in this round

    • Common spatial types: geometry, point, linestring, polygon, multipoint, multilinestring, multipolygon, geometrycollection.
    • MO *32 spatial types: geometry32, point32, linestring32, polygon32, multipoint32, multilinestring32, multipolygon32, geometrycollection32.
    • SET type.
    • tinytext/text/mediumtext/longtext and tinyblob/blob/mediumblob/longblob.
    • Defaults, AUTO_INCREMENT, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
    • Composite unique keys with NULL values.
    • Generated columns and secondary/fulltext/vector/spatial indexes were checked separately and replacement updated the stored row as expected.
  7. added
    severity/s0
    on Jun 11, 2026
  8. 2 remaining items

  9. modified the milestones: MO-202606, MO-202607 on Jun 30, 2026
  10. Ariznawlll commented on Aug 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:607a9263616ea883ac02c052529d2aa4c62ac051
    • 部署方式:本地单 LOG + TN + CN;另启隔离 MySQL 8.0.45 对照
    • 测试日期:2026-08-05

    原问题验证

    REPLACE 主线大部分子问题已修复,但仍有两个公开路径失败:显式 PARTITION(p0) 会在 MO 接受并写入不属于 p0 的 (15,150),MySQL 返回 1748;对简单可更新 view 执行 REPLACE 在 MO 返回 20301,MySQL 成功更新 base table。两项均 3/3 稳定。

    Happy Path 覆盖

    1. REPLACE SET 右值引用默认值、CHECK、子表 FK、父表 RESTRICT/CASCADE 均正确。
    2. PK/UK 冲突、多唯一键 fanout、NULL unique、重复源行 last-wins 和 NOT NULL 原子失败均通过。
    3. 无 INTO、REPLACE SELECT、prepared、事务回滚均通过。
    4. 新增/替换/混合场景的 ROW_COUNT() 和客户端 affected rows 分别为 1/2/3,与 MySQL 一致。

    Unhappy Path 覆盖

    1. wrong-partition REPLACE 在 MO 错误提交了行;MySQL 拒绝且无数据变化,3/3。
    2. 简单可更新 view 的 REPLACE 在 MO 被整体拒绝;MySQL 成功,3/3;MO 失败后 base table 保持不变且会话可用。
    3. CHECK、FK、NOT NULL、唯一键等拒绝路径均检查失败后的数据原子性。

    白盒验证

    REPLACE 规划/FK/默认值聚焦 Go 测试通过。源码分析确认 bindReplace 未使用语句中的 partition 选择;另有全局 DML guard 直接拒绝 view,这与两个剩余失败一致。

    回归测试

    已修复子路径由 motr issue_24945_replace_set_default_refs、issue_24946_replace_child_fk 及 MatrixOne BVT/UT 覆盖。wrong-partition 和 view 路径当前仍失败,未写入 baseline;修复后需要各自新增公开 SQL 回归。

    测试结论

    测试不通过。

  11. Ariznawlll commented on Aug 6, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:09e02eea38bfa87e4c4333ffb57b370091136bc8
    • 部署方式:本地单机 LogService + TN + CN,独立数据目录。
    • 测试日期:2026-08-06
    • MySQL oracle:本地 MySQL 8,同一组确定性数据。

    原问题验证

    REPLACE INTO t_set SET id=1,v=v+1 在已有 (1,10,'old') 时稳定得到 (1,6,'d'),即 RHS 读取新行默认值而非被替换旧行,与 MySQL 一致。

    Happy Path 覆盖

    1. REPLACE ... SET 的默认值和列引用:1,6,d。
    2. 二级唯一键冲突替换:旧 (2,20,'two') 被 (3,20,'replaced') 替换。
    3. 同一 VALUES 批内重复主键:最终保留最后一个 (4,50,'last')。
    4. 上述行结果连续 3 轮与 MySQL 一致。

    Unhappy Path 覆盖

    1. CHECK(v>0) 下 REPLACE (1,-1) 返回 3819,旧行 (1,10) 保持。
    2. 子表 REPLACE (1,99) 返回 FK 错误,原子保留 (1,1)。
    3. 有子表引用时 REPLACE parent(1) 返回 FK RESTRICT 错误,父行仍为 1。

    白盒验证

    不适用:默认值重建、唯一冲突、CHECK/FK 检查均由稳定 SQL 覆盖。

    回归测试

    • motr PR:matrixorigin/motr#81
    • 文件:suites/14_issue_regression/t/issue_24918_replace_compat_matrix.test
    • 结果:record 后连续 3 轮成功;每轮 26 条语句/断言成功。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions