Skip to content

fix(dml): preserve join scope for indexed deletes - #28310

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-28309-delete-join
Sep 7, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-28309-delete-join

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Bug

What this PR does / why we need it

Irregular indexes currently route DELETE through the legacy DML planner. That planner classified every single-target statement without WHERE/LIMIT as an unrestricted whole-table delete, even when TableRefs contained a JOIN or USING source. The resulting plan could truncate the base table or independently delete every hidden-index row without evaluating the join.

This change establishes one shared semantic predicate for an unrestricted delete: exactly one target, no WHERE, no LIMIT, no additional table references, and no partition restriction. Both modern truncate rewriting and the legacy fallback use that predicate. Physical truncate eligibility remains a separate decision for RETURNING, foreign keys, partitions, session settings, and other table metadata.

The internal flag is renamed to isUnrestrictedDelete so all base-table, regular-index, IVF, MASTER, FULLTEXT, and POSTDML consumers express the actual contract.

Closes #28309.

Test plan

Automated

  • Planner UT covers plain DELETE, ORDER BY-only, WHERE, LIMIT 0, JOIN, USING, partition restriction, multiple targets, and nil input.
  • Planner shape UT forces the synchronous FULLTEXT legacy fallback and asserts the origin DELETE cannot truncate and row-scoped hidden-index maintenance is retained.
  • Distributed SQL regression covers:
    • empty JOIN with delete_opt_to_truncate=0, verifying both base rows and FULLTEXT contents survive;
    • duplicate matches with truncate enabled, verifying one target row is deleted once and surviving/deleted FULLTEXT terms agree with the base table.

Validation

Linux/amd64 on 10.222.1.55, base 4fdb9e91615f9fc37703039ae28a47849206d1d6, head 6c8f7fbe3c:

  • make clean && make -j12
  • focused planner UT: pass
  • focused planner UT with -race: pass
  • ./pkg/sql/plan and ./pkg/sql/colexec/postdml: pass
  • delete_multiple_table.sql through mo-tester normal comparison mode twice on the same instance: 128 pass, 0 fail, 2 pre-existing ignored (100%); 1.283s and 1.487s
  • black-box FULLTEXT matrix: empty source, ON false, CROSS/RIGHT/LEFT JOIN, NULL key, derived empty source, USING, WHERE TRUE control, prepared-statement reuse, and duplicate matches: pass
  • black-box MASTER hidden-table counts: empty JOIN 6 -> 6; partial duplicate match 6 -> 4 while base rows 3 -> 2
  • full repository golangci-lint 2.6.2: 0 issues
  • license header check: 5106 checked, 0 invalid
  • git diff --check and gofmt: pass

The 55 test service and isolated data were removed after validation.

@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

Copy link
Copy Markdown
Contributor Author

Additional 55 Linux/amd64 black-box validation on the exact PR source (6c8f7fbe3c file hashes matched):

  • HNSW: empty JOIN preserved 1,2,3; duplicate partial JOIN deleted only 2, leaving 1,3.
  • IVF-FLAT: same empty/duplicate partial matrix passed.
  • FULLTEXT2: same empty/duplicate partial matrix passed.
  • Mixed synchronous FULLTEXT + HNSW: transaction-local partial delete showed 1,3; rollback restored 1,2,3 and FULLTEXT beta search returned 2; empty JOIN preserved all; plain unconditional DELETE still removed all as the intended control.
  • The existing delete_multiple_table BVT had already passed twice on one instance (128 pass, 0 fail, 2 pre-existing ignored each run).

The service was stopped using an exact executable-path check and the isolated data/logs were removed afterward.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed head 6c8f7fbe3cd2d4a7b4b9cedb5366c46a80970401 against base/merge-base 4fdb9e91615f9fc37703039ae28a47849206d1d6. No blocking findings in this revision.

The fix closes the root boundary in #28309: absence of WHERE/LIMIT alone does not imply that the target row set is unrestricted. Both JOIN and USING have TableRefs populated by the parser, so the shared predicate now rejects them before either a base-table truncate or an independent hidden-table delete-all shortcut can be selected.

Review coverage:

  • Read all six changed files/hunks. R2 behavioral closure: modern/legacy DELETE classification through base-table and regular/IVF/MASTER/FULLTEXT maintenance. The context-field rename is internal; the existing POSTDML/protobuf field and producer/consumer propagation remain compatible. This is a focused bug fix, not a feature/major-refactor design-gate case.
  • Checked that ordinary unrestricted DELETE retains its prior classification and that WHERE, LIMIT 0, multi-target, JOIN/USING and explicit partition restrictions cannot enter the new unrestricted branch. Physical restrictions for foreign keys, table metadata, session settings and the modern RETURNING route remain separate.
  • Followed the row-producing SELECT/source stream, hidden-index row-scoped branches and POSTDML consumer. The classification change does not introduce an alternative row-selection algorithm; it removes the incorrect delete-all shortcut. Base primary-key locking remains present, and the relevant index lock targets retain the established row-lock path.
  • Reviewed the helper/typed-plan UT and the BVT/result pair: empty source with truncate disabled independently checks surviving base rows and FULLTEXT contents; duplicate source matches with truncate enabled checks surviving rows and deleted/surviving search terms. Existing multi-table cases were not weakened.

Scoped unhappy-path check:

Dimension Result for this change
Q1 ownership No new resource owner; pooled DML context is reset as a whole, including the renamed boolean.
Q2 waits No new wait/cancel/retry protocol; restricted deletes reuse the existing row-lock/maintenance path.
Q3 growth No new registry, cache or persistent queue; added classification work is constant-size. Existing row-scoped work is necessary to evaluate the requested delete set.

Evidence: reviewed/reused the author's exact-head Linux/amd64 validation record in the PR description (focused planner UT/race, planner and postdml packages, normal-comparison BVT twice, and FULLTEXT/MASTER public-path controls). Independently checked the complete committed diff, parser/consumer closure and git diff --check (pass). I did not rerun a service, heavy UT or BVT locally. Public CI was still partially in progress when checked; this approval is the code-review conclusion, not a claim that all CI has completed.

VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] Bug

## What this PR does / why we need it

Irregular indexes currently route `DELETE` through the legacy DML
planner. That planner classified every single-target statement without
`WHERE`/`LIMIT` as an unrestricted whole-table delete, even when
`TableRefs` contained a JOIN or USING source. The resulting plan could
truncate the base table or independently delete every hidden-index row
without evaluating the join.

This change establishes one shared semantic predicate for an
unrestricted delete: exactly one target, no WHERE, no LIMIT, no
additional table references, and no partition restriction. Both modern
truncate rewriting and the legacy fallback use that predicate. Physical
truncate eligibility remains a separate decision for RETURNING, foreign
keys, partitions, session settings, and other table metadata.

The internal flag is renamed to `isUnrestrictedDelete` so all
base-table, regular-index, IVF, MASTER, FULLTEXT, and POSTDML consumers
express the actual contract.

Closes matrixorigin#28309.

## Test plan

### Automated

- Planner UT covers plain DELETE, ORDER BY-only, WHERE, LIMIT 0, JOIN,
USING, partition restriction, multiple targets, and nil input.
- Planner shape UT forces the synchronous FULLTEXT legacy fallback and
asserts the origin DELETE cannot truncate and row-scoped hidden-index
maintenance is retained.
- Distributed SQL regression covers:
- empty JOIN with `delete_opt_to_truncate=0`, verifying both base rows
and FULLTEXT contents survive;
- duplicate matches with truncate enabled, verifying one target row is
deleted once and surviving/deleted FULLTEXT terms agree with the base
table.

### Validation

Linux/amd64 on 10.222.1.55, base
`4fdb9e91615f9fc37703039ae28a47849206d1d6`, head `6c8f7fbe3c`:

- `make clean && make -j12`
- focused planner UT: pass
- focused planner UT with `-race`: pass
- `./pkg/sql/plan` and `./pkg/sql/colexec/postdml`: pass
- `delete_multiple_table.sql` through mo-tester normal comparison mode
twice on the same instance: 128 pass, 0 fail, 2 pre-existing ignored
(100%); 1.283s and 1.487s
- black-box FULLTEXT matrix: empty source, ON false, CROSS/RIGHT/LEFT
JOIN, NULL key, derived empty source, USING, WHERE TRUE control,
prepared-statement reuse, and duplicate matches: pass
- black-box MASTER hidden-table counts: empty JOIN 6 -> 6; partial
duplicate match 6 -> 4 while base rows 3 -> 2
- full repository `golangci-lint` 2.6.2: 0 issues
- license header check: 5106 checked, 0 invalid
- `git diff --check` and gofmt: pass

The 55 test service and isolated data were removed after validation.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] Bug

## What this PR does / why we need it

Irregular indexes currently route `DELETE` through the legacy DML
planner. That planner classified every single-target statement without
`WHERE`/`LIMIT` as an unrestricted whole-table delete, even when
`TableRefs` contained a JOIN or USING source. The resulting plan could
truncate the base table or independently delete every hidden-index row
without evaluating the join.

This change establishes one shared semantic predicate for an
unrestricted delete: exactly one target, no WHERE, no LIMIT, no
additional table references, and no partition restriction. Both modern
truncate rewriting and the legacy fallback use that predicate. Physical
truncate eligibility remains a separate decision for RETURNING, foreign
keys, partitions, session settings, and other table metadata.

The internal flag is renamed to `isUnrestrictedDelete` so all
base-table, regular-index, IVF, MASTER, FULLTEXT, and POSTDML consumers
express the actual contract.

Closes matrixorigin#28309.

## Test plan

### Automated

- Planner UT covers plain DELETE, ORDER BY-only, WHERE, LIMIT 0, JOIN,
USING, partition restriction, multiple targets, and nil input.
- Planner shape UT forces the synchronous FULLTEXT legacy fallback and
asserts the origin DELETE cannot truncate and row-scoped hidden-index
maintenance is retained.
- Distributed SQL regression covers:
- empty JOIN with `delete_opt_to_truncate=0`, verifying both base rows
and FULLTEXT contents survive;
- duplicate matches with truncate enabled, verifying one target row is
deleted once and surviving/deleted FULLTEXT terms agree with the base
table.

### Validation

Linux/amd64 on 10.222.1.55, base
`4fdb9e91615f9fc37703039ae28a47849206d1d6`, head `6c8f7fbe3c`:

- `make clean && make -j12`
- focused planner UT: pass
- focused planner UT with `-race`: pass
- `./pkg/sql/plan` and `./pkg/sql/colexec/postdml`: pass
- `delete_multiple_table.sql` through mo-tester normal comparison mode
twice on the same instance: 128 pass, 0 fail, 2 pre-existing ignored
(100%); 1.283s and 1.487s
- black-box FULLTEXT matrix: empty source, ON false, CROSS/RIGHT/LEFT
JOIN, NULL key, derived empty source, USING, WHERE TRUE control,
prepared-statement reuse, and duplicate matches: pass
- black-box MASTER hidden-table counts: empty JOIN 6 -> 6; partial
duplicate match 6 -> 4 while base rows 3 -> 2
- full repository `golangci-lint` 2.6.2: 0 issues
- license header check: 5106 checked, 0 invalid
- `git diff --check` and gofmt: pass

The 55 test service and isolated data were removed after validation.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] Bug

## What this PR does / why we need it

Irregular indexes currently route `DELETE` through the legacy DML
planner. That planner classified every single-target statement without
`WHERE`/`LIMIT` as an unrestricted whole-table delete, even when
`TableRefs` contained a JOIN or USING source. The resulting plan could
truncate the base table or independently delete every hidden-index row
without evaluating the join.

This change establishes one shared semantic predicate for an
unrestricted delete: exactly one target, no WHERE, no LIMIT, no
additional table references, and no partition restriction. Both modern
truncate rewriting and the legacy fallback use that predicate. Physical
truncate eligibility remains a separate decision for RETURNING, foreign
keys, partitions, session settings, and other table metadata.

The internal flag is renamed to `isUnrestrictedDelete` so all
base-table, regular-index, IVF, MASTER, FULLTEXT, and POSTDML consumers
express the actual contract.

Closes matrixorigin#28309.

## Test plan

### Automated

- Planner UT covers plain DELETE, ORDER BY-only, WHERE, LIMIT 0, JOIN,
USING, partition restriction, multiple targets, and nil input.
- Planner shape UT forces the synchronous FULLTEXT legacy fallback and
asserts the origin DELETE cannot truncate and row-scoped hidden-index
maintenance is retained.
- Distributed SQL regression covers:
- empty JOIN with `delete_opt_to_truncate=0`, verifying both base rows
and FULLTEXT contents survive;
- duplicate matches with truncate enabled, verifying one target row is
deleted once and surviving/deleted FULLTEXT terms agree with the base
table.

### Validation

Linux/amd64 on 10.222.1.55, base
`4fdb9e91615f9fc37703039ae28a47849206d1d6`, head `6c8f7fbe3c`:

- `make clean && make -j12`
- focused planner UT: pass
- focused planner UT with `-race`: pass
- `./pkg/sql/plan` and `./pkg/sql/colexec/postdml`: pass
- `delete_multiple_table.sql` through mo-tester normal comparison mode
twice on the same instance: 128 pass, 0 fail, 2 pre-existing ignored
(100%); 1.283s and 1.487s
- black-box FULLTEXT matrix: empty source, ON false, CROSS/RIGHT/LEFT
JOIN, NULL key, derived empty source, USING, WHERE TRUE control,
prepared-statement reuse, and duplicate matches: pass
- black-box MASTER hidden-table counts: empty JOIN 6 -> 6; partial
duplicate match 6 -> 4 while base rows 3 -> 2
- full repository `golangci-lint` 2.6.2: 0 issues
- license header check: 5106 checked, 0 invalid
- `git diff --check` and gofmt: pass

The 55 test service and isolated data were removed after validation.

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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DELETE JOIN without WHERE deletes every row from irregular-index targets

3 participants