Skip to content

fix: allow modify of generated column sources - #28770

Merged
XuPeng-SH merged 13 commits into
mainfrom
codex/issue-28242-modify-dependent-generated
Sep 14, 2026
Merged

XuPeng-SH merged 13 commits into
mainfrom
codex/issue-28242-modify-dependent-generated

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

Fixes #28242

What this PR does / why we need it:

Allow COPY-based ALTER TABLE ... MODIFY COLUMN and same-name CHANGE COLUMN when an existing generated column depends on the modified column, while preserving every foreign-key endpoint whose value or comparison semantics can change.

The COPY path rebuilds the generated expression against the final schema and recomputes generated values while the temporary relation has no foreign-key metadata. The original implementation protected only the stored-generated dependency closure; a direct source column used by an outgoing, incoming, self-referencing, or composite foreign key could still be converted and leave the replacement relation inconsistent with retained child rows.

AlterCopyAffectedForeignKeyColumns now maps original column IDs to the final COPY schema and takes the union of:

  • direct source endpoints whose conversion is not proven value-preserving; and
  • the transitive closure of stored-generated dependents that are recomputed by COPY.

The direct classifier is conservative for cross-type/narrowing conversions, scale, charset, enum, padding, binary/bit, and unknown legacy widths. It keeps proven positive-width decimal, CHAR, VARCHAR, and VARBINARY capacity widening legal when the remaining comparison metadata is unchanged. Type.Table is provenance metadata rather than value semantics and is intentionally not treated as a value change. Index invalidation remains a safe superset of the FK guard, and no new DML-path wait or retained cache was introduced.

Planner validation covers outgoing, incoming, self-referencing (both durable self markers), and every component of composite keys. The compiler rechecks lock-held live child metadata immediately before creating the replacement relation, including legacy tables without generated-column dependency metadata. Virtual-only generated definitions do not force the stored-dependency metadata walker. Ordinary-to-stored transitions on legacy ordinary tables with no original generated-column metadata retain the source endpoint in the affected set and skip the unavailable dependency graph; stored-to-virtual transitions still use the closure. Existing rollback, generated-expression binding, and index rebuild protections remain in place.

Test plan

Focused tests on 10.222.1.55 (source files synchronized to this revision):

  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s -run '^(TestAlterCopyAffectedForeignKeyColumns|TestAlterForeignKeyValidationRejectsScaleChanges)' ./pkg/sql/plan — PASS.
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=120s -run 'TestAlterCopyAffectedForeignKeyColumns|TestAlterForeignKeyValidationRejectsScaleChanges' ./pkg/sql/plan — PASS; all intended tests executed.
  • Final virtual-only metadata guard: .agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=120s -run 'TestAlterCopyAffectedForeignKeyColumns' ./pkg/sql/plan — PASS, including TestAlterCopyAffectedForeignKeyColumnsSkipsVirtualOnlyDependencyMetadata.
  • Final ordinary-to-stored metadata guard: the same focused plan suite also passes TestAlterCopyAffectedForeignKeyColumnsSkipsMetadataForNewStoredColumn, retaining the source endpoint when a legacy ordinary table has no original generated-column metadata.
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s -run '^TestCheckAlterCopy' ./pkg/sql/compile — PASS.
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -v -count=1 -timeout=120s -run 'TestCheckAlterCopy' ./pkg/sql/compile — PASS.
  • Native TMPDIR=/mnt/nvme/xupeng/tmp-pr28770 make -j12 build — PASS.
  • Fresh-service test/distributed/cases/ddl/generated_column.sql BVT — TOTAL 325, SUCCESS 325, FAILED 0, IGNORED 0, ABNORMAL 0; direct decimal FK changes are rejected across outgoing/incoming/self cases, and legal CHAR widening preserves HEX=65646765, LENGTH=4, CHAR_LENGTH=4.
  • gofmt and git diff --check — PASS.

The focused tests and BVT cover the new regression paths plus existing generated-column/index cases. The final metadata-gate-only change is covered by the current plan normal/race suites. Full CI is intentionally not awaited here; GitHub CI should run on the rebased PR head.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify mergify Bot added the kind/bug Something isn't working label Sep 12, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Sep 12, 2026
@XuPeng-SH
XuPeng-SH force-pushed the codex/issue-28242-modify-dependent-generated branch from 43e29fa to 6e63dac Compare September 12, 2026 08:53
@XuPeng-SH
XuPeng-SH force-pushed the codex/issue-28242-modify-dependent-generated branch from 6e63dac to 7ca82a1 Compare September 12, 2026 10:53

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 37e3b4ac89bbb25f1156c0cc7c419e3a4e60268b. The previous P1 foreign-key integrity issue is resolved: COPY ALTER now snapshots live FK constraints, computes the stored-generated dependency closure for source type changes, and rejects conversions when an affected generated key is referenced by outgoing, incoming, or self-referential FKs before creating the temporary copy. The new distributed cases cover parent-side, child-side, and both self-reference directions. I found no new blocker in generated-dependent index rebuilding, primary-key payload invalidation, column lineage, or FK reconciliation.

Exact-head CI run 34726958194 passed UT, SCA, UT coverage, shared build, PROXY and PESSIMISTIC multi-CN BVT, coverage, and CI Required. Upgrade compatibility jobs were skipped. COMMENT only because this is my PR.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: same-type generated-expression changes bypass the FK guard

AlterCopyAffectedStoredGeneratedColumns seeds the dependency closure only when a source column's type changes (build_alter_table.go:688). This PR now allows modifying a stored generated column's expression, so the expression itself can change dependent stored generated values while every column type remains identical. The closure is then empty and the FK validation returns without checking the recomputed keys.

For example, with g1 INT AS (a + 1) STORED, g2 INT AS (g1 * 10) STORED, and FOREIGN KEY (g2) REFERENCES p(k), a row with a=1 and parent key 20 becomes g2=30 after MODIFY COLUMN g1 INT AS (a + 2) STORED. COPY recomputes the generated values and restores the FK metadata without validating copied rows, leaving an orphan. Please seed the closure for generated-expression changes as well as type conversions (including affected incoming/self-referencing FK columns).

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 medium deep re-review at exact head df08a3efd5ec3428a752e77de30adbf5c3d467e3 found one verified P1. Recommendation: do not pass until fixed. Submitted as COMMENT because this is a self-authored PR.

[P1] Protect value-changing source FK columns, not only generated dependents.

The change removes the old rejection that prevented modifying a column with stored generated dependents, but the new FK guard only returns affected generated columns. A source column that is itself an FK endpoint can still change its stored value without being included in that set.

Repro:

SET foreign_key_checks = 1;
CREATE TABLE p (id INT PRIMARY KEY, a DECIMAL(10,1), g INT GENERATED ALWAYS AS (a * 10) STORED, UNIQUE KEY uk_a(a));
CREATE TABLE c (id INT PRIMARY KEY, pa DECIMAL(10,1), CONSTRAINT fk_pa FOREIGN KEY(pa) REFERENCES p(a));
INSERT INTO p(id,a) VALUES (1,1.1);
INSERT INTO c VALUES (1,1.1);
ALTER TABLE p MODIFY COLUMN a DECIMAL(10,0);
SELECT c.id FROM c LEFT JOIN p ON c.pa=p.a WHERE p.id IS NULL;

checkColumnForeignkeyConstraint returns early when type ID, width, and auto-increment match, ignoring scale. The generated-column closure seeds a but only returns generated columns such as g, so the incoming FK on p.a is missed. COPY converts the parent value 1.1 to 1; FK reconciliation rewrites metadata but does not scan existing child rows. The child remains 1.1, leaving an orphan reference.

Please protect all value-changing source FK endpoints (including scale changes), or include affected original source IDs in the live FK validation alongside generated dependents. Add public parent-side, child-side, and self-reference scale-change coverage plus an unrelated-column control.

The earlier generated-expression FK findings are otherwise closed. Exact-head CI run 34769695643 is green, and its PROXY artifact reports ddl/generated_column.sql 300/300 in 3.573s; neither covers this direct-source scale counterexample. The change does not introduce a credible major racing-UT slowdown mechanism.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 medium deep review of exact head c0a0a03946b6c70d248fefac9d0ac7ba04d7e763: no blocker remains.

The previous direct-source FK P1 is closed: value-changing endpoint checks now include scale and run against the live source/child FK metadata before the temporary COPY relation is created. The generated-dependent closure also covers stored generated endpoints, changed generated expressions, both directions of self-references, and primary-key/index rebuild impact. The new tests cover the scale-change orphan counterexample, unrelated safe widening, generated parent/child/self FKs, rollback, and affected versus unaffected indexes. I found no remaining concrete correctness path to an orphaned FK or stale generated index. Added planner tests are bounded fixtures and distributed cases use small row counts; no mechanism for a major racing-UT slowdown is apparent.

Exact-head CI run 34781421644 passed UT, SCA, coverage, shared build, and required BVTs. This is a self-authored PR, so this is a COMMENT review rather than an approval.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope clarification for this review: “no remaining concrete path” refers to regressions introduced by this PR's newly enabled generated-column MODIFY/COPY paths and the FK dependency protections it adds. GPT-6 medium also checked the AUTO_INCREMENT edge case: generated-expression rebinding rejects auto-increment sources before COPY, while a direct nullable outgoing-FK AUTO_INCREMENT conversion is a pre-existing behavior outside the change enabled here. It is not counted as a blocker for this PR.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: generated-column dependencies block MySQL-supported base-column MODIFY operations

2 participants