Skip to content

Commit ed34bf3

Browse files
authored
fix: disable truncate optimization for partition tables in DELETE (#23970) (#24016)
`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

File tree

pkg/sql/plan/bind_delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func canDeleteRewriteToTruncate(ctx CompilerContext, dmlCtx *DMLContext) (bool,
4848
if enabled && len(tableDef.RefChildTbls) > 0 ||
4949
tableDef.ViewSql != nil ||
5050
(dmlCtx.isClusterTable[i] && accountId != catalog.System_Account) ||
51-
dmlCtx.objRefs[i].PubInfo != nil {
51+
dmlCtx.objRefs[i].PubInfo != nil ||
52+
tableDef.Partition != nil {
5253
return false, nil
5354
}
5455
}

pkg/sql/plan/build_dml_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
414414
if enabled && len(delCtx.tableDef.RefChildTbls) > 0 ||
415415
delCtx.tableDef.ViewSql != nil ||
416416
(util.TableIsClusterTable(delCtx.tableDef.GetTableType()) && accountId != catalog.System_Account) ||
417-
delCtx.objRef.PubInfo != nil || !deleteOptToTruncate {
417+
delCtx.objRef.PubInfo != nil || !deleteOptToTruncate ||
418+
delCtx.tableDef.Partition != nil {
418419
canTruncate = false
419420
}
420421

0 commit comments

Comments
 (0)