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

add session system var delete_opt_to_truncate #20950

Merged
merged 2 commits into from
Dec 27, 2024
Merged
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
8 changes: 8 additions & 0 deletions pkg/frontend/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,14 @@ var gSysVarsDefs = map[string]SystemVariable{
Type: InitSystemVariableIntType("delayed_queue_size", 1, math.MaxInt64, false),
Default: int64(1000),
},
"delete_opt_to_truncate": {
Name: "delete_opt_to_truncate",
Scope: ScopeSession,
Dynamic: true,
SetVarHintApplies: false,
Type: InitSystemVariableBoolType("delete_opt_to_truncate"),
Default: int64(1),
},
"disabled_storage_engines": {
Name: "disabled_storage_engines",
Scope: ScopeGlobal,
Expand Down
26 changes: 25 additions & 1 deletion pkg/sql/plan/build_dml_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ func getStepByNodeId(builder *QueryBuilder, nodeId int32) int {
return -1
}

func checkDeleteOptToTruncate(ctx CompilerContext) (bool, error) {
value, err := ctx.ResolveVariable("delete_opt_to_truncate", true, false)
if err != nil {
return false, err
}

if value == nil {
return true, nil
}

if v, ok := value.(int64); ok {
return v == 1, nil
} else if v1, ok := value.(int8); ok {
return v1 == 1, nil
} else {
return false, moerr.NewInternalErrorf(ctx.GetContext(), "invalid %v ", value)
}
}

// buildDeletePlans build preinsert plan.
/*
[o1]sink_scan -> join[u1] -> sink
Expand Down Expand Up @@ -334,10 +353,15 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
return err
}

deleteOptToTruncate, err := checkDeleteOptToTruncate(ctx)
if err != nil {
return err
}

if enabled && len(delCtx.tableDef.RefChildTbls) > 0 ||
delCtx.tableDef.ViewSql != nil ||
(util.TableIsClusterTable(delCtx.tableDef.GetTableType()) && accountId != catalog.System_Account) ||
delCtx.objRef.PubInfo != nil {
delCtx.objRef.PubInfo != nil || !deleteOptToTruncate {
canTruncate = false
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/plan/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (m *MockCompilerContext) ResolveVariable(varName string, isSystemVar, isGlo
dec, _ := types.ParseDecimal128("200.001", 38, 3)
vars["decimal_var"] = dec
vars["null_var"] = nil
vars["delete_opt_to_truncate"] = int64(1)

if m.mysqlCompatible {
vars["sql_mode"] = ""
Expand Down
37 changes: 37 additions & 0 deletions test/distributed/cases/prepare/prepare.result
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,40 @@ prepare stmt1x from 'select count(*) from system.statement_info limit 0';
execute stmt1x;
count(*)
drop account prepare_acc01;
create database prepare_test_2;
use prepare_test_2;
drop table if exists tb_delete_opt_to_truncate;
create table tb_delete_opt_to_truncate(col1 int, col2 int);
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
col1 col2
1 2
2 3
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;
internal error: table 'tb_delete_opt_to_truncate' has been changed, please reset prepare statement 'st_delete_opt_to_truncate'
set session delete_opt_to_truncate = 0;
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
col1 col2
1 2
2 3
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;
col1 col2
set session delete_opt_to_truncate = 1;
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
col1 col2
1 2
2 3
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;
internal error: table 'tb_delete_opt_to_truncate' has been changed, please reset prepare statement 'st_delete_opt_to_truncate'
set session delete_opt_to_truncate = 123;
internal error: convert to the system variable bool type failed
drop table tb_delete_opt_to_truncate;
drop database prepare_test_2;
32 changes: 32 additions & 0 deletions test/distributed/cases/prepare/prepare.test
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,35 @@ execute stmt1x;
-- @session

drop account prepare_acc01;

create database prepare_test_2;
use prepare_test_2;
drop table if exists tb_delete_opt_to_truncate;
create table tb_delete_opt_to_truncate(col1 int, col2 int);
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;

set session delete_opt_to_truncate = 0;
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;

set session delete_opt_to_truncate = 1;
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
execute st_delete_opt_to_truncate;
delete from tb_delete_opt_to_truncate;
execute st_delete_opt_to_truncate;

set session delete_opt_to_truncate = 123;

drop table tb_delete_opt_to_truncate;
drop database prepare_test_2;



Loading