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

Open
#33,355 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@matrix-meow is already working on this.

Since Nov 27, 2026.

Assessment

This issue has not been assessed yet.

Description

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.

Dominant language
Go
Stars
1.9k
Forks
312
Avg merge
22h 59m
Merged PRs (30d)
736

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matrixorigin/matrixone

All issues in matrixorigin/matrixone

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.