add autoID to varchar dataType (#24907)

Signed-off-by: shunjiezhao <939038111@qq.com>
pull/24921/head
Zhao Shunjie 2023-06-16 17:00:40 +08:00 committed by GitHub
parent c7dc1c067a
commit 3b5b50bda8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 21 deletions

View File

@ -341,11 +341,11 @@ func validatePrimaryKey(coll *schemapb.CollectionSchema) error {
// varchar field do not support autoID
// If autoID is required, it is recommended to use int64 field as the primary key
if field.DataType == schemapb.DataType_VarChar {
if field.AutoID {
return fmt.Errorf("autoID is not supported when the VarChar field is the primary key")
}
}
//if field.DataType == schemapb.DataType_VarChar {
// if field.AutoID {
// return fmt.Errorf("autoID is not supported when the VarChar field is the primary key")
// }
//}
idx = i
}
@ -551,20 +551,36 @@ func autoGenPrimaryFieldData(fieldSchema *schemapb.FieldSchema, data interface{}
fieldData.Type = fieldSchema.DataType
switch data := data.(type) {
case []int64:
if fieldSchema.DataType != schemapb.DataType_Int64 {
return nil, errors.New("the data type of the data and the schema do not match")
}
fieldData.Field = &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_LongData{
LongData: &schemapb.LongArray{
Data: data,
switch fieldData.Type {
case schemapb.DataType_Int64:
fieldData.Field = &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_LongData{
LongData: &schemapb.LongArray{
Data: data,
},
},
},
},
}
case schemapb.DataType_VarChar:
strIds := make([]string, len(data))
for i, v := range data {
strIds[i] = strconv.FormatInt(v, 10)
}
fieldData.Field = &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_StringData{
StringData: &schemapb.StringArray{
Data: strIds,
},
},
},
}
default:
return nil, errors.New("currently only support autoID for int64 and varchar PrimaryField")
}
default:
return nil, errors.New("currently only support autoID for int64 PrimaryField")
return nil, errors.New("currently only int64 is supported as the data source for the autoID of a PrimaryField")
}
return &fieldData, nil
@ -1050,7 +1066,7 @@ func checkPrimaryFieldData(schema *schemapb.CollectionSchema, result *milvuspb.M
if typeutil.IsPrimaryFieldDataExist(insertMsg.GetFieldsData(), primaryFieldSchema) {
return nil, fmt.Errorf("can not assign primary field data when auto id enabled %v", primaryFieldSchema.Name)
}
// if autoID == true, currently only support autoID for int64 PrimaryField
// if autoID == true, currently support autoID for int64 and varchar PrimaryField
primaryFieldData, err = autoGenPrimaryFieldData(primaryFieldSchema, insertMsg.GetRowIDs())
if err != nil {
log.Info("generate primary field data failed when autoID == true", zap.String("collectionName", insertMsg.CollectionName), zap.Error(err))

View File

@ -309,7 +309,7 @@ func TestValidatePrimaryKey(t *testing.T) {
// test collection with varChar field as primary and autoID = true
VarCharField.AutoID = true
assert.Error(t, validatePrimaryKey(&schemapb.CollectionSchema{
assert.Nil(t, validatePrimaryKey(&schemapb.CollectionSchema{
Name: "coll1",
Description: "",
AutoID: true,

View File

@ -3754,7 +3754,7 @@ class TestCollectionString(TestcaseBase):
"""
target: test create collection with string field
method: create collection with string field, the string field primary and auto id are true
expected: Raise exception
expected: Create collection successfully
"""
self._connect()
int_field = cf.gen_int64_field()
@ -3762,9 +3762,11 @@ class TestCollectionString(TestcaseBase):
string_field = cf.gen_string_field(is_primary=True, auto_id=True)
fields = [int_field, string_field, vec_field]
schema, _ = self.collection_schema_wrap.init_collection_schema(fields=fields)
error = {ct.err_code: 0, ct.err_msg: "autoID is not supported when the VarChar field is the primary key"}
self.collection_wrap.init_collection(name=cf.gen_unique_str(prefix), schema=schema,
check_task=CheckTasks.err_res, check_items=error)
# error = {ct.err_code: 0, ct.err_msg: "autoID is not supported when the VarChar field is the primary key"}
c_name = cf.gen_unique_str(prefix)
self.collection_wrap.init_collection(name=c_name, schema=schema,
check_task=CheckTasks.check_collection_property,
check_items={exp_name: c_name, exp_schema: schema})
class TestCollectionJSON(TestcaseBase):