From 80a9efd96d927874c928d3281d55b37d140f1c37 Mon Sep 17 00:00:00 2001 From: smellthemoon <64083300+smellthemoon@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:27:04 +0800 Subject: [PATCH] fix: not check nullable and default value in pk field (#35987) #35926 Signed-off-by: lixinguo Co-authored-by: lixinguo --- internal/rootcoord/create_collection_task.go | 8 ++++ .../rootcoord/create_collection_task_test.go | 48 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/internal/rootcoord/create_collection_task.go b/internal/rootcoord/create_collection_task.go index 2c5df5d03d..e625ba0315 100644 --- a/internal/rootcoord/create_collection_task.go +++ b/internal/rootcoord/create_collection_task.go @@ -153,7 +153,15 @@ func checkFieldSchema(schema *schemapb.CollectionSchema) error { msg := fmt.Sprintf("vector type not support null, type:%s, name:%s", fieldSchema.GetDataType().String(), fieldSchema.GetName()) return merr.WrapErrParameterInvalidMsg(msg) } + if fieldSchema.GetNullable() && fieldSchema.IsPrimaryKey { + msg := fmt.Sprintf("primary field not support null, type:%s, name:%s", fieldSchema.GetDataType().String(), fieldSchema.GetName()) + return merr.WrapErrParameterInvalidMsg(msg) + } if fieldSchema.GetDefaultValue() != nil { + if fieldSchema.IsPrimaryKey { + msg := fmt.Sprintf("primary field not support default_value, type:%s, name:%s", fieldSchema.GetDataType().String(), fieldSchema.GetName()) + return merr.WrapErrParameterInvalidMsg(msg) + } switch fieldSchema.GetDefaultValue().Data.(type) { case *schemapb.ValueField_BoolData: if fieldSchema.GetDataType() != schemapb.DataType_Bool { diff --git a/internal/rootcoord/create_collection_task_test.go b/internal/rootcoord/create_collection_task_test.go index 5e13b6573c..b4f68e4de0 100644 --- a/internal/rootcoord/create_collection_task_test.go +++ b/internal/rootcoord/create_collection_task_test.go @@ -334,6 +334,54 @@ func Test_createCollectionTask_validateSchema(t *testing.T) { assert.Error(t, err) }) + t.Run("primary field set nullable", func(t *testing.T) { + collectionName := funcutil.GenRandomStr() + task := createCollectionTask{ + Req: &milvuspb.CreateCollectionRequest{ + Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_CreateCollection}, + CollectionName: collectionName, + }, + } + schema := &schemapb.CollectionSchema{ + Name: collectionName, + Fields: []*schemapb.FieldSchema{ + { + Name: "pk", + IsPrimaryKey: true, + Nullable: true, + }, + }, + } + err := task.validateSchema(schema) + assert.Error(t, err) + }) + + t.Run("primary field set default_value", func(t *testing.T) { + collectionName := funcutil.GenRandomStr() + task := createCollectionTask{ + Req: &milvuspb.CreateCollectionRequest{ + Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_CreateCollection}, + CollectionName: collectionName, + }, + } + schema := &schemapb.CollectionSchema{ + Name: collectionName, + Fields: []*schemapb.FieldSchema{ + { + Name: "pk", + IsPrimaryKey: true, + DefaultValue: &schemapb.ValueField{ + Data: &schemapb.ValueField_LongData{ + LongData: 1, + }, + }, + }, + }, + } + err := task.validateSchema(schema) + assert.Error(t, err) + }) + t.Run("has system fields", func(t *testing.T) { collectionName := funcutil.GenRandomStr() task := createCollectionTask{