未验证 提交 3b5b50bd 编写于 作者: Z Zhao Shunjie 提交者: GitHub

add autoID to varchar dataType (#24907)

Signed-off-by: Nshunjiezhao <939038111@qq.com>
上级 c7dc1c06
...@@ -341,11 +341,11 @@ func validatePrimaryKey(coll *schemapb.CollectionSchema) error { ...@@ -341,11 +341,11 @@ func validatePrimaryKey(coll *schemapb.CollectionSchema) error {
// varchar field do not support autoID // varchar field do not support autoID
// If autoID is required, it is recommended to use int64 field as the primary key // If autoID is required, it is recommended to use int64 field as the primary key
if field.DataType == schemapb.DataType_VarChar { //if field.DataType == schemapb.DataType_VarChar {
if field.AutoID { // if field.AutoID {
return fmt.Errorf("autoID is not supported when the VarChar field is the primary key") // return fmt.Errorf("autoID is not supported when the VarChar field is the primary key")
} // }
} //}
idx = i idx = i
} }
...@@ -551,20 +551,36 @@ func autoGenPrimaryFieldData(fieldSchema *schemapb.FieldSchema, data interface{} ...@@ -551,20 +551,36 @@ func autoGenPrimaryFieldData(fieldSchema *schemapb.FieldSchema, data interface{}
fieldData.Type = fieldSchema.DataType fieldData.Type = fieldSchema.DataType
switch data := data.(type) { switch data := data.(type) {
case []int64: case []int64:
if fieldSchema.DataType != schemapb.DataType_Int64 { switch fieldData.Type {
return nil, errors.New("the data type of the data and the schema do not match") case schemapb.DataType_Int64:
} fieldData.Field = &schemapb.FieldData_Scalars{
fieldData.Field = &schemapb.FieldData_Scalars{ Scalars: &schemapb.ScalarField{
Scalars: &schemapb.ScalarField{ Data: &schemapb.ScalarField_LongData{
Data: &schemapb.ScalarField_LongData{ LongData: &schemapb.LongArray{
LongData: &schemapb.LongArray{ Data: data,
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: 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 return &fieldData, nil
...@@ -1050,7 +1066,7 @@ func checkPrimaryFieldData(schema *schemapb.CollectionSchema, result *milvuspb.M ...@@ -1050,7 +1066,7 @@ func checkPrimaryFieldData(schema *schemapb.CollectionSchema, result *milvuspb.M
if typeutil.IsPrimaryFieldDataExist(insertMsg.GetFieldsData(), primaryFieldSchema) { if typeutil.IsPrimaryFieldDataExist(insertMsg.GetFieldsData(), primaryFieldSchema) {
return nil, fmt.Errorf("can not assign primary field data when auto id enabled %v", primaryFieldSchema.Name) 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()) primaryFieldData, err = autoGenPrimaryFieldData(primaryFieldSchema, insertMsg.GetRowIDs())
if err != nil { if err != nil {
log.Info("generate primary field data failed when autoID == true", zap.String("collectionName", insertMsg.CollectionName), zap.Error(err)) log.Info("generate primary field data failed when autoID == true", zap.String("collectionName", insertMsg.CollectionName), zap.Error(err))
......
...@@ -309,7 +309,7 @@ func TestValidatePrimaryKey(t *testing.T) { ...@@ -309,7 +309,7 @@ func TestValidatePrimaryKey(t *testing.T) {
// test collection with varChar field as primary and autoID = true // test collection with varChar field as primary and autoID = true
VarCharField.AutoID = true VarCharField.AutoID = true
assert.Error(t, validatePrimaryKey(&schemapb.CollectionSchema{ assert.Nil(t, validatePrimaryKey(&schemapb.CollectionSchema{
Name: "coll1", Name: "coll1",
Description: "", Description: "",
AutoID: true, AutoID: true,
......
...@@ -3754,7 +3754,7 @@ class TestCollectionString(TestcaseBase): ...@@ -3754,7 +3754,7 @@ class TestCollectionString(TestcaseBase):
""" """
target: test create collection with string field target: test create collection with string field
method: create collection with string field, the string field primary and auto id are true method: create collection with string field, the string field primary and auto id are true
expected: Raise exception expected: Create collection successfully
""" """
self._connect() self._connect()
int_field = cf.gen_int64_field() int_field = cf.gen_int64_field()
...@@ -3762,9 +3762,11 @@ class TestCollectionString(TestcaseBase): ...@@ -3762,9 +3762,11 @@ class TestCollectionString(TestcaseBase):
string_field = cf.gen_string_field(is_primary=True, auto_id=True) string_field = cf.gen_string_field(is_primary=True, auto_id=True)
fields = [int_field, string_field, vec_field] fields = [int_field, string_field, vec_field]
schema, _ = self.collection_schema_wrap.init_collection_schema(fields=fields) 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"} # 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, c_name = cf.gen_unique_str(prefix)
check_task=CheckTasks.err_res, check_items=error) 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): class TestCollectionJSON(TestcaseBase):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册