Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi committed Jul 27, 2019
1 parent 1a480aa commit 6a5db8e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions expression/builtin_time.go
Expand Up @@ -2795,10 +2795,6 @@ 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 All @@ -2809,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
10 changes: 10 additions & 0 deletions expression/builtin_time_test.go
Expand Up @@ -1845,6 +1845,16 @@ func (s *testEvaluatorSuite) TestDateArithFuncs(c *C) {
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
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 6a5db8e

Please sign in to comment.