Repository navigation
Description
Summary
While testing REPLACE compatibility against MySQL, I found three behavior gaps in MatrixOne:
REPLACE ... SET col = col + 1does not treat the RHS column reference asDEFAULT(col).CHECKconstraints are not enforced duringREPLACE.- Foreign key checks/actions are not applied with MySQL delete+insert semantics during
REPLACE.
Tested with:
- MatrixOne:
main@6c0d12dfc,select version()returns8.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
NULLvalues in a unique key do not conflict. AUTO_INCREMENTwith unique-key replacement allocates a new id.- Duplicate source keys in one
REPLACEstatement keep the last row. NOT NULLviolations fail and preserve the original row.- MatrixOne-specific types also worked with basic PK/UK
REPLACE:uuid,vecf32,vecf64,datalink,geometry,geometry32,point, andpoint32.
Activity
[-]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[/+][-][Bug] REPLACE has MySQL compatibility gaps for SET expressions, CHECK, and foreign keys[/-][+][Bug] REPLACE has multiple MySQL compatibility gaps[/+]Ariznawlll commentedon Jun 10, 2026
Additional edge coverage against local MO
main(6c0d12dfc,select version()=8.0.30-MatrixOne-v) and MySQL8.0.45found more compatibility gaps.4. Affected rows /
ROW_COUNT()are not MySQL-compatibleMySQL counts a replacement as delete + insert:
5.
DELETEprivilege is not required by MO when replacing an existing rowMySQL requires
DELETEprivilege whenREPLACEdeletes an existing conflicting row. MO allows the replacement with onlyINSERT+SELECT.Expected MySQL behavior: fails with
DELETE command denied.Actual MO behavior: succeeds and changes the row to
(1,20).6. Explicit
PARTITION(...)restriction is ignoredExpected MySQL behavior:
ERROR 1748 (HY000): Found a row not matching the given partition set.Actual MO behavior: the row is inserted.
7. MySQL-compatible
REPLACEsyntax variants are missingREPLACE ... TABLE srcworks in MySQL but fails in MO parser:MySQL result:
t_table_dstbecomes(1,20),(2,30).MO result: syntax error near
table t_table_src.LOW_PRIORITYandDELAYEDmodifiers are also accepted by MySQL 8.0 (DELAYEDis converted to plainREPLACEwith a warning), but rejected by MO parser:8. Updatable view case: broader MO view DML gap
This is probably broader than
REPLACE, but it is visible throughREPLACEcompatibility testing:MySQL updates the base table to
(1,20). MO returnsinvalid input: cannot insert/update/delete from view.Edge cases that matched in this round
geometry,point,linestring,polygon,multipoint,multilinestring,multipolygon,geometrycollection.*32spatial types:geometry32,point32,linestring32,polygon32,multipoint32,multilinestring32,multipolygon32,geometrycollection32.SETtype.tinytext/text/mediumtext/longtextandtinyblob/blob/mediumblob/longblob.AUTO_INCREMENT,DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.NULLvalues.2 remaining items
feat(parser): support MySQL REPLACE syntax variants (#25083)
fix(plan): evaluate REPLACE ... SET RHS column references as DEFAULT(…
Ariznawlll commentedon Aug 5, 2026
测试版本
main607a9263616ea883ac02c052529d2aa4c62ac051原问题验证
REPLACE 主线大部分子问题已修复,但仍有两个公开路径失败:显式
PARTITION(p0)会在 MO 接受并写入不属于 p0 的(15,150),MySQL 返回 1748;对简单可更新 view 执行 REPLACE 在 MO 返回 20301,MySQL 成功更新 base table。两项均 3/3 稳定。Happy Path 覆盖
ROW_COUNT()和客户端 affected rows 分别为 1/2/3,与 MySQL 一致。Unhappy Path 覆盖
白盒验证
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 回归。测试结论
测试不通过。
Ariznawlll commentedon Aug 6, 2026
测试版本
main09e02eea38bfa87e4c4333ffb57b370091136bc8原问题验证
REPLACE INTO t_set SET id=1,v=v+1在已有(1,10,'old')时稳定得到(1,6,'d'),即 RHS 读取新行默认值而非被替换旧行,与 MySQL 一致。Happy Path 覆盖
REPLACE ... SET的默认值和列引用:1,6,d。(2,20,'two')被(3,20,'replaced')替换。(4,50,'last')。Unhappy Path 覆盖
CHECK(v>0)下REPLACE (1,-1)返回 3819,旧行(1,10)保持。REPLACE (1,99)返回 FK 错误,原子保留(1,1)。REPLACE parent(1)返回 FK RESTRICT 错误,父行仍为1。白盒验证
不适用:默认值重建、唯一冲突、CHECK/FK 检查均由稳定 SQL 覆盖。
回归测试
suites/14_issue_regression/t/issue_24918_replace_compat_matrix.test测试结论
测试通过。
feat(parser): support MySQL REPLACE syntax variants (matrixorigin#25083)
fix(plan): evaluate REPLACE ... SET RHS column references as DEFAULT(…
feat(parser): support MySQL REPLACE syntax variants (matrixorigin#25083)
fix(plan): evaluate REPLACE ... SET RHS column references as DEFAULT(…
feat(parser): support MySQL REPLACE syntax variants (matrixorigin#25083)
fix(plan): evaluate REPLACE ... SET RHS column references as DEFAULT(…