Skip to content

planner: fix primary index hint when isolation read engine is tiflash #61234

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/planner/core/casetest/hint/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestIsolationReadTiFlashUseIndexHint(t *testing.T) {

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, vec vector(3), index idx(a), vector index idx_vec ((VEC_COSINE_DISTANCE(`vec`))) USING HNSW);")
tk.MustExec("create table t(a int, vec vector(3), PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, index idx(a), vector index idx_vec ((VEC_COSINE_DISTANCE(`vec`))) USING HNSW);")
tblInfo, err := dom.InfoSchema().TableByName(context.Background(), ast.NewCIStr("test"), ast.NewCIStr("t"))
require.NoError(t, err)
err = domain.GetDomain(tk.Session()).DDLExecutor().UpdateTableReplicaInfo(tk.Session(), tblInfo.Meta().ID, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"explain format = 'brief' select * from t use index();",
"explain format = 'brief' select /*+ use_index(t, idx)*/ * from t",
"explain format = 'brief' select /*+ use_index(t)*/ * from t",
"explain format = 'brief' select /*+ use_index(t, PRIMARY)*/ * from t",
"explain format = 'brief' select /*+ use_index(t, idx_vec)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;",
"explain format = 'brief' select /*+ use_index(t, idx_v)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;",
"explain format = 'brief' select /*+ ignore_index(t, idx_vec)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;"
Expand Down
29 changes: 20 additions & 9 deletions pkg/planner/core/casetest/hint/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1844,36 +1844,47 @@
],
"Warn": null
},
{
"SQL": "explain format = 'brief' select /*+ use_index(t, PRIMARY)*/ * from t",
"Plan": [
"TableReader 10000.00 root MppVersion: 3, data:ExchangeSender",
"└─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
],
"Warn": [
"TiDB doesn't support index 'PRIMARY' in the isolation read engines(value: 'tiflash')"
]
},
{
"SQL": "explain format = 'brief' select /*+ use_index(t, idx_vec)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;",
"Plan": [
"TopN 1.00 root Column#6, offset:0, count:1",
"TopN 1.00 root Column#5, offset:0, count:1",
"└─TableReader 1.00 root MppVersion: 3, data:ExchangeSender",
" └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough",
" └─TopN 1.00 mpp[tiflash] Column#6, offset:0, count:1",
" └─TableFullScan 1.00 mpp[tiflash] table:t, index:idx_vec(vec) keep order:false, stats:pseudo, annIndex:COSINE(vec..[1,2,3], limit:1)->Column#6"
" └─TopN 1.00 mpp[tiflash] Column#5, offset:0, count:1",
" └─TableFullScan 1.00 mpp[tiflash] table:t, index:idx_vec(vec) keep order:false, stats:pseudo, annIndex:COSINE(vec..[1,2,3], limit:1)->Column#5"
],
"Warn": null
},
{
"SQL": "explain format = 'brief' select /*+ use_index(t, idx_v)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;",
"Plan": [
"TopN 1.00 root Column#6, offset:0, count:1",
"TopN 1.00 root Column#5, offset:0, count:1",
"└─TableReader 1.00 root MppVersion: 3, data:ExchangeSender",
" └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough",
" └─TopN 1.00 mpp[tiflash] Column#6, offset:0, count:1",
" └─TableFullScan 1.00 mpp[tiflash] table:t, index:idx_vec(vec) keep order:false, stats:pseudo, annIndex:COSINE(vec..[1,2,3], limit:1)->Column#6"
" └─TopN 1.00 mpp[tiflash] Column#5, offset:0, count:1",
" └─TableFullScan 1.00 mpp[tiflash] table:t, index:idx_vec(vec) keep order:false, stats:pseudo, annIndex:COSINE(vec..[1,2,3], limit:1)->Column#5"
],
"Warn": null
},
{
"SQL": "explain format = 'brief' select /*+ ignore_index(t, idx_v)*/ a from t order by VEC_COSINE_DISTANCE(`vec`, '[1, 2, 3]') limit 1;",
"Plan": [
"TopN 1.00 root Column#5, offset:0, count:1",
"TopN 1.00 root Column#4, offset:0, count:1",
"└─TableReader 1.00 root MppVersion: 3, data:ExchangeSender",
" └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough",
" └─TopN 1.00 mpp[tiflash] Column#5, offset:0, count:1",
" └─Projection 1.00 mpp[tiflash] test.t.a, vec_cosine_distance(test.t.vec, [1,2,3])->Column#5",
" └─TopN 1.00 mpp[tiflash] Column#4, offset:0, count:1",
" └─Projection 1.00 mpp[tiflash] test.t.a, vec_cosine_distance(test.t.vec, [1,2,3])->Column#4",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
],
"Warn": null
Expand Down
5 changes: 4 additions & 1 deletion pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,10 @@ func isForUpdateReadSelectLock(lock *ast.SelectLockInfo) bool {
}

func isTiKVIndexByName(idxName string, tblInfo *model.TableInfo) bool {
// when the PKIsHandle of table is true, the primary key is not in the indices list.
if idxName == "primary" {
return true
}
for _, index := range tblInfo.Indices {
if index.Name.L == idxName {
return !index.IsColumnarIndex()
Expand Down Expand Up @@ -1303,7 +1307,6 @@ func getPossibleAccessPaths(ctx base.PlanContext, tableHints *hint.PlanHints, in
continue
}
if isTiKVIndexByName(idxName.L, tblInfo) && !isolationReadEnginesHasTiKV {
fmt.Println("TiKV is not supported in isolation read engines")
engineVals, _ := ctx.GetSessionVars().GetSystemVar(vardef.TiDBIsolationReadEngines)
err := fmt.Errorf("TiDB doesn't support index '%v' in the isolation read engines(value: '%v')", idxName, engineVals)
if i < indexHintsLen {
Expand Down