From 699c63d5a2985a3b76cf862310ea06e90074d270 Mon Sep 17 00:00:00 2001 From: Zhuomin Liu Date: Tue, 6 Jul 2021 18:51:29 +0800 Subject: [PATCH] cherry pick #25953 to release-5.0 Signed-off-by: ti-srebot --- expression/builtin_time.go | 2 +- expression/builtin_time_test.go | 33 +++++++++++++++++++++++---------- expression/builtin_time_vec.go | 6 +++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 1d52cf6adc2c3..6c318e7955b41 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -7034,7 +7034,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 aaaa4514192ec..d877c32e9e843 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -2781,22 +2781,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 94c1cd8b6f0c4..bc20b54bc17b9 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) }