Skip to content

Commit

Permalink
types: fix converting decimal to datetime and timestamp (#9899) (#10734)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored and zz-jason committed Jun 6, 2019
1 parent d07bdb2 commit 011caea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4057,3 +4057,15 @@ func (s *testIntegrationSuite) TestIssue10181(c *C) {
tk.MustExec(`insert into t values(9223372036854775807), (18446744073709551615)`)
tk.MustQuery(`select * from t where a > 9223372036854775807-0.5 order by a`).Check(testkit.Rows(`9223372036854775807`, `18446744073709551615`))
}

// for issue #9770
func (s *testIntegrationSuite) TestDecimalConvertToTime(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer s.cleanEnv(c)

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a datetime(6), b timestamp)")
tk.MustExec("insert t values (20010101100000.123456, 20110707101112.123456)")
tk.MustQuery("select * from t").Check(testkit.Rows("2001-01-01 10:00:00.123456 2011-07-07 10:11:12"))
}
2 changes: 2 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ func (s *testTypeConvertSuite) TestConvert(c *C) {
signedAccept(c, mysql.TypeDatetime, "2012-08-23 12:34:03.123456", "2012-08-23 12:34:03")
signedAccept(c, mysql.TypeDatetime, ZeroDatetime, "0000-00-00 00:00:00")
signedAccept(c, mysql.TypeDatetime, int64(0), "0000-00-00 00:00:00")
signedAccept(c, mysql.TypeDatetime, NewDecFromFloatForTest(20010101100000.123456), "2001-01-01 10:00:00")
signedAccept(c, mysql.TypeTimestamp, "2012-08-23 12:34:03.123456", "2012-08-23 12:34:03")
signedAccept(c, mysql.TypeTimestamp, NewDecFromFloatForTest(20010101100000.123456), "2001-01-01 10:00:00")
signedAccept(c, mysql.TypeDuration, "10:11:12", "10:11:12")
signedAccept(c, mysql.TypeDuration, ZeroDatetime, "00:00:00")
signedAccept(c, mysql.TypeDuration, ZeroDuration, "00:00:00")
Expand Down
4 changes: 4 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ func (d *Datum) convertToMysqlTimestamp(sc *stmtctx.StatementContext, target *Fi
t, err = ParseTime(sc, d.GetString(), mysql.TypeTimestamp, fsp)
case KindInt64:
t, err = ParseTimeFromNum(sc, d.GetInt64(), mysql.TypeTimestamp, fsp)
case KindMysqlDecimal:
t, err = ParseTimeFromFloatString(sc, d.GetMysqlDecimal().String(), mysql.TypeTimestamp, fsp)
default:
return invalidConv(d, mysql.TypeTimestamp)
}
Expand Down Expand Up @@ -985,6 +987,8 @@ func (d *Datum) convertToMysqlTime(sc *stmtctx.StatementContext, target *FieldTy
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(sc, fsp)
case KindMysqlDecimal:
t, err = ParseTimeFromFloatString(sc, d.GetMysqlDecimal().String(), tp, fsp)
case KindString, KindBytes:
t, err = ParseTime(sc, d.GetString(), tp, fsp)
case KindInt64:
Expand Down

0 comments on commit 011caea

Please sign in to comment.