Skip to content

Commit

Permalink
expression: handle builtin add_date/sub_date func overflow (#11472)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi authored and wshwsh12 committed Jul 27, 2019
1 parent 5611acd commit 5c35082
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,10 @@ func (du *baseDateArithmitical) add(ctx sessionctx.Context, date types.Time, int
date.Fsp = 6
}

if goTime.Year() < 0 || goTime.Year() > (1<<16-1) {
return types.Time{}, true, handleInvalidTimeError(ctx, types.ErrDatetimeFunctionOverflow.GenWithStackByArgs("datetime"))
}

date.Time = types.FromGoTime(goTime)
overflow, err := types.DateTimeIsOverflow(ctx.GetSessionVars().StmtCtx, date)
if err := handleInvalidTimeError(ctx, err); err != nil {
Expand Down Expand Up @@ -2801,6 +2805,10 @@ func (du *baseDateArithmitical) sub(ctx sessionctx.Context, date types.Time, int
date.Fsp = 6
}

if goTime.Year() < 0 || goTime.Year() > (1<<16-1) {
return types.Time{}, true, handleInvalidTimeError(ctx, types.ErrDatetimeFunctionOverflow.GenWithStackByArgs("datetime"))
}

date.Time = types.FromGoTime(goTime)
overflow, err := types.DateTimeIsOverflow(ctx.GetSessionVars().StmtCtx, date)
if err := handleInvalidTimeError(ctx, err); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,35 @@ func (s *testEvaluatorSuite) TestDateArithFuncs(c *C) {
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime().String(), Equals, test.expected)
}

testOverflowYears := []struct {
input string
year int
}{
{"2008-11-23", -1465647104},
{"2008-11-23", 1465647104},
}

for _, test := range testOverflowYears {
args = types.MakeDatums(test.input, test.year, "YEAR")
f, err = fcAdd.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.IsNull(), IsTrue)
}

for _, test := range testOverflowYears {
args = types.MakeDatums(test.input, test.year, "YEAR")
f, err = fcSub.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.IsNull(), IsTrue)
}

testDurations := []struct {
fc functionClass
dur string
Expand Down
6 changes: 6 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,12 @@ func (s *testIntegrationSuite) TestDatetimeOverflow(c *C) {
rows = append(rows, "<nil>")
}
tk.MustQuery("select * from t1").Check(testkit.Rows(rows...))

//Fix ISSUE 11256
tk.MustQuery(`select DATE_ADD('2000-04-13 07:17:02',INTERVAL -1465647104 YEAR);`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select DATE_ADD('2008-11-23 22:47:31',INTERVAL 266076160 QUARTER);`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select DATE_SUB('2000-04-13 07:17:02',INTERVAL 1465647104 YEAR);`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select DATE_SUB('2008-11-23 22:47:31',INTERVAL -266076160 QUARTER);`).Check(testkit.Rows("<nil>"))
}

func (s *testIntegrationSuite) TestBuiltin(c *C) {
Expand Down

0 comments on commit 5c35082

Please sign in to comment.