From 0965849a7c6ce3d21813eaaf11f874c80501447d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 24 Nov 2022 11:50:21 +0100 Subject: [PATCH 1/4] planner: fix index prefix matching --- planner/core/planbuilder.go | 12 +++++++++++- planner/core/planbuilder_test.go | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 0ec0e73d37fc4..dbdcf36688a85 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1111,7 +1111,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 @@ -1123,10 +1124,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 } diff --git a/planner/core/planbuilder_test.go b/planner/core/planbuilder_test.go index 13494433f0fbe..d80c8cc4fb8d0 100644 --- a/planner/core/planbuilder_test.go +++ b/planner/core/planbuilder_test.go @@ -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) From eb360f7531d1c615b4657b0309922a55926b6bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 24 Nov 2022 11:55:36 +0100 Subject: [PATCH 2/4] Fix TestGetPathByIndexName --- planner/core/planbuilder_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/planbuilder_test.go b/planner/core/planbuilder_test.go index d80c8cc4fb8d0..8ad1d5c49c0e2 100644 --- a/planner/core/planbuilder_test.go +++ b/planner/core/planbuilder_test.go @@ -92,7 +92,7 @@ func TestGetPathByIndexName(t *testing.T) { require.Equal(t, accessPath[1], path) // "id" is a prefix of "idx" - path := getPathByIndexName(accessPath, model.NewCIStr("id"), tblInfo) + path = getPathByIndexName(accessPath, model.NewCIStr("id"), tblInfo) require.NotNil(t, path) require.Equal(t, accessPath[1], path) From db62fe3ef9c8b276b58438323c245a49198e645d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 24 Nov 2022 14:03:03 +0100 Subject: [PATCH 3/4] Fix TestValidate --- planner/core/logical_plan_test.go | 2 +- planner/core/mock.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index 7458c7307c19c..82786703338f4 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -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, }, { diff --git a/planner/core/mock.go b/planner/core/mock.go index e96c10ca20d13..472c980fa3777 100644 --- a/planner/core/mock.go +++ b/planner/core/mock.go @@ -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"), From 409922ec7ca56b0d81b072d030b88a24bab16cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 24 Nov 2022 14:31:05 +0100 Subject: [PATCH 4/4] Fix use of index prefix in TestMultiSchemaChangeAddDropIndexes --- ddl/multi_schema_change_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ddl/multi_schema_change_test.go b/ddl/multi_schema_change_test.go index d48aa6752f8ee..ace021551c7ea 100644 --- a/ddl/multi_schema_change_test.go +++ b/ddl/multi_schema_change_test.go @@ -721,8 +721,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)