forked from milvus-io/milvus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable sort for segcore RetrieveResults (milvus-io#18894)
See also: milvus-io#18893 Signed-off-by: yangxuan <xuan.yang@zilliz.com> Signed-off-by: yangxuan <xuan.yang@zilliz.com>
- Loading branch information
1 parent
65fffa6
commit 086eb92
Showing
12 changed files
with
475 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package querynode | ||
|
||
import ( | ||
"github.com/milvus-io/milvus/internal/proto/schemapb" | ||
"github.com/milvus-io/milvus/internal/proto/segcorepb" | ||
"github.com/milvus-io/milvus/internal/util/typeutil" | ||
) | ||
|
||
type byPK struct { | ||
r *segcorepb.RetrieveResults | ||
} | ||
|
||
func (s *byPK) Len() int { | ||
if s.r == nil { | ||
return 0 | ||
} | ||
|
||
switch id := s.r.GetIds().GetIdField().(type) { | ||
case *schemapb.IDs_IntId: | ||
return len(id.IntId.GetData()) | ||
case *schemapb.IDs_StrId: | ||
return len(id.StrId.GetData()) | ||
} | ||
return 0 | ||
} | ||
|
||
func (s *byPK) Swap(i, j int) { | ||
s.r.Offset[i], s.r.Offset[j] = s.r.Offset[j], s.r.Offset[i] | ||
|
||
typeutil.SwapPK(s.r.GetIds(), i, j) | ||
|
||
for _, field := range s.r.GetFieldsData() { | ||
swapFieldData(field, i, j) | ||
} | ||
} | ||
|
||
func (s *byPK) Less(i, j int) bool { | ||
return typeutil.ComparePK(s.r.GetIds(), i, j) | ||
} | ||
|
||
func swapFieldData(field *schemapb.FieldData, i int, j int) { | ||
switch field.GetField().(type) { | ||
case *schemapb.FieldData_Scalars: | ||
switch sd := field.GetScalars().GetData().(type) { | ||
case *schemapb.ScalarField_BoolData: | ||
data := sd.BoolData.Data | ||
data[i], data[j] = data[j], data[i] | ||
case *schemapb.ScalarField_IntData: | ||
data := sd.IntData.Data | ||
data[i], data[j] = data[j], data[i] | ||
case *schemapb.ScalarField_LongData: | ||
data := sd.LongData.Data | ||
data[i], data[j] = data[j], data[i] | ||
case *schemapb.ScalarField_FloatData: | ||
data := sd.FloatData.Data | ||
data[i], data[j] = data[j], data[i] | ||
case *schemapb.ScalarField_DoubleData: | ||
data := sd.DoubleData.Data | ||
data[i], data[j] = data[j], data[i] | ||
case *schemapb.ScalarField_StringData: | ||
data := sd.StringData.Data | ||
data[i], data[j] = data[j], data[i] | ||
} | ||
case *schemapb.FieldData_Vectors: | ||
dim := int(field.GetVectors().GetDim()) | ||
switch vd := field.GetVectors().GetData().(type) { | ||
case *schemapb.VectorField_BinaryVector: | ||
steps := dim / 8 // dim for binary vector must be multiplier of 8 | ||
srcToSwap := vd.BinaryVector[i*steps : (i+1)*steps] | ||
dstToSwap := vd.BinaryVector[j*steps : (j+1)*steps] | ||
|
||
for k := range srcToSwap { | ||
srcToSwap[k], dstToSwap[k] = dstToSwap[k], srcToSwap[k] | ||
} | ||
case *schemapb.VectorField_FloatVector: | ||
srcToSwap := vd.FloatVector.Data[i*dim : (i+1)*dim] | ||
dstToSwap := vd.FloatVector.Data[j*dim : (j+1)*dim] | ||
|
||
for k := range srcToSwap { | ||
srcToSwap[k], dstToSwap[k] = dstToSwap[k], srcToSwap[k] | ||
} | ||
} | ||
default: | ||
errMsg := "undefined data type " + field.Type.String() | ||
panic(errMsg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package querynode | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/milvus-io/milvus/internal/proto/schemapb" | ||
"github.com/milvus-io/milvus/internal/proto/segcorepb" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResultSorter_ByIntPK(t *testing.T) { | ||
result := &segcorepb.RetrieveResults{ | ||
Ids: &schemapb.IDs{ | ||
IdField: &schemapb.IDs_IntId{ | ||
IntId: &schemapb.LongArray{ | ||
Data: []int64{5, 4, 3, 2, 9, 8, 7, 6}, | ||
}}, | ||
}, | ||
Offset: []int64{5, 4, 3, 2, 9, 8, 7, 6}, | ||
FieldsData: []*schemapb.FieldData{ | ||
genFieldData("int64 field", 100, schemapb.DataType_Int64, | ||
[]int64{5, 4, 3, 2, 9, 8, 7, 6}, 1), | ||
genFieldData("double field", 101, schemapb.DataType_Double, | ||
[]float64{5, 4, 3, 2, 9, 8, 7, 6}, 1), | ||
genFieldData("string field", 102, schemapb.DataType_VarChar, | ||
[]string{"5", "4", "3", "2", "9", "8", "7", "6"}, 1), | ||
genFieldData("bool field", 103, schemapb.DataType_Bool, | ||
[]bool{false, true, false, true, false, true, false, true}, 1), | ||
genFieldData("float field", 104, schemapb.DataType_Float, | ||
[]float32{5, 4, 3, 2, 9, 8, 7, 6}, 1), | ||
genFieldData("int field", 105, schemapb.DataType_Int32, | ||
[]int32{5, 4, 3, 2, 9, 8, 7, 6}, 1), | ||
genFieldData("float vector field", 106, schemapb.DataType_FloatVector, | ||
[]float32{5, 4, 3, 2, 9, 8, 7, 6}, 1), | ||
genFieldData("binary vector field", 107, schemapb.DataType_BinaryVector, | ||
[]byte{5, 4, 3, 2, 9, 8, 7, 6}, 8), | ||
}, | ||
} | ||
|
||
sort.Sort(&byPK{result}) | ||
|
||
assert.Equal(t, []int64{2, 3, 4, 5, 6, 7, 8, 9}, result.GetIds().GetIntId().GetData()) | ||
assert.Equal(t, []int64{2, 3, 4, 5, 6, 7, 8, 9}, result.GetOffset()) | ||
assert.Equal(t, []int64{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[0].GetScalars().GetLongData().Data) | ||
assert.InDeltaSlice(t, []float64{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[1].GetScalars().GetDoubleData().Data, 10e-10) | ||
assert.Equal(t, []string{"2", "3", "4", "5", "6", "7", "8", "9"}, result.FieldsData[2].GetScalars().GetStringData().Data) | ||
assert.Equal(t, []bool{true, false, true, false, true, false, true, false}, result.FieldsData[3].GetScalars().GetBoolData().Data) | ||
assert.InDeltaSlice(t, []float32{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[4].GetScalars().GetFloatData().Data, 10e-10) | ||
assert.Equal(t, []int32{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[5].GetScalars().GetIntData().Data) | ||
assert.InDeltaSlice(t, []float32{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[6].GetVectors().GetFloatVector().GetData(), 10e-10) | ||
assert.Equal(t, []byte{2, 3, 4, 5, 6, 7, 8, 9}, result.FieldsData[7].GetVectors().GetBinaryVector()) | ||
} |
Oops, something went wrong.