Skip to content

[Bug]: LOAD DATA writes NULL to a stored generated foreign-key column #28054

Closed as not planned

Description

@Ariznawlll

Description

LOAD DATA INLINE with an explicit list of writable base columns stores NULL in a stored generated column when that generated column is a child foreign key. With a JSON document as the source expression, the document is stored correctly but its materialized foreign-key value is not recomputed. The resulting NULL bypasses the foreign-key check and can leave a dangling logical reference after the parent row is deleted.

Environment

  • Branch: main
  • Commit: 95d04586eb80864499d0fde27aeb912b463ae4cd
  • Deployment: local single-CN MatrixOne

Steps to reproduce

CREATE DATABASE load_generated_fk_lifecycle;
USE load_generated_fk_lifecycle;

CREATE TABLE parent(id INT PRIMARY KEY);
INSERT INTO parent VALUES (1);

CREATE TABLE child(
  id INT PRIMARY KEY,
  doc JSON,
  parent_id INT GENERATED ALWAYS AS
    (CAST(doc ->> '$.parent_id' AS SIGNED)) STORED,
  FOREIGN KEY(parent_id) REFERENCES parent(id)
);

LOAD DATA INLINE FORMAT='jsonline',
  DATA='{"id":10,"doc":{"parent_id":1,"label":"loaded"}}',
  jsontype='object'
INTO TABLE child (id, doc);

SELECT id, doc ->> '$.parent_id', parent_id FROM child;
DELETE FROM parent WHERE id = 1;
SELECT id, doc ->> '$.parent_id', parent_id FROM child;

UPDATE child
SET doc = JSON_SET(doc, '$.label', 'updated')
WHERE id = 10;

Actual behavior

The load succeeds, but the first read is:

10 | 1 | NULL

DELETE FROM parent WHERE id = 1 succeeds, leaving the child document logically referring to the deleted parent while its stored FK key remains NULL. The later unrelated JSON update returns ERROR 1452 and leaves the row unchanged:

10 | 1 | NULL | loaded

Without an explicit column list, the same JSONLine load fails before writing any row with the table column is larger than input data column.

Expected behavior

The generated value must be computed as 1 during the load, so the foreign-key check is enforced and deletion of parent 1 is rejected while the child exists. The no-column-list form should also load the writable columns without treating generated/internal columns as input fields.

Stability and controls

  • Reproducer: 3/3 runs produced doc.parent_id = 1 and stored parent_id = NULL; each allowed the parent delete; each later update returned 1452 and preserved the original child row.
  • Direct INSERT control: INSERT INTO child(id, doc) VALUES (9, '{"parent_id":1}') stores parent_id = 1.
  • Generated-column-only control: the same explicit JSONLine load into the same table shape without the FK stores parent_id = 1.
  • Plain-FK control: JSONLine load into a table with only ordinary parent_id FK column succeeds.
  • Atomicity: the post-delete JSON update is rejected and does not change the document or stored generated value.

Evidence

The issue is reproducible using only the SQL above on the recorded main commit.

Code analysis

pkg/sql/plan/bind_load.go:91-127 excludes explicitly named generated columns from load input, as expected. The failure occurs only when that omitted generated column is also present in TableDef.Fkeys; pkg/sql/plan/build_load.go:668-673 takes a dedicated FK planning path. The exact projection/recompute omission in that path is not yet confirmed.

Regression coverage

Add a deterministic LOAD DATA regression for stored generated child-FK columns covering JSONLine with and without an explicit column list, valid and invalid parent references, and post-load parent deletion.

Activity

  1. added this to the MO-202609 milestone on Sep 3, 2026
  2. added
    kind/bug
    on Sep 3, 2026
  3. added theissue type on Sep 3, 2026
  4. changed the title [-][Bug]: LOAD DATA writes NULL to a JSON-backed generated foreign-key column[/-] [+][Bug]: LOAD DATA writes NULL to a stored generated foreign-key column[/+] on Sep 3, 2026
  5. Ariznawlll commented on Sep 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    范围补充

    该问题不只发生在 JSON 表达式。最新 main(95d04586eb80864499d0fde27aeb912b463ae4cd)上,以下数值生成列场景连续 3 轮均复现:

    CREATE TABLE parent(id INT PRIMARY KEY);
    INSERT INTO parent VALUES (1);
    
    CREATE TABLE child(
      id INT PRIMARY KEY,
      base_id INT,
      parent_id INT GENERATED ALWAYS AS (base_id) STORED,
      FOREIGN KEY(parent_id) REFERENCES parent(id)
    );
    
    LOAD DATA INLINE FORMAT='csv', DATA='10,1'
    INTO TABLE child FIELDS TERMINATED BY ',' (id, base_id);
    
    SELECT id, base_id, parent_id FROM child;

    每轮结果均为 10 | 1 | NULL。因此根因范围是 LOAD DATA 对显式基础列列表与“存储生成的子外键列”的组合处理,不依赖 JSON 或 JSONLine;原 issue 中的 JSON 用例是该通用缺陷的一个可见业务场景。

  6. Ariznawlll commented on Oct 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一个更广的触发条件:不要求生成列本身是外键。表中存在一个与生成列表达式无关的普通外键,也会触发 LOAD 后生成列变成 NULL。

    2026-10-20,在 main 的 3829ee49c7c118f2f3b0ebf7ed3acbd52483d870,隔离的单 CN 集群验证。以下是实际导出再导入的最小场景;文件路径请换成服务端可写且尚不存在的路径:

    CREATE DATABASE load_generated_unrelated_fk;
    USE load_generated_unrelated_fk;
    CREATE TABLE p(id INT PRIMARY KEY);
    INSERT INTO p VALUES(10);
    
    CREATE TABLE src(
      id INT PRIMARY KEY, pid INT, a INT,
      g INT AS(a*2) STORED,
      v INT AS(g+1) VIRTUAL,
      INDEX ig(g)
    );
    INSERT INTO src(id,pid,a) VALUES(1,10,7),(2,10,NULL);
    
    SELECT id,pid,a FROM src ORDER BY id
    INTO OUTFILE '/tmp/load_generated_unrelated_fk.csv' HEADER 'false';
    
    CREATE TABLE dst(
      id INT PRIMARY KEY, pid INT, a INT,
      g INT AS(a*2) STORED,
      v INT AS(g+1) VIRTUAL,
      INDEX ig(g),
      FOREIGN KEY(pid) REFERENCES p(id)
    );
    LOAD DATA INFILE '/tmp/load_generated_unrelated_fk.csv'
    INTO TABLE dst FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
    (id,pid,a);
    
    SELECT id,pid,a,g,v FROM dst ORDER BY id;
    SELECT id FROM dst FORCE INDEX(ig) WHERE g=14;

    LOAD 成功,行数和基础字段正确,但实际结果是:

    id  pid  a     g     v
    1   10   7     NULL  NULL
    2   10   NULL  NULL  NULL
    

    第一行应为 1,10,7,14,15;后面的索引过滤实际返回空集,应返回 id=1。这里外键列是 pid,生成列只依赖 a,二者没有表达式依赖关系。

    扩展验证结果:

    场景 结果
    原始 CSV / 将同一导出文件压缩为 GZIP,再 LOAD 到有外键的表 各 3/3:生成列为 NULL
    去掉外键,保留相同生成列和索引 CSV、GZIP 各 3/3:g=14,v=15,索引过滤返回 1
    MO 导出 Parquet,再导入有普通外键的表 独立的 STORED g AS(a*2) 和 VIRTUAL g AS(a*2) 各 3/3 为 NULL;无外键对照均正确
    40 行实际 CSV 分片导出为 6 个文件,分别压缩为 GZIP/BZIP2/ZSTD/LZ4 后恢复 系统租户服务端 glob、普通租户 LOCAL 各 3/3:40 行均在,但应非 NULL 的 32 个生成值全部丢失
    普通 INSERT 写入同一目标表 生成值正确;例如 a=8 得到 g=16,v=17

    相关失败和后续操作也检查了:普通外键无效值的 INSERT 被拒绝,拒绝前后全部行保持不变;分片恢复表上显式修改生成列被拒绝、删除被引用父行被拒绝;随后正常更新基础列能够重算生成值,索引查询也恢复该行。它们不能消除 LOAD 已写入错误值的问题。

    白盒和计划对照:无外键 LOAD 走 Multi Update 及多层 Project;有外键时走 PreInsert → Sink / Insert,额外做外键 LEFT JOIN 断言。pkg/sql/plan/dml_context.go 中 FK 导致现代 DML 路径回退,build_load.go 的基础列补齐与该旧路径生成值重算值得一起检查。这是定位线索,不是已经验证过的修复结论。

    这是本 issue 的同类范围扩展,不另提重复问题。本轮只保存复现 SQL、精确输出和执行计划,未新增仓库测试或 PR。

    测试不通过。

  7. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    This report is now tracked together with #30500. The canonical issue retains explicit acceptance cases for the behavior described here. This is an administrative duplicate/consolidation closure only; the underlying defect is not claimed fixed. The original description and discussion remain available on this issue.

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