Repository navigation
Description
Description
CREATE TABLE IF NOT EXISTS dst CLONE src is not a no-op when dst already exists. The statement returns success and appends the source table's physical objects to the existing destination. This can bypass primary-key enforcement and leave duplicate primary-key rows in a table declared with PRIMARY KEY.
The documented syntax supports IF NOT EXISTS, and an existing target is expected to be left unchanged: https://docs.matrixorigin.cn/v26.3.0.14/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-clone/
Environment
- Branch:
main - Commit:
2d9ee9c75398b5492f14db7d9bd4d9f777251bd3 - Test date:
2026-08-12 - Deployment: local single process, 1 Log Service + 1 TN + 1 CN
Steps to reproduce
DROP DATABASE IF EXISTS clone_ifne_repro;
CREATE DATABASE clone_ifne_repro;
USE clone_ifne_repro;
CREATE TABLE src(id INT PRIMARY KEY, v VARCHAR(20));
INSERT INTO src VALUES (1, 'src1'), (2, 'src2');
CREATE TABLE dst(id INT PRIMARY KEY, v VARCHAR(20));
INSERT INTO dst VALUES (1, 'keep1'), (99, 'keep99');
CREATE TABLE IF NOT EXISTS dst CLONE src;
SELECT id, v FROM dst ORDER BY id, v;
SELECT id, COUNT(*) AS cnt
FROM dst
GROUP BY id
HAVING COUNT(*) > 1;Actual behavior
The CLONE statement returns Query OK, but dst contains both its original rows and the source rows:
id v
1 keep1
1 src1
2 src2
99 keep99
id cnt
1 2
The destination still reports PRIMARY KEY (id), but now contains two rows with id=1.
Expected behavior
Because dst already exists and IF NOT EXISTS is present, the statement must be a no-op. Its schema, rows, indexes, and physical objects must remain unchanged.
Stability and controls
- Reproducer:
3/3on the commit above; every run returned success and produced twoid=1rows. - Control without
IF NOT EXISTS:CREATE TABLE dst CLONE srcreturnedERROR 1050: table dst already exists. - Failure-state check for the control:
dstremained exactly(1,'keep1'), (99,'keep99'), with no duplicate key. - The source remained unchanged.
Evidence
Minimal 3/3 output:
table-ifne-duplicate-r1 1 2
table-ifne-duplicate-r2 1 2
table-ifne-duplicate-r3 1 2
Local reproducer and captured evidence are preserved at:
/private/tmp/mo-clone-2d9-validation/reproduce.sql
/private/tmp/mo-clone-2d9-validation/evidence/
Code analysis
The failure path is deterministic:
pkg/sql/compile/ddl.go:1454-1467detects thatdstexists. WithIfNotExists,Scope.CreateTablereturnsnilwithout creating a table.pkg/sql/compile/ddl.go:4033-4047cannot distinguish that no-op from a newly created destination.Scope.TableClonetherefore unconditionally callsRestoreTable.RestoreTableruns the block-level clone path and appends source objects to the already-existing destination, bypassing normal DML uniqueness checks.
The frontend pre-check at pkg/frontend/clone.go:776-790 only records target existence for the COPY GRANTS path and still calls ExecRestore unconditionally.
Regression coverage
Add a distributed CLONE BVT under test/distributed/cases/git4data/clone/ that verifies an existing destination is byte-for-byte unchanged for:
- overlapping primary keys;
- incompatible schemas;
- a destination with a secondary index;
- the normal no-
IF NOT EXISTSrejection path.
Assertions must cover rows, schema, duplicate-key count, index results, and hidden index-table state.
Related
No matching open or closed issue was found using exact searches for CREATE TABLE IF NOT EXISTS ... CLONE, existing CLONE targets, and duplicate primary keys. #26057 concerns a large DELETE hang after a normal clone; #24975 concerns duplicate keys from REPLACE/UPDATE and is a different write path.
Activity
fix: make IF NOT EXISTS table clone a no-op (#27056)
gouhongshen commentedon Aug 14, 2026
fixed by #27056
Ariznawlll commentedon Aug 14, 2026
测试版本
origin/main@263567cf1a56da904788bc6d1d613ff19cb44783(2026-08-14)。
原问题验证
已复现“目标表已存在时执行
CREATE TABLE IF NOT EXISTS target CLONE source”的场景;目标表中的保留行和 sentinel 行均未被再次写入。Happy Path 覆盖
源表克隆到不存在的目标表可正常完成;已有目标表使用
IF NOT EXISTS后内容保持为(1,'preserved')、(99,'sentinel')。Unhappy Path 覆盖
对同名目标表不带
IF NOT EXISTS的 CLONE 返回预期 1050;已有目标表按主键分组无重复行。白盒验证
执行现有 Git4Data clone BVT 用例
test/distributed/cases/git4data/clone/clone_table_if_not_exists.sql,并以实际查询结果确认 no-op 语义。回归测试
已加入 motr#111 的 issue_27039_27040_27041_clone_if_not_exists.test,
./motr --branch main --repeat 3 -v 12_git4data.issue_27039_27040_27041_clone_if_not_exists连续 3 次通过(138/138 statements)。测试证据
MOTR 结果基线记录了目标表内容、重复行检查及预期错误码;对应提交为
37ea270d。测试结论
测试通过。
fix: make IF NOT EXISTS table clone a no-op (matrixorigin#27056)
fix: make IF NOT EXISTS table clone a no-op (matrixorigin#27056)
fix: make IF NOT EXISTS table clone a no-op (matrixorigin#27056)