Skip to content

Commit

Permalink
planner: fix the problem of using enum like 'x%' to build the wrong…
Browse files Browse the repository at this point in the history
… range (pingcap#27267)
  • Loading branch information
rebelice committed Sep 15, 2021
1 parent a894e34 commit db2edfb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
39 changes: 39 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3937,3 +3937,42 @@ func (s *testIntegrationSuite) TestGroupBySetVar(c *C) {
res.Check(testkit.Rows(output[i].Plan...))
}
}

func (s *testIntegrationSuite) TestIssues27130(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1( a enum('y','b','Abc','null'),b enum('y','b','Abc','null'),key(a));")
tk.MustQuery(`explain format=brief select * from t1 where a like "A%"`).Check(testkit.Rows(
"TableReader 8000.00 root data:Selection",
"└─Selection 8000.00 cop[tikv] like(test.t1.a, \"A%\", 92)",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
))
tk.MustQuery(`explain format=brief select * from t1 where b like "A%"`).Check(testkit.Rows(
"TableReader 8000.00 root data:Selection",
"└─Selection 8000.00 cop[tikv] like(test.t1.b, \"A%\", 92)",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
))

tk.MustExec("drop table if exists t2")
tk.MustExec("create table t2( a enum('y','b','Abc','null'),b enum('y','b','Abc','null'),key(a, b));")
tk.MustQuery(`explain format=brief select * from t2 where a like "A%"`).Check(testkit.Rows(
"TableReader 8000.00 root data:Selection",
"└─Selection 8000.00 cop[tikv] like(test.t2.a, \"A%\", 92)",
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
))
tk.MustQuery(`explain format=brief select * from t2 where a like "A%" and b like "A%"`).Check(testkit.Rows(
"TableReader 8000.00 root data:Selection",
"└─Selection 8000.00 cop[tikv] like(test.t2.a, \"A%\", 92), like(test.t2.b, \"A%\", 92)",
" └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
))

tk.MustExec("drop table if exists t3")
tk.MustExec("create table t3( a int,b enum('y','b','Abc','null'), c enum('y','b','Abc','null'),key(a, b, c));")
tk.MustQuery(`explain format=brief select * from t3 where a = 1 and b like "A%"`).Check(testkit.Rows(
"IndexReader 8.00 root index:Selection",
"└─Selection 8.00 cop[tikv] like(test.t3.b, \"A%\", 92)",
" └─IndexRangeScan 10.00 cop[tikv] table:t3, index:a(a, b, c) range:[1,1], keep order:false, stats:pseudo",
))
}
11 changes: 11 additions & 0 deletions util/ranger/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ranger

import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
Expand Down Expand Up @@ -143,12 +144,22 @@ func (c *conditionChecker) checkLikeFunc(scalar *expression.ScalarFunction) bool
return false
}
if patternStr[i] == '%' {
// We currently do not support using `enum like 'xxx%'` to build range
// see https://github.com/pingcap/tidb/issues/27130 for more details
if scalar.GetArgs()[0].GetType().Tp == mysql.TypeEnum {
return false
}
if i != len(patternStr)-1 {
c.shouldReserve = true
}
break
}
if patternStr[i] == '_' {
// We currently do not support using `enum like 'xxx_'` to build range
// see https://github.com/pingcap/tidb/issues/27130 for more details
if scalar.GetArgs()[0].GetType().Tp == mysql.TypeEnum {
return false
}
c.shouldReserve = true
break
}
Expand Down

0 comments on commit db2edfb

Please sign in to comment.