|
| 1 | +// Copyright 2025-present the zvec project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// A range filter on an indexed FLOAT or DOUBLE field must return the same |
| 16 | +// documents whether a segment runs it on the inverted index or, after the |
| 17 | +// optimizer moved it, as a forward filter. The first segment is sealed and |
| 18 | +// its range covers most of its values, so the optimizer moves the filter to a |
| 19 | +// forward scan there; the writing segment keeps the inverted index. |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <filesystem> |
| 23 | +#include <iostream> |
| 24 | +#include <memory> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | +#include <gtest/gtest.h> |
| 28 | +#include <zvec/ailego/buffer/block_eviction_queue.h> |
| 29 | +#include <zvec/db/collection.h> |
| 30 | +#include <zvec/db/index_params.h> |
| 31 | + |
| 32 | +using namespace zvec; |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +size_t QueryCount(const Collection::Ptr &collection, |
| 37 | + const std::string &filter) { |
| 38 | + SearchQuery query; |
| 39 | + query.filter_ = filter; |
| 40 | + query.topk_ = 2000; |
| 41 | + auto result = collection->query(query); |
| 42 | + EXPECT_TRUE(result.has_value()) << filter; |
| 43 | + if (!result.has_value()) return 0; |
| 44 | + std::cout << filter << " -> " << result.value().size() << " documents\n"; |
| 45 | + return result.value().size(); |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
| 49 | + |
| 50 | +TEST(FloatRangeFilter, ForwardScanKeepsExactBound) { |
| 51 | + ailego::MemoryLimitPool::get_instance().init(256 * 1024 * 1024); |
| 52 | + const std::string path = "./collection_float_range_filter_dir"; |
| 53 | + std::filesystem::remove_all(path); |
| 54 | + |
| 55 | + CollectionSchema schema("float_range_filter"); |
| 56 | + schema.set_max_doc_count_per_segment(1000); |
| 57 | + ASSERT_TRUE(schema |
| 58 | + .add_field(std::make_shared<FieldSchema>( |
| 59 | + "price", DataType::FLOAT, false, |
| 60 | + std::make_shared<InvertIndexParams>())) |
| 61 | + .ok()); |
| 62 | + ASSERT_TRUE(schema |
| 63 | + .add_field(std::make_shared<FieldSchema>( |
| 64 | + "weight", DataType::DOUBLE, false, |
| 65 | + std::make_shared<InvertIndexParams>())) |
| 66 | + .ok()); |
| 67 | + auto opened = Collection::CreateAndOpen(path, schema, CollectionOptions{}); |
| 68 | + ASSERT_TRUE(opened.has_value()) << opened.error().message(); |
| 69 | + auto collection = std::move(opened.value()); |
| 70 | + |
| 71 | + // 50 documents sit exactly on the bounds used below, 951 above them. The |
| 72 | + // 1001st document starts a new segment, so the first 1000 are sealed. |
| 73 | + std::vector<Doc> docs; |
| 74 | + for (int i = 0; i < 1001; ++i) { |
| 75 | + Doc doc; |
| 76 | + doc.set_pk(std::to_string(i)); |
| 77 | + doc.set<float>("price", i < 50 ? 1.0000001f : 2.0f); |
| 78 | + doc.set<double>("weight", i < 50 ? 1.0000000001 : 2.0); |
| 79 | + docs.push_back(std::move(doc)); |
| 80 | + } |
| 81 | + for (size_t start = 0; start < docs.size(); start += 500) { |
| 82 | + std::vector<Doc> batch(docs.begin() + start, |
| 83 | + docs.begin() + std::min(docs.size(), start + 500)); |
| 84 | + auto inserted = collection->insert(batch); |
| 85 | + ASSERT_TRUE(inserted.has_value()) << inserted.error().message(); |
| 86 | + for (const auto &status : inserted.value()) { |
| 87 | + ASSERT_TRUE(status.ok()) << status.message(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + EXPECT_EQ(QueryCount(collection, "price > 1.0000001"), 951u); |
| 92 | + EXPECT_EQ(QueryCount(collection, "weight > 1.0000000001"), 951u); |
| 93 | + |
| 94 | + collection.reset(); |
| 95 | + std::filesystem::remove_all(path); |
| 96 | +} |
0 commit comments