2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2020-11-26 16:01:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReduce_AllFunc(t *testing.T) {
|
2020-12-08 14:41:04 +08:00
|
|
|
collectionID := UniqueID(0)
|
|
|
|
segmentID := UniqueID(0)
|
2021-02-03 11:52:19 +08:00
|
|
|
collectionMeta := genTestCollectionMeta(collectionID, false)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-01-18 10:38:41 +08:00
|
|
|
collection := newCollection(collectionMeta.ID, collectionMeta.Schema)
|
2021-02-03 18:12:48 +08:00
|
|
|
segment := newSegment(collection, segmentID, defaultPartitionID, collectionID, segTypeGrowing)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
const DIM = 16
|
|
|
|
var vec = [DIM]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
|
|
|
|
|
|
|
|
// start search service
|
|
|
|
dslString := "{\"bool\": { \n\"vector\": {\n \"vec\": {\n \"metric_type\": \"L2\", \n \"params\": {\n \"nprobe\": 10 \n},\n \"query\": \"$0\",\"topk\": 10 \n } \n } \n } \n }"
|
|
|
|
var searchRawData1 []byte
|
|
|
|
var searchRawData2 []byte
|
|
|
|
for i, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, math.Float32bits(ele+float32(i*2)))
|
|
|
|
searchRawData1 = append(searchRawData1, buf...)
|
|
|
|
}
|
|
|
|
for i, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, math.Float32bits(ele+float32(i*4)))
|
|
|
|
searchRawData2 = append(searchRawData2, buf...)
|
|
|
|
}
|
2021-01-22 09:36:18 +08:00
|
|
|
placeholderValue := milvuspb.PlaceholderValue{
|
2020-11-26 16:01:31 +08:00
|
|
|
Tag: "$0",
|
2021-01-22 09:36:18 +08:00
|
|
|
Type: milvuspb.PlaceholderType_VECTOR_FLOAT,
|
2020-11-26 16:01:31 +08:00
|
|
|
Values: [][]byte{searchRawData1, searchRawData2},
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
placeholderGroup := milvuspb.PlaceholderGroup{
|
|
|
|
Placeholders: []*milvuspb.PlaceholderValue{&placeholderValue},
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
placeGroupByte, err := proto.Marshal(&placeholderGroup)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("marshal placeholderGroup failed")
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:58:23 +08:00
|
|
|
plan, err := createPlan(*collection, dslString)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
holder, err := parserPlaceholderGroup(plan, placeGroupByte)
|
|
|
|
assert.NoError(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
placeholderGroups := make([]*PlaceholderGroup, 0)
|
|
|
|
placeholderGroups = append(placeholderGroups, holder)
|
|
|
|
|
|
|
|
searchResults := make([]*SearchResult, 0)
|
2020-12-03 19:00:11 +08:00
|
|
|
matchedSegment := make([]*Segment, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
searchResult, err := segment.segmentSearch(plan, placeholderGroups, []Timestamp{0})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
searchResults = append(searchResults, searchResult)
|
2020-12-03 19:00:11 +08:00
|
|
|
matchedSegment = append(matchedSegment, segment)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
testReduce := make([]bool, len(searchResults))
|
|
|
|
err = reduceSearchResults(searchResults, 1, testReduce)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = fillTargetEntry(plan, searchResults, matchedSegment, testReduce)
|
|
|
|
assert.Nil(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
marshaledHits, err := reorganizeQueryResults(plan, placeholderGroups, searchResults, 1, testReduce)
|
2020-11-26 16:01:31 +08:00
|
|
|
assert.NotNil(t, marshaledHits)
|
2020-12-03 19:00:11 +08:00
|
|
|
assert.Nil(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
hitsBlob, err := marshaledHits.getHitsBlob()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var offset int64 = 0
|
|
|
|
for index := range placeholderGroups {
|
|
|
|
hitBolbSizePeerQuery, err := marshaledHits.hitBlobSizeInGroup(int64(index))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
for _, len := range hitBolbSizePeerQuery {
|
|
|
|
marshaledHit := hitsBlob[offset : offset+len]
|
2021-01-22 09:36:18 +08:00
|
|
|
unMarshaledHit := milvuspb.Hits{}
|
2020-11-26 16:01:31 +08:00
|
|
|
err = proto.Unmarshal(marshaledHit, &unMarshaledHit)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
log.Println("hits msg = ", unMarshaledHit)
|
|
|
|
offset += len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plan.delete()
|
|
|
|
holder.delete()
|
|
|
|
deleteSearchResults(searchResults)
|
|
|
|
deleteMarshaledHits(marshaledHits)
|
|
|
|
deleteSegment(segment)
|
|
|
|
deleteCollection(collection)
|
|
|
|
}
|