[Bug]: COPY ALTER DECIMAL parent key to UNSIGNED leaves persistent incoming-FK orphans

オープン
#33,355 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@matrix-meow がすでに取り組んでいます。

2026年11月27日 から。

評価

この issue はまだ評価されていません。

説明

kind/bug needs-triage

Description

Tested on MatrixOne main c0cef5c9cce2164fedd201c188bfffe4d2d09a96 (version() = 8.0.30-MatrixOne-v1.3.0, git_version() = c0cef5c), single CN.

In non-strict mode, COPY ALTER can convert an incoming-FK-referenced DECIMAL parent key to UNSIGNED, keep the FK declared and enforced, yet leave existing child rows permanently orphaned.

The non-strict negative-to-zero conversion itself is not the bug. The integrity defect is that COPY rewrites the parent key without rejecting the operation or revalidating the existing incoming FK rows.

Minimal reproduction

CREATE TABLE p(id DECIMAL(10,2) PRIMARY KEY);
CREATE TABLE c(
  pid DECIMAL(10,2),
  CONSTRAINT fk_c_p FOREIGN KEY(pid) REFERENCES p(id)
);
INSERT INTO p VALUES(-1.00);
INSERT INTO c VALUES(-1.00);

SET SESSION foreign_key_checks=1;
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';

ALTER TABLE p ALGORITHM=COPY,MODIFY id DECIMAL(10,2) UNSIGNED;
-- Actual: succeeds with Warning 1264
-- Expected: reject the referenced-parent conversion and atomically preserve the valid FK

SELECT id FROM p;
-- Actual: 0.00

SELECT pid FROM c;
-- Actual: -1.00

SELECT c.pid
FROM c LEFT JOIN p ON p.id=c.pid
WHERE p.id IS NULL;
-- Actual: -1.00 (an orphaned child row)

SHOW CREATE TABLE c;
-- The same FOREIGN KEY(pid) REFERENCES p(id) is still declared.

A single parent row is intentional: using two negative parent keys can fail earlier with PK duplicate error 1062 and mask this incoming-FK path.

The orphan is persistent and visible from fresh connections. Inserting a child that matches the new parent value succeeds, inserting another unmatched child still fails with 1452, and the old orphan remains. This is therefore not disabled foreign_key_checks or a client/session-cache artifact.

Controls and affected variants

These controls establish the expected boundary:

  • Strict mode rejects the same ALTER with 1264 and preserves the signed type, both rows, the valid join, and the FK.
  • A same-signed-type COPY succeeds and preserves the valid join.
  • A parent table with no incoming reference performs the normal non-strict truncation successfully.
  • Omitting ALGORITHM selects COPY in this shape and reproduces the same orphan; explicit INPLACE is rejected as requiring COPY and preserves the original valid state.
  • Colliding negative values in a base PK or single-column UNIQUE are rejected atomically with 1062; this report does not allege duplicate-key publication.
  • Child-side UNSIGNED conversion and self-FK parent conversion are rejected atomically with 1452. The defect reported here is specifically the external table's incoming-parent path.

The same root cause was also reproduced with:

  • a referenced UNIQUE parent key;
  • a composite parent key;
  • a cross-database FK;
  • a STORED generated parent key whose dependency source column is changed to UNSIGNED.

For the generated case, the source and generated parent values are both correctly recomputed from -1.00 to 0.00, while the child remains -1.00. Thus generated recomputation works; the missing incoming-FK validation is the defect.

Root cause

The COPY FK affected-column analysis omits numeric signedness:

  1. checkColumnForeignkeyConstraint in pkg/sql/plan/build_alter_modify_column.go returns early when ID, width, scale, enum/collation/padding/auto-increment attributes match, without comparing NumericAttributes.
  2. alterCopyForeignKeyColumnMayChangeValues in pkg/sql/plan/build_alter_table.go, used for direct FK endpoints, likewise does not compare NumericAttributes.
  3. alterCopyColumnTypeMayChangeValues, which seeds the STORED-generated dependency closure, also omits that attribute.

Consequently DECIMAL signed-to-UNSIGNED with unchanged ID/width/scale produces an empty affected set. The live FK guard in pkg/sql/compile/alter.go performs outgoing/self/incoming validation from that set, so the external incoming reference never enters a reject-or-revalidate path. The replacement parent rows are populated and the FK binding is restored without checking the pre-existing child rows.

Validation

The direct-parent reproduction, strict/signed/no-reference controls, PK/UNIQUE collision controls, child/self-FK controls, fresh-connection reads, and continued FK enforcement passed 30/30 probe assertions in each of two independent fresh-database runs. Referenced UNIQUE, composite, and cross-database variants produced the same -1.00 orphan.

The STORED-generated variant and its controls independently passed 28/28 assertions in each of two runs. Earlier probe outputs with SHOW COLUMNS or SQL-alias oracle mistakes are not used as evidence.

Duplicate check and related work

Open and closed issue/PR searches covered DECIMAL UNSIGNED, incoming FK, orphan rows, generated dependencies, NumericAttributes, and the affected guard functions. No matching issue or open fix was found.

Merged PR #31739 introduced NumericAttributes and the strict/non-strict DECIMAL conversion used here, but its tests do not include FK validation. Merged PR #28770 previously added incoming-parent scale protection; the corresponding guards still omit signedness. Open #30940 covers UNSIGNED/ZEROFILL metadata and value-domain behavior, not incoming-FK orphans. #28242 concerns blocking safe generated-column MODIFY, and #30256/#30261/#32056/#33316-#33318 cover different ALTER paths.

主要言語
Go
スター
1.9k
フォーク
312
平均マージ
22時間 59分
マージ済み PR(30日)
736

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

matrixorigin/matrixone のほかの issue

matrixorigin/matrixone の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。