From fc89a6421a4c3ace8624ba17ac127e958d422732 Mon Sep 17 00:00:00 2001 From: lysu Date: Tue, 14 Aug 2018 13:20:01 +0800 Subject: [PATCH] expression,ddl: fix fraction part handle of current_timestamp (#7355) --- ddl/db_test.go | 2 +- executor/executor_test.go | 34 ++++++++++++++++++++++++++++++++++ expression/helper.go | 3 ++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 69921cb423071..e134ddadbc615 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -1760,7 +1760,7 @@ func (s *testDBSuite) TestTableDDLWithTimeType(c *C) { s.testErrorCode(c, "alter table t change column a aa time(7)", tmysql.ErrTooBigPrecision) s.testErrorCode(c, "alter table t change column a aa datetime(7)", tmysql.ErrTooBigPrecision) s.testErrorCode(c, "alter table t change column a aa timestamp(7)", tmysql.ErrTooBigPrecision) - s.mustExec(c, "alter table t change column a aa timestamp(0)") + s.mustExec(c, "alter table t change column a aa datetime(0)") s.mustExec(c, "drop table t") } diff --git a/executor/executor_test.go b/executor/executor_test.go index e2e53470e550e..ae0156ea61570 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -3088,3 +3088,37 @@ func (s *testSuite) TestMaxOneRow(c *C) { err = rs.Close() c.Assert(err, IsNil) } + +func (s *testSuite) TestCurrentTimestampValueSelection(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t,t1") + + tk.MustExec("create table t (id int, t0 timestamp null default current_timestamp, t1 timestamp(1) null default current_timestamp(1), t2 timestamp(2) null default current_timestamp(2) on update current_timestamp(2))") + tk.MustExec("insert into t (id) values (1)") + rs := tk.MustQuery("select t0, t1, t2 from t where id = 1") + t0 := rs.Rows()[0][0].(string) + t1 := rs.Rows()[0][1].(string) + t2 := rs.Rows()[0][2].(string) + c.Assert(len(strings.Split(t0, ".")), Equals, 1) + c.Assert(len(strings.Split(t1, ".")[1]), Equals, 1) + c.Assert(len(strings.Split(t2, ".")[1]), Equals, 2) + tk.MustQuery("select id from t where t0 = ?", t0).Check(testkit.Rows("1")) + tk.MustQuery("select id from t where t1 = ?", t1).Check(testkit.Rows("1")) + tk.MustQuery("select id from t where t2 = ?", t2).Check(testkit.Rows("1")) + time.Sleep(time.Second / 2) + tk.MustExec("update t set t0 = now() where id = 1") + rs = tk.MustQuery("select t2 from t where id = 1") + newT2 := rs.Rows()[0][0].(string) + c.Assert(newT2 != t2, IsTrue) + + tk.MustExec("create table t1 (id int, a timestamp, b timestamp(2), c timestamp(3))") + tk.MustExec("insert into t1 (id, a, b, c) values (1, current_timestamp(2), current_timestamp, current_timestamp(3))") + rs = tk.MustQuery("select a, b, c from t1 where id = 1") + a := rs.Rows()[0][0].(string) + b := rs.Rows()[0][1].(string) + d := rs.Rows()[0][2].(string) + c.Assert(len(strings.Split(a, ".")), Equals, 1) + c.Assert(strings.Split(b, ".")[1], Equals, "00") + c.Assert(len(strings.Split(d, ".")[1]), Equals, 3) +} diff --git a/expression/helper.go b/expression/helper.go index a7bc83fcb7770..813f45e903c85 100644 --- a/expression/helper.go +++ b/expression/helper.go @@ -14,6 +14,7 @@ package expression import ( + "math" "strings" "time" @@ -57,7 +58,7 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d ty case string: upperX := strings.ToUpper(x) if upperX == strings.ToUpper(ast.CurrentTimestamp) { - value.Time = types.FromGoTime(defaultTime) + value.Time = types.FromGoTime(defaultTime.Truncate(time.Duration(math.Pow10(9-fsp)) * time.Nanosecond)) if tp == mysql.TypeTimestamp { err = value.ConvertTimeZone(time.Local, ctx.GetSessionVars().Location()) if err != nil {