From 41ca11b33d4d1a506fb4d132b4e76e7a0f9b4733 Mon Sep 17 00:00:00 2001 From: Lynn Date: Wed, 16 Oct 2024 15:14:40 +0800 Subject: [PATCH 1/3] planner: update access pathes when using the vector index --- .../vectorsearch/vector_index_test.go | 36 +++++++++++++++++-- pkg/planner/core/planbuilder.go | 7 +++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/planner/core/casetest/vectorsearch/vector_index_test.go b/pkg/planner/core/casetest/vectorsearch/vector_index_test.go index 16528e1968208..1e90cbd3022e8 100644 --- a/pkg/planner/core/casetest/vectorsearch/vector_index_test.go +++ b/pkg/planner/core/casetest/vectorsearch/vector_index_test.go @@ -15,6 +15,7 @@ package vectorsearch import ( + "context" "strings" "testing" "time" @@ -22,6 +23,7 @@ import ( "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/domain/infosync" "github.com/pingcap/tidb/pkg/meta/model" + pmodel "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/planner/core" "github.com/pingcap/tidb/pkg/planner/core/base" "github.com/pingcap/tidb/pkg/planner/util/coretestsdk" @@ -141,7 +143,7 @@ func TestANNIndexNormalizedPlan(t *testing.T) { } tk.MustExec("use test") - tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t, tt") tk.MustExec(` create table t ( vec vector(3) @@ -161,7 +163,7 @@ func TestANNIndexNormalizedPlan(t *testing.T) { tk.MustExec("analyze table t") - tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash'") + tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash, tikv'") tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[0,0,0]') limit 1") p1, d1 := getNormalizedPlan() @@ -190,6 +192,36 @@ func TestANNIndexNormalizedPlan(t *testing.T) { require.Equal(t, d1, d2) require.Equal(t, d1, d3) require.NotEqual(t, d1, dx1) + + tk.MustExec(` + create table tt ( + vec vector(3), + VECTOR INDEX vecIdx((VEC_L2_DISTANCE(vec))) + ) + `) + tk.MustExec(` + insert into t values + ('[1,1,1]'), + ('[2,2,2]'), + ('[3,3,3]') + `) + tbl, err := dom.InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t")) + require.NoError(t, err) + tbl.Meta().TiFlashReplica.Available = false + tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[1,2,3]') limit 3") + p2, _ := getNormalizedPlan() + require.Equal(t, []string{ + " Projection root test.t.vec", + " └─TopN root ?", + " └─Projection root test.t.vec, vec_cosine_distance(test.t.vec, ?)", + " └─TableReader root ", + " └─TopN cop vec_cosine_distance(test.t.vec, ?)", + " └─TableFullScan cop table:t, range:[?,?], keep order:false", + }, p2) + tbl.Meta().TiFlashReplica.Available = true + tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[1,2,3]') limit 3") + _, d4 := getNormalizedPlan() + require.Equal(t, d1, d4) } func TestANNInexWithSimpleCBO(t *testing.T) { diff --git a/pkg/planner/core/planbuilder.go b/pkg/planner/core/planbuilder.go index b49400fb4dc72..764e776ecbae8 100644 --- a/pkg/planner/core/planbuilder.go +++ b/pkg/planner/core/planbuilder.go @@ -1192,7 +1192,12 @@ func getPossibleAccessPaths(ctx base.PlanContext, tableHints *hint.PlanHints, in } } path := &util.AccessPath{Index: index} - if index.VectorInfo != nil && tblInfo.TiFlashReplica.Available { + if index.VectorInfo != nil { + // Because the value of `TiFlashReplica.Available` changes as the user modify replica, it is not ideal if the state of index changes accordingly. + // So the current way to use the vector indexes is to require the TiFlash Replica to be available. + if !tblInfo.TiFlashReplica.Available { + continue + } path.StoreType = kv.TiFlash } publicPaths = append(publicPaths, path) From 93b5fff4ec0d78dc5d67959fd463a07138766cf7 Mon Sep 17 00:00:00 2001 From: Lynn Date: Wed, 16 Oct 2024 15:19:24 +0800 Subject: [PATCH 2/3] *: tiny update tests --- .../casetest/vectorsearch/vector_index_test.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pkg/planner/core/casetest/vectorsearch/vector_index_test.go b/pkg/planner/core/casetest/vectorsearch/vector_index_test.go index 1e90cbd3022e8..0f4e10231a7d2 100644 --- a/pkg/planner/core/casetest/vectorsearch/vector_index_test.go +++ b/pkg/planner/core/casetest/vectorsearch/vector_index_test.go @@ -143,7 +143,7 @@ func TestANNIndexNormalizedPlan(t *testing.T) { } tk.MustExec("use test") - tk.MustExec("drop table if exists t, tt") + tk.MustExec("drop table if exists t") tk.MustExec(` create table t ( vec vector(3) @@ -193,18 +193,7 @@ func TestANNIndexNormalizedPlan(t *testing.T) { require.Equal(t, d1, d3) require.NotEqual(t, d1, dx1) - tk.MustExec(` - create table tt ( - vec vector(3), - VECTOR INDEX vecIdx((VEC_L2_DISTANCE(vec))) - ) - `) - tk.MustExec(` - insert into t values - ('[1,1,1]'), - ('[2,2,2]'), - ('[3,3,3]') - `) + // test for TiFlashReplica's Available tbl, err := dom.InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t")) require.NoError(t, err) tbl.Meta().TiFlashReplica.Available = false From 81aa5708807929137cb59d28478ea736689259f7 Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 17 Oct 2024 11:41:36 +0800 Subject: [PATCH 3/3] *: make bazel happy --- pkg/planner/core/casetest/vectorsearch/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/planner/core/casetest/vectorsearch/BUILD.bazel b/pkg/planner/core/casetest/vectorsearch/BUILD.bazel index d311e27898f55..14c1613c2d858 100644 --- a/pkg/planner/core/casetest/vectorsearch/BUILD.bazel +++ b/pkg/planner/core/casetest/vectorsearch/BUILD.bazel @@ -15,6 +15,7 @@ go_test( "//pkg/domain", "//pkg/domain/infosync", "//pkg/meta/model", + "//pkg/parser/model", "//pkg/planner/core", "//pkg/planner/core/base", "//pkg/planner/util/coretestsdk",