Skip to content

Commit

Permalink
ddl: fix reorg cannot handle divide 0 error (pingcap#50057)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Jan 4, 2024
1 parent ebe334f commit 6f32664
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/ddl/backfilling_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func initSessCtx(
if err := setSessCtxLocation(sessCtx, tzLocation); err != nil {
return errors.Trace(err)
}
sessCtx.GetSessionVars().StmtCtx.InReorg = true
sessCtx.GetSessionVars().StmtCtx.SetTimeZone(sessCtx.GetSessionVars().Location())
sessCtx.GetSessionVars().StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.DividedByZeroAsWarning = !sqlMode.HasStrictMode()
Expand Down Expand Up @@ -206,6 +207,7 @@ func restoreSessCtx(sessCtx sessionctx.Context) func(sessCtx sessionctx.Context)
uv.StmtCtx.DividedByZeroAsWarning = dividedZeroAsWarn
uv.StmtCtx.SetTypeFlags(typeFlags)
uv.StmtCtx.ResourceGroupName = resGroupName
uv.StmtCtx.InReorg = false
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func handleInvalidTimeError(ctx EvalContext, err error) error {
// handleDivisionByZeroError reports error or warning depend on the context.
func handleDivisionByZeroError(ctx EvalContext) error {
sc := ctx.GetSessionVars().StmtCtx
if sc.InInsertStmt || sc.InUpdateStmt || sc.InDeleteStmt {
if sc.InInsertStmt || sc.InUpdateStmt || sc.InDeleteStmt || sc.InReorg {
if !ctx.GetSessionVars().SQLMode.HasErrorForDivisionByZeroMode() {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ type StatementContext struct {
InCreateOrAlterStmt bool
InSetSessionStatesStmt bool
InPreparedPlanBuilding bool
InReorg bool
DupKeyAsWarning bool
BadNullAsWarning bool
DividedByZeroAsWarning bool
Expand Down
13 changes: 13 additions & 0 deletions tests/integrationtest/r/generated_columns.result
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,16 @@ EXPLAIN format = 'brief' SELECT c, a FROM tu WHERE c in(1, 2, 3);
id estRows task access object operator info
Projection 3.00 root generated_columns.tu.c, generated_columns.tu.a
└─Batch_Point_Get 3.00 root table:tu, index:uk(c) keep order:false, desc:false
set @@sql_mode=default;
drop table if exists t1;
create table t1(a int);
insert into t1 values(0);
alter table t1 add index i((100/a));
Error 1365 (22012): Division by 0
drop table t1;
set @@sql_mode='';
create table t1(a int);
insert into t1 values(0);
alter table t1 add index i((100/a));
drop table t1;
set @@sql_mode=default;
17 changes: 17 additions & 0 deletions tests/integrationtest/t/generated_columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ EXPLAIN format = 'brief' SELECT * FROM tu WHERE c = 1;
EXPLAIN format = 'brief' SELECT a, c FROM tu WHERE c = 1;
EXPLAIN format = 'brief' SELECT * FROM tu WHERE c in(1, 2, 3);
EXPLAIN format = 'brief' SELECT c, a FROM tu WHERE c in(1, 2, 3);

# should handle divide zero error for default sql mode
set @@sql_mode=default;
drop table if exists t1;
create table t1(a int);
insert into t1 values(0);
--error 1365
alter table t1 add index i((100/a));
drop table t1;

# should ignore divide zero error for default sql mode
set @@sql_mode='';
create table t1(a int);
insert into t1 values(0);
alter table t1 add index i((100/a));
drop table t1;
set @@sql_mode=default;

0 comments on commit 6f32664

Please sign in to comment.