test_query.cpp 17.6 KB
Newer Older
F
FluorineDog 已提交
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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

F
FluorineDog 已提交
12
#include <gtest/gtest.h>
F
FluorineDog 已提交
13
#include "query/deprecated/ParserDeprecated.h"
14 15 16 17
#include "query/Expr.h"
#include "query/PlanNode.h"
#include "query/generated/ExprVisitor.h"
#include "query/generated/PlanNodeVisitor.h"
18 19
#include "test_utils/DataGen.h"
#include "query/generated/ShowPlanNodeVisitor.h"
X
xige-16 已提交
20 21
#include "query/generated/ExecPlanNodeVisitor.h"
#include "query/PlanImpl.h"
22
#include "segcore/SegmentGrowingImpl.h"
F
FluorineDog 已提交
23
#include "segcore/SegmentSealed.h"
C
cai.zhang 已提交
24
#include "pb/schema.pb.h"
F
FluorineDog 已提交
25

26 27 28
using namespace milvus;
using namespace milvus::query;
using namespace milvus::segcore;
29 30 31 32

TEST(Query, ShowExecutor) {
    using namespace milvus::query;
    using namespace milvus::segcore;
33
    using namespace milvus;
34 35
    auto node = std::make_unique<FloatVectorANNS>();
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
36
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
37
    int64_t num_queries = 100L;
38
    auto raw_data = DataGen(schema, num_queries);
39
    auto& info = node->query_info_;
F
FluorineDog 已提交
40
    info.metric_type_ = MetricType::METRIC_L2;
41
    info.topK_ = 20;
G
GuoRentong 已提交
42
    info.field_offset_ = FieldOffset(1000);
43 44 45 46
    node->predicate_ = std::nullopt;
    ShowPlanNodeVisitor show_visitor;
    PlanNodePtr base(node.release());
    auto res = show_visitor.call_child(*base);
47 48
    auto dup = res;
    std::cout << dup.dump(4);
X
xige-16 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
}

TEST(Query, DSL) {
    using namespace milvus::query;
    using namespace milvus::segcore;
    ShowPlanNodeVisitor shower;

    std::string dsl_string = R"(
{
    "bool": {
        "must": [
            {
                "vector": {
X
XuanYang-cn 已提交
62
                    "fakevec": {
X
xige-16 已提交
63 64 65 66 67 68 69 70 71 72 73 74
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 10
                    }
                }
            }
        ]
    }
})";
75 76

    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
77
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
78 79

    auto plan = CreatePlan(*schema, dsl_string);
X
xige-16 已提交
80 81 82 83 84 85 86
    auto res = shower.call_child(*plan->plan_node_);
    std::cout << res.dump(4) << std::endl;

    std::string dsl_string2 = R"(
{
    "bool": {
        "vector": {
X
XuanYang-cn 已提交
87
            "fakevec": {
X
xige-16 已提交
88 89 90 91 92 93 94 95 96 97
                "metric_type": "L2",
                "params": {
                    "nprobe": 10
                },
                "query": "$0",
                "topk": 10
            }
        }
    }
})";
98
    auto plan2 = CreatePlan(*schema, dsl_string2);
X
xige-16 已提交
99 100 101 102 103 104 105
    auto res2 = shower.call_child(*plan2->plan_node_);
    std::cout << res2.dump(4) << std::endl;
    ASSERT_EQ(res, res2);
}

TEST(Query, ParsePlaceholderGroup) {
    namespace ser = milvus::proto::service;
106 107 108 109
    std::string dsl_string = R"(
{
    "bool": {
        "vector": {
N
neza2017 已提交
110
            "fakevec": {
111 112 113 114 115 116 117 118 119 120 121 122
                "metric_type": "L2",
                "params": {
                    "nprobe": 10
                },
                "query": "$0",
                "topk": 10
            }
        }
    }
})";

    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
123
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
124
    auto plan = CreatePlan(*schema, dsl_string);
F
FluorineDog 已提交
125
    int64_t num_queries = 100000;
X
xige-16 已提交
126
    int dim = 16;
F
FluorineDog 已提交
127
    auto raw_group = CreatePlaceholderGroup(num_queries, dim);
X
xige-16 已提交
128
    auto blob = raw_group.SerializeAsString();
129
    auto placeholder = ParsePlaceholderGroup(plan.get(), blob);
X
xige-16 已提交
130 131
}

F
FluorineDog 已提交
132 133 134 135
TEST(Query, ExecWithPredicate) {
    using namespace milvus::query;
    using namespace milvus::segcore;
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
136 137
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
    schema->AddDebugField("age", DataType::FLOAT);
F
FluorineDog 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "range": {
                    "age": {
                        "GE": -1,
                        "LT": 1
                    }
                }
            },
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
    int64_t N = 1000 * 1000;
    auto dataset = DataGen(schema, N);
166
    auto segment = CreateGrowingSegment(schema);
F
FluorineDog 已提交
167 168 169 170 171 172 173 174 175
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);

    auto plan = CreatePlan(*schema, dsl);
    auto num_queries = 5;
    auto ph_group_raw = CreatePlaceholderGroup(num_queries, 16, 1024);
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    Timestamp time = 1000000;
    std::vector<const PlaceholderGroup*> ph_group_arr = {ph_group.get()};
176
    auto qr = segment->Search(plan.get(), ph_group_arr.data(), &time, 1);
F
FluorineDog 已提交
177
    int topk = 5;
Z
zhenshan.cao 已提交
178 179

    Json json = QueryResultToJson(qr);
Z
zhenshan.cao 已提交
180
    auto ref = json::parse(R"(
X
XuanYang-cn 已提交
181
[
F
FluorineDog 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  [
    [
      "980486->3.149221",
      "318367->3.661235",
      "302798->4.553688",
      "321424->4.757450",
      "565529->5.083780"
    ],
    [
      "233390->7.931535",
      "238958->8.109344",
      "230645->8.439169",
      "901939->8.658772",
      "380328->8.731251"
    ],
    [
      "897246->3.749835",
      "750683->3.897577",
      "857598->4.230977",
      "299009->4.379639",
      "440010->4.454046"
    ],
    [
      "840855->4.782170",
      "709627->5.063170",
      "72322->5.166143",
      "107142->5.180207",
      "948403->5.247065"
    ],
    [
      "810401->3.926393",
      "46575->4.054171",
      "201740->4.274491",
      "669040->4.399628",
      "231500->4.831223"
    ]
  ]
])");
X
XuanYang-cn 已提交
220
    ASSERT_EQ(json.dump(2), ref.dump(2));
F
FluorineDog 已提交
221
}
F
FluorineDog 已提交
222

F
FluorineDog 已提交
223 224 225 226
TEST(Query, ExecTerm) {
    using namespace milvus::query;
    using namespace milvus::segcore;
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
227 228
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
    schema->AddDebugField("age", DataType::FLOAT);
F
FluorineDog 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "term": {
                    "age": {
                        "values": []
                    }
                }
            },
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
    int64_t N = 1000 * 1000;
    auto dataset = DataGen(schema, N);
256
    auto segment = CreateGrowingSegment(schema);
F
FluorineDog 已提交
257 258 259 260 261 262 263 264 265 266
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);

    auto plan = CreatePlan(*schema, dsl);
    auto num_queries = 3;
    auto ph_group_raw = CreatePlaceholderGroup(num_queries, 16, 1024);
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    QueryResult qr;
    Timestamp time = 1000000;
    std::vector<const PlaceholderGroup*> ph_group_arr = {ph_group.get()};
267
    qr = segment->Search(plan.get(), ph_group_arr.data(), &time, 1);
F
FluorineDog 已提交
268 269 270 271 272 273 274
    std::vector<std::vector<std::string>> results;
    int topk = 5;
    auto json = QueryResultToJson(qr);
    ASSERT_EQ(qr.num_queries_, num_queries);
    ASSERT_EQ(qr.topK_, topk);
    // for(auto x: )
}
F
FluorineDog 已提交
275

F
FluorineDog 已提交
276 277 278 279
TEST(Query, ExecEmpty) {
    using namespace milvus::query;
    using namespace milvus::segcore;
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
280 281
    schema->AddDebugField("age", DataType::FLOAT);
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
F
FluorineDog 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
    int64_t N = 1000 * 1000;
301
    auto segment = CreateGrowingSegment(schema);
F
FluorineDog 已提交
302 303 304 305 306 307
    auto plan = CreatePlan(*schema, dsl);
    auto num_queries = 5;
    auto ph_group_raw = CreatePlaceholderGroup(num_queries, 16, 1024);
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    Timestamp time = 1000000;
    std::vector<const PlaceholderGroup*> ph_group_arr = {ph_group.get()};
308
    auto qr = segment->Search(plan.get(), ph_group_arr.data(), &time, 1);
F
FluorineDog 已提交
309 310 311 312 313 314 315 316 317 318 319
    std::cout << QueryResultToJson(qr);

    for (auto i : qr.internal_seg_offsets_) {
        ASSERT_EQ(i, -1);
    }

    for (auto v : qr.result_distances_) {
        ASSERT_EQ(v, std::numeric_limits<float>::max());
    }
}

X
XuanYang-cn 已提交
320
TEST(Query, ExecWithoutPredicate) {
X
xige-16 已提交
321 322
    using namespace milvus::query;
    using namespace milvus::segcore;
F
FluorineDog 已提交
323
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
324 325
    schema->AddDebugField("fakevec", DataType::VECTOR_FLOAT, 16, MetricType::METRIC_L2);
    schema->AddDebugField("age", DataType::FLOAT);
F
FluorineDog 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
F
FluorineDog 已提交
344
    auto plan = CreatePlan(*schema, dsl);
F
FluorineDog 已提交
345 346
    int64_t N = 1000 * 1000;
    auto dataset = DataGen(schema, N);
347
    auto segment = CreateGrowingSegment(schema);
F
FluorineDog 已提交
348 349 350 351 352 353 354 355 356
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);

    auto num_queries = 5;
    auto ph_group_raw = CreatePlaceholderGroup(num_queries, 16, 1024);
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    QueryResult qr;
    Timestamp time = 1000000;
    std::vector<const PlaceholderGroup*> ph_group_arr = {ph_group.get()};
357
    qr = segment->Search(plan.get(), ph_group_arr.data(), &time, 1);
F
FluorineDog 已提交
358 359
    std::vector<std::vector<std::string>> results;
    int topk = 5;
X
XuanYang-cn 已提交
360
    auto json = QueryResultToJson(qr);
Z
zhenshan.cao 已提交
361
    auto ref = json::parse(R"(
X
XuanYang-cn 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
[
  [
    [
      "980486->3.149221",
      "318367->3.661235",
      "302798->4.553688",
      "321424->4.757450",
      "565529->5.083780"
    ],
    [
      "233390->7.931535",
      "238958->8.109344",
      "230645->8.439169",
      "901939->8.658772",
      "380328->8.731251"
    ],
    [
      "749862->3.398494",
      "701321->3.632437",
      "897246->3.749835",
      "750683->3.897577",
      "105995->4.073595"
    ],
    [
      "138274->3.454446",
      "124548->3.783290",
      "840855->4.782170",
      "936719->5.026924",
      "709627->5.063170"
    ],
    [
      "810401->3.926393",
      "46575->4.054171",
      "201740->4.274491",
      "669040->4.399628",
      "231500->4.831223"
    ]
  ]
]
)");
    ASSERT_EQ(json.dump(2), ref.dump(2));
X
xige-16 已提交
403
}
C
cai.zhang 已提交
404

F
FluorineDog 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
TEST(Indexing, InnerProduct) {
    int64_t N = 100000;
    constexpr auto dim = 16;
    constexpr auto topk = 10;
    auto num_queries = 5;
    auto schema = std::make_shared<Schema>();
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "vector": {
                    "normalized": {
                        "metric_type": "IP",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
G
GuoRentong 已提交
429
    schema->AddDebugField("normalized", DataType::VECTOR_FLOAT, dim, MetricType::METRIC_INNER_PRODUCT);
F
FluorineDog 已提交
430
    auto dataset = DataGen(schema, N);
431
    auto segment = CreateGrowingSegment(schema);
F
FluorineDog 已提交
432 433 434 435 436 437 438 439 440 441
    auto plan = CreatePlan(*schema, dsl);
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);
    auto col = dataset.get_col<float>(0);

    auto ph_group_raw = CreatePlaceholderGroupFromBlob(num_queries, 16, col.data());
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    std::vector<Timestamp> ts{(Timestamp)N * 2};
    const auto* ptr = ph_group.get();
    QueryResult qr;
442
    qr = segment->Search(plan.get(), &ptr, ts.data(), 1);
F
FluorineDog 已提交
443 444 445
    std::cout << QueryResultToJson(qr).dump(2);
}

C
cai.zhang 已提交
446 447 448 449 450
TEST(Query, FillSegment) {
    namespace pb = milvus::proto;
    pb::schema::CollectionSchema proto;
    proto.set_name("col");
    proto.set_description("asdfhsalkgfhsadg");
N
neza2017 已提交
451
    proto.set_autoid(false);
C
cai.zhang 已提交
452 453 454 455 456 457

    {
        auto field = proto.add_fields();
        field->set_name("fakevec");
        field->set_is_primary_key(false);
        field->set_description("asdgfsagf");
B
bigsheeper 已提交
458
        field->set_fieldid(100);
C
cai.zhang 已提交
459 460 461 462
        field->set_data_type(pb::schema::DataType::VECTOR_FLOAT);
        auto param = field->add_type_params();
        param->set_key("dim");
        param->set_value("16");
X
XuanYang-cn 已提交
463 464 465
        auto iparam = field->add_index_params();
        iparam->set_key("metric_type");
        iparam->set_value("L2");
C
cai.zhang 已提交
466 467 468 469 470
    }

    {
        auto field = proto.add_fields();
        field->set_name("the_key");
B
bigsheeper 已提交
471
        field->set_fieldid(101);
C
cai.zhang 已提交
472 473
        field->set_is_primary_key(true);
        field->set_description("asdgfsagf");
N
neza2017 已提交
474
        field->set_data_type(pb::schema::DataType::INT64);
C
cai.zhang 已提交
475 476 477
    }

    auto schema = Schema::ParseFrom(proto);
478
    auto segment = CreateGrowingSegment(schema);
C
cai.zhang 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    int N = 100000;
    auto dataset = DataGen(schema, N);
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "L2",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
    auto plan = CreatePlan(*schema, dsl);
    auto ph_proto = CreatePlaceholderGroup(10, 16, 443);
    auto ph = ParsePlaceholderGroup(plan.get(), ph_proto.SerializeAsString());
    std::vector<const PlaceholderGroup*> groups = {ph.get()};
    std::vector<Timestamp> timestamps = {N * 2UL};
    QueryResult result;
507
    result = segment->Search(plan.get(), groups.data(), timestamps.data(), 1);
C
cai.zhang 已提交
508 509 510 511 512 513

    auto topk = 5;
    auto num_queries = 10;

    result.result_offsets_.resize(topk * num_queries);
    segment->FillTargetEntry(plan.get(), result);
514

C
cai.zhang 已提交
515 516 517
    auto ans = result.row_data_;
    ASSERT_EQ(ans.size(), topk * num_queries);
    int64_t std_index = 0;
N
neza2017 已提交
518
    auto std_vec = dataset.get_col<int64_t>(1);
C
cai.zhang 已提交
519 520 521 522
    for (auto& vec : ans) {
        ASSERT_EQ(vec.size(), sizeof(int64_t));
        int64_t val;
        memcpy(&val, vec.data(), sizeof(int64_t));
N
neza2017 已提交
523 524 525
        auto internal_offset = result.internal_seg_offsets_[std_index];
        auto std_val = std_vec[internal_offset];
        ASSERT_EQ(val, std_val) << "io:" << internal_offset;
C
cai.zhang 已提交
526 527 528
        ++std_index;
    }
}
X
XuanYang-cn 已提交
529 530 531 532 533

TEST(Query, ExecWithPredicateBinary) {
    using namespace milvus::query;
    using namespace milvus::segcore;
    auto schema = std::make_shared<Schema>();
G
GuoRentong 已提交
534 535
    schema->AddDebugField("fakevec", DataType::VECTOR_BINARY, 512, MetricType::METRIC_Jaccard);
    schema->AddDebugField("age", DataType::FLOAT);
X
XuanYang-cn 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    std::string dsl = R"({
        "bool": {
            "must": [
            {
                "range": {
                    "age": {
                        "GE": -1,
                        "LT": 1
                    }
                }
            },
            {
                "vector": {
                    "fakevec": {
                        "metric_type": "Jaccard",
                        "params": {
                            "nprobe": 10
                        },
                        "query": "$0",
                        "topk": 5
                    }
                }
            }
            ]
        }
    })";
    int64_t N = 1000 * 1000;
    auto dataset = DataGen(schema, N);
564
    auto segment = CreateGrowingSegment(schema);
X
XuanYang-cn 已提交
565 566 567 568 569 570 571 572 573 574 575
    segment->PreInsert(N);
    segment->Insert(0, N, dataset.row_ids_.data(), dataset.timestamps_.data(), dataset.raw_);
    auto vec_ptr = dataset.get_col<uint8_t>(0);

    auto plan = CreatePlan(*schema, dsl);
    auto num_queries = 5;
    auto ph_group_raw = CreateBinaryPlaceholderGroupFromBlob(num_queries, 512, vec_ptr.data() + 1024 * 512 / 8);
    auto ph_group = ParsePlaceholderGroup(plan.get(), ph_group_raw.SerializeAsString());
    QueryResult qr;
    Timestamp time = 1000000;
    std::vector<const PlaceholderGroup*> ph_group_arr = {ph_group.get()};
576
    qr = segment->Search(plan.get(), ph_group_arr.data(), &time, 1);
X
XuanYang-cn 已提交
577 578 579 580 581 582
    int topk = 5;

    Json json = QueryResultToJson(qr);
    std::cout << json.dump(2);
    // ASSERT_EQ(json.dump(2), ref.dump(2));
}