From 8bc1f6a9f654e3cc321563a047879a3b84a2cdaa Mon Sep 17 00:00:00 2001 From: robi Date: Mon, 27 Aug 2018 21:54:17 +0800 Subject: [PATCH 1/3] server: fix insert zero timestamp bug with prepared statement --- server/conn_stmt.go | 2 +- server/conn_stmt_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/conn_stmt.go b/server/conn_stmt.go index 9ab74ec2cb72e..66e4a1dde8c7f 100644 --- a/server/conn_stmt.go +++ b/server/conn_stmt.go @@ -354,7 +354,7 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy pos++ switch length { case 0: - args[i] = "0" + args[i] = "00000000000000" case 4: pos, args[i] = parseBinaryDate(pos, paramValues) case 7: diff --git a/server/conn_stmt_test.go b/server/conn_stmt_test.go index fdfce13a338fe..7196c3b6e8c00 100644 --- a/server/conn_stmt_test.go +++ b/server/conn_stmt_test.go @@ -120,7 +120,7 @@ func (ts ConnTestSuite) TestParseStmtArgs(c *C) { []byte{0x00}, }, nil, - "0", + "00000000000000", }, // Tests for time { From f503c6244cff7ef5e1f767ae09c7e8f2430e49a7 Mon Sep 17 00:00:00 2001 From: robi Date: Mon, 27 Aug 2018 22:06:56 +0800 Subject: [PATCH 2/3] add test for half-part --- session/session_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/session/session_test.go b/session/session_test.go index 88f921afc7391..39f36e13b2ab0 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -630,6 +630,19 @@ func (s *testSessionSuite) TestLastInsertID(c *C) { c.Assert(lastInsertID+2, Equals, currLastInsertID) } +func (s *testSessionSuite) TestPrepareZero(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(v timestamp)") + tk.MustExec("prepare s1 from 'insert into t (v) values (?)'") + tk.MustExec("set @v1='0'") + _, rs := tk.Exec("execute s1 using @v1") + c.Assert(rs, NotNil) + tk.MustExec("set @v2='00000000000000'") + tk.MustExec("execute s1 using @v2") + tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00")) +} + func (s *testSessionSuite) TestPrimaryKeyAutoIncrement(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("drop table if exists t") From 106ae6fd6846c615aa385902a1806e2ad9747936 Mon Sep 17 00:00:00 2001 From: robi Date: Tue, 28 Aug 2018 11:00:32 +0800 Subject: [PATCH 3/3] remove magic const --- server/conn_stmt.go | 3 ++- server/conn_stmt_test.go | 3 ++- session/session_test.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/server/conn_stmt.go b/server/conn_stmt.go index 66e4a1dde8c7f..d874d8ea3d182 100644 --- a/server/conn_stmt.go +++ b/server/conn_stmt.go @@ -42,6 +42,7 @@ import ( "github.com/juju/errors" "github.com/pingcap/tidb/mysql" + "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/hack" "golang.org/x/net/context" ) @@ -354,7 +355,7 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy pos++ switch length { case 0: - args[i] = "00000000000000" + args[i] = types.ZeroDatetimeStr case 4: pos, args[i] = parseBinaryDate(pos, paramValues) case 7: diff --git a/server/conn_stmt_test.go b/server/conn_stmt_test.go index 7196c3b6e8c00..d131e39d6f22c 100644 --- a/server/conn_stmt_test.go +++ b/server/conn_stmt_test.go @@ -17,6 +17,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/terror" + "github.com/pingcap/tidb/types" ) func (ts ConnTestSuite) TestParseStmtArgs(c *C) { @@ -120,7 +121,7 @@ func (ts ConnTestSuite) TestParseStmtArgs(c *C) { []byte{0x00}, }, nil, - "00000000000000", + types.ZeroDatetimeStr, }, // Tests for time { diff --git a/session/session_test.go b/session/session_test.go index 39f36e13b2ab0..d95d7e1a58032 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -638,7 +638,7 @@ func (s *testSessionSuite) TestPrepareZero(c *C) { tk.MustExec("set @v1='0'") _, rs := tk.Exec("execute s1 using @v1") c.Assert(rs, NotNil) - tk.MustExec("set @v2='00000000000000'") + tk.MustExec("set @v2='" + types.ZeroDatetimeStr + "'") tk.MustExec("execute s1 using @v2") tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00")) }