Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix microseconds behaviour in DATE_ADD() (#11280) #11289

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
5 changes: 4 additions & 1 deletion types/fsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
122 changes: 122 additions & 0 deletions types/fsp_test.go
Original file line number Diff line number Diff line change
@@ -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")
}