Skip to content

Commit

Permalink
planner: update access pathes when using the vector index (#56680)
Browse files Browse the repository at this point in the history
close #56551
  • Loading branch information
zimulala authored Oct 17, 2024
1 parent 1052a55 commit 457ff81
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/planner/core/casetest/vectorsearch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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/store/mockstore",
Expand Down
23 changes: 22 additions & 1 deletion pkg/planner/core/casetest/vectorsearch/vector_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package vectorsearch

import (
"context"
"strings"
"testing"
"time"

"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/store/mockstore"
Expand Down Expand Up @@ -160,7 +162,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()
Expand Down Expand Up @@ -189,6 +191,25 @@ func TestANNIndexNormalizedPlan(t *testing.T) {
require.Equal(t, d1, d2)
require.Equal(t, d1, d3)
require.NotEqual(t, d1, dx1)

// 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
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) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 457ff81

Please sign in to comment.