Skip to content

Commit

Permalink
types: fix wrong hash key for decimal (#19131) (#19188)
Browse files Browse the repository at this point in the history
* cherry pick #19131 to release-4.0

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

* fix conflicts

Co-authored-by: Zhuomin(Charming) Liu <lzmhhh123@gmail.com>
Co-authored-by: jebter <jebter@126.com>
  • Loading branch information
3 people authored Aug 18, 2020
1 parent e10bd72 commit d890499
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
12 changes: 12 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,15 @@ func (s *testSuiteAgg) TestIssue15690(c *C) {
tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("<nil>", "a", "b"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(0))
}

func (s *testSuiteAgg) TestIssue17216(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec(`CREATE TABLE t1 (
pk int(11) NOT NULL,
col1 decimal(40,20) DEFAULT NULL
)`)
tk.MustExec(`INSERT INTO t1 VALUES (2084,0.02040000000000000000),(35324,0.02190000000000000000),(43760,0.00510000000000000000),(46084,0.01400000000000000000),(46312,0.00560000000000000000),(61632,0.02730000000000000000),(94676,0.00660000000000000000),(102244,0.01810000000000000000),(113144,0.02140000000000000000),(157024,0.02750000000000000000),(157144,0.01750000000000000000),(182076,0.02370000000000000000),(188696,0.02330000000000000000),(833,0.00390000000000000000),(6701,0.00230000000000000000),(8533,0.01690000000000000000),(13801,0.01360000000000000000),(20797,0.00680000000000000000),(36677,0.00550000000000000000),(46305,0.01290000000000000000),(76113,0.00430000000000000000),(76753,0.02400000000000000000),(92393,0.01720000000000000000),(111733,0.02690000000000000000),(152757,0.00250000000000000000),(162393,0.02760000000000000000),(167169,0.00440000000000000000),(168097,0.01360000000000000000),(180309,0.01720000000000000000),(19918,0.02620000000000000000),(58674,0.01820000000000000000),(67454,0.01510000000000000000),(70870,0.02880000000000000000),(89614,0.02530000000000000000),(106742,0.00180000000000000000),(107886,0.01580000000000000000),(147506,0.02230000000000000000),(148366,0.01340000000000000000),(167258,0.01860000000000000000),(194438,0.00500000000000000000),(10307,0.02850000000000000000),(14539,0.02210000000000000000),(27703,0.00050000000000000000),(32495,0.00680000000000000000),(39235,0.01450000000000000000),(52379,0.01640000000000000000),(54551,0.01910000000000000000),(85659,0.02330000000000000000),(104483,0.02670000000000000000),(109911,0.02040000000000000000),(114523,0.02110000000000000000),(119495,0.02120000000000000000),(137603,0.01910000000000000000),(154031,0.02580000000000000000);`)
tk.MustQuery("SELECT count(distinct col1) FROM t1").Check(testkit.Rows("48"))
}
12 changes: 12 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2162,3 +2162,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"))
}
8 changes: 6 additions & 2 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,13 @@ func (d *MyDecimal) ToBin(precision, frac int) ([]byte, error) {
}
}

if fracSize < fracSizeFrom {
if fracSize < fracSizeFrom ||
(fracSize == fracSizeFrom && (trailingDigits <= trailingDigitsFrom || wordsFrac <= wordsFracFrom)) {
if fracSize < fracSizeFrom || (fracSize == fracSizeFrom && trailingDigits < trailingDigitsFrom) || (fracSize == fracSizeFrom && wordsFrac < wordsFracFrom) {
err = ErrTruncated
}
wordsFracFrom = wordsFrac
trailingDigitsFrom = trailingDigits
err = ErrTruncated
} else if fracSize > fracSizeFrom && trailingDigitsFrom > 0 {
if wordsFrac == wordsFracFrom {
trailingDigitsFrom = trailingDigits
Expand Down Expand Up @@ -1290,6 +1293,7 @@ func (d *MyDecimal) ToHashKey() ([]byte, error) {
// thus ErrTruncated may be raised, we can ignore it here.
err = nil
}
buf = append(buf, byte(digitsFrac))
return buf, err
}

Expand Down
20 changes: 19 additions & 1 deletion types/mydecimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *testMyDecimalSuite) TestToHashKey(c *C) {
}{
{[]string{"1.1", "1.1000", "1.1000000", "1.10000000000", "01.1", "0001.1", "001.1000000"}},
{[]string{"-1.1", "-1.1000", "-1.1000000", "-1.10000000000", "-01.1", "-0001.1", "-001.1000000"}},
{[]string{".1", "0.1", "000000.1", ".10000", "0000.10000", "000000000000000000.1"}},
{[]string{".1", "0.1", "0.10", "000000.1", ".10000", "0000.10000", "000000000000000000.1"}},
{[]string{"0", "0000", ".0", ".00000", "00000.00000", "-0", "-0000", "-.0", "-.00000", "-00000.00000"}},
{[]string{".123456789123456789", ".1234567891234567890", ".12345678912345678900", ".123456789123456789000", ".1234567891234567890000", "0.123456789123456789",
".1234567891234567890000000000", "0000000.123456789123456789000"}},
Expand Down 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 digit len
key = key[:len(key)-1]
c.Check(err, IsNil)
keys = append(keys, string(key))
}
Expand Down Expand Up @@ -571,6 +573,22 @@ func (s *testMyDecimalSuite) TestToBinFromBin(c *C) {
{"000000000.01", 7, 3, "0.010", nil},
{"123.4", 10, 2, "123.40", nil},
{"1000", 3, 0, "0", ErrOverflow},
{"0.1", 1, 1, "0.1", nil},
{"0.100", 1, 1, "0.1", ErrTruncated},
{"0.1000", 1, 1, "0.1", ErrTruncated},
{"0.10000", 1, 1, "0.1", ErrTruncated},
{"0.100000", 1, 1, "0.1", ErrTruncated},
{"0.1000000", 1, 1, "0.1", ErrTruncated},
{"0.10", 1, 1, "0.1", ErrTruncated},
{"0000000000000000000000000000000000000000000.000000000000123000000000000000", 15, 15, "0.000000000000123", ErrTruncated},
{"00000000000000000000000000000.00000000000012300", 15, 15, "0.000000000000123", ErrTruncated},
{"0000000000000000000000000000000000000000000.0000000000001234000000000000000", 16, 16, "0.0000000000001234", ErrTruncated},
{"00000000000000000000000000000.000000000000123400", 16, 16, "0.0000000000001234", ErrTruncated},
{"0.1", 2, 2, "0.10", nil},
{"0.10", 3, 3, "0.100", nil},
{"0.1", 3, 1, "0.1", nil},
{"0.0000000000001234", 32, 17, "0.00000000000012340", nil},
{"0.0000000000001234", 20, 20, "0.00000000000012340000", nil},
}
for _, ca := range tests {
var dec MyDecimal
Expand Down

0 comments on commit d890499

Please sign in to comment.