Skip to content

Commit

Permalink
cherry pick pingcap#19131 to release-3.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
lzmhhh123 authored and ti-srebot committed Aug 13, 2020
1 parent f6455c1 commit 8f73e01
Show file tree
Hide file tree
Showing 4 changed files with 196 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 @@ -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"))
}
159 changes: 159 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<nil> <nil>",
"<nil> 2",
"<nil> 2",
"<nil> 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<T.b order by T.a,T.c;").Check(testkit.Rows(
"0 0 <nil>",
"0 0 <nil>",
"0 0 <nil>",
"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<T.b order by T.a,T.c;").Check(testkit.Rows(
"Sort_8 12487.50 root test.t.a, test.t.c",
"└─Projection_10 12487.50 root test.t.a, test.t.a, test.t.c",
" └─HashJoin_11 12487.50 root inner join, equal:[eq(test.s.a, test.t.a)], other cond:lt(test.s.b, test.t.b)",
" ├─TableReader_17(Build) 9990.00 root data:Selection_16",
" │ └─Selection_16 9990.00 cop[tikv] not(isnull(test.t.b))",
" │ └─TableFullScan_15 10000.00 cop[tikv] table:T keep order:false, stats:pseudo",
" └─TableReader_14(Probe) 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop[tikv] not(isnull(test.s.b))",
" └─TableFullScan_12 10000.00 cop[tikv] table:S keep order:false, stats:pseudo"))
}

func (s *testSuiteJoinSerial) TestIssue18070(c *C) {
config.GetGlobalConfig().OOMAction = config.OOMActionCancel
defer func() { config.GetGlobalConfig().OOMAction = config.OOMActionLog }()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, index(a))")
tk.MustExec("create table t2(a int, index(a))")
tk.MustExec("insert into t1 values(1),(2)")
tk.MustExec("insert into t2 values(1),(1),(2),(2)")
tk.MustExec("set @@tidb_mem_quota_query=1000")
err := tk.QueryToErr("select /*+ inl_hash_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
c.Assert(strings.Contains(err.Error(), "Out Of Memory Quota!"), IsTrue)

fpName := "github.com/pingcap/tidb/executor/mockIndexMergeJoinOOMPanic"
c.Assert(failpoint.Enable(fpName, `panic("ERROR 1105 (HY000): Out Of Memory Quota![conn_id=1]")`), IsNil)
defer func() {
c.Assert(failpoint.Disable(fpName), IsNil)
}()
err = tk.QueryToErr("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
c.Assert(strings.Contains(err.Error(), "Out Of Memory Quota!"), IsTrue)
}

func (s *testSuiteJoin1) TestIssue18564(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b int, primary key(a), index idx(b,a));")
tk.MustExec("create table t2(a int, b int, primary key(a), index idx(b,a));")
tk.MustExec("insert into t1 values(1, 1)")
tk.MustExec("insert into t2 values(1, 1)")
tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t1 FORCE INDEX (idx) join t2 on t1.b=t2.b and t1.a = t2.a").Check(testkit.Rows("1 1 1 1"))
}

func (s *testSuite9) TestIssue18572_1(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index idx(b));")
tk.MustExec("insert into t1 values(1, 1);")
tk.MustExec("insert into t1 select * from t1;")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testIndexHashJoinInnerWorkerErr", "return"), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testIndexHashJoinInnerWorkerErr"), IsNil)
}()

rs, err := tk.Exec("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(context.Background(), nil, rs)
c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinInnerWorkerErr"), IsTrue)
}

func (s *testSuite9) TestIssue18572_2(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index idx(b));")
tk.MustExec("insert into t1 values(1, 1);")
tk.MustExec("insert into t1 select * from t1;")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testIndexHashJoinOuterWorkerErr", "return"), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testIndexHashJoinOuterWorkerErr"), IsNil)
}()

rs, err := tk.Exec("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(context.Background(), nil, rs)
c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinOuterWorkerErr"), IsTrue)
}

func (s *testSuite9) TestIssue18572_3(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, index idx(b));")
tk.MustExec("insert into t1 values(1, 1);")
tk.MustExec("insert into t1 select * from t1;")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testIndexHashJoinBuildErr", "return"), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testIndexHashJoinBuildErr"), IsNil)
}()

rs, err := tk.Exec("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
c.Assert(err, IsNil)
_, 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"))
}
>>>>>>> 0448a54... types: fix wrong hash key for decimal (#19131)
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

0 comments on commit 8f73e01

Please sign in to comment.