fix: Add index param duplication check (#39289)

Related to #39288

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/38403/head
congqixia 2025-01-15 19:23:01 +08:00 committed by GitHub
parent eb63334312
commit 82bdf9a6a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 0 deletions

View File

@ -282,6 +282,15 @@ func (s *Server) CreateIndex(ctx context.Context, req *indexpb.CreateIndexReques
}
func ValidateIndexParams(index *model.Index) error {
if err := CheckDuplidateKey(index.IndexParams, "indexParams"); err != nil {
return err
}
if err := CheckDuplidateKey(index.UserIndexParams, "userIndexParams"); err != nil {
return err
}
if err := CheckDuplidateKey(index.TypeParams, "typeParams"); err != nil {
return err
}
indexType := GetIndexType(index.IndexParams)
indexParams := funcutil.KeyValuePair2Map(index.IndexParams)
userIndexParams := funcutil.KeyValuePair2Map(index.UserIndexParams)
@ -300,6 +309,17 @@ func ValidateIndexParams(index *model.Index) error {
return nil
}
func CheckDuplidateKey(kvs []*commonpb.KeyValuePair, tag string) error {
keySet := typeutil.NewSet[string]()
for _, kv := range kvs {
if keySet.Contain(kv.GetKey()) {
return merr.WrapErrParameterInvalidMsg("duplicate %s key in %s params", kv.GetKey(), tag)
}
keySet.Insert(kv.GetKey())
}
return nil
}
func UpdateParams(index *model.Index, from []*commonpb.KeyValuePair, updates []*commonpb.KeyValuePair) []*commonpb.KeyValuePair {
params := make(map[string]string)
for _, param := range from {

View File

@ -2510,4 +2510,55 @@ func TestValidateIndexParams(t *testing.T) {
err := ValidateIndexParams(index)
assert.Error(t, err)
})
t.Run("duplicated_index_params", func(t *testing.T) {
index := &model.Index{
IndexParams: []*commonpb.KeyValuePair{
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
},
}
err := ValidateIndexParams(index)
assert.Error(t, err)
})
t.Run("duplicated_user_index_params", func(t *testing.T) {
index := &model.Index{
UserIndexParams: []*commonpb.KeyValuePair{
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
},
}
err := ValidateIndexParams(index)
assert.Error(t, err)
})
t.Run("duplicated_user_index_params", func(t *testing.T) {
index := &model.Index{
TypeParams: []*commonpb.KeyValuePair{
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
{
Key: common.IndexTypeKey,
Value: indexparamcheck.AutoIndex,
},
},
}
err := ValidateIndexParams(index)
assert.Error(t, err)
})
}