Skip to content

[Bug]: DATA BRANCH DIFF fails when LCA primary-key columns require quoting #26071

Closed

Description

@Ariznawlll

Describe the bug

DATA BRANCH DIFF fails for sibling branches when a primary-key column name requires identifier quoting.

The failure occurs while reconstructing rows deleted on one branch from the common ancestor. The generated join query quotes projected columns and table names, but inserts primary-key names into the VALUES alias list and join predicates without quoting.

Tested at commit 2eb7f523e5a10f352d3e258183ed8440510388de.

Steps to reproduce

Single-column primary key using a reserved word:

drop database if exists g4d_lca_names;
create database g4d_lca_names;
use g4d_lca_names;

create table base_single(`select` int primary key, payload varchar(20));
insert into base_single values (1,'one'),(2,'two');
data branch create table left_single from base_single;
data branch create table right_single from base_single;

delete from left_single where `select`=1;
update right_single set payload='two-right' where `select`=2;

data branch diff left_single against right_single;

Composite primary key using a reserved word and punctuation:

create table base_composite(
  `left` int,
  `a-b` varchar(10),
  payload varchar(20),
  primary key(`left`,`a-b`)
);
insert into base_composite values (1,'x','one'),(2,'y','two');
data branch create table left_composite from base_composite;
data branch create table right_composite from base_composite;

delete from left_composite where `left`=1 and `a-b`='x';
update right_composite set payload='two-right' where `left`=2 and `a-b`='y';

data branch diff left_composite against right_composite;

The same generated query is shared by MERGE and PICK when applying a deletion from a sibling branch:

create table apply_base(`select` int primary key, payload varchar(20));
insert into apply_base values (1,'one'),(2,'two');
data branch create table apply_src from apply_base;
data branch create table apply_dst from apply_base;
delete from apply_src where `select`=1;

data branch merge apply_src into apply_dst;
-- or, on a freshly recreated pair:
data branch pick apply_src into apply_dst keys(1);

Actual behavior

The single-key case fails near this generated fragment:

... as pks(__idx_,select) on lca.select = cast(pks.select as INT) ...

The composite-key case fails near:

... as pks(__idx_,left,a-b) on lca.left = ... AND lca.a-b = ...

DIFF, MERGE, and PICK return a SQL syntax error instead of completing. MERGE and PICK leave the destination table unchanged after the failure.

Expected behavior

Every generated reference to a user-defined primary-key column should use identifier quoting. Both DIFF commands should return their rows normally, regardless of valid primary-key names.

Reproducibility

Reproduced in 3/3 runs for both the single-column and composite-primary-key DIFF cases, and in 3/3 runs for the MERGE and PICK variants.

Code context

handleDelsOnLCA in pkg/frontend/data_branch_hashdiff.go builds the LCA lookup query. It uses raw pkNames in:

  • the VALUES alias list;
  • lca.<pk> expressions;
  • pks.<pk> expressions.

The projected LCA columns in the same query already use quoteIdentifierForSQL, but these primary-key references do not.

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. added
    severity/s0
    and removed on Jul 27, 2026
  5. added a commit that references this issue on Jul 27, 2026
    9c1121e
  6. added and removed
    severity/s0
    on Jul 29, 2026
  7. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

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

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

    测试过程:

    1. 创建主键列名需要引用的表,覆盖保留字、标点字符和空格。
    2. 分别覆盖单列主键和复合主键。
    3. 从同一 LCA 创建 source、base、pick destination 和 merge destination。
    4. 在 source 执行 UPDATE、INSERT、DELETE。
    5. 执行 sibling DIFF、按 key 选择性 PICK 和完整 MERGE。
    6. 回读 key/value,验证变更集合及最终数据。

    测试结果:

    • DIFF 能正确生成并执行带引用主键列名的 LCA probe SQL。
    • 单列和复合主键的变更均能正确识别。
    • PICK 只同步指定 key。
    • MERGE 后目标表与 source 的更新、插入、删除结果一致。
    • 连续 3 轮全部通过,未出现 malformed SQL 或 internal error。

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

  8. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

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

    黑盒验证:

    • MOTR 用例:issue_26071_data_branch_quoted_primary_keys.test
    • 先使用普通单列主键验证正常控制组。
    • 再覆盖需要引用的单列主键、保留字主键以及带标点的复合主键。
    • 对 sibling branch 执行 DIFF,并分别执行 PICK、MERGE;PICK 同时选择更新键和删除键。
    • 检查删除被正确应用、更新值正确、未选择的新增键不会写入、无关行保持不变,MERGE 后目标行集与源 branch 完全一致。

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

    白盒验证:

    go test ./pkg/frontend \
      -run '^TestHandleDelsOnLCA_SQLPaths$' -count=1
    ok github.com/matrixorigin/matrixone/pkg/frontend
    

    该 UT 检查 LCA 删除行查询生成路径,包含需要引用的主键列,确认生成的 VALUES alias 和 join predicate 均使用合法标识符引用。

    Unhappy path / 边界验证:

    • sibling branch 一侧删除 LCA 中的行,强制进入历史行重建路径,而不只验证普通新增/更新。
    • quoted single PK 和 quoted composite PK 均覆盖删除应用。
    • PICK 同时处理被更新键和被删除键,并确认未选择的 INSERT 不会越界写入。
    • 目标表无关行保持原值,避免“语句成功但多改/漏改数据”。

    结论:原 issue 涉及的 quoted PK DIFF/PICK/MERGE 路径在上述提交验证通过,黑盒回归已加入 motr#69。

  9. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Unhappy Path 专项验证

    测试版本:main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c

    本轮专门覆盖正常新增/更新之外的 LCA 删除与选择性应用路径:

    1. sibling branch 一侧删除 LCA 中的行,强制进入历史行重建逻辑。
    2. 分别使用需要引用的单列主键、保留字主键和带标点的复合主键。
    3. PICK 同时选择一个 UPDATE key 和一个 DELETE key。
    4. 同时制造一个未被选择的 INSERT key,检查它不会越界写入目标。
    5. MERGE 后核对完整行集,检查无关行未被误改。

    结果:

    • deleted key 正确从目标删除;
    • selected update 正确应用;
    • unselected insert 未写入;
    • quoted/composite PK 未再触发生成 SQL 的语法错误;
    • 目标无关行保持不变。

    专项用例 36/36 条 SQL/断言通过。回归见 motr#69。

  10. added 3 commits that reference this issue on Sep 17, 2026
    1951ddf
    4ad7cbf
    77aac3d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions