Skip to content

Commit

Permalink
planner, executor: check col.OrigColName instead of col.ColName in is…
Browse files Browse the repository at this point in the history
…CoveringIndex (pingcap#9637) (pingcap#9658)
  • Loading branch information
XuHuaiyu authored and zz-jason committed Mar 12, 2019
1 parent 8f69f9a commit 83d9d4d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 6 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,12 @@ func (s *testSuite) TestIndexScan(c *C) {
tk.MustExec("create table t(a varchar(50) primary key, b int, c int, index idx(b))")
tk.MustExec("insert into t values('aa', 1, 1)")
tk.MustQuery("select * from t use index(idx) where a > 'a'").Check(testkit.Rows("aa 1 1"))

// fix issue9636
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (a int, KEY (a))")
result = tk.MustQuery(`SELECT * FROM (SELECT * FROM (SELECT a as d FROM t WHERE a IN ('100')) AS x WHERE x.d < "123" ) tmp_count"`)
result.Check(testkit.Rows())
}

func (s *testSuite) TestIndexReverseOrder(c *C) {
Expand Down
4 changes: 3 additions & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ func isCoveringIndex(columns []*expression.Column, indexColumns []*model.IndexCo
isIndexColumn := false
for _, indexCol := range indexColumns {
isFullLen := indexCol.Length == types.UnspecifiedLength || indexCol.Length == col.RetType.Flen
if col.ColName.L == indexCol.Name.L && isFullLen {
// We use col.OrigColName instead of col.ColName.
// Related issue: https://github.com/pingcap/tidb/issues/9636.
if col.OrigColName.L == indexCol.Name.L && isFullLen {
isIndexColumn = true
break
}
Expand Down
13 changes: 7 additions & 6 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1873,12 +1873,13 @@ func (b *planBuilder) buildDataSource(tn *ast.TableName) (LogicalPlan, error) {
for _, col := range columns {
ds.Columns = append(ds.Columns, col.ToInfo())
newCol := &expression.Column{
UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(),
DBName: dbName,
TblName: tableInfo.Name,
ColName: col.Name,
ID: col.ID,
RetType: &col.FieldType,
UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(),
DBName: dbName,
TblName: tableInfo.Name,
ColName: col.Name,
OrigColName: col.Name,
ID: col.ID,
RetType: &col.FieldType,
}

if tableInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag) {
Expand Down

0 comments on commit 83d9d4d

Please sign in to comment.