diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index ad16f3b90aba2..b21a92823f2bc 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -64,7 +64,7 @@ const ( HintHashAgg = "hash_agg" // HintStreamAgg is hint enforce stream aggregation. HintStreamAgg = "stream_agg" - // HintIndex is hint enforce use some indexes. + // HintIndex is hint enforce using some indexes. HintIndex = "index" ) @@ -2277,7 +2277,11 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as return nil, ErrPartitionClauseOnNonpartitioned } - possiblePaths, err := b.getPossibleAccessPaths(tn.IndexHints, tableInfo, asName) + tblName := *asName + if tblName.L == "" { + tblName = tn.Name + } + possiblePaths, err := b.getPossibleAccessPaths(tn.IndexHints, tableInfo, tblName) if err != nil { return nil, err } diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 0a7b80b3d9fbe..7c43127c1d459 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1724,22 +1724,20 @@ func (s *testPlanSuite) TestIndexHint(c *C) { }{ // simple case { - sql: "select /*+ INDEX(t, c_d_e) */ * from t t1", + sql: "select /*+ INDEX(t, c_d_e) */ * from t", best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))", hasWarn: false, }, { - sql: "select /*+ INDEX(t1, c_d_e) */ * from t t1", - best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))", + sql: "select /*+ INDEX(t, c_d_e) */ * from t t1", + best: "TableReader(Table(t))", hasWarn: false, }, - // test use original table name { - sql: "select /*+ INDEX(t, c_d_e) */ * from t t1, t t2 where t1.a = t2.b", - best: "LeftHashJoin{IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))->IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))}(test.t1.a,test.t2.b)", + sql: "select /*+ INDEX(t1, c_d_e) */ * from t t1", + best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))", hasWarn: false, }, - // test use table alias { sql: "select /*+ INDEX(t1, c_d_e), INDEX(t2, f) */ * from t t1, t t2 where t1.a = t2.b", best: "LeftHashJoin{IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))->IndexLookUp(Index(t.f)[[NULL,+inf]], Table(t))}(test.t1.a,test.t2.b)", diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index e948ceaec812c..987ba680edb8c 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -525,7 +525,7 @@ func isPrimaryIndex(indexName model.CIStr) bool { return indexName.L == "primary" } -func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInfo *model.TableInfo, asName *model.CIStr) ([]*accessPath, error) { +func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInfo *model.TableInfo, tblName model.CIStr) ([]*accessPath, error) { publicPaths := make([]*accessPath, 0, len(tblInfo.Indices)+1) publicPaths = append(publicPaths, &accessPath{isTablePath: true}) for _, index := range tblInfo.Indices { @@ -542,7 +542,7 @@ func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInf indexHintsLen := len(indexHints) if hints := b.TableHints(); hints != nil { for _, hint := range hints.indexHintList { - if hint.tblName == *asName { + if hint.tblName == tblName { indexHints = append(indexHints, hint.indexHint) } }