From 8f73e010238c6c0e70a6ac4ef665b38744cf3924 Mon Sep 17 00:00:00 2001 From: "Zhuomin(Charming) Liu" Date: Thu, 13 Aug 2020 19:29:17 +0800 Subject: [PATCH 1/3] cherry pick #19131 to release-3.0 Signed-off-by: ti-srebot --- executor/aggregate_test.go | 12 +++ executor/join_test.go | 159 +++++++++++++++++++++++++++++++++++++ types/mydecimal.go | 8 +- types/mydecimal_test.go | 20 ++++- 4 files changed, 196 insertions(+), 3 deletions(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index 311c55ace4fe7..a46eaa57d23b3 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -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 *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")) +} diff --git a/executor/join_test.go b/executor/join_test.go index 1b34c8c868ba0..e4c49af0e7ac1 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -1458,3 +1458,162 @@ 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()) } +<<<<<<< HEAD +======= + +func (s *testSuiteJoinSerial) TestOuterMatchStatusIssue14742(c *C) { + plannercore.ForceUseOuterBuild4Test = true + defer func() { plannercore.ForceUseOuterBuild4Test = false }() + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists testjoin;") + tk.MustExec("create table testjoin(a int);") + tk.Se.GetSessionVars().MaxChunkSize = 2 + + tk.MustExec("insert into testjoin values (NULL);") + tk.MustExec("insert into testjoin values (1);") + tk.MustExec("insert into testjoin values (2), (2), (2);") + tk.MustQuery("SELECT * FROM testjoin t1 RIGHT JOIN testjoin t2 ON t1.a > t2.a order by t1.a, t2.a;").Check(testkit.Rows( + " ", + " 2", + " 2", + " 2", + "2 1", + "2 1", + "2 1", + )) +} + +func (s *testSuiteJoinSerial) TestInlineProjection4HashJoinIssue15316(c *C) { + // Two necessary factors to reproduce this issue: + // (1) taking HashLeftJoin, i.e., letting the probing tuple lay at the left side of joined tuples + // (2) the projection only contains a part of columns from the build side, i.e., pruning the same probe side + plannercore.ForcedHashLeftJoin4Test = true + defer func() { plannercore.ForcedHashLeftJoin4Test = false }() + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table S (a int not null, b int, c int);") + tk.MustExec("create table T (a int not null, b int, c int);") + tk.MustExec("insert into S values (0,1,2),(0,1,null),(0,1,2);") + tk.MustExec("insert into T values (0,10,2),(0,10,null),(1,10,2);") + tk.MustQuery("select T.a,T.a,T.c from S join T on T.a = S.a where S.b", + "0 0 ", + "0 0 ", + "0 0 2", + "0 0 2", + "0 0 2", + )) + // NOTE: the HashLeftJoin should be kept + tk.MustQuery("explain select T.a,T.a,T.c from S join T on T.a = S.a where S.b>>>>>> 0448a54... types: fix wrong hash key for decimal (#19131) diff --git a/types/mydecimal.go b/types/mydecimal.go index 996323fa8706f..2f20006008561 100644 --- a/types/mydecimal.go +++ b/types/mydecimal.go @@ -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 @@ -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 } diff --git a/types/mydecimal_test.go b/types/mydecimal_test.go index 551105987b3d0..abe026f791942 100644 --- a/types/mydecimal_test.go +++ b/types/mydecimal_test.go @@ -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"}}, @@ -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)) } @@ -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 From c214ffb54624382cd810e61dfa53b9ceb99d13ec Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Fri, 14 Aug 2020 13:07:18 +0800 Subject: [PATCH 2/3] fix conflicts --- executor/join_test.go | 149 +----------------------------------------- 1 file changed, 1 insertion(+), 148 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index e4c49af0e7ac1..63c97fb710724 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -1458,154 +1458,8 @@ 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()) } -<<<<<<< HEAD -======= -func (s *testSuiteJoinSerial) TestOuterMatchStatusIssue14742(c *C) { - plannercore.ForceUseOuterBuild4Test = true - defer func() { plannercore.ForceUseOuterBuild4Test = false }() - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists testjoin;") - tk.MustExec("create table testjoin(a int);") - tk.Se.GetSessionVars().MaxChunkSize = 2 - - tk.MustExec("insert into testjoin values (NULL);") - tk.MustExec("insert into testjoin values (1);") - tk.MustExec("insert into testjoin values (2), (2), (2);") - tk.MustQuery("SELECT * FROM testjoin t1 RIGHT JOIN testjoin t2 ON t1.a > t2.a order by t1.a, t2.a;").Check(testkit.Rows( - " ", - " 2", - " 2", - " 2", - "2 1", - "2 1", - "2 1", - )) -} - -func (s *testSuiteJoinSerial) TestInlineProjection4HashJoinIssue15316(c *C) { - // Two necessary factors to reproduce this issue: - // (1) taking HashLeftJoin, i.e., letting the probing tuple lay at the left side of joined tuples - // (2) the projection only contains a part of columns from the build side, i.e., pruning the same probe side - plannercore.ForcedHashLeftJoin4Test = true - defer func() { plannercore.ForcedHashLeftJoin4Test = false }() - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("create table S (a int not null, b int, c int);") - tk.MustExec("create table T (a int not null, b int, c int);") - tk.MustExec("insert into S values (0,1,2),(0,1,null),(0,1,2);") - tk.MustExec("insert into T values (0,10,2),(0,10,null),(1,10,2);") - tk.MustQuery("select T.a,T.a,T.c from S join T on T.a = S.a where S.b", - "0 0 ", - "0 0 ", - "0 0 2", - "0 0 2", - "0 0 2", - )) - // NOTE: the HashLeftJoin should be kept - tk.MustQuery("explain select T.a,T.a,T.c from S join T on T.a = S.a where S.b>>>>>> 0448a54... types: fix wrong hash key for decimal (#19131) From 9e7794d5d67ded3b5629ad4929435671dbbd324b Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Fri, 14 Aug 2020 13:11:35 +0800 Subject: [PATCH 3/3] fix --- executor/aggregate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index a46eaa57d23b3..f8dd0ab426bee 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -881,7 +881,7 @@ func (s *testSuite) TestIssue15958(c *C) { tk.MustQuery(`select avg(y) from t`).Check(testkit.Rows("2023.3333")) } -func (s *testSuiteAgg) TestIssue17216(c *C) { +func (s *testSuite) TestIssue17216(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("use test") tk.MustExec("drop table if exists t1")