From b58c62cf60f40cdeb8207b4cb6444ee4ae3d9d82 Mon Sep 17 00:00:00 2001 From: Zhuomin Liu Date: Fri, 20 Aug 2021 17:28:02 +0800 Subject: [PATCH] cherry pick #27244 to release-5.0 Signed-off-by: ti-srebot --- executor/executor_test.go | 10 ++++++++++ expression/builtin_time.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index 0784661c4e1dc..90c01be92fdf7 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -4951,6 +4951,16 @@ func (s *testSuiteP2) TestAddDateBuiltinWithWarnings(c *C) { tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Incorrect datetime value: '2001-01-00'")) } +func (s *testSuiteP2) TestIssue27232(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a timestamp)") + tk.MustExec("insert into t values (\"1970-07-23 10:04:59\"), (\"2038-01-19 03:14:07\")") + tk.MustQuery("select * from t where date_sub(a, interval 10 month) = date_sub(\"1970-07-23 10:04:59\", interval 10 month)").Check(testkit.Rows("1970-07-23 10:04:59")) + tk.MustQuery("select * from t where timestampadd(hour, 1, a ) = timestampadd(hour, 1, \"2038-01-19 03:14:07\")").Check(testkit.Rows("2038-01-19 03:14:07")) +} + func (s *testSuiteP2) TestStrToDateBuiltinWithWarnings(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("set @@sql_mode='NO_ZERO_DATE'") diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 1d52cf6adc2c3..dcfc6c22d95f0 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -3348,6 +3348,16 @@ func (c *addDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres if err != nil { return nil, err } + if dateEvalTp == types.ETDatetime && args[0].GetType().Tp == mysql.TypeTimestamp { + tp := types.NewFieldType(mysql.TypeDatetime) + tp.Decimal = args[0].GetType().Decimal + tp.Flen = mysql.MaxDatetimeWidthNoFsp + if tp.Decimal > 0 { + tp.Flen = tp.Flen + 1 + tp.Decimal + } + types.SetBinChsClnFlag(tp) + args[0] = BuildCastFunction(ctx, args[0], tp) + } bf.tp.Flen, bf.tp.Decimal = mysql.MaxDatetimeFullWidth, types.UnspecifiedLength } @@ -4022,6 +4032,16 @@ func (c *subDateFunctionClass) getFunction(ctx sessionctx.Context, args []Expres if err != nil { return nil, err } + if dateEvalTp == types.ETDatetime && args[0].GetType().Tp == mysql.TypeTimestamp { + tp := types.NewFieldType(mysql.TypeDatetime) + tp.Decimal = args[0].GetType().Decimal + tp.Flen = mysql.MaxDatetimeWidthNoFsp + if tp.Decimal > 0 { + tp.Flen = tp.Flen + 1 + tp.Decimal + } + types.SetBinChsClnFlag(tp) + args[0] = BuildCastFunction(ctx, args[0], tp) + } bf.tp.Flen, bf.tp.Decimal = mysql.MaxDatetimeFullWidth, types.UnspecifiedLength }