Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if delete can't rewrite to truncate table, use bind_delete make new plan #21014

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading