Skip to content

Commit

Permalink
types: fix converting decimal to datetime and timestamp (#9899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Debiancc authored and zz-jason committed Mar 30, 2019
1 parent b758907 commit 424995d
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 @@ -4116,3 +4116,15 @@ func (s *testIntegrationSuite) TestIssue9710(c *C) {
break
}
}

// 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 @@ -630,7 +630,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 @@ -961,6 +961,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 @@ -998,6 +1000,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 424995d

Please sign in to comment.