From 81134ceca624d21b50f86d1308bb815907f7f808 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 1 Aug 2019 13:12:30 +0800 Subject: [PATCH 1/3] ranger: BuildColumnRange should merge ranges when column has prefix len --- executor/join_test.go | 10 ++++++++++ util/ranger/ranger.go | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/executor/join_test.go b/executor/join_test.go index 2b768d3d53e6f..bc0b1614b79b3 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -1381,3 +1381,13 @@ func (s *testSuite2) TestInjectProjOnTopN(c *C) { "2", )) } + +func (s *testSuite2) TestIssue11544(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table 11544t(a int)") + tk.MustExec("create table 11544tt(a int, b varchar(10), index idx(a, b(3)))") + tk.MustExec("insert into 11544t values(1)") + tk.MustExec("insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc')") + tk.MustQuery("select /*+ TIDB_INLJ(11544tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb")) +} diff --git a/util/ranger/ranger.go b/util/ranger/ranger.go index 4b9b12e0b6e13..6f858af480a39 100644 --- a/util/ranger/ranger.go +++ b/util/ranger/ranger.go @@ -270,6 +270,10 @@ func buildColumnRange(accessConditions []expression.Expression, sc *stmtctx.Stat ran.HighExclude = false } } + ranges, err = unionRanges(sc, ranges) + if err != nil { + return nil, err + } } return ranges, nil } From afbe51eb78f46eb1c3852aa5a12341224f02152d Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 1 Aug 2019 14:37:50 +0800 Subject: [PATCH 2/3] add more tests --- util/ranger/ranger_test.go | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/util/ranger/ranger_test.go b/util/ranger/ranger_test.go index 6013fe4320d5f..76cc426a5e309 100644 --- a/util/ranger/ranger_test.go +++ b/util/ranger/ranger_test.go @@ -743,6 +743,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds string filterConds string resultStr string + length int }{ { colPos: 0, @@ -750,6 +751,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[eq(test.t.a, 1)]", filterConds: "[gt(test.t.b, 1)]", resultStr: "[[1,1]]", + length: types.UnspecifiedLength, }, { colPos: 1, @@ -757,6 +759,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.b, 1)]", filterConds: "[]", resultStr: "[(1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -764,6 +767,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[eq(1, test.t.a)]", filterConds: "[]", resultStr: "[[1,1]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -771,6 +775,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ne(test.t.a, 1)]", filterConds: "[]", resultStr: "[[-inf,1) (1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -778,6 +783,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ne(1, test.t.a)]", filterConds: "[]", resultStr: "[[-inf,1) (1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -785,6 +791,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.a, 1)]", filterConds: "[]", resultStr: "[(1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -792,6 +799,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[lt(1, test.t.a)]", filterConds: "[]", resultStr: "[(1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -799,6 +807,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ge(test.t.a, 1)]", filterConds: "[]", resultStr: "[[1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -806,6 +815,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[le(1, test.t.a)]", filterConds: "[]", resultStr: "[[1,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -813,6 +823,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[lt(test.t.a, 1)]", filterConds: "[]", resultStr: "[[-inf,1)]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -820,6 +831,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(1, test.t.a)]", filterConds: "[]", resultStr: "[[-inf,1)]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -827,6 +839,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[le(test.t.a, 1)]", filterConds: "[]", resultStr: "[[-inf,1]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -834,6 +847,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ge(1, test.t.a)]", filterConds: "[]", resultStr: "[[-inf,1]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -841,6 +855,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[test.t.a]", filterConds: "[]", resultStr: "[[-inf,0) (0,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -848,6 +863,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[in(test.t.a, 1, 3, , 2)]", filterConds: "[]", resultStr: "[[1,1] [2,2] [3,3]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -855,6 +871,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[in(test.t.a, 8, 8, 81, 45)]", filterConds: "[]", resultStr: `[[8,8] [45,45] [81,81]]`, + length: types.UnspecifiedLength, }, { colPos: 0, @@ -862,6 +879,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ge(test.t.a, 1) le(test.t.a, 2)]", filterConds: "[]", resultStr: "[[1,2]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -869,6 +887,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[or(lt(test.t.a, 1), gt(test.t.a, 2))]", filterConds: "[]", resultStr: "[[-inf,1) (2,+inf]]", + length: types.UnspecifiedLength, }, //{ // `a > null` will be converted to `castAsString(a) > null` which can not be extracted as access condition. @@ -881,6 +900,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[ge(test.t.a, 2) le(test.t.a, 1)]", filterConds: "[]", resultStr: "[]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -888,6 +908,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[or(lt(test.t.a, 2), gt(test.t.a, 1))]", filterConds: "[]", resultStr: "[[-inf,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -895,6 +916,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[isnull(test.t.a)]", filterConds: "[]", resultStr: "[[NULL,NULL]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -902,6 +924,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[not(isnull(test.t.a))]", filterConds: "[]", resultStr: "[[-inf,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -909,6 +932,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[istrue(test.t.a)]", filterConds: "[]", resultStr: "[[-inf,0) (0,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -916,6 +940,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[not(istrue(test.t.a))]", filterConds: "[]", resultStr: "[[NULL,NULL] [0,0]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -923,6 +948,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[isfalse(test.t.a)]", filterConds: "[]", resultStr: "[[0,0]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -930,6 +956,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[not(isfalse(test.t.a))]", filterConds: "[]", resultStr: "[[NULL,0) (0,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 1, @@ -937,6 +964,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[in(test.t.b, 1, 2.1)]", filterConds: "[]", resultStr: "[[1,1] [2.1,2.1]]", + length: types.UnspecifiedLength, }, { colPos: 0, @@ -944,6 +972,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.a, 9223372036854775807)]", filterConds: "[]", resultStr: "[(9223372036854775807,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 2, @@ -951,6 +980,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.c, 111.11111111)]", filterConds: "[]", resultStr: "[[111.111115,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 3, @@ -958,6 +988,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.d, aaaaaaaaaaaaaa)]", filterConds: "[]", resultStr: "[(\"aaaaaaaaaaaaaa\",+inf]]", + length: types.UnspecifiedLength, }, { colPos: 4, @@ -965,6 +996,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.e, 18446744073709500000)]", filterConds: "[]", resultStr: "[(18446744073709500000,+inf]]", + length: types.UnspecifiedLength, }, { colPos: 4, @@ -972,6 +1004,15 @@ func (s *testRangerSuite) TestColumnRange(c *C) { accessConds: "[gt(test.t.e, -2147483648)]", filterConds: "[]", resultStr: "[[0,+inf]]", + length: types.UnspecifiedLength, + }, + { + colPos: 3, + exprStr: "d = 'aab' or d = 'aac'", + accessConds: "[or(eq(test.t.d, aab), eq(test.t.d, aac))]", + filterConds: "[]", + resultStr: "[[\"a\",\"a\"]]", + length: 1, }, } @@ -997,7 +1038,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) { c.Assert(col, NotNil) conds = ranger.ExtractAccessConditionsForColumn(conds, col.UniqueID) c.Assert(fmt.Sprintf("%s", conds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr)) - result, err := ranger.BuildColumnRange(conds, new(stmtctx.StatementContext), col.RetType, types.UnspecifiedLength) + result, err := ranger.BuildColumnRange(conds, new(stmtctx.StatementContext), col.RetType, tt.length) c.Assert(err, IsNil) got := fmt.Sprintf("%v", result) c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s, col: %v", tt.exprStr, col)) From a135134985c1452a11f9048d86dc62e4b5133e37 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 1 Aug 2019 15:06:05 +0800 Subject: [PATCH 3/3] fix test --- executor/join_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/join_test.go b/executor/join_test.go index bc0b1614b79b3..0f0b66214bf9d 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -1389,5 +1389,5 @@ func (s *testSuite2) TestIssue11544(c *C) { tk.MustExec("create table 11544tt(a int, b varchar(10), index idx(a, b(3)))") tk.MustExec("insert into 11544t values(1)") tk.MustExec("insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc')") - tk.MustQuery("select /*+ TIDB_INLJ(11544tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb")) + tk.MustQuery("select /*+ TIDB_INLJ(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb")) }