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
…11477)

All tests passed, auto merged by Bot
  • Loading branch information
sre-bot committed Jul 27, 2019
1 parent bc3ba6d commit 6c3920c
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
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 @@ -2789,6 +2793,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
Expand Up @@ -1762,6 +1762,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 {
dur string
fsp int
Expand Down
6 changes: 6 additions & 0 deletions expression/integration_test.go
Expand Up @@ -1966,6 +1966,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 6c3920c

Please sign in to comment.