From 335b65f1bd546c6857bd16e67435b9e6105e7349 Mon Sep 17 00:00:00 2001 From: robi Date: Thu, 23 Aug 2018 11:23:15 +0800 Subject: [PATCH] expression: fix fraction part handle of current_timestamp (#7355) --- executor/executor_test.go | 34 ++++++++++++++++++++++++++++++++++ expression/helper.go | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index 855fb6cede171..5241f94c49a80 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2833,3 +2833,37 @@ func (s *testSuite) TestIndexJoinTableDualPanic(c *C) { tk.MustQuery("select a.* from a inner join (select 1 as k1,'k2-1' as k2) as k on a.f1=k.k1;"). Check(testkit.Rows("1 a")) } + +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 9210cf47dbafe..6e21bed2f2525 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().GetTimeZone()) if err != nil {