Skip to content

Commit

Permalink
cherry pick pingcap#22507 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
jyz0309 authored and ti-srebot committed Mar 9, 2021
1 parent 765c362 commit 32cf4b1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
59 changes: 59 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,65 @@ func (s *testSerialSuite1) TestIssue20724(c *C) {
tk.MustExec("drop table t1")
}

<<<<<<< HEAD
=======
func (s *testSerialSuite) TestIssue20840(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableClusteredIndex = false
tk.MustExec("create table t1 (i varchar(20) unique key) collate=utf8mb4_general_ci")
tk.MustExec("insert into t1 values ('a')")
tk.MustExec("replace into t1 values ('A')")
tk.MustQuery("select * from t1").Check(testkit.Rows("A"))
tk.MustExec("drop table t1")
}

func (s *testSerialSuite) TestIssue22496(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t12")
tk.MustExec("create table t12(d decimal(15,2));")
_, err := tk.Exec("insert into t12 values('1,9999.00')")
c.Assert(err, NotNil)
tk.MustExec("set sql_mode=''")
tk.MustExec("insert into t12 values('1,999.00');")
tk.MustQuery("SELECT * FROM t12;").Check(testkit.Rows("1.00"))
tk.MustExec("drop table t12")
}

func (s *testSuite) TestEqualDatumsAsBinary(c *C) {
tests := []struct {
a []interface{}
b []interface{}
same bool
}{
// Positive cases
{[]interface{}{1}, []interface{}{1}, true},
{[]interface{}{1, "aa"}, []interface{}{1, "aa"}, true},
{[]interface{}{1, "aa", 1}, []interface{}{1, "aa", 1}, true},

// negative cases
{[]interface{}{1}, []interface{}{2}, false},
{[]interface{}{1, "a"}, []interface{}{1, "aaaaaa"}, false},
{[]interface{}{1, "aa", 3}, []interface{}{1, "aa", 2}, false},

// Corner cases
{[]interface{}{}, []interface{}{}, true},
{[]interface{}{nil}, []interface{}{nil}, true},
{[]interface{}{}, []interface{}{1}, false},
{[]interface{}{1}, []interface{}{1, 1}, false},
{[]interface{}{nil}, []interface{}{1}, false},
}
for _, tt := range tests {
testEqualDatumsAsBinary(c, tt.a, tt.b, tt.same)
}
}

>>>>>>> aa82a038f... types: fix the bug about the wrong query result for decimal type (#22507)
func (s *testSuite) TestIssue21232(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
9 changes: 9 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
v, err = Convert("-10000", ft)
c.Assert(terror.ErrorEqual(err, ErrOverflow), IsTrue, Commentf("err %v", err))
c.Assert(v.(*MyDecimal).String(), Equals, "-9999.9999")
v, err = Convert("1,999.00", ft)
c.Assert(terror.ErrorEqual(err, ErrBadNumber), IsTrue, Commentf("err %v", err))
c.Assert(v.(*MyDecimal).String(), Equals, "1.0000")
v, err = Convert("1,999,999.00", ft)
c.Assert(terror.ErrorEqual(err, ErrBadNumber), IsTrue, Commentf("err %v", err))
c.Assert(v.(*MyDecimal).String(), Equals, "1.0000")
v, err = Convert("199.00 ", ft)
c.Assert(err, IsNil)
c.Assert(v.(*MyDecimal).String(), Equals, "199.0000")

// Test Datum.ToDecimal with bad number.
d := NewDatum("hello")
Expand Down
7 changes: 7 additions & 0 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ func (d *MyDecimal) ToString() (str []byte) {

// FromString parses decimal from string.
func (d *MyDecimal) FromString(str []byte) error {
// strErr is used to check str is bad number or not
var strErr error
for i := 0; i < len(str); i++ {
if !isSpace(str[i]) {
str = str[i:]
Expand Down Expand Up @@ -419,6 +421,8 @@ func (d *MyDecimal) FromString(str []byte) error {
endIdx++
}
digitsFrac = endIdx - strIdx - 1
} else if strIdx < len(str) && (str[strIdx] != 'e' && str[strIdx] != 'E' && str[strIdx] != ' ') {
strErr = ErrBadNumber
} else {
digitsFrac = 0
endIdx = strIdx
Expand Down Expand Up @@ -519,6 +523,9 @@ func (d *MyDecimal) FromString(str []byte) error {
d.negative = false
}
d.resultFrac = d.digitsFrac
if strErr != nil {
return strErr
}
return err
}

Expand Down

0 comments on commit 32cf4b1

Please sign in to comment.