From 8f6537f63f4e41e5d9cf4084be4b341772ebdf47 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 17 Jul 2019 14:39:55 +0800 Subject: [PATCH] fix microseconds behaviour in DATE_ADD() (#11280) --- expression/integration_test.go | 12 ++++ types/fsp.go | 5 +- types/fsp_test.go | 122 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 types/fsp_test.go diff --git a/expression/integration_test.go b/expression/integration_test.go index be14c40c421a2..4f865aeb1ae13 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4180,3 +4180,15 @@ func (s *testIntegrationSuite) TestIssue10675(c *C) { testkit.Rows("1")) tk.MustQuery(`select * from t where a > 184467440737095516167.1;`).Check(testkit.Rows()) } + +func (s *testIntegrationSuite) TestIssue11257(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND);`).Check( + testkit.Rows("2007-03-28 22:08:27.800000")) + tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND);`).Check( + testkit.Rows("2007-03-28 22:08:27.800000")) + tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND);`).Check( + testkit.Rows("2007-03-28 22:08:27.800000")) + tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND);`).Check( + testkit.Rows("2007-03-28 22:08:27.800000")) +} diff --git a/types/fsp.go b/types/fsp.go index fe5a656cd87bb..c9709822c6453 100644 --- a/types/fsp.go +++ b/types/fsp.go @@ -86,9 +86,12 @@ func ParseFrac(s string, fsp int) (v int, overflow bool, err error) { return } -// alignFrac is used to generate alignment frac, like `100` -> `100000` +// alignFrac is used to generate alignment frac, like `100` -> `100000` ,`-100` -> `-100000` func alignFrac(s string, fsp int) string { sl := len(s) + if sl > 0 && s[0] == '-' { + sl = sl - 1 + } if sl < fsp { return s + strings.Repeat("0", fsp-sl) } diff --git a/types/fsp_test.go b/types/fsp_test.go new file mode 100644 index 0000000000000..b8f29cd4077d7 --- /dev/null +++ b/types/fsp_test.go @@ -0,0 +1,122 @@ +// Copyright 2019 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package types + +import ( + "strconv" + + . "github.com/pingcap/check" +) + +var _ = Suite(&FspTest{}) + +type FspTest struct{} + +func (s *FspTest) TestCheckFsp(c *C) { + c.Parallel() + obtained, err := CheckFsp(UnspecifiedFsp) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, IsNil) + + obtained, err = CheckFsp(-2019) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, ErrorMatches, "Invalid fsp -2019") + + obtained, err = CheckFsp(MinFsp - 4294967296) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MinFsp-4294967296)) + + // UnspecifiedFsp + obtained, err = CheckFsp(-1) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, IsNil) + + obtained, err = CheckFsp(MaxFsp + 1) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+1)) + + obtained, err = CheckFsp(MaxFsp + 2019) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+2019)) + + obtained, err = CheckFsp(MaxFsp + 4294967296) + c.Assert(obtained, Equals, DefaultFsp) + c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+4294967296)) + + obtained, err = CheckFsp((MaxFsp + MinFsp) / 2) + c.Assert(obtained, Equals, (MaxFsp+MinFsp)/2) + c.Assert(err, IsNil) + + obtained, err = CheckFsp(5) + c.Assert(obtained, Equals, 5) + c.Assert(err, IsNil) +} + +func (s *FspTest) TestParseFrac(c *C) { + c.Parallel() + obtained, overflow, err := ParseFrac("", 5) + c.Assert(obtained, Equals, 0) + c.Assert(overflow, Equals, false) + c.Assert(err, IsNil) + + obtained, overflow, err = ParseFrac("999", 200) + c.Assert(obtained, Equals, 0) + c.Assert(overflow, Equals, false) + c.Assert(err, ErrorMatches, "Invalid fsp .*") + + obtained, overflow, err = ParseFrac("NotNum", MaxFsp) + c.Assert(obtained, Equals, 0) + c.Assert(overflow, Equals, false) + c.Assert(err, ErrorMatches, "strconv.ParseInt:.*") + + obtained, overflow, err = ParseFrac("1235", 6) + c.Assert(obtained, Equals, 123500) + c.Assert(overflow, Equals, false) + c.Assert(err, IsNil) + + obtained, overflow, err = ParseFrac("123456", 4) + c.Assert(obtained, Equals, 123500) + c.Assert(overflow, Equals, false) + c.Assert(err, IsNil) + + // 1236 round 3 -> 124 -> 124000 + obtained, overflow, err = ParseFrac("1236", 3) + c.Assert(obtained, Equals, 124000) + c.Assert(overflow, Equals, false) + c.Assert(err, IsNil) + + // 03123 round 2 -> 3 -> 30000 + obtained, overflow, err = ParseFrac("0312", 2) + c.Assert(obtained, Equals, 30000) + c.Assert(overflow, Equals, false) + c.Assert(err, IsNil) + + // 999 round 2 -> 100 -> overflow + obtained, overflow, err = ParseFrac("999", 2) + c.Assert(obtained, Equals, 0) + c.Assert(overflow, Equals, true) + c.Assert(err, IsNil) +} + +func (s *FspTest) TestAlignFrac(c *C) { + c.Parallel() + obtained := alignFrac("100", 6) + c.Assert(obtained, Equals, "100000") + obtained = alignFrac("10000000000", 6) + c.Assert(obtained, Equals, "10000000000") + obtained = alignFrac("-100", 6) + c.Assert(obtained, Equals, "-100000") + obtained = alignFrac("-10000000000", 6) + c.Assert(obtained, Equals, "-10000000000") +}