Skip to content
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/aliyun/alibaba-cloud-sdk-go v1.63.34
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/aliyun/credentials-go v1.3.10
github.com/ashvardanian/stringzilla/golang v0.0.0-20251027161958-645407b56fd1
github.com/aws/aws-sdk-go v1.55.5
github.com/aws/aws-sdk-go-v2 v1.32.5
github.com/aws/aws-sdk-go-v2/config v1.28.5
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer5
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/ashvardanian/stringzilla/golang v0.0.0-20251027161958-645407b56fd1 h1:wnXz4jgXTTFjDZy357Bq/jCxTHUTb105vS4bogI0Jww=
github.com/ashvardanian/stringzilla/golang v0.0.0-20251027161958-645407b56fd1/go.mod h1:iQfeN5MGwtAidmePxLg0w7DsKRNA3L83OHfkCYMBPEQ=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.32.5 h1:U8vdWJuY7ruAkzaOdD7guwJjD06YSKmnKCJs7s3IkIo=
Expand Down
6 changes: 5 additions & 1 deletion pkg/sql/colexec/table_function/hnsw_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ func runHnswSearch[T types.RealNumbers](proc *process.Process, u *hnswSearchStat

algo := newHnswAlgo(u.idxcfg, u.tblcfg)

rt := vectorindex.RuntimeConfig{
Limit: uint(u.limit),
OrigFuncName: u.tblcfg.OrigFuncName,
}
var keys any
keys, u.distances, err = veccache.Cache.Search(sqlexec.NewSqlProcess(proc), u.tblcfg.IndexTable, algo, fa, vectorindex.RuntimeConfig{Limit: uint(u.limit)})
keys, u.distances, err = veccache.Cache.Search(sqlexec.NewSqlProcess(proc), u.tblcfg.IndexTable, algo, fa, rt)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colexec/table_function/ivf_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func runIvfSearchVector[T types.RealNumbers](tf *TableFunction, u *ivfSearchStat
rt := vectorindex.RuntimeConfig{
Limit: uint(u.limit),
Probe: uint(u.tblcfg.Nprobe),
OrigFuncName: u.tblcfg.OrigFuncName,
BackgroundQueries: make([]*plan.Query, 1),
}
u.keys, u.distances, err = veccache.Cache.Search(sqlexec.NewSqlProcess(proc), key, algo, fa, rt)
Expand Down
8 changes: 5 additions & 3 deletions pkg/sql/plan/apply_indices_hnsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (builder *QueryBuilder) applyIndicesForSortUsingHnsw(nodeID int32, projNode
return nodeID, nil
}

if opType != metric.DistFuncOpTypes[distFnExpr.Func.ObjName] {
origFuncName := distFnExpr.Func.ObjName
if opType != metric.DistFuncOpTypes[origFuncName] {
return nodeID, nil
}

Expand All @@ -93,12 +94,13 @@ func (builder *QueryBuilder) applyIndicesForSortUsingHnsw(nodeID int32, projNode
}

// generate JSON by fmt.Sprintf instead of sonic.Marshal for performance
tblCfgStr := fmt.Sprintf(`{"db": "%s", "src": "%s", "metadata":"%s", "index":"%s", "threads_search": %d}`,
tblCfgStr := fmt.Sprintf(`{"db": "%s", "src": "%s", "metadata":"%s", "index":"%s", "threads_search": %d, "orig_func_name": "%s"}`,
scanNode.ObjRef.SchemaName,
scanNode.TableDef.Name,
metaDef.IndexTableName,
idxDef.IndexTableName,
nThread.(int64))
nThread.(int64),
origFuncName)

// JOIN between source table and hnsw_search table function
tableFuncTag := builder.genNewTag()
Expand Down
8 changes: 5 additions & 3 deletions pkg/sql/plan/apply_indices_ivfflat.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (builder *QueryBuilder) applyIndicesForSortUsingIvfflat(nodeID int32, projN
return nodeID, nil
}

if opType != metric.DistFuncOpTypes[distFnExpr.Func.ObjName] {
origFuncName := distFnExpr.Func.ObjName
if opType != metric.DistFuncOpTypes[origFuncName] {
return nodeID, nil
}

Expand Down Expand Up @@ -108,7 +109,7 @@ func (builder *QueryBuilder) applyIndicesForSortUsingIvfflat(nodeID int32, projN
params := idxDef.IndexAlgoParams

tblCfgStr := fmt.Sprintf(`{"db": "%s", "src": "%s", "metadata":"%s", "index":"%s", "threads_search": %d,
"entries": "%s", "nprobe" : %d, "pktype" : %d, "pkey" : "%s", "part" : "%s", "parttype" : %d}`,
"entries": "%s", "nprobe" : %d, "pktype" : %d, "pkey" : "%s", "part" : "%s", "parttype" : %d, "orig_func_name": "%s"}`,
scanNode.ObjRef.SchemaName,
scanNode.TableDef.Name,
metaDef.IndexTableName,
Expand All @@ -119,7 +120,8 @@ func (builder *QueryBuilder) applyIndicesForSortUsingIvfflat(nodeID int32, projN
pkType.Id,
scanNode.TableDef.Pkey.PkeyColName,
keyPart,
partType.Id)
partType.Id,
origFuncName)

// JOIN between source table and hnsw_search table function
tableFuncTag := builder.genNewTag()
Expand Down
3 changes: 1 addition & 2 deletions pkg/vectorindex/hnsw/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *HnswSearch[T]) Search(sqlproc *sqlexec.SqlProcess, anyquery any, rt vec
return nil, nil, moerr.NewInternalError(sqlproc.GetContext(), "heap return key is not int64")
}
reskeys = append(reskeys, sr.Id)
sr.Distance = metric.DistanceTransformHnsw(sr.Distance, s.Idxcfg.OpType, s.Idxcfg.Usearch.Metric)
sr.Distance = metric.DistanceTransformHnsw(sr.Distance, metric.DistFuncNameToMetricType[rt.OrigFuncName], s.Idxcfg.Usearch.Metric)
resdistances = append(resdistances, sr.Distance)
}

Expand Down Expand Up @@ -247,6 +247,5 @@ func (s *HnswSearch[T]) Load(sqlproc *sqlexec.SqlProcess) error {

// check config and update some parameters such as ef_search
func (s *HnswSearch[T]) UpdateConfig(newalgo cache.VectorIndexSearchIf) error {

return nil
}
2 changes: 0 additions & 2 deletions pkg/vectorindex/hnsw/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

fallocate "github.com/detailyang/go-fallocate"

"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/types"
Expand All @@ -36,7 +35,6 @@ import (
"github.com/matrixorigin/matrixone/pkg/vectorindex/sqlexec"
"github.com/matrixorigin/matrixone/pkg/vm/process"
"github.com/stretchr/testify/require"

usearch "github.com/unum-cloud/usearch/golang"
)

Expand Down
3 changes: 1 addition & 2 deletions pkg/vectorindex/ivfflat/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (idx *IvfflatSearchIndex[T]) Search(
resid = append(resid, pk)

dist := vector.GetFixedAtNoTypeCheck[float64](bat.Vecs[1], i)
dist = metric.DistanceTransformIvfflat(dist, idxcfg.OpType, metric.MetricType(idxcfg.Ivfflat.Metric))
dist = metric.DistanceTransformIvfflat(dist, metric.DistFuncNameToMetricType[rt.OrigFuncName], metric.MetricType(idxcfg.Ivfflat.Metric))
distances = append(distances, dist)
}
}
Expand Down Expand Up @@ -269,6 +269,5 @@ func (s *IvfflatSearch[T]) Load(sqlproc *sqlexec.SqlProcess) error {

// check config and update some parameters such as ef_search
func (s *IvfflatSearch[T]) UpdateConfig(newalgo cache.VectorIndexSearchIf) error {

return nil
}
14 changes: 7 additions & 7 deletions pkg/vectorindex/metric/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
var (
DistFuncOpTypes = map[string]string{
DistFn_L2Distance: OpType_L2Distance,
DistFn_L2sqDistance: OpType_L2sqDistance,
DistFn_L2sqDistance: OpType_L2Distance,
DistFn_InnerProduct: OpType_InnerProduct,
DistFn_CosineDistance: OpType_CosineDistance,
}
Expand Down Expand Up @@ -120,17 +120,17 @@ func MaxFloat[T types.RealNumbers]() T {
}
}

func DistanceTransformHnsw(dist float64, optype string, metric usearch.Metric) float64 {
if optype == OpType_L2Distance && metric == usearch.L2sq {
// metric is l2sq but optype is l2_distance
func DistanceTransformHnsw(dist float64, origMetricType MetricType, metricType usearch.Metric) float64 {
if origMetricType == Metric_L2Distance && metricType == usearch.L2sq {
// metric is l2sq but origin is l2_distance
return math.Sqrt(dist)
}
return dist
}

func DistanceTransformIvfflat(dist float64, optype string, metric MetricType) float64 {
if optype == OpType_L2Distance && metric == Metric_L2sqDistance {
// metric is l2sq but optype is l2_distance
func DistanceTransformIvfflat(dist float64, origMetricType, metricType MetricType) float64 {
if origMetricType == Metric_L2Distance && metricType == Metric_L2sqDistance {
// metric is l2sq but origin is l2_distance
return math.Sqrt(dist)
}
return dist
Expand Down
2 changes: 2 additions & 0 deletions pkg/vectorindex/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type IndexTableConfig struct {
IndexTable string `json:"index"`
PKey string `json:"pkey"`
KeyPart string `json:"part"`
OrigFuncName string `json:"orig_func_name"`
ThreadsBuild int64 `json:"threads_build"`
ThreadsSearch int64 `json:"threads_search"`
IndexCapacity int64 `json:"index_capacity"`
Expand Down Expand Up @@ -105,6 +106,7 @@ type IndexConfig struct {
type RuntimeConfig struct {
Limit uint
Probe uint
OrigFuncName string
BackgroundQueries []*plan.Query
}

Expand Down
Loading