Skip to content

Commit

Permalink
if delete can't rewrite to truncate table, use bind_delete make new plan
Browse files Browse the repository at this point in the history
  • Loading branch information
huby2358 committed Jan 2, 2025
1 parent 63d4557 commit b1e20c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
49 changes: 43 additions & 6 deletions pkg/sql/plan/bind_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,40 @@ import (
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
)

func (builder *QueryBuilder) bindDelete(stmt *tree.Delete, bindCtx *BindContext) (int32, error) {
if len(stmt.Tables) != 1 {
return 0, moerr.NewUnsupportedDML(builder.GetContext(), "delete from multiple tables")
func canDeleteRewriteToTruncate(ctx CompilerContext, dmlCtx DMLContext) (bool, error) {
accountId, err := ctx.GetAccountId()
if err != nil {
return false, err
}

//FIXME: optimize truncate table?
if stmt.Where == nil && stmt.Limit == nil {
return 0, moerr.NewUnsupportedDML(builder.GetContext(), "rewrite to truncate table")
deleteOptToTruncate, err := checkDeleteOptToTruncate(ctx)
if err != nil {
return false, err
}

if !deleteOptToTruncate {
return false, nil
}

enabled, err := IsForeignKeyChecksEnabled(ctx)
if err != nil {
return false, err
}

for i, tableDef := range dmlCtx.tableDefs {
if enabled && len(tableDef.RefChildTbls) > 0 ||
tableDef.ViewSql != nil ||
(dmlCtx.isClusterTable[i] && accountId != catalog.System_Account) ||
dmlCtx.objRefs[i].PubInfo != nil {
return false, nil
}
}
return true, nil
}

func (builder *QueryBuilder) bindDelete(ctx CompilerContext, stmt *tree.Delete, bindCtx *BindContext) (int32, error) {
if len(stmt.Tables) != 1 {
return 0, moerr.NewUnsupportedDML(builder.GetContext(), "delete from multiple tables")
}

aliasMap := make(map[string][2]string)
Expand All @@ -45,6 +71,17 @@ func (builder *QueryBuilder) bindDelete(stmt *tree.Delete, bindCtx *BindContext)
return 0, err
}

//FIXME: optimize truncate table?
if stmt.Where == nil && stmt.Limit == nil {
var cantrucate bool
if cantrucate, err = canDeleteRewriteToTruncate(ctx, *dmlCtx); err != nil {
return 0, err
}
if cantrucate {
return 0, moerr.NewUnsupportedDML(builder.GetContext(), "rewrite to truncate table")
}
}

var selectList []tree.SelectExpr
colName2Idx := make([]map[string]int32, len(stmt.Tables))

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/plan/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func bindAndOptimizeDeleteQuery(ctx CompilerContext, stmt *tree.Delete, isPrepar
bindCtx.snapshot = ctx.GetSnapshot()
}

rootId, err := builder.bindDelete(stmt, bindCtx)
rootId, err := builder.bindDelete(ctx, stmt, bindCtx)
if err != nil {
if err.(*moerr.Error).ErrorCode() == moerr.ErrUnsupportedDML {
return buildDelete(stmt, ctx, isPrepareStmt)
Expand Down

0 comments on commit b1e20c0

Please sign in to comment.