Commit ed34bf3
authored
`DELETE FROM partition_table` (without WHERE clause) fails with:
```
ERROR 20101 (HY000): internal error: can not find table by id 0: accountId: 0
```
### Root Cause
MO has a truncate optimization that rewrites `DELETE FROM table` (no WHERE) into `DROP TABLE + CREATE TABLE`. This optimization does not check for partition tables. When applied to a partition table, the `TruncateTable` execution path iterates over `ForeignTbl` (which stores partition sub-table IDs), but for partition tables these IDs are 0-valued placeholders. The engine then calls `GetRelationById(0)`, which fails.
Two code paths needed the fix:
1. **New binder** (`bind_delete.go`): `canDeleteRewriteToTruncate()` was missing a partition table check
2. **Old builder fallback** (`build_dml_util.go`): `buildDeletePlans()` `canTruncate` condition was also missing the partition check
### Fix
Add `tableDef.Partition != nil` guard in both paths so partition tables are never rewritten to truncate. Instead, they follow the normal `PartitionDelete` operator path which correctly handles per-partition deletes.
### Changes
- `pkg/sql/plan/bind_delete.go`: Return `false` from `canDeleteRewriteToTruncate()` when `tableDef.Partition != nil`
- `pkg/sql/plan/build_dml_util.go`: Set `canTruncate = false` when `delCtx.tableDef.Partition != nil`
Approved by: @aunjgr, @ouyuanning
1 parent 16814a6 commit ed34bf3
2 files changed
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
414 | 414 | | |
415 | 415 | | |
416 | 416 | | |
417 | | - | |
| 417 | + | |
| 418 | + | |
418 | 419 | | |
419 | 420 | | |
420 | 421 | | |
| |||
0 commit comments