fix: Check whether the DataType of the column is legal (#31697)

issue: #31696

---------

Signed-off-by: Cai Zhang <cai.zhang@zilliz.com>
pull/31190/head
cai.zhang 2024-03-29 09:33:16 +08:00 committed by GitHub
parent 6c6764bcad
commit be6b468a3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 3 deletions

View File

@ -182,6 +182,15 @@ func hasSystemFields(schema *schemapb.CollectionSchema, systemFields []string) b
return false
}
func validateFieldDataType(schema *schemapb.CollectionSchema) error {
for _, field := range schema.GetFields() {
if _, ok := schemapb.DataType_name[int32(field.GetDataType())]; !ok || field.GetDataType() == schemapb.DataType_None {
return merr.WrapErrParameterInvalid("valid field", fmt.Sprintf("field data type: %s is not supported", field.GetDataType()))
}
}
return nil
}
func (t *createCollectionTask) validateSchema(schema *schemapb.CollectionSchema) error {
log.With(zap.String("CollectionName", t.Req.CollectionName))
if t.Req.GetCollectionName() != schema.GetName() {
@ -204,7 +213,7 @@ func (t *createCollectionTask) validateSchema(schema *schemapb.CollectionSchema)
msg := fmt.Sprintf("schema contains system field: %s, %s, %s", RowIDFieldName, TimeStampFieldName, MetaFieldName)
return merr.WrapErrParameterInvalid("schema don't contains system field", "contains", msg)
}
return nil
return validateFieldDataType(schema)
}
func (t *createCollectionTask) assignFieldID(schema *schemapb.CollectionSchema) {

View File

@ -522,7 +522,10 @@ func Test_createCollectionTask_prepareSchema(t *testing.T) {
Description: "",
AutoID: false,
Fields: []*schemapb.FieldSchema{
{Name: field1},
{
Name: field1,
DataType: schemapb.DataType_Int64,
},
},
}
marshaledSchema, err := proto.Marshal(schema)
@ -537,6 +540,33 @@ func Test_createCollectionTask_prepareSchema(t *testing.T) {
err = task.prepareSchema()
assert.NoError(t, err)
})
t.Run("invalid data type", func(t *testing.T) {
collectionName := funcutil.GenRandomStr()
field1 := funcutil.GenRandomStr()
schema := &schemapb.CollectionSchema{
Name: collectionName,
Description: "",
AutoID: false,
Fields: []*schemapb.FieldSchema{
{
Name: field1,
DataType: 200,
},
},
}
marshaledSchema, err := proto.Marshal(schema)
assert.NoError(t, err)
task := createCollectionTask{
Req: &milvuspb.CreateCollectionRequest{
Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_CreateCollection},
CollectionName: collectionName,
Schema: marshaledSchema,
},
}
err = task.prepareSchema()
assert.Error(t, err)
})
}
func Test_createCollectionTask_Prepare(t *testing.T) {
@ -636,7 +666,10 @@ func Test_createCollectionTask_Prepare(t *testing.T) {
Description: "",
AutoID: false,
Fields: []*schemapb.FieldSchema{
{Name: field1},
{
Name: field1,
DataType: schemapb.DataType_Int64,
},
},
}
marshaledSchema, err := proto.Marshal(schema)