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 (
|
2022-04-08 20:27:31 +08:00
|
|
|
"context"
|
2020-11-26 16:01:31 +08:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
"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"
|
2022-05-23 16:41:58 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2020-11-26 16:01:31 +08:00
|
|
|
)
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
func TestReduce_parseSliceInfo(t *testing.T) {
|
|
|
|
originNQs := []int64{2, 3, 2}
|
|
|
|
originTopKs := []int64{10, 5, 20}
|
|
|
|
nqPerSlice := int64(2)
|
|
|
|
sInfo := parseSliceInfo(originNQs, originTopKs, nqPerSlice)
|
|
|
|
|
|
|
|
expectedSliceNQs := []int32{2, 2, 1, 2}
|
|
|
|
expectedSliceTopKs := []int32{10, 5, 5, 20}
|
|
|
|
assert.True(t, funcutil.SliceSetEqual(sInfo.sliceNQs, expectedSliceNQs))
|
|
|
|
assert.True(t, funcutil.SliceSetEqual(sInfo.sliceTopKs, expectedSliceTopKs))
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
func TestReduce_AllFunc(t *testing.T) {
|
2022-04-08 20:27:31 +08:00
|
|
|
nq := int64(10)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2022-04-08 20:27:31 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
node, err := genSimpleQueryNode(ctx)
|
|
|
|
assert.NoError(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2022-05-31 13:42:03 +08:00
|
|
|
collection, err := node.metaReplica.getCollectionByID(defaultCollectionID)
|
2022-04-08 20:27:31 +08:00
|
|
|
assert.NoError(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2022-05-31 13:42:03 +08:00
|
|
|
segment, err := node.metaReplica.getSegmentByID(defaultSegmentID, segmentTypeSealed)
|
2022-04-08 20:27:31 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// TODO: replace below by genPlaceholderGroup(nq)
|
2022-04-29 13:35:49 +08:00
|
|
|
vec := generateFloatVectors(1, defaultDim)
|
2022-04-08 20:27:31 +08:00
|
|
|
var searchRawData []byte
|
2020-11-26 16:01:31 +08:00
|
|
|
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)))
|
2022-04-08 20:27:31 +08:00
|
|
|
searchRawData = append(searchRawData, buf...)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2022-04-08 20:27:31 +08:00
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
placeholderValue := commonpb.PlaceholderValue{
|
2020-11-26 16:01:31 +08:00
|
|
|
Tag: "$0",
|
2022-05-23 16:41:58 +08:00
|
|
|
Type: commonpb.PlaceholderType_FloatVector,
|
2022-04-08 20:27:31 +08:00
|
|
|
Values: [][]byte{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < int(nq); i++ {
|
|
|
|
placeholderValue.Values = append(placeholderValue.Values, searchRawData)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
placeholderGroup := commonpb.PlaceholderGroup{
|
|
|
|
Placeholders: []*commonpb.PlaceholderValue{&placeholderValue},
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
placeGroupByte, err := proto.Marshal(&placeholderGroup)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("marshal placeholderGroup failed")
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
dslString := "{\"bool\": { \n\"vector\": {\n \"floatVectorField\": {\n \"metric_type\": \"L2\", \n \"params\": {\n \"nprobe\": 10 \n},\n \"query\": \"$0\",\n \"topk\": 10 \n,\"round_decimal\": 6\n } \n } \n } \n }"
|
2022-04-08 20:27:31 +08:00
|
|
|
|
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)
|
2022-05-23 16:41:58 +08:00
|
|
|
searchReq, err := parseSearchRequest(plan, placeGroupByte)
|
|
|
|
searchReq.timestamp = 0
|
2020-11-30 17:58:23 +08:00
|
|
|
assert.NoError(t, err)
|
2022-04-08 20:27:31 +08:00
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
searchResult, err := segment.search(searchReq)
|
2022-04-08 20:27:31 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = checkSearchResult(nq, plan, searchResult)
|
|
|
|
assert.NoError(t, err)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
searchReq.delete()
|
2020-11-26 16:01:31 +08:00
|
|
|
deleteSegment(segment)
|
|
|
|
deleteCollection(collection)
|
|
|
|
}
|
2021-09-10 18:12:01 +08:00
|
|
|
|
2022-06-22 16:12:14 +08:00
|
|
|
func TestReduce_Invalid(t *testing.T) {
|
|
|
|
t.Run("nil plan", func(t *testing.T) {
|
|
|
|
plan := &SearchPlan{}
|
|
|
|
_, err := reduceSearchResultsAndFillData(plan, nil, 1, nil, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil search result", func(t *testing.T) {
|
|
|
|
collection := newCollection(defaultCollectionID, genTestCollectionSchema())
|
|
|
|
searchReq, err := genSearchPlanAndRequests(collection, IndexHNSW, 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
searchResults := make([]*SearchResult, 0)
|
|
|
|
searchResults = append(searchResults, nil)
|
|
|
|
_, err = reduceSearchResultsAndFillData(searchReq.plan, searchResults, 1, []int32{10}, []int32{10})
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2021-09-10 18:12:01 +08:00
|
|
|
}
|