Skip to content

Commit

Permalink
expression, executor: castStringAsTime returns null when sql_mode is …
Browse files Browse the repository at this point in the history
…not strict (#8516) (#8966)
  • Loading branch information
XuHuaiyu authored and zz-jason committed Jan 7, 2019
1 parent 665db64 commit ae225de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,17 @@ func (s *testSuite) TestUpdate(c *C) {
tk.MustExec("insert into t values (0.0);")
_, err = tk.Exec("update t set c1 = 2.0;")
c.Assert(types.ErrWarnDataOutOfRange.Equal(err), IsTrue)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a datetime not null, b datetime)")
tk.MustExec("insert into t value('1999-12-12', '1999-12-13')")
tk.MustExec(" set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustQuery("select * from t").Check(testkit.Rows("1999-12-12 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set a = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set b = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 <nil>"))
tk.MustExec("set @@sql_mode=@orig_sql_mode;")
}

func (s *testSuite) TestPartitionedTableUpdate(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,13 +1175,13 @@ func (b *builtinCastStringAsTimeSig) evalTime(row chunk.Row) (res types.Time, is
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
return types.Time{}, true, handleInvalidTimeError(b.ctx, err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, errors.Trace(err)
return res, false, nil
}

type builtinCastStringAsDurationSig struct {
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,8 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1969-12-31 23:59:59');")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-13-01 00:00:00');")
result.Check(testkit.Rows("0"))
// FIXME: MySQL returns 0 here.
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:07.999999');")
result.Check(testkit.Rows("2147483647.999999"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:08');")
Expand Down

0 comments on commit ae225de

Please sign in to comment.