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

planner: fix index prefix matching #39377

Merged
merged 9 commits into from
Dec 12, 2022
4 changes: 2 additions & 2 deletions ddl/multi_schema_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ func TestMultiSchemaChangeAddDropIndexes(t *testing.T) {
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index (a), index(b), index(c));")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add index aa(a), drop index a, add index cc(c), drop index b, drop index c, add index bb(b);")
tk.MustQuery("select * from t use index(aa, bb, cc);").Check(testkit.Rows("1 2 3"))
tk.MustExec("alter table t add index xa(a), drop index a, add index xc(c), drop index b, drop index c, add index xb(b);")
tk.MustQuery("select * from t use index(xa, xb, xc);").Check(testkit.Rows("1 2 3"))
tk.MustGetErrCode("select * from t use index(a);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(b);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(c);", errno.ErrKeyDoesNotExist)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ func TestValidate(t *testing.T) {
err: ErrUnknownColumn,
},
{
sql: "select * from t t1 use index(e)",
sql: "select * from t t1 use index(x)",
err: ErrKeyDoesNotExist,
},
{
Expand Down
2 changes: 1 addition & 1 deletion planner/core/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func MockSignedTable() *model.TableInfo {
Unique: true,
},
{
Name: model.NewCIStr("e"),
Name: model.NewCIStr("x"),
Columns: []*model.IndexColumn{
{
Name: model.NewCIStr("e"),
Expand Down
12 changes: 11 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,8 @@ func (b *PlanBuilder) detectSelectWindow(sel *ast.SelectStmt) bool {
}

func getPathByIndexName(paths []*util.AccessPath, idxName model.CIStr, tblInfo *model.TableInfo) *util.AccessPath {
var primaryIdxPath *util.AccessPath
var primaryIdxPath, indexPrefixPath *util.AccessPath
prefixMatches := 0
for _, path := range paths {
if path.StoreType == kv.TiFlash {
continue
Expand All @@ -1180,10 +1181,19 @@ func getPathByIndexName(paths []*util.AccessPath, idxName model.CIStr, tblInfo *
if path.Index.Name.L == idxName.L {
return path
}
if strings.HasPrefix(path.Index.Name.L, idxName.L) {
indexPrefixPath = path
prefixMatches++
}
}
if isPrimaryIndex(idxName) && tblInfo.HasClusteredIndex() {
return primaryIdxPath
}

// Return only unique prefix matches
if prefixMatches == 1 {
return indexPrefixPath
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions planner/core/planbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func TestGetPathByIndexName(t *testing.T) {
require.NotNil(t, path)
require.Equal(t, accessPath[1], path)

// "id" is a prefix of "idx"
path = getPathByIndexName(accessPath, model.NewCIStr("id"), tblInfo)
require.NotNil(t, path)
require.Equal(t, accessPath[1], path)

path = getPathByIndexName(accessPath, model.NewCIStr("primary"), tblInfo)
require.NotNil(t, path)
require.Equal(t, accessPath[0], path)
Expand Down