Skip to content

Commit 79da63c

Browse files
committed
fix: add session system var delete_opt_to_truncate
1 parent 34f9f9b commit 79da63c

5 files changed

Lines changed: 103 additions & 1 deletion

File tree

pkg/frontend/variables.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,14 @@ var gSysVarsDefs = map[string]SystemVariable{
17261726
Type: InitSystemVariableIntType("delayed_queue_size", 1, math.MaxInt64, false),
17271727
Default: int64(1000),
17281728
},
1729+
"delete_opt_to_truncate": {
1730+
Name: "delete_opt_to_truncate",
1731+
Scope: ScopeSession,
1732+
Dynamic: true,
1733+
SetVarHintApplies: false,
1734+
Type: InitSystemVariableBoolType("delete_opt_to_truncate"),
1735+
Default: int64(1),
1736+
},
17291737
"disabled_storage_engines": {
17301738
Name: "disabled_storage_engines",
17311739
Scope: ScopeGlobal,

pkg/sql/plan/build_dml_util.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ func getStepByNodeId(builder *QueryBuilder, nodeId int32) int {
265265
return -1
266266
}
267267

268+
func checkDeleteOptToTruncate(ctx CompilerContext) (bool, error) {
269+
value, err := ctx.ResolveVariable("delete_opt_to_truncate", true, false)
270+
if err != nil {
271+
return false, err
272+
}
273+
274+
if value == nil {
275+
return true, nil
276+
}
277+
278+
if v, ok := value.(int64); ok {
279+
return v == 1, nil
280+
} else if v1, ok := value.(int8); ok {
281+
return v1 == 1, nil
282+
} else {
283+
return false, moerr.NewInternalErrorf(ctx.GetContext(), "invalid %v ", value)
284+
}
285+
}
286+
268287
// buildDeletePlans build preinsert plan.
269288
/*
270289
[o1]sink_scan -> join[u1] -> sink
@@ -334,10 +353,15 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
334353
return err
335354
}
336355

356+
deleteOptToTruncate, err := checkDeleteOptToTruncate(ctx)
357+
if err != nil {
358+
return err
359+
}
360+
337361
if enabled && len(delCtx.tableDef.RefChildTbls) > 0 ||
338362
delCtx.tableDef.ViewSql != nil ||
339363
(util.TableIsClusterTable(delCtx.tableDef.GetTableType()) && accountId != catalog.System_Account) ||
340-
delCtx.objRef.PubInfo != nil {
364+
delCtx.objRef.PubInfo != nil || !deleteOptToTruncate {
341365
canTruncate = false
342366
}
343367

pkg/sql/plan/mock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (m *MockCompilerContext) ResolveVariable(varName string, isSystemVar, isGlo
9696
dec, _ := types.ParseDecimal128("200.001", 38, 3)
9797
vars["decimal_var"] = dec
9898
vars["null_var"] = nil
99+
vars["delete_opt_to_truncate"] = int64(1)
99100

100101
if m.mysqlCompatible {
101102
vars["sql_mode"] = ""

test/distributed/cases/prepare/prepare.result

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,40 @@ prepare stmt1x from 'select count(*) from system.statement_info limit 0';
605605
execute stmt1x;
606606
count(*)
607607
drop account prepare_acc01;
608+
create database prepare_test_2;
609+
use prepare_test_2;
610+
drop table if exists tb_delete_opt_to_truncate;
611+
create table tb_delete_opt_to_truncate(col1 int, col2 int);
612+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
613+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
614+
execute st_delete_opt_to_truncate;
615+
col1 col2
616+
1 2
617+
2 3
618+
delete from tb_delete_opt_to_truncate;
619+
execute st_delete_opt_to_truncate;
620+
internal error: table 'tb_delete_opt_to_truncate' has been changed, please reset prepare statement 'st_delete_opt_to_truncate'
621+
set session delete_opt_to_truncate = 0;
622+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
623+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
624+
execute st_delete_opt_to_truncate;
625+
col1 col2
626+
1 2
627+
2 3
628+
delete from tb_delete_opt_to_truncate;
629+
execute st_delete_opt_to_truncate;
630+
col1 col2
631+
set session delete_opt_to_truncate = 1;
632+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
633+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
634+
execute st_delete_opt_to_truncate;
635+
col1 col2
636+
1 2
637+
2 3
638+
delete from tb_delete_opt_to_truncate;
639+
execute st_delete_opt_to_truncate;
640+
internal error: table 'tb_delete_opt_to_truncate' has been changed, please reset prepare statement 'st_delete_opt_to_truncate'
641+
set session delete_opt_to_truncate = 123;
642+
internal error: convert to the system variable bool type failed
643+
drop table tb_delete_opt_to_truncate;
644+
drop database prepare_test_2;

test/distributed/cases/prepare/prepare.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,35 @@ execute stmt1x;
466466
-- @session
467467

468468
drop account prepare_acc01;
469+
470+
create database prepare_test_2;
471+
use prepare_test_2;
472+
drop table if exists tb_delete_opt_to_truncate;
473+
create table tb_delete_opt_to_truncate(col1 int, col2 int);
474+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
475+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
476+
execute st_delete_opt_to_truncate;
477+
delete from tb_delete_opt_to_truncate;
478+
execute st_delete_opt_to_truncate;
479+
480+
set session delete_opt_to_truncate = 0;
481+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
482+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
483+
execute st_delete_opt_to_truncate;
484+
delete from tb_delete_opt_to_truncate;
485+
execute st_delete_opt_to_truncate;
486+
487+
set session delete_opt_to_truncate = 1;
488+
insert into tb_delete_opt_to_truncate values(1, 2), (2,3);
489+
prepare st_delete_opt_to_truncate from select * from tb_delete_opt_to_truncate order by col1;
490+
execute st_delete_opt_to_truncate;
491+
delete from tb_delete_opt_to_truncate;
492+
execute st_delete_opt_to_truncate;
493+
494+
set session delete_opt_to_truncate = 123;
495+
496+
drop table tb_delete_opt_to_truncate;
497+
drop database prepare_test_2;
498+
499+
500+

0 commit comments

Comments
 (0)