-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1e24d39
commit 8516d55
Showing
46 changed files
with
1,414 additions
and
157 deletions.
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.