Skip to content

Commit

Permalink
Handle LongData returned when queryresult is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
  • Loading branch information
congqixia committed May 4, 2023
1 parent 0f8c85b commit c6d22de
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
25 changes: 22 additions & 3 deletions entity/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
return NewColumnBool(fd.GetFieldName(), data.BoolData.GetData()[begin:end]), nil

case schema.DataType_Int8:
data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
data, ok := getIntData(fd)
if !ok {
return nil, errFieldDataTypeNotMatch
}
Expand All @@ -140,7 +140,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
return NewColumnInt8(fd.GetFieldName(), values[begin:end]), nil

case schema.DataType_Int16:
data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
data, ok := getIntData(fd)
if !ok {
return nil, errFieldDataTypeNotMatch
}
Expand All @@ -155,7 +155,7 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
return NewColumnInt16(fd.GetFieldName(), values[begin:end]), nil

case schema.DataType_Int32:
data, ok := fd.GetScalars().GetData().(*schema.ScalarField_IntData)
data, ok := getIntData(fd)
if !ok {
return nil, errFieldDataTypeNotMatch
}
Expand Down Expand Up @@ -218,6 +218,25 @@ func FieldDataColumn(fd *schema.FieldData, begin, end int) (Column, error) {
}
}

// getIntData get int32 slice from result field data
// also handles LongData bug (see also https://github.com/milvus-io/milvus/issues/23850)
func getIntData(fd *schema.FieldData) (*schema.ScalarField_IntData, bool) {
switch data := fd.GetScalars().GetData().(type) {
case *schema.ScalarField_IntData:
return data, true
case *schema.ScalarField_LongData:
// only alway empty LongData for backward compatibility
if len(data.LongData.GetData()) == 0 {
return &schema.ScalarField_IntData{
IntData: &schema.IntArray{},
}, true
}
return nil, false
default:
return nil, false
}
}

// FieldDataColumn converts schema.FieldData to vector Column
func FieldDataVector(fd *schema.FieldData) (Column, error) {
switch fd.GetType() {
Expand Down
75 changes: 75 additions & 0 deletions entity/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,78 @@ func TestIDColumns(t *testing.T) {
assert.Equal(t, dataLen, column.Len())
})
}

func TestGetIntData(t *testing.T) {
type testCase struct {
tag string
fd *schema.FieldData
expectOK bool
}

cases := []testCase{
{
tag: "normal_IntData",
fd: &schema.FieldData{
Field: &schema.FieldData_Scalars{
Scalars: &schema.ScalarField{
Data: &schema.ScalarField_IntData{
IntData: &schema.IntArray{Data: []int32{1, 2, 3}},
},
},
},
},
expectOK: true,
},
{
tag: "empty_LongData",
fd: &schema.FieldData{
Field: &schema.FieldData_Scalars{
Scalars: &schema.ScalarField{
Data: &schema.ScalarField_LongData{
LongData: &schema.LongArray{Data: nil},
},
},
},
},
expectOK: true,
},
{
tag: "nonempty_LongData",
fd: &schema.FieldData{
Field: &schema.FieldData_Scalars{
Scalars: &schema.ScalarField{
Data: &schema.ScalarField_LongData{
LongData: &schema.LongArray{Data: []int64{1, 2, 3}},
},
},
},
},
expectOK: false,
},
{
tag: "other_data",
fd: &schema.FieldData{
Field: &schema.FieldData_Scalars{
Scalars: &schema.ScalarField{
Data: &schema.ScalarField_BoolData{},
},
},
},
expectOK: false,
},
{
tag: "vector_data",
fd: &schema.FieldData{
Field: &schema.FieldData_Vectors{},
},
expectOK: false,
},
}

for _, tc := range cases {
t.Run(tc.tag, func(t *testing.T) {
_, ok := getIntData(tc.fd)
assert.Equal(t, tc.expectOK, ok)
})
}
}

0 comments on commit c6d22de

Please sign in to comment.