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: update access pathes when using the vector index (#56680) #56700

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/planner/util/coretestsdk",
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/planner/util/coretestsdk"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -190,6 +192,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