From 83d9d4d16bf72e7443dc0ce962a940cb4077a034 Mon Sep 17 00:00:00 2001 From: HuaiyuXu Date: Tue, 12 Mar 2019 20:59:15 +0800 Subject: [PATCH] planner, executor: check col.OrigColName instead of col.ColName in isCoveringIndex (#9637) (#9658) --- executor/executor_test.go | 6 ++++++ planner/core/find_best_task.go | 4 +++- planner/core/logical_plan_builder.go | 13 +++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index c2662d33c1027..878cfd56c9857 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -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) { diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 024ada9e8d1b2..e4fe91ee11501 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -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 } diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 2e71162b7641e..ee4b1084e8d0b 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -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) {