Skip to content

Commit

Permalink
cherry pick pingcap#27244 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
lzmhhh123 authored and ti-srebot committed Aug 20, 2021
1 parent 21ef753 commit 1f9ffec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
29 changes: 29 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4457,6 +4457,35 @@ func (s *testSuiteP2) TestStrToDateBuiltin(c *C) {
tk.MustQuery(`SELECT STR_TO_DATE('2020-07-04 00:22:33', '%Y-%m-%d %T')`).Check(testkit.Rows("2020-07-04 00:22:33"))
}

<<<<<<< HEAD
=======
func (s *testSuiteP2) TestAddDateBuiltinWithWarnings(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@sql_mode='NO_ZERO_DATE'")
result := tk.MustQuery(`select date_add('2001-01-00', interval -2 hour);`)
result.Check(testkit.Rows("<nil>"))
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'")
tk.MustExec("use test")
tk.MustQuery(`SELECT STR_TO_DATE('0000-1-01', '%Y-%m-%d');`).Check(testkit.Rows("<nil>"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1411 Incorrect datetime value: '0000-1-01' for function str_to_date"))
}

>>>>>>> c9c3463ac... expression: fix wrong result for date add sub (#27244)
func (s *testSuiteP2) TestReadPartitionedTable(c *C) {
// Test three reader on partitioned table.
tk := testkit.NewTestKit(c, s.store)
Expand Down
20 changes: 20 additions & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3308,6 +3308,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
}

Expand Down Expand Up @@ -3982,6 +3992,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
}

Expand Down

0 comments on commit 1f9ffec

Please sign in to comment.