2021-12-10 21:17:51 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 13:47:10 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-10 21:17:51 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:47:10 +08:00
|
|
|
//
|
2021-12-10 21:17:51 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 13:47:10 +08:00
|
|
|
|
2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-11-02 18:16:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/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-06-15 12:41:40 +08:00
|
|
|
segment := newSegment(collection, segmentID, defaultPartitionID, collectionID, "", segmentTypeGrowing, true)
|
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
|
2021-10-08 17:39:55 +08:00
|
|
|
dslString := "{\"bool\": { \n\"vector\": {\n \"vec\": {\n \"metric_type\": \"L2\", \n \"params\": {\n \"nprobe\": 10 \n},\n \"query\": \"$0\",\n \"topk\": 10 \n,\"round_decimal\": 6\n } \n } \n } \n }"
|
2020-11-26 16:01:31 +08:00
|
|
|
var searchRawData1 []byte
|
|
|
|
var searchRawData2 []byte
|
|
|
|
for i, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
2021-11-02 18:16:32 +08:00
|
|
|
common.Endian.PutUint32(buf, math.Float32bits(ele+float32(i*2)))
|
2020-11-26 16:01:31 +08:00
|
|
|
searchRawData1 = append(searchRawData1, buf...)
|
|
|
|
}
|
|
|
|
for i, ele := range vec {
|
|
|
|
buf := make([]byte, 4)
|
2021-11-02 18:16:32 +08:00
|
|
|
common.Endian.PutUint32(buf, math.Float32bits(ele+float32(i*4)))
|
2020-11-26 16:01:31 +08:00
|
|
|
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-03-12 14:22:09 +08:00
|
|
|
Type: milvuspb.PlaceholderType_FloatVector,
|
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")
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:20:33 +08:00
|
|
|
plan, err := createSearchPlan(collection, dslString)
|
2020-11-30 17:58:23 +08:00
|
|
|
assert.NoError(t, err)
|
2021-03-30 22:16:58 +08:00
|
|
|
holder, err := parseSearchRequest(plan, placeGroupByte)
|
2020-11-30 17:58:23 +08:00
|
|
|
assert.NoError(t, err)
|
2021-03-30 22:16:58 +08:00
|
|
|
placeholderGroups := make([]*searchRequest, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
placeholderGroups = append(placeholderGroups, holder)
|
|
|
|
|
|
|
|
searchResults := make([]*SearchResult, 0)
|
2021-07-13 22:20:33 +08:00
|
|
|
searchResult, err := segment.search(plan, placeholderGroups, []Timestamp{0})
|
2020-11-26 16:01:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
searchResults = append(searchResults, searchResult)
|
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
err = reduceSearchResultsAndFillData(plan, searchResults, 1)
|
2020-12-03 19:00:11 +08:00
|
|
|
assert.Nil(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
marshaledHits, err := reorganizeSearchResults(searchResults, 1)
|
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)
|
|
|
|
|
2021-12-14 15:31:07 +08:00
|
|
|
var offset int64
|
2020-11-26 16:01:31 +08:00
|
|
|
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)
|
|
|
|
}
|
2021-09-10 18:12:01 +08:00
|
|
|
|
|
|
|
func TestReduce_nilPlan(t *testing.T) {
|
|
|
|
plan := &SearchPlan{}
|
|
|
|
err := reduceSearchResultsAndFillData(plan, nil, 1)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|