Skip to content

[Bug]: Database CLONE and DATA BRANCH mishandle object identifiers containing # #26118

Closed

Description

@Ariznawlll

Summary

DATA BRANCH CREATE DATABASE mishandles database objects when a database, foreign-key table, or dependent view name contains the legal identifier character #.

For foreign-key table names containing #, the statement reports success, but none of the affected tables or rows are copied. For a source database name containing #, the statement fails while resolving the foreign key and rolls the destination back. Dependent view names containing # either make the operation fail or produce a successful destination that is missing all views. The ordinary database CLONE path has the same table-name behavior because both paths share the database source collector.

Environment

  • Branch: main
  • Commit: 2eb7f523e5a10f352d3e258183ed8440510388de
  • MatrixOne endpoint: local standalone server

Reproduction

DROP DATABASE IF EXISTS g4d_hash_fk_src;
DROP DATABASE IF EXISTS g4d_hash_fk_dst;
CREATE DATABASE g4d_hash_fk_src;
USE g4d_hash_fk_src;

CREATE TABLE `parent#p` (
    id INT PRIMARY KEY,
    note VARCHAR(32)
);
CREATE TABLE `child#c` (
    id INT PRIMARY KEY,
    parent_id INT,
    CONSTRAINT fk_hash_parent
        FOREIGN KEY (parent_id) REFERENCES `parent#p`(id)
);

INSERT INTO `parent#p` VALUES (1, 'one'), (2, 'two');
INSERT INTO `child#c` VALUES (10, 1), (20, 2);

DATA BRANCH CREATE DATABASE g4d_hash_fk_dst FROM g4d_hash_fk_src;
SHOW FULL TABLES FROM g4d_hash_fk_dst;

The source objects and join are valid:

child#c   BASE TABLE
parent#p  BASE TABLE

10  one
20  two

Actual result

DATA BRANCH CREATE DATABASE returns success, while SHOW FULL TABLES FROM g4d_hash_fk_dst returns no rows.

The result reproduced in 3 out of 3 independent destination databases. The source database remained readable after every attempt.

The shared ordinary path behaves the same way:

CREATE DATABASE g4d_hash_fk_clone_dst CLONE g4d_hash_fk_src;
SHOW FULL TABLES FROM g4d_hash_fk_clone_dst;

It also succeeds with an empty destination.

A source database name containing # reproduces the same key-decoding defect through a different later path:

CREATE DATABASE `g4d#hash_fk_src`;
USE `g4d#hash_fk_src`;
CREATE TABLE parent_p (id INT PRIMARY KEY);
CREATE TABLE child_c (
    id INT PRIMARY KEY,
    parent_id INT,
    FOREIGN KEY (parent_id) REFERENCES parent_p(id)
);
DATA BRANCH CREATE DATABASE g4d_hash_fk_db_dst FROM `g4d#hash_fk_src`;

This variant failed in 3 out of 3 attempts with:

internal error: can't find fkTable from fk ...child_c.(parent_id)

The failed destination database was absent after each attempt, so rollback remained atomic.

Dependent views are affected by the same key representation:

CREATE DATABASE g4d_hash_view_src;
USE g4d_hash_view_src;
CREATE TABLE base_t (id INT PRIMARY KEY, v INT);
CREATE VIEW `view#1` AS SELECT id, v FROM base_t;
CREATE VIEW `view#2` AS SELECT id, v + 1 AS v2 FROM `view#1`;

DATA BRANCH CREATE DATABASE g4d_hash_view_dst FROM g4d_hash_view_src;
SHOW FULL TABLES FROM g4d_hash_view_dst;

Across four attempts, every result was incorrect:

  • two attempts returned success but copied only base_t, silently omitting both views;
  • two attempts failed while restoring view#1 and rolled the destination back.

A single view whose name contains # but does not depend on another view copied successfully. The defect appears when the encoded object keys participate in dependency ordering.

Expected result

Both foreign-key-related tables, their data, and the foreign-key definition should be present in the destination database. If this identifier form cannot be supported, the statement should return an explicit error and leave no successful-but-empty result.

Code analysis

Database cloning represents an object key as:

const KeySep = "#"

func genKey(dbName, tblName string) string {
    return fmt.Sprintf("%s%s%s", dbName, KeySep, tblName)
}

The reverse operation uses an unrestricted split and keeps only the first two components:

func splitKey(key string) (string, string) {
    parts := strings.Split(key, KeySep)
    if len(parts) >= 2 {
        return parts[0], parts[1]
    }
    return parts[0], ""
}

For g4d_hash_fk_src#parent#p, this resolves the table name as parent instead of parent#p. getTableInfoMap therefore cannot populate the expected table information. Later, handleCloneDatabaseWithSource skips these tables in the ordinary loop because they are foreign-key members, but the foreign-key loop has no valid table information to clone.

For a key such as g4d#hash_fk_src#parent_p, the decoded database and table are g4d and hash_fk_src, so foreign-key restoration cannot find the referenced table. The same lossy decoding is used while rewriting and ordering dependent views.

Relevant locations:

  • pkg/frontend/util.go: genKey, splitKey
  • pkg/frontend/snapshot.go: getFkDeps, getTableInfoMap, fkTablesTopoSort
  • pkg/frontend/clone.go: handleCloneDatabaseWithSource

Activity

  1. added this to the MO-202607 milestone on Jul 23, 2026
  2. added
    kind/bug
    on Jul 23, 2026
  3. added theissue type on Jul 23, 2026
  4. changed the title [-][Bug]: Database CLONE and DATA BRANCH silently skip FK tables whose names contain #[/-] [+][Bug]: Database CLONE and DATA BRANCH mishandle FK identifiers containing #[/+] on Jul 23, 2026
  5. changed the title [-][Bug]: Database CLONE and DATA BRANCH mishandle FK identifiers containing #[/-] [+][Bug]: Database CLONE and DATA BRANCH mishandle object identifiers containing #[/+] on Jul 23, 2026
  6. VioletQwQ-0 commented on Jul 29, 2026

    @VioletQwQ-0
    Collaborator

    PR #26230 merged as c6d7a1168c5c02e4578359dedaee27dd1dc1950e (reviewed head 9b1c5bcc20601780a9351eab5359b5e1fa3741ea). The corrected repeatable embedded regression, codec coverage, and required CI passed.

    QA handoff on main f6dab28046d70412cec132f0068840896852101c:

    • Run database CLONE and DATA BRANCH with # in database, table, and view identifiers.
    • Include FK tables and dependent views, then verify rows and destination-local FK metadata.
    • Repeat the scenario and verify cleanup does not leak state.

    Pass: identifiers round-trip losslessly and all rows/FKs/views survive both copy modes. Fail: truncated identity, omitted dependency, wrong FK target, or repeat-run cleanup failure.

  7. VioletQwQ-0 commented on Jul 29, 2026

    @VioletQwQ-0
    Collaborator

    PR #26230 merged as c6d7a1168c5c02e4578359dedaee27dd1dc1950e (reviewed head 9b1c5bcc20601780a9351eab5359b5e1fa3741ea). The corrected repeatable embedded regression, codec coverage, and required CI passed.

    QA handoff on main f6dab28046d70412cec132f0068840896852101c:

    • Run database CLONE and DATA BRANCH with # in database, table, and view identifiers.
    • Include FK tables and dependent views, then verify rows and destination-local FK metadata.
    • Repeat the scenario and verify cleanup does not leak state.

    Pass: identifiers round-trip losslessly and all rows/FKs/views survive both copy modes. Fail: truncated identity, omitted dependency, wrong FK target, or repeat-run cleanup failure.

  8. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成验证。

    测试版本:52ea9f874f492c368a20efef16b46ab8ced2de37
    测试拓扑:Proxy + 2 CN
    重复次数:3 轮

    测试过程:

    1. 在数据库名、表名、FK constraint 名和 view 名中加入 #。
    2. 创建父子 FK 表、普通视图和嵌套视图并写入数据。
    3. 分别执行数据库级 CLONE 和 Data Branch。
    4. 校验目标数据、视图定义、FK catalog 记录和 FK 执行效果。
    5. 清理后重复执行 3 轮。

    测试结果:

    • 所有包含 # 的标识符均能无损 round-trip,没有被截断。
    • 父子表数据、FK 元数据、普通视图和嵌套视图均正确复制。
    • 目标 FK 指向目标数据库中的父表,并能拒绝非法子记录。
    • 连续 3 轮均通过,清理后未发现状态泄漏。

    结论:该问题在上述最新 main commit 上验证通过。

  9. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在 main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c 补充完成黑盒和白盒验证。

    黑盒验证:

    • MOTR 用例:issue_26118_data_branch_hash_identifiers.test
    • 覆盖数据库、parent/child 表、约束和依赖 View 名称中的 #。
    • 同时执行数据库 Data Branch 和数据库 CLONE。
    • 检查 parent/child join 和 dependent View 精确结果。
    • 对两个目标库分别执行非法 FK 写入,均必须被目标库自己的 FK 拒绝。
    • 向一个目标库合法写入后检查源库和另一目标库不变,并验证清理后可重建。

    结果:该 case 27/27 条 SQL/断言通过;本轮 Data Branch 全集 13/13 case、280/280 条 SQL/断言通过。

    白盒验证:

    go test ./pkg/common/objectkey \
      -run '^(TestRoundTrip|TestLegacyKeyCompatibility)$' -count=1
    ok github.com/matrixorigin/matrixone/pkg/common/objectkey
    
    go test ./pkg/frontend -run '^TestObjectKeyRoundTrip$' -count=1
    ok github.com/matrixorigin/matrixone/pkg/frontend
    
    go test ./pkg/tests/issues \
      -run '^TestIssue26118DatabaseCopiesPreserveHashIdentifiers$' -count=1
    ok github.com/matrixorigin/matrixone/pkg/tests/issues
    

    Unhappy path / 依赖完整性:

    • 在 Data Branch 和 CLONE 两个目标库中分别插入不存在 parent 的 child,均必须由各自目标库的 FK 拒绝。
    • 对含 # 的 database/table/view dependency key 做 round-trip,防止 split 后静默丢失对象。
    • 向一个目标库合法写入后,源库和另一个目标库保持原行数,验证隔离性。
    • DROP 后重新创建同名目标,排除残留 dependency metadata 影响下一轮。

    结论:含 # object key 的编码/兼容解码以及数据库复制依赖路径均验证通过,黑盒回归已加入 motr#69。

  10. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Unhappy Path / 依赖完整性专项验证

    测试版本:main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c

    专项覆盖:

    1. database/table/view/dependency key 中均包含 #。
    2. 在 Data Branch 目标库插入不存在 parent 的 child,必须被 FK 拒绝。
    3. 在 CLONE 目标库执行相同非法写入,也必须被目标库自己的 FK 拒绝。
    4. 对含 # 的 object key 做 encode/decode round-trip,避免 split 后静默丢对象。
    5. 只向一个目标库合法写入,检查源库和另一目标库不变。
    6. DROP 目标后重新创建同名目标,检查 dependency metadata 不残留。

    结果:两种非法 FK 写入均被拒绝;失败后无部分数据写入;源/branch/clone 相互隔离;重建成功。专项用例 27/27 条 SQL/断言通过,object key 和 issue Go 测试返回 ok。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions