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 #21345

Open
wants to merge 2 commits into
base: 2.0-dev
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
51 changes: 45 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,19 @@ 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
cantrucate, err = canDeleteRewriteToTruncate(ctx, dmlCtx)
if 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
15 changes: 15 additions & 0 deletions pkg/sql/plan/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"strings"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect/mysql"
Expand Down Expand Up @@ -1243,3 +1246,15 @@ func Test_limitUint64(t *testing.T) {
outPutPlan(logicPlan, true, t)
}
}

// test canDeleteRewriteToTruncate
func Test_bind_delete(t *testing.T) {
ctx := context.TODO()
ctrl := gomock.NewController(t)
compileCtx := NewMockCompilerContext2(ctrl)
compileCtx.EXPECT().ResolveVariable(gomock.Any(), gomock.Any(), gomock.Any()).Return("", nil).AnyTimes()
compileCtx.EXPECT().GetAccountId().Return(catalog.System_Account, moerr.NewInternalError(ctx, "no account id in context")).AnyTimes()
dmlCtx := &DMLContext{}
_, err := canDeleteRewriteToTruncate(compileCtx, dmlCtx)
assert.Error(t, err)
}
Loading