diff --git a/internal/datacoord/index_service.go b/internal/datacoord/index_service.go index 6139306e2bb0f..d039bf20ae9e4 100644 --- a/internal/datacoord/index_service.go +++ b/internal/datacoord/index_service.go @@ -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 { diff --git a/internal/datacoord/index_service_test.go b/internal/datacoord/index_service_test.go index 040fcfe124372..b551a892f260c 100644 --- a/internal/datacoord/index_service_test.go +++ b/internal/datacoord/index_service_test.go @@ -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) + }) }