Skip to content

Commit

Permalink
fix: Add index param duplication check (#39289)
Browse files Browse the repository at this point in the history
Related to #39288

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Jan 15, 2025
1 parent eb63334 commit 82bdf9a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/datacoord/index_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions internal/datacoord/index_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 82bdf9a

Please sign in to comment.