diff --git a/expression/integration_test.go b/expression/integration_test.go index 71ecef6c6b692..5c45fe3edcdd8 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -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")) +} diff --git a/types/convert_test.go b/types/convert_test.go index 51816860f2795..dd50805cb6122 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -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") diff --git a/types/datum.go b/types/datum.go index 7fa790c4ded85..bea8aff87d2e2 100644 --- a/types/datum.go +++ b/types/datum.go @@ -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) } @@ -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: