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

types: fix wrong hash key for decimal #19131

Merged
12 changes: 12 additions & 0 deletions executor/join_test.go
Expand Up @@ -2166,3 +2166,15 @@ func (s *testSuite9) TestIssue18572_3(c *C) {
_, err = session.GetRows4Test(context.Background(), nil, rs)
c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinBuildErr"), IsTrue)
}

func (s *testSuite9) TestIssue19112(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 ( c_int int, c_decimal decimal(12, 6), key(c_int), unique key(c_decimal) )")
tk.MustExec("create table t2 like t1")
tk.MustExec("insert into t1 (c_int, c_decimal) values (1, 4.064000), (2, 0.257000), (3, 1.010000)")
tk.MustExec("insert into t2 (c_int, c_decimal) values (1, 4.064000), (3, 1.010000)")
tk.MustQuery("select /*+ HASH_JOIN(t1,t2) */ * from t1 join t2 on t1.c_decimal = t2.c_decimal order by t1.c_int").Check(testkit.Rows(
"1 4.064000 1 4.064000",
"3 1.010000 3 1.010000"))
}
7 changes: 7 additions & 0 deletions types/mydecimal.go
Expand Up @@ -14,6 +14,7 @@
package types

import (
"encoding/binary"
"math"
"strconv"

Expand Down Expand Up @@ -1301,6 +1302,12 @@ func (d *MyDecimal) ToHashKey() ([]byte, error) {
// thus ErrTruncated may be raised, we can ignore it here.
err = nil
}
precBuff := make([]byte, 4)
SunRunAway marked this conversation as resolved.
Show resolved Hide resolved
digitBuff := make([]byte, 4)
binary.LittleEndian.PutUint32(precBuff, uint32(prec))
binary.LittleEndian.PutUint32(digitBuff, uint32(digitsFrac))
SunRunAway marked this conversation as resolved.
Show resolved Hide resolved
buf = append(buf, precBuff...)
buf = append(buf, digitBuff...)
SunRunAway marked this conversation as resolved.
Show resolved Hide resolved
return buf, err
}

Expand Down
2 changes: 2 additions & 0 deletions types/mydecimal_test.go
Expand Up @@ -208,6 +208,8 @@ func (s *testMyDecimalSuite) TestToHashKey(c *C) {
var dec MyDecimal
c.Check(dec.FromString([]byte(num)), IsNil)
key, err := dec.ToHashKey()
// remove prec and digit len
key = key[:len(key)-8]
c.Check(err, IsNil)
keys = append(keys, string(key))
}
Expand Down