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

ranger: BuildColumnRange should merge ranges when column has prefix len (#11563) #11565

Merged
merged 4 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(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"))
}
4 changes: 4 additions & 0 deletions util/ranger/ranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
43 changes: 42 additions & 1 deletion util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,132 +743,151 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
accessConds string
filterConds string
resultStr string
length int
}{
{
colPos: 0,
exprStr: "a = 1 and b > 1",
accessConds: "[eq(test.t.a, 1)]",
filterConds: "[gt(test.t.b, 1)]",
resultStr: "[[1,1]]",
length: types.UnspecifiedLength,
},
{
colPos: 1,
exprStr: "b > 1",
accessConds: "[gt(test.t.b, 1)]",
filterConds: "[]",
resultStr: "[(1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 = a",
accessConds: "[eq(1, test.t.a)]",
filterConds: "[]",
resultStr: "[[1,1]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a != 1",
accessConds: "[ne(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[[-inf,1) (1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 != a",
accessConds: "[ne(1, test.t.a)]",
filterConds: "[]",
resultStr: "[[-inf,1) (1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a > 1",
accessConds: "[gt(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[(1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 < a",
accessConds: "[lt(1, test.t.a)]",
filterConds: "[]",
resultStr: "[(1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a >= 1",
accessConds: "[ge(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[[1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 <= a",
accessConds: "[le(1, test.t.a)]",
filterConds: "[]",
resultStr: "[[1,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a < 1",
accessConds: "[lt(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[[-inf,1)]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 > a",
accessConds: "[gt(1, test.t.a)]",
filterConds: "[]",
resultStr: "[[-inf,1)]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a <= 1",
accessConds: "[le(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[[-inf,1]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "1 >= a",
accessConds: "[ge(1, test.t.a)]",
filterConds: "[]",
resultStr: "[[-inf,1]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "(a)",
accessConds: "[test.t.a]",
filterConds: "[]",
resultStr: "[[-inf,0) (0,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a in (1, 3, NULL, 2)",
accessConds: "[in(test.t.a, 1, 3, <nil>, 2)]",
filterConds: "[]",
resultStr: "[[1,1] [2,2] [3,3]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: `a IN (8,8,81,45)`,
accessConds: "[in(test.t.a, 8, 8, 81, 45)]",
filterConds: "[]",
resultStr: `[[8,8] [45,45] [81,81]]`,
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a between 1 and 2",
accessConds: "[ge(test.t.a, 1) le(test.t.a, 2)]",
filterConds: "[]",
resultStr: "[[1,2]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a not between 1 and 2",
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.
Expand All @@ -881,97 +900,119 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
accessConds: "[ge(test.t.a, 2) le(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a not between 2 and 1",
accessConds: "[or(lt(test.t.a, 2), gt(test.t.a, 1))]",
filterConds: "[]",
resultStr: "[[-inf,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS NULL",
accessConds: "[isnull(test.t.a)]",
filterConds: "[]",
resultStr: "[[NULL,NULL]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS NOT NULL",
accessConds: "[not(isnull(test.t.a))]",
filterConds: "[]",
resultStr: "[[-inf,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS TRUE",
accessConds: "[istrue(test.t.a)]",
filterConds: "[]",
resultStr: "[[-inf,0) (0,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS NOT TRUE",
accessConds: "[not(istrue(test.t.a))]",
filterConds: "[]",
resultStr: "[[NULL,NULL] [0,0]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS FALSE",
accessConds: "[isfalse(test.t.a)]",
filterConds: "[]",
resultStr: "[[0,0]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: "a IS NOT FALSE",
accessConds: "[not(isfalse(test.t.a))]",
filterConds: "[]",
resultStr: "[[NULL,0) (0,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 1,
exprStr: `b in (1, '2.1')`,
accessConds: "[in(test.t.b, 1, 2.1)]",
filterConds: "[]",
resultStr: "[[1,1] [2.1,2.1]]",
length: types.UnspecifiedLength,
},
{
colPos: 0,
exprStr: `a > 9223372036854775807`,
accessConds: "[gt(test.t.a, 9223372036854775807)]",
filterConds: "[]",
resultStr: "[(9223372036854775807,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 2,
exprStr: `c > 111.11111111`,
accessConds: "[gt(test.t.c, 111.11111111)]",
filterConds: "[]",
resultStr: "[[111.111115,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 3,
exprStr: `d > 'aaaaaaaaaaaaaa'`,
accessConds: "[gt(test.t.d, aaaaaaaaaaaaaa)]",
filterConds: "[]",
resultStr: "[(\"aaaaaaaaaaaaaa\",+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 4,
exprStr: `e > 18446744073709500000`,
accessConds: "[gt(test.t.e, 18446744073709500000)]",
filterConds: "[]",
resultStr: "[(18446744073709500000,+inf]]",
length: types.UnspecifiedLength,
},
{
colPos: 4,
exprStr: `e > -2147483648`,
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,
},
}

Expand All @@ -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))
Expand Down