Skip to content

Commit

Permalink
fix date_add/date_sub function issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi committed Jul 27, 2019
1 parent 5611acd commit 1a480aa
Show file tree
Hide file tree
Showing 2 changed files with 27 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 @@ -2791,6 +2795,10 @@ func (du *baseDateArithmitical) sub(ctx sessionctx.Context, date types.Time, int
return types.Time{}, true, err
}

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

duration := time.Duration(nano)
goTime = goTime.Add(duration)
goTime = types.AddDate(year, month, day, goTime)
Expand Down
19 changes: 19 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,25 @@ 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)
}

testDurations := []struct {
fc functionClass
dur string
Expand Down

0 comments on commit 1a480aa

Please sign in to comment.