Skip to content

Commit

Permalink
fix: update check for sparse hnsw index
Browse files Browse the repository at this point in the history
issue: #29419

Signed-off-by: Buqian Zheng <zhengbuqian@gmail.com>
  • Loading branch information
zhengbuqian committed Jun 27, 2024
1 parent be23495 commit 3b7ae10
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
12 changes: 11 additions & 1 deletion internal/proxy/task_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (

AutoIndexName = "AUTOINDEX"
DimKey = common.DimKey
IsSparseKey = common.IsSparseKey
)

type createIndexTask struct {
Expand Down Expand Up @@ -383,10 +384,15 @@ func checkTrain(field *schemapb.FieldSchema, indexParams map[string]string) erro
}
}

if !typeutil.IsSparseFloatVectorType(field.DataType) {
isSparse := typeutil.IsSparseFloatVectorType(field.DataType)

if !isSparse {
if err := fillDimension(field, indexParams); err != nil {
return err
}
} else {
// used only for checker, should be deleted after checking
indexParams[IsSparseKey] = "true"
}

if err := checker.CheckValidDataType(field.GetDataType()); err != nil {
Expand All @@ -399,6 +405,10 @@ func checkTrain(field *schemapb.FieldSchema, indexParams map[string]string) erro
return err
}

if isSparse {
delete(indexParams, IsSparseKey)
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const (
DropRatioBuildKey = "drop_ratio_build"

BitmapCardinalityLimitKey = "bitmap_cardinality_limit"
IsSparseKey = "is_sparse"
)

// Collection properties key
Expand Down
23 changes: 21 additions & 2 deletions pkg/util/indexparamcheck/base_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,37 @@ package indexparamcheck
import (
"fmt"
"math"
"strings"

"github.com/cockroachdb/errors"

"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/pkg/common"
)

type baseChecker struct{}

func (c baseChecker) CheckTrain(params map[string]string) error {
// vector dimension should be checked on collection creation. this is just some basic check
if !CheckIntByRange(params, DIM, 1, math.MaxInt) {
return fmt.Errorf("failed to check vector dimension, should be larger than 0 and smaller than math.MaxInt")
isSparse := false
if val, exist := params[common.IsSparseKey]; exist {
val = strings.ToLower(val)
if val != "true" && val != "false" {
return fmt.Errorf("invalid is_sparse value: %s, must be true or false", val)
}
if val == "true" {
isSparse = true
}
}
if isSparse {
if !CheckStrByValues(params, Metric, SparseMetrics) {
return fmt.Errorf("metric type not found or not supported for sparse float vectors, supported: %v", SparseMetrics)
}
} else {
// we do not check dim for sparse
if !CheckIntByRange(params, DIM, 1, math.MaxInt) {
return fmt.Errorf("failed to check vector dimension, should be larger than 0 and smaller than math.MaxInt")
}
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/indexparamcheck/base_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/pkg/common"
"github.com/milvus-io/milvus/pkg/util/metric"
)

Expand All @@ -18,12 +19,27 @@ func Test_baseChecker_CheckTrain(t *testing.T) {
paramsWithoutDim := map[string]string{
Metric: metric.L2,
}
sparseParamsWithoutDim := map[string]string{
Metric: metric.IP,
common.IsSparseKey: "tRue",
}
sparseParamsWrongMetric := map[string]string{
Metric: metric.L2,
common.IsSparseKey: "True",
}
badSparseParams := map[string]string{
Metric: metric.IP,
common.IsSparseKey: "ds",
}
cases := []struct {
params map[string]string
errIsNil bool
}{
{validParams, true},
{paramsWithoutDim, false},
{sparseParamsWithoutDim, true},
{sparseParamsWrongMetric, false},
{badSparseParams, false},
}

c := newBaseChecker()
Expand Down

0 comments on commit 3b7ae10

Please sign in to comment.