Repository navigation
Description
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 = 1and storedparent_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}')storesparent_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_idFK 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
[-][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[/+]Ariznawlll commentedon Sep 3, 2026
范围补充
该问题不只发生在 JSON 表达式。最新
main(95d04586eb80864499d0fde27aeb912b463ae4cd)上,以下数值生成列场景连续 3 轮均复现:每轮结果均为
10 | 1 | NULL。因此根因范围是LOAD DATA对显式基础列列表与“存储生成的子外键列”的组合处理,不依赖 JSON 或 JSONLine;原 issue 中的 JSON 用例是该通用缺陷的一个可见业务场景。Ariznawlll commentedon Oct 20, 2026
补充一个更广的触发条件:不要求生成列本身是外键。表中存在一个与生成列表达式无关的普通外键,也会触发 LOAD 后生成列变成 NULL。
2026-10-20,在
main的3829ee49c7c118f2f3b0ebf7ed3acbd52483d870,隔离的单 CN 集群验证。以下是实际导出再导入的最小场景;文件路径请换成服务端可写且尚不存在的路径:LOAD 成功,行数和基础字段正确,但实际结果是:
第一行应为
1,10,7,14,15;后面的索引过滤实际返回空集,应返回id=1。这里外键列是pid,生成列只依赖a,二者没有表达式依赖关系。扩展验证结果:
g=14,v=15,索引过滤返回 1g AS(a*2)和 VIRTUALg AS(a*2)各 3/3 为 NULL;无外键对照均正确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。
测试不通过。
XuPeng-SH commentedon Oct 22, 2026
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.