diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 98e7876321609..d3ca3138a29df 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -7037,7 +7037,7 @@ func (b *builtinLastDaySig) evalTime(row chunk.Row) (types.Time, bool, error) { } tm := arg year, month := tm.Year(), tm.Month() - if arg.InvalidZero() { + if tm.Month() == 0 || (tm.Day() == 0 && b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode()) { return types.ZeroTime, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String())) } lastDay := types.GetLastDay(year, month) diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index 4015794377486..d466b0a8070ea 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -2758,22 +2758,35 @@ func (s *testEvaluatorSuite) TestLastDay(c *C) { c.Assert(result, Equals, test.expect) } - testsNull := []interface{}{ - "0000-00-00", - "1992-13-00", - "2007-10-07 23:59:61", - "2005-00-00", - "2005-00-01", - "2243-01 00:00:00", - 123456789} + var timeData types.Time + timeData.StrToDate(s.ctx.GetSessionVars().StmtCtx, "202010", "%Y%m") + testsNull := []struct { + param interface{} + isNilNoZeroDate bool + isNil bool + }{ + {"0000-00-00", true, true}, + {"1992-13-00", true, true}, + {"2007-10-07 23:59:61", true, true}, + {"2005-00-00", true, true}, + {"2005-00-01", true, true}, + {"2243-01 00:00:00", true, true}, + {123456789, true, true}, + {timeData, true, false}, + } for _, i := range testsNull { - t := []types.Datum{types.NewDatum(i)} + t := []types.Datum{types.NewDatum(i.param)} f, err := fc.getFunction(s.ctx, s.datumsToConstants(t)) c.Assert(err, IsNil) d, err := evalBuiltinFunc(f, chunk.Row{}) c.Assert(err, IsNil) - c.Assert(d.IsNull(), IsTrue) + c.Assert(d.IsNull() == i.isNilNoZeroDate, IsTrue) + s.ctx.GetSessionVars().SQLMode &= ^mysql.ModeNoZeroDate + d, err = evalBuiltinFunc(f, chunk.Row{}) + c.Assert(err, IsNil) + c.Assert(d.IsNull() == i.isNil, IsTrue) + s.ctx.GetSessionVars().SQLMode |= mysql.ModeNoZeroDate } } diff --git a/expression/builtin_time_vec.go b/expression/builtin_time_vec.go index 6f74a8f587e50..6bbad73063468 100644 --- a/expression/builtin_time_vec.go +++ b/expression/builtin_time_vec.go @@ -712,15 +712,15 @@ func (b *builtinLastDaySig) vecEvalTime(input *chunk.Chunk, result *chunk.Column if result.IsNull(i) { continue } - if times[i].InvalidZero() { + tm := times[i] + year, month := tm.Year(), tm.Month() + if tm.Month() == 0 || (tm.Day() == 0 && b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode()) { if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, times[i].String())); err != nil { return err } result.SetNull(i, true) continue } - tm := times[i] - year, month := tm.Year(), tm.Month() lastDay := types.GetLastDay(year, month) times[i] = types.NewTime(types.FromDate(year, month, lastDay, 0, 0, 0, 0), mysql.TypeDate, types.DefaultFsp) }