Skip to content

Commit 2205f33

Browse files
authored
planner: update access pathes when using the vector index (#56680) (#56700)
close #56551
1 parent ff1206c commit 2205f33

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

pkg/planner/core/casetest/vectorsearch/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_test(
1515
"//pkg/domain",
1616
"//pkg/domain/infosync",
1717
"//pkg/meta/model",
18+
"//pkg/parser/model",
1819
"//pkg/planner/core",
1920
"//pkg/planner/core/base",
2021
"//pkg/planner/util/coretestsdk",

pkg/planner/core/casetest/vectorsearch/vector_index_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package vectorsearch
1616

1717
import (
18+
"context"
1819
"strings"
1920
"testing"
2021
"time"
2122

2223
"github.com/pingcap/tidb/pkg/domain"
2324
"github.com/pingcap/tidb/pkg/domain/infosync"
2425
"github.com/pingcap/tidb/pkg/meta/model"
26+
pmodel "github.com/pingcap/tidb/pkg/parser/model"
2527
"github.com/pingcap/tidb/pkg/planner/core"
2628
"github.com/pingcap/tidb/pkg/planner/core/base"
2729
"github.com/pingcap/tidb/pkg/planner/util/coretestsdk"
@@ -161,7 +163,7 @@ func TestANNIndexNormalizedPlan(t *testing.T) {
161163

162164
tk.MustExec("analyze table t")
163165

164-
tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash'")
166+
tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash, tikv'")
165167

166168
tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[0,0,0]') limit 1")
167169
p1, d1 := getNormalizedPlan()
@@ -190,6 +192,25 @@ func TestANNIndexNormalizedPlan(t *testing.T) {
190192
require.Equal(t, d1, d2)
191193
require.Equal(t, d1, d3)
192194
require.NotEqual(t, d1, dx1)
195+
196+
// test for TiFlashReplica's Available
197+
tbl, err := dom.InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
198+
require.NoError(t, err)
199+
tbl.Meta().TiFlashReplica.Available = false
200+
tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[1,2,3]') limit 3")
201+
p2, _ := getNormalizedPlan()
202+
require.Equal(t, []string{
203+
" Projection root test.t.vec",
204+
" └─TopN root ?",
205+
" └─Projection root test.t.vec, vec_cosine_distance(test.t.vec, ?)",
206+
" └─TableReader root ",
207+
" └─TopN cop vec_cosine_distance(test.t.vec, ?)",
208+
" └─TableFullScan cop table:t, range:[?,?], keep order:false",
209+
}, p2)
210+
tbl.Meta().TiFlashReplica.Available = true
211+
tk.MustExec("explain select * from t order by vec_cosine_distance(vec, '[1,2,3]') limit 3")
212+
_, d4 := getNormalizedPlan()
213+
require.Equal(t, d1, d4)
193214
}
194215

195216
func TestANNInexWithSimpleCBO(t *testing.T) {

pkg/planner/core/planbuilder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,12 @@ func getPossibleAccessPaths(ctx base.PlanContext, tableHints *hint.PlanHints, in
11921192
}
11931193
}
11941194
path := &util.AccessPath{Index: index}
1195-
if index.VectorInfo != nil && tblInfo.TiFlashReplica.Available {
1195+
if index.VectorInfo != nil {
1196+
// Because the value of `TiFlashReplica.Available` changes as the user modify replica, it is not ideal if the state of index changes accordingly.
1197+
// So the current way to use the vector indexes is to require the TiFlash Replica to be available.
1198+
if !tblInfo.TiFlashReplica.Available {
1199+
continue
1200+
}
11961201
path.StoreType = kv.TiFlash
11971202
}
11981203
publicPaths = append(publicPaths, path)

0 commit comments

Comments
 (0)