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) #19185

Merged
merged 3 commits into from
Aug 14, 2020
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 executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,15 @@ func (s *testSuite) TestIssue15958(c *C) {
tk.MustQuery(`select sum(y) from t`).Check(testkit.Rows("6070"))
tk.MustQuery(`select avg(y) from t`).Check(testkit.Rows("2023.3333"))
}

func (s *testSuite) 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 @@ -1458,3 +1458,15 @@ func (s *testSuite2) TestIssue14514(c *C) {
tk.MustExec("create table t (pk varchar(14) primary key, a varchar(12));")
tk.MustQuery("select * from (select t1.pk or '/' as c from t as t1 left join t as t2 on t1.a = t2.pk) as t where t.c = 1;").Check(testkit.Rows())
}

func (s *testSuite2) 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 @@ -1209,10 +1209,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 @@ -1283,6 +1286,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 @@ -152,7 +152,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 @@ -203,6 +203,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 @@ -564,6 +566,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