fix: check array field data is nil before getting the type (#33114)

issue: https://github.com/milvus-io/milvus/issues/33074

Signed-off-by: sunby <sunbingyi1992@gmail.com>
This commit is contained in:
Bingyi Sun 2024-05-17 17:29:37 +08:00 committed by GitHub
parent bcaacf6fe6
commit 5c6de473bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View File

@ -759,6 +759,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
switch field.GetElementType() {
case schemapb.DataType_Bool:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("bool array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_BoolData)(nil)) {
return merr.WrapErrParameterInvalid("bool array",
@ -767,6 +770,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Int8, schemapb.DataType_Int16, schemapb.DataType_Int32:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("int array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_IntData)(nil)) {
return merr.WrapErrParameterInvalid("int array",
@ -787,6 +793,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Int64:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("int64 array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_LongData)(nil)) {
return merr.WrapErrParameterInvalid("int64 array",
@ -795,6 +804,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Float:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("float array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_FloatData)(nil)) {
return merr.WrapErrParameterInvalid("float array",
@ -803,6 +815,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Double:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("double array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_DoubleData)(nil)) {
return merr.WrapErrParameterInvalid("double array",
@ -811,6 +826,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_VarChar, schemapb.DataType_String:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("string array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_StringData)(nil)) {
return merr.WrapErrParameterInvalid("string array",

View File

@ -6026,3 +6026,19 @@ func Test_validateUtil_checkDoubleFieldData(t *testing.T) {
},
}, nil))
}
func TestCheckArrayElementNilData(t *testing.T) {
data := &schemapb.ArrayArray{
Data: []*schemapb.ScalarField{nil},
}
fieldSchema := &schemapb.FieldSchema{
Name: "test",
DataType: schemapb.DataType_Array,
ElementType: schemapb.DataType_Int64,
}
v := newValidateUtil()
err := v.checkArrayElement(data, fieldSchema)
assert.True(t, merr.ErrParameterInvalid.Is(err))
}