diff --git a/executor/executor_test.go b/executor/executor_test.go index 2c0aaf55584d3..106dff3332423 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -5533,3 +5533,16 @@ func (s *testSuite1) TestInsertValuesWithSubQuery(c *C) { tk.MustExec("insert into t2 set a = 3, b = 5, c = b") tk.MustQuery("select * from t2").Check(testkit.Rows("2 4 2", "3 5 5")) } + +func (s *testSuite1) TestDIVZeroInPartitionExpr(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t1(a int) partition by range (10 div a) (partition p0 values less than (10), partition p1 values less than maxvalue)") + defer tk.MustExec("drop table if exists t1") + + tk.MustExec("set @@sql_mode=''") + tk.MustExec("insert into t1 values (NULL), (0), (1)") + tk.MustExec("set @@sql_mode='STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO'") + tk.MustGetErrCode("insert into t1 values (NULL), (0), (1)", mysql.ErrDivisionByZero) +} diff --git a/expression/builtin_arithmetic.go b/expression/builtin_arithmetic.go index 7bd59ec95347a..e2e7b839c9784 100644 --- a/expression/builtin_arithmetic.go +++ b/expression/builtin_arithmetic.go @@ -747,16 +747,20 @@ func (s *builtinArithmeticIntDivideDecimalSig) Clone() builtinFunc { } func (s *builtinArithmeticIntDivideIntSig) evalInt(row chunk.Row) (int64, bool, error) { - b, isNull, err := s.args[1].EvalInt(s.ctx, row) + return s.evalIntWithCtx(s.ctx, row) +} + +func (s *builtinArithmeticIntDivideIntSig) evalIntWithCtx(sctx sessionctx.Context, row chunk.Row) (int64, bool, error) { + b, isNull, err := s.args[1].EvalInt(sctx, row) if isNull || err != nil { return 0, isNull, err } if b == 0 { - return 0, true, handleDivisionByZeroError(s.ctx) + return 0, true, handleDivisionByZeroError(sctx) } - a, isNull, err := s.args[0].EvalInt(s.ctx, row) + a, isNull, err := s.args[0].EvalInt(sctx, row) if isNull || err != nil { return 0, isNull, err }