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

util: build right range when where stmt only have string column. (#16645) #16660

Merged
merged 3 commits into from
May 7, 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 expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6262,3 +6262,15 @@ func (s *testIntegrationSuite) TestIssue16426(c *C) {
tk.MustQuery("select a from t where a/1000000").Check(testkit.Rows("42"))
tk.MustQuery("select a from t where a/10000000").Check(testkit.Rows("42"))
}

func (s *testIntegrationSuite) TestIssue16505(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("CREATE TABLE t(c varchar(100), index idx(c(100)));")
tk.MustExec("INSERT INTO t VALUES (NULL),('1'),('0'),(''),('aaabbb'),('0abc'),('123e456'),('0.0001deadsfeww');")
tk.MustQuery("select * from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustQuery("select /*+ USE_INDEX(t, idx) */ * from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustQuery("select /*+ IGNORE_INDEX(t, idx) */* from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustExec("drop table t;")
}
13 changes: 12 additions & 1 deletion util/ranger/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (c *conditionChecker) check(condition expression.Expression) bool {
case *expression.ScalarFunction:
return c.checkScalarFunction(x)
case *expression.Column:
s, _ := condition.(*expression.Column)
if s.RetType.EvalType() == types.ETString {
return false
}
return c.checkColumn(x)
case *expression.Constant:
return true
Expand Down Expand Up @@ -63,7 +67,14 @@ func (c *conditionChecker) checkScalarFunction(scalar *expression.ScalarFunction
return scalar.FuncName.L != ast.NE || c.length == types.UnspecifiedLength
}
}
case ast.IsNull, ast.IsTruth, ast.IsFalsity:
case ast.IsNull:
return c.checkColumn(scalar.GetArgs()[0])
case ast.IsTruth, ast.IsFalsity:
if s, ok := scalar.GetArgs()[0].(*expression.Column); ok {
if s.RetType.EvalType() == types.ETString {
return false
}
}
return c.checkColumn(scalar.GetArgs()[0])
case ast.UnaryNot:
// TODO: support "not like" convert to access conditions.
Expand Down
31 changes: 31 additions & 0 deletions util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,3 +1168,34 @@ func (s *testRangerSuite) TestCompIndexInExprCorrCol(c *C) {
testKit.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
}
}

func (s *testRangerSuite) TestIndexStringIsTrueRange(c *C) {
defer testleak.AfterTest(c)()
dom, store, err := newDomainStoreWithBootstrap(c)
defer func() {
dom.Close()
store.Close()
}()
c.Assert(err, IsNil)
testKit := testkit.NewTestKit(c, store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t0")
testKit.MustExec("CREATE TABLE t0(c0 TEXT(10));")
testKit.MustExec("INSERT INTO t0(c0) VALUES (1);")
testKit.MustExec("CREATE INDEX i0 ON t0(c0(10));")
testKit.MustExec("analyze table t0;")

var input []string
var output []struct {
SQL string
Result []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Result = s.testData.ConvertRowsToStrings(testKit.MustQuery(tt).Rows())
})
testKit.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
}
}
12 changes: 12 additions & 0 deletions util/ranger/testdata/ranger_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@
"explain select t.e in (select count(*) from t s use index(idx), t t1 where s.b = 1 and s.c in (1, 2) and s.d = t.a and s.a = t1.a) from t",
"select t.e in (select count(*) from t s use index(idx), t t1 where s.b = 1 and s.c in (1, 2) and s.d = t.a and s.a = t1.a) from t"
]
},
{
"name": "TestIndexStringIsTrueRange",
"cases": [
"explain select * from t0 where c0",
"explain select * from t0 where c0 and c0 > '123'",
"explain select * from t0 where c0 and c0 <> '123'",
"explain select * from t0 where c0 is true",
"explain select * from t0 where c0 is false",
"explain select * from t0 where c0 and c0 in ('123','456','789')",
"explain SELECT * FROM t0 WHERE ('a' != t0.c0) AND t0.c0;"
]
}
]
61 changes: 61 additions & 0 deletions util/ranger/testdata/ranger_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,66 @@
]
}
]
},
{
"Name": "TestIndexStringIsTrueRange",
"Cases": [
{
"SQL": "explain select * from t0 where c0",
"Result": [
"TableReader_7 0.80 root data:Selection_6",
"└─Selection_6 0.80 cop[tikv] test.t0.c0",
" └─TableFullScan_5 1.00 cop[tikv] table:t0 keep order:false"
]
},
{
"SQL": "explain select * from t0 where c0 and c0 > '123'",
"Result": [
"IndexReader_7 1.00 root index:Selection_6",
"└─Selection_6 1.00 cop[tikv] test.t0.c0",
" └─IndexRangeScan_5 1.00 cop[tikv] table:t0, index:i0(c0) range:(\"123\",+inf], keep order:false"
]
},
{
"SQL": "explain select * from t0 where c0 and c0 <> '123'",
"Result": [
"IndexReader_7 1.00 root index:Selection_6",
"└─Selection_6 1.00 cop[tikv] test.t0.c0",
" └─IndexRangeScan_5 1.00 cop[tikv] table:t0, index:i0(c0) range:[-inf,\"123\"), (\"123\",+inf], keep order:false"
]
},
{
"SQL": "explain select * from t0 where c0 is true",
"Result": [
"TableReader_7 0.80 root data:Selection_6",
"└─Selection_6 0.80 cop[tikv] istrue(cast(test.t0.c0))",
" └─TableFullScan_5 1.00 cop[tikv] table:t0 keep order:false"
]
},
{
"SQL": "explain select * from t0 where c0 is false",
"Result": [
"TableReader_7 0.80 root data:Selection_6",
"└─Selection_6 0.80 cop[tikv] isfalse(cast(test.t0.c0))",
" └─TableFullScan_5 1.00 cop[tikv] table:t0 keep order:false"
]
},
{
"SQL": "explain select * from t0 where c0 and c0 in ('123','456','789')",
"Result": [
"IndexReader_7 1.00 root index:Selection_6",
"└─Selection_6 1.00 cop[tikv] test.t0.c0",
" └─IndexRangeScan_5 1.00 cop[tikv] table:t0, index:i0(c0) range:[\"123\",\"123\"], [\"456\",\"456\"], [\"789\",\"789\"], keep order:false"
]
},
{
"SQL": "explain SELECT * FROM t0 WHERE ('a' != t0.c0) AND t0.c0;",
"Result": [
"IndexReader_7 1.00 root index:Selection_6",
"└─Selection_6 1.00 cop[tikv] test.t0.c0",
" └─IndexRangeScan_5 1.00 cop[tikv] table:t0, index:i0(c0) range:[-inf,\"a\"), (\"a\",+inf], keep order:false"
]
}
]
}
]