-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: support vector index in planner #56204
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
add4469
planner: support vector index by CBO
winoros e35fd47
Pass column_id to TiFlash
breezewish 2d015d8
Cherry-pick
breezewish bf3a8e7
address comments
winoros 784fd88
Merge branch 'support-planner-cbo' of ssh://ssh.github.com:443/winoro…
breezewish a0b1755
Fix lint
breezewish c638b26
Fix bazel
breezewish 9c577fa
Merge commit 'f31b09149d2f962d61cd27d0da64341d87f478da' into support-…
breezewish 0650ca5
Fix
breezewish 19748a5
Fix
breezewish b73f16b
Regenerate
breezewish 75c621f
fix row count
winoros 1e3853c
Fix
breezewish bcbea5c
Merge remote-tracking branch 'origin/master' into support-planner-cbo
breezewish 1e59ad2
correct USE INDEX's behavior
winoros fb5dfff
update tests
winoros b0d4767
re-implement the logic
winoros e8074a2
fix tests
winoros bce942f
fix tests
winoros e1f54ae
go mod tidy & fix test
winoros 0f761e6
go mod tidy & don't do sync load
winoros f60bfc7
fix wrong test
winoros 8692688
Merge branch 'master' into support-planner-cbo
winoros 95feba8
fix static check
winoros 8b9840f
Merge branch 'support-planner-cbo' of https://github.com/winoros/tidb…
winoros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5815,13 +5815,13 @@ def go_deps(): | |
name = "com_github_pingcap_tipb", | ||
build_file_proto_mode = "disable_global", | ||
importpath = "github.com/pingcap/tipb", | ||
sha256 = "119f73cede33a7d3712a9fedeb80f806228ecf1ec5258112b2b62af88731a304", | ||
strip_prefix = "github.com/pingcap/[email protected]20240919023442-cf70966bef25", | ||
sha256 = "b39e154272ba36d145c6049947a012a76be740b32a44a46d7253caa145c56cc9", | ||
strip_prefix = "github.com/pingcap/[email protected]20241008083645-0bcddae67837", | ||
urls = [ | ||
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20240919023442-cf70966bef25.zip", | ||
"http://ats.apps.svc/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20240919023442-cf70966bef25.zip", | ||
"https://cache.hawkingrei.com/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20240919023442-cf70966bef25.zip", | ||
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20240919023442-cf70966bef25.zip", | ||
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241008083645-0bcddae67837.zip", | ||
"http://ats.apps.svc/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241008083645-0bcddae67837.zip", | ||
"https://cache.hawkingrei.com/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241008083645-0bcddae67837.zip", | ||
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241008083645-0bcddae67837.zip", | ||
], | ||
) | ||
go_repository( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package expression | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/pingcap/tidb/pkg/parser/ast" | ||
"github.com/pingcap/tidb/pkg/parser/model" | ||
"github.com/pingcap/tidb/pkg/parser/mysql" | ||
"github.com/pingcap/tidb/pkg/types" | ||
"github.com/pingcap/tidb/pkg/util/intest" | ||
"github.com/pingcap/tipb/go-tipb" | ||
) | ||
|
||
var ( | ||
vsDistanceFnNamesLower = map[string]struct{}{ | ||
strings.ToLower(ast.VecL1Distance): {}, | ||
strings.ToLower(ast.VecL2Distance): {}, | ||
strings.ToLower(ast.VecCosineDistance): {}, | ||
strings.ToLower(ast.VecNegativeInnerProduct): {}, | ||
} | ||
) | ||
|
||
// VectorHelper is a helper struct for vector indexes. | ||
type VectorHelper struct { | ||
DistanceFnName model.CIStr | ||
FnPbCode tipb.ScalarFuncSig | ||
Vec types.VectorFloat32 | ||
Column *Column | ||
} | ||
|
||
// ExtractVectorHelper extracts a VectorSearchExpr from an expression. | ||
// NOTE: not all VectorSearch functions are supported by the index. The caller | ||
// needs to check the distance function name. | ||
func ExtractVectorHelper(expr Expression) *VectorHelper { | ||
x, ok := expr.(*ScalarFunction) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if _, isVecFn := vsDistanceFnNamesLower[x.FuncName.L]; !isVecFn { | ||
return nil | ||
} | ||
|
||
args := x.GetArgs() | ||
// One arg must be a vector column ref, and one arg must be a vector constant. | ||
var vectorConstant *Constant = nil | ||
var vectorColumn *Column = nil | ||
nVectorColumns := 0 | ||
nVectorConstants := 0 | ||
for _, arg := range args { | ||
if v, ok := arg.(*Column); ok { | ||
if v.RetType.GetType() != mysql.TypeTiDBVectorFloat32 { | ||
return nil | ||
} | ||
vectorColumn = v | ||
nVectorColumns++ | ||
} else if v, ok := arg.(*Constant); ok { | ||
if v.RetType.GetType() != mysql.TypeTiDBVectorFloat32 { | ||
return nil | ||
} | ||
vectorConstant = v | ||
nVectorConstants++ | ||
} | ||
} | ||
if nVectorColumns != 1 || nVectorConstants != 1 { | ||
return nil | ||
} | ||
|
||
intest.Assert(vectorConstant.Value.Kind() == types.KindVectorFloat32, "internal: expect vectorFloat32 constant, but got %s", vectorConstant.Value.String()) | ||
|
||
return &VectorHelper{ | ||
DistanceFnName: x.FuncName, | ||
FnPbCode: x.Function.PbCode(), | ||
Vec: vectorConstant.Value.GetVectorFloat32(), | ||
Column: vectorColumn, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vector indexes haven't hidden columns, currently only expression indexes seem to be used.