Skip to content

fix: maintain indexes for REPLACE foreign key actions - #27062

Merged
XuPeng-SH merged 18 commits into
matrixorigin:mainfrom
ck89119:issue-26875-main
Aug 23, 2026
Merged

XuPeng-SH merged 18 commits into
matrixorigin:mainfrom
ck89119:issue-26875-main

Conversation

@ck89119

@ck89119 ck89119 commented Aug 12, 2026

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 #26875

What this PR does / why we need it:

Fixes REPLACE foreign-key actions when child tables have explicit unique or secondary indexes.

  • Keeps binding tags and positional projections only on recursive FK index-maintenance paths.
  • Gives hidden-index DELETE operators a stable rowid/primary-key input layout.
  • Remaps regular UK/SK pre-insert metadata after projection pruning.
  • Preserves secondary-index pre-insert context when copying prepared plans.
  • Avoids unsafe optimizer assumptions for untagged internal DML scans.
  • Disables the internal RIGHT JOIN runtime filter only when it would create an FK-action wait cycle.
  • Adds end-to-end coverage for indexed CASCADE and SET NULL through literal REPLACE and binary-protocol prepared statements.

Validation:

  • go test ./pkg/sql/plan
  • TestIssue26875ReplaceMaintainsIndexedForeignKeyChildren
  • TestIssue25526PreparedUpdateJoinSecondExecute
  • golangci-lint run -v -c .golangci.yml ./...

@ck89119
ck89119 requested review from aptend and aunjgr as code owners August 21, 2026 06:42
@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 →

@aunjgr aunjgr 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.

Reviewed at 0f7194d. The recursive FK paths now retain a stable source layout through hidden-index delete and pre-insert maintenance, with coordinator wait-cycle closure where required. Focused planner tests and all four new multi-CN issue regressions pass.

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

P1: close the indexed mixed-action row-domain/projection path before merging.

At exact head 0f7194dc3bc2f74723a2e37e12ab3c7734ada29a, a child table with both indexed ON DELETE SET NULL and indexed ON DELETE CASCADE still makes REPLACE fail with an internal runtime panic. This reproduces with only ordinary secondary indexes (no UNIQUE index is required):

create table p(id int primary key);
create table c(
  id int primary key, set_id int, cascade_id int, marker int,
  key sk_set(set_id), key sk_cascade(cascade_id),
  foreign key(set_id) references p(id) on delete set null,
  foreign key(cascade_id) references p(id) on delete cascade
);
insert into p values(1),(2);
insert into c values
  (10,1,2,10), -- SET NULL only
  (20,2,1,20), -- CASCADE only
  (30,1,1,30), -- both actions; CASCADE owns it
  (40,2,2,40); -- unaffected
replace into p values(1);

The final statement deterministically returns error 20101 with a nil dereference in hashjoin.(*container).appendOneNotMatch -> Vector.UnionNull. The hash join's outer-row path has an output/result slot without a valid vector, showing that the mixed SET NULL/CASCADE action graph still reaches execution with an incomplete relation/projection layout.

Expected: row 10 remains with set_id IS NULL; rows 20 and 30 are deleted; row 40 and both secondary indexes remain valid. Actual: the statement aborts with an internal panic.

The full pkg/sql/plan suite, build, vet, and current CI pass, so this action-composition edge is not covered. Please make the SET NULL and CASCADE branches share an explicit, fully remapped ownership/output contract, and add a regression covering SET-only, CASCADE-only, overlap, and unaffected rows (including index-forced validation; prepared execution should use the same matrix).

@aptend aptend 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.

Deep review of exact head 0f7194d. I inspected the full diff and all existing review history, then ran the full pkg/sql/plan suite, the four checked-in issue_26875 cluster regressions, focused planner tests, build, vet, and diff-check. Those pass, but an independently reproduced mixed-action path still panics at runtime, so this head is not safe to merge.

Comment thread pkg/sql/plan/build_dml_util.go Resolved

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

P1: close the combined SET NULL + CASCADE stream-isolation path before merging.

At exact head 9e59df41b569cbaabd4b16eb67106e5f1f8b2651, the new single-SET-NULL regression passes, but the adjacent supported case with two indexed ON DELETE SET NULL FKs plus one indexed ON DELETE CASCADE FK still aborts REPLACE with an internal panic.

Minimal shape:

create table p(id int primary key);
create table c(
  id int primary key, set_a int, set_b int, cascade_id int,
  key sk_a(set_a), key sk_b(set_b), key sk_c(cascade_id),
  foreign key(set_a) references p(id) on delete set null,
  foreign key(set_b) references p(id) on delete set null,
  foreign key(cascade_id) references p(id) on delete cascade
);
insert into p values(1),(2);
insert into c values
  (10,1,2,2),(11,2,1,2),(20,2,2,1),(30,1,1,1),(40,2,2,2);
replace into p values(1);

Actual: error 20101, index out of range [4] with length 4 in colexec.(*ColumnExpressionExecutor).Eval, reached from projection/hashjoin. Expected: rows 10/11 survive with the matching SET NULL applied, rows 20/30 are deleted by CASCADE, and row 40 plus all secondary indexes remain valid.

The combined-SET-NULL branch builds its anti-joined update stream but does not mark it pending for the later CASCADE branch, so builder.deleteNode can still merge the disjoint action sources and narrow the projection ABI. Please establish the same isolation invariant for every SET NULL producer, including the combined branch, and add a regression covering this multi-SET-NULL + CASCADE state, preferably with FK-order/prepared controls.

Comment thread pkg/sql/plan/build_dml_util.go Resolved

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

Reviewed exact head 04ea25e. The combined SET NULL producer now owns an isolated TableDef and marks its delete stream pending before sibling CASCADE planning, closing the previously reproduced projection/row-domain failure. The new literal and prepared regression covers reversed FK order, SET-only rows, CASCADE-owned rows, overlap, unaffected rows, and forced-index validation. The focused regression, all TestIssue26875 cases, and the full pkg/sql/plan suite pass locally; required CI is green.

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/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants