From be56437ad493c3fe08b9fe85fa5c8da4c7fc3ef3 Mon Sep 17 00:00:00 2001 From: Ewan Chou Date: Sun, 6 Sep 2015 17:13:42 +0800 Subject: [PATCH 1/4] util/types: support convert year Convert support more types --- util/types/convert.go | 17 +++++++++++++++++ util/types/convert_test.go | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/util/types/convert.go b/util/types/convert.go index d725015e1774b..009fe17ff3a31 100644 --- a/util/types/convert.go +++ b/util/types/convert.go @@ -138,6 +138,23 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // } // TODO: check Flen return x, nil + case mysql.TypeYear: + var intVal int64 + var err error + switch x := val.(type) { + case string: + intVal, err = StrToInt(x) + default: + intVal, err = ToInt64(val) + } + if err != nil { + return InvConv(val, tp) + } + y, err := mysql.AdjustYear(int(intVal)) + if err != nil { + return InvConv(val, tp) + } + return int16(y), nil default: panic("should never happen") } diff --git a/util/types/convert_test.go b/util/types/convert_test.go index d5c69830b28e9..d07945ed5b9a5 100644 --- a/util/types/convert_test.go +++ b/util/types/convert_test.go @@ -153,6 +153,17 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) { v, err = Convert(3.1415926, ft) c.Assert(err, IsNil) c.Assert(v.(mysql.Decimal).String(), Equals, "3.14159") + + // Fot TypeYear + ft = NewFieldType(mysql.TypeYear) + v, err = Convert("2015-11-11", ft) + c.Assert(err, IsNil) + c.Assert(v, Equals, int16(2015)) + v, err = Convert(2015, ft) + c.Assert(err, IsNil) + c.Assert(v, Equals, int16(2015)) + v, err = Convert(1800, ft) + c.Assert(err, NotNil) } func testToInt64(c *C, val interface{}, expect int64) { From 0054f81c18163f0f90136a3499c98edbe1fb3342 Mon Sep 17 00:00:00 2001 From: Ewan Chou Date: Sun, 6 Sep 2015 17:22:09 +0800 Subject: [PATCH 2/4] util/types: address comments --- util/types/convert.go | 8 +++++--- util/types/convert_test.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/util/types/convert.go b/util/types/convert.go index 009fe17ff3a31..d5ea0b3dbcaf4 100644 --- a/util/types/convert.go +++ b/util/types/convert.go @@ -139,13 +139,15 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // // TODO: check Flen return x, nil case mysql.TypeYear: - var intVal int64 - var err error + var ( + intVal int64 + err error + ) switch x := val.(type) { case string: intVal, err = StrToInt(x) default: - intVal, err = ToInt64(val) + intVal, err = ToInt64(x) } if err != nil { return InvConv(val, tp) diff --git a/util/types/convert_test.go b/util/types/convert_test.go index d07945ed5b9a5..4df248cdc1bf5 100644 --- a/util/types/convert_test.go +++ b/util/types/convert_test.go @@ -154,7 +154,7 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) { c.Assert(err, IsNil) c.Assert(v.(mysql.Decimal).String(), Equals, "3.14159") - // Fot TypeYear + // For TypeYear ft = NewFieldType(mysql.TypeYear) v, err = Convert("2015-11-11", ft) c.Assert(err, IsNil) From 97ea35c4ba96840d197623b9890a834eea6c63e2 Mon Sep 17 00:00:00 2001 From: Ewan Chou Date: Sun, 6 Sep 2015 19:04:15 +0800 Subject: [PATCH 3/4] util/convert: convert year handle case mysql.Time --- util/types/convert.go | 2 ++ util/types/convert_test.go | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/util/types/convert.go b/util/types/convert.go index d5ea0b3dbcaf4..3e93c2a242601 100644 --- a/util/types/convert.go +++ b/util/types/convert.go @@ -146,6 +146,8 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // switch x := val.(type) { case string: intVal, err = StrToInt(x) + case mysql.Time: + return int16(x.Year()), nil default: intVal, err = ToInt64(x) } diff --git a/util/types/convert_test.go b/util/types/convert_test.go index 4df248cdc1bf5..9ba6e1ac769ec 100644 --- a/util/types/convert_test.go +++ b/util/types/convert_test.go @@ -164,6 +164,10 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) { c.Assert(v, Equals, int16(2015)) v, err = Convert(1800, ft) c.Assert(err, NotNil) + dt, err := mysql.ParseDate("2015-11-11") + c.Assert(err, IsNil) + v, err = Convert(dt, ft) + c.Assert(v, Equals, int16(2015)) } func testToInt64(c *C, val interface{}, expect int64) { From b595679d47e92d13bceafc70559029ae0d01c1fd Mon Sep 17 00:00:00 2001 From: Ewan Chou Date: Sun, 6 Sep 2015 20:09:52 +0800 Subject: [PATCH 4/4] util/types: handle convert year from duration --- util/types/convert.go | 3 +++ util/types/convert_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/util/types/convert.go b/util/types/convert.go index 3e93c2a242601..00577070a3e80 100644 --- a/util/types/convert.go +++ b/util/types/convert.go @@ -15,6 +15,7 @@ package types import ( "strconv" + "time" "unicode" "github.com/juju/errors" @@ -148,6 +149,8 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // intVal, err = StrToInt(x) case mysql.Time: return int16(x.Year()), nil + case mysql.Duration: + return int16(time.Now().Year()), nil default: intVal, err = ToInt64(x) } diff --git a/util/types/convert_test.go b/util/types/convert_test.go index 9ba6e1ac769ec..53f5495de1238 100644 --- a/util/types/convert_test.go +++ b/util/types/convert_test.go @@ -15,6 +15,7 @@ package types import ( "math" + "time" . "github.com/pingcap/check" mysql "github.com/pingcap/tidb/mysqldef" @@ -168,6 +169,8 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) { c.Assert(err, IsNil) v, err = Convert(dt, ft) c.Assert(v, Equals, int16(2015)) + v, err = Convert(mysql.ZeroDuration, ft) + c.Assert(v, Equals, int16(time.Now().Year())) } func testToInt64(c *C, val interface{}, expect int64) {