Skip to content

[Bug]: CREATE TABLE IF NOT EXISTS ... CLONE mutates an existing table and creates duplicate primary keys #27039

Closed

Description

@Ariznawlll

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/3 on the commit above; every run returned success and produced two id=1 rows.
  • Control without IF NOT EXISTS: CREATE TABLE dst CLONE src returned ERROR 1050: table dst already exists.
  • Failure-state check for the control: dst remained 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:

  1. pkg/sql/compile/ddl.go:1454-1467 detects that dst exists. With IfNotExists, Scope.CreateTable returns nil without creating a table.
  2. pkg/sql/compile/ddl.go:4033-4047 cannot distinguish that no-op from a newly created destination. Scope.TableClone therefore unconditionally calls RestoreTable.
  3. RestoreTable runs 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:

  1. overlapping primary keys;
  2. incompatible schemas;
  3. a destination with a secondary index;
  4. the normal no-IF NOT EXISTS rejection 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

  1. added this to the MO-202608 milestone on Aug 12, 2026
  2. added
    kind/bug
    on Aug 12, 2026
  3. added theissue type on Aug 12, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Aug 12, 2026
  5. added a commit that references this issue on Aug 13, 2026
    1cd1b9b
  6. added and removed
    severity/s0
    on Aug 14, 2026
  7. gouhongshen commented on Aug 14, 2026

    @gouhongshen
    Contributor

    fixed by #27056

  8. Ariznawlll commented on Aug 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    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。

    测试结论

    测试通过。

  9. added 3 commits that reference this issue on Sep 17, 2026
    4895c44
    f59b48b
    9953e74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions