Skip to content

fix fk meta - #26469

Open
daviszhen wants to merge 30 commits into
matrixorigin:mainfrom
daviszhen:0730-fix-fk-meta
Open

fix fk meta#26469
daviszhen wants to merge 30 commits into
matrixorigin:mainfrom
daviszhen:0730-fix-fk-meta

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25388

What this PR does / why we need it:

  • information_schema.KEY_COLUMN_USAGE 改为从 mo_foreign_keys 提供 FK 本地列到引用列的映射及复合键顺序。
  • information_schema.REFERENTIAL_CONSTRAINTS 正确展示默认 NO ACTION、显式 RESTRICT,并按父表 ID 关联索引以避免同名列导致重复。
  • 新建 FK 持久化默认动作为 NO_ACTION,显式动作保持原值。
  • v4_0_6 租户升级解析历史 rel_createsql,回填旧复合 FK 的列序和默认/显式动作语义。
  • 增加 planner、升级、sysview UT,以及 FK BVT 覆盖默认/显式动作、复合 FK 与无关索引场景。

@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 →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes for two blocking legacy-upgrade gaps at f8c66fd.\n\n1. pkg/bootstrap/versions/v4_0_6/upgrade.go:129: Foreign keys added with ALTER TABLE are silently left unconverted because mo_tables.rel_createsql is the original CREATE snapshot and ALTER does not rewrite it. Parsing that SQL yields no tree.ForeignKey, legacyForeignKeyMetadataUpdates returns an empty update list, and the tenant upgrade reports success while constraint_id stays 0 and the action stays RESTRICT. The new KEY_COLUMN_USAGE / REFERENTIAL_CONSTRAINTS views therefore remain wrong for this supported path.\n\n2. pkg/bootstrap/versions/v4_0_6/upgrade.go:154: A valid CREATE TABLE with an unnamed FK can abort the whole tenant upgrade. createTableSQLForCatalog persists the original one-statement SQL before adjustConstraintName generates the catalog UUID, so parsing the persisted SQL produces an empty ConstraintSymbol; this branch treats it as an error. The new test at upgrade_test.go:224 currently codifies this valid syntax as an invalid persisted definition.\n\nPlease backfill from authoritative catalog state, or otherwise reconcile missing/unnamed AST constraints with mo_foreign_keys rows, and add upgrade tests for both ALTER-added and unnamed legacy FKs.\n\nLocal validation otherwise passed: all 15 added/modified tests repeated independently under race 100x, all three owning packages under full race, and build/vet/non-race for v4_0_6, bootstrap, plan, compile, sysview, and frontend.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Requesting changes for two blocking legacy-upgrade gaps at f8c66fd.\n\n1. pkg/bootstrap/versions/v4_0_6/upgrade.go:129: Foreign keys added with ALTER TABLE are silently left unconverted because mo_tables.rel_createsql is the original CREATE snapshot and ALTER does not rewrite it. Parsing that SQL yields no tree.ForeignKey, legacyForeignKeyMetadataUpdates returns an empty update list, and the tenant upgrade reports success while constraint_id stays 0 and the action stays RESTRICT. The new KEY_COLUMN_USAGE / REFERENTIAL_CONSTRAINTS views therefore remain wrong for this supported path.\n\n2. pkg/bootstrap/versions/v4_0_6/upgrade.go:154: A valid CREATE TABLE with an unnamed FK can abort the whole tenant upgrade. createTableSQLForCatalog persists the original one-statement SQL before adjustConstraintName generates the catalog UUID, so parsing the persisted SQL produces an empty ConstraintSymbol; this branch treats it as an error. The new test at upgrade_test.go:224 currently codifies this valid syntax as an invalid persisted definition.\n\nPlease backfill from authoritative catalog state, or otherwise reconcile missing/unnamed AST constraints with mo_foreign_keys rows, and add upgrade tests for both ALTER-added and unnamed legacy FKs.\n\nLocal validation otherwise passed: all 15 added/modified tests repeated independently under race 100x, all three owning packages under full race, and build/vet/non-race for v4_0_6, bootstrap, plan, compile, sysview, and frontend.

  • 不再依赖过时的 mo_tables.rel_createsql,改用 SHOW CREATE TABLE 获取当前外键定义。
  • 以 mo_foreign_keys 为权威数据源,回填 ALTER 新增外键的 constraint_id、引用动作等元数据。
  • 支持未命名外键,通过子表列、父表及引用列签名匹配系统生成的约束名。
  • 保留复合外键的真实列顺序;无法唯一匹配时直接报错回滚,避免静默写错。
  • 使用统一 SQL 转义工具处理约束名、表名和列名,修复特殊字符风险。
  • 增加 ALTER 外键、未命名外键、复合外键、动作转换及特殊字符等 UT/BVT 覆盖。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One blocking upgrade regression remains on exact head a9e70ae. The ALTER-added and unnamed-FK gaps from my previous review are now covered, but explicit legacy RESTRICT actions are no longer preserved.

Before this update, the migration parsed the original CREATE definition and kept an explicit ON DELETE/UPDATE RESTRICT distinct from an omitted action. The new catalog/SHOW CREATE path cannot recover that distinction because both legacy catalog rows and SHOW CREATE render RESTRICT. legacyCatalogReferenceActionName then unconditionally rewrites every RESTRICT to NO_ACTION. The changed TestLegacyForeignKeyMetadataUpdatesPreserveOrderAndActions confirms the regression: its fk_restrict expectation was changed from RESTRICT to NO_ACTION. A 4.0.5 table created with an explicit RESTRICT will therefore report NO ACTION after upgrade, contradicting the PR contract and MySQL metadata.

Please combine the authoritative current-catalog reconciliation with the historical definition where it is available: preserve explicit/default action syntax for CREATE-time constraints, while using catalog plus SHOW CREATE as the fallback needed for ALTER-added and unnamed constraints. Add upgrade controls for legacy explicit RESTRICT versus omitted action, including the ALTER path where the distinction may be irrecoverable and must have a documented conservative policy.

Local validation: the three owning packages v4_0_6, plan, and sysview pass. This is a semantic migration issue, not a compile/test failure.

Comment thread pkg/bootstrap/versions/v4_0_6/upgrade.go Outdated
Comment thread pkg/util/sysview/predefined.go Outdated

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes on exact head a9e70ae after full diff and upgrade-closure review.

Two blocking metadata defects remain:

  1. The legacy migration rewrites every catalog RESTRICT action to NO_ACTION. For CREATE-time constraints, mo_tables.rel_createsql still preserves whether RESTRICT was explicit or omitted, so a 4.0.5 table with explicit ON DELETE/UPDATE RESTRICT is misreported after upgrade. Preserve the historical syntax when it can be reconciled, and use catalog plus SHOW CREATE only as the fallback for ALTER-added/unnamed constraints.

  2. REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME is derived as min(idx.type) from every index row sharing a referenced column. With parent(id PRIMARY KEY, KEY secondary(id)), the join contains PRIMARY and MULTIPLE and returns MULTIPLE; named UNIQUE constraints are also reported as a type instead of their name. Match the exact PRIMARY/UNIQUE index by the full ordered referenced-column list and project idx.name.

The earlier ALTER-added and unnamed-FK upgrade gaps are otherwise closed. Local validation passed for build, vet, focused tests, and full owning-package tests in pkg/bootstrap/versions/v4_0_6, pkg/sql/plan, and pkg/util/sysview; dependent pkg/bootstrap and pkg/sql/compile build/vet also passed. CI UT/BVT is green, while the upgrade compatibility jobs on this run were skipped.

@daviszhen

Copy link
Copy Markdown
Contributor Author

One blocking upgrade regression remains on exact head a9e70ae. The ALTER-added and unnamed-FK gaps from my previous review are now covered, but explicit legacy RESTRICT actions are no longer preserved.

Before this update, the migration parsed the original CREATE definition and kept an explicit ON DELETE/UPDATE RESTRICT distinct from an omitted action. The new catalog/SHOW CREATE path cannot recover that distinction because both legacy catalog rows and SHOW CREATE render RESTRICT. legacyCatalogReferenceActionName then unconditionally rewrites every RESTRICT to NO_ACTION. The changed TestLegacyForeignKeyMetadataUpdatesPreserveOrderAndActions confirms the regression: its fk_restrict expectation was changed from RESTRICT to NO_ACTION. A 4.0.5 table created with an explicit RESTRICT will therefore report NO ACTION after upgrade, contradicting the PR contract and MySQL metadata.

Please combine the authoritative current-catalog reconciliation with the historical definition where it is available: preserve explicit/default action syntax for CREATE-time constraints, while using catalog plus SHOW CREATE as the fallback needed for ALTER-added and unnamed constraints. Add upgrade controls for legacy explicit RESTRICT versus omitted action, including the ALTER path where the distinction may be irrecoverable and must have a documented conservative policy.

Local validation: the three owning packages v4_0_6, plan, and sysview pass. This is a semantic migration issue, not a compile/test failure.

  • 旧 FK 动作迁移:以 catalog 和 SHOW CREATE TABLE 获取当前约束;仅将 rel_createsql 作为 CREATE-time FK 的历史语法补充,恢复显式 RESTRICT 与默认 NO ACTION 的区别。ALTER-added、unnamed 或无法可靠匹配的 FK 使用 catalog 回退,并标记为历史歧义。
  • 引用索引名称:新增并回填 referenced_index_name,按照完整、有序的引用列精确匹配 PRIMARY/UNIQUE 索引,视图直接返回索引名称,不再使用 min(idx.type)。

同时增加了相关 UT/BVT,覆盖显式/默认动作、ALTER-added、unnamed FK、过期历史定义、竞争 secondary index 和命名复合 UNIQUE key。

@daviszhen

Copy link
Copy Markdown
Contributor Author

Requesting changes on exact head a9e70ae after full diff and upgrade-closure review.

Two blocking metadata defects remain:

  1. The legacy migration rewrites every catalog RESTRICT action to NO_ACTION. For CREATE-time constraints, mo_tables.rel_createsql still preserves whether RESTRICT was explicit or omitted, so a 4.0.5 table with explicit ON DELETE/UPDATE RESTRICT is misreported after upgrade. Preserve the historical syntax when it can be reconciled, and use catalog plus SHOW CREATE only as the fallback for ALTER-added/unnamed constraints.
  2. REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME is derived as min(idx.type) from every index row sharing a referenced column. With parent(id PRIMARY KEY, KEY secondary(id)), the join contains PRIMARY and MULTIPLE and returns MULTIPLE; named UNIQUE constraints are also reported as a type instead of their name. Match the exact PRIMARY/UNIQUE index by the full ordered referenced-column list and project idx.name.

The earlier ALTER-added and unnamed-FK upgrade gaps are otherwise closed. Local validation passed for build, vet, focused tests, and full owning-package tests in pkg/bootstrap/versions/v4_0_6, pkg/sql/plan, and pkg/util/sysview; dependent pkg/bootstrap and pkg/sql/compile build/vet also passed. CI UT/BVT is green, while the upgrade compatibility jobs on this run were skipped.

  • 旧 FK 动作迁移:以 catalog 和 SHOW CREATE TABLE 获取当前约束;仅将 rel_createsql 作为 CREATE-time FK 的历史语法补充,恢复显式 RESTRICT 与默认 NO ACTION 的区别。ALTER-added、unnamed 或无法可靠匹配的 FK 使用 catalog 回退,并标记为历史歧义。
  • 引用索引名称:新增并回填 referenced_index_name,按照完整、有序的引用列精确匹配 PRIMARY/UNIQUE 索引,视图直接返回索引名称,不再使用 min(idx.type)。

同时增加了相关 UT/BVT,覆盖显式/默认动作、ALTER-added、unnamed FK、过期历史定义、竞争 secondary index 和命名复合 UNIQUE key。

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.

5 participants