Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix converting decimal to datetime and timestamp (#9899) #10734

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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