|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +#include "vec/functions/function_ip.h" |
| 19 | + |
18 | 20 | #include "function_test_util.h" |
19 | 21 | #include "gtest/gtest_pred_impl.h" |
| 22 | +#include "olap/rowset/segment_v2/index_iterator.h" |
| 23 | +#include "olap/rowset/segment_v2/inverted_index_reader.h" |
| 24 | +#include "vec/columns/column_const.h" |
20 | 25 | #include "vec/core/types.h" |
21 | 26 | #include "vec/data_types/data_type_ipv6.h" |
22 | 27 | #include "vec/data_types/data_type_number.h" |
@@ -158,4 +163,120 @@ TEST(FunctionIpTest, FunctionCutIPv6Test) { |
158 | 163 | static_cast<void>(check_function<DataTypeString, true>(func_name, input_types, data_set)); |
159 | 164 | } |
160 | 165 |
|
| 166 | +class MockIndexReader : public segment_v2::InvertedIndexReader { |
| 167 | +public: |
| 168 | + MockIndexReader(const TabletIndex& index_meta) |
| 169 | + : segment_v2::InvertedIndexReader(&index_meta, nullptr) {} |
| 170 | + ~MockIndexReader() override = default; |
| 171 | + segment_v2::InvertedIndexReaderType type() override { |
| 172 | + return segment_v2::InvertedIndexReaderType::BKD; |
| 173 | + } |
| 174 | + Status query(const segment_v2::IndexQueryContextPtr& context, const std::string& column_name, |
| 175 | + const void* query_value, segment_v2::InvertedIndexQueryType query_type, |
| 176 | + std::shared_ptr<roaring::Roaring>& bit_map) override { |
| 177 | + return Status::OK(); |
| 178 | + } |
| 179 | + Status try_query(const segment_v2::IndexQueryContextPtr& context, |
| 180 | + const std::string& column_name, const void* query_value, |
| 181 | + segment_v2::InvertedIndexQueryType query_type, size_t* count) override { |
| 182 | + return Status::OK(); |
| 183 | + } |
| 184 | + Status new_iterator(std::unique_ptr<segment_v2::IndexIterator>* iterator) override { |
| 185 | + return Status::OK(); |
| 186 | + } |
| 187 | +}; |
| 188 | + |
| 189 | +class MockIndexIterator : public segment_v2::IndexIterator { |
| 190 | +public: |
| 191 | + MockIndexIterator(std::shared_ptr<MockIndexReader> reader) : _reader(reader) {} |
| 192 | + ~MockIndexIterator() override = default; |
| 193 | + segment_v2::IndexReaderPtr get_reader(segment_v2::IndexReaderType reader_type) const override { |
| 194 | + if (std::holds_alternative<segment_v2::InvertedIndexReaderType>(reader_type)) { |
| 195 | + if (std::get<segment_v2::InvertedIndexReaderType>(reader_type) == |
| 196 | + segment_v2::InvertedIndexReaderType::BKD) { |
| 197 | + return _reader; |
| 198 | + } |
| 199 | + } |
| 200 | + return nullptr; |
| 201 | + } |
| 202 | + Status read_from_index(const segment_v2::IndexParam& param) override { |
| 203 | + auto* p = std::get<segment_v2::InvertedIndexParam*>(param); |
| 204 | + if (p->query_type == segment_v2::InvertedIndexQueryType::GREATER_EQUAL_QUERY) { |
| 205 | + p->roaring->addRange(10, 20); |
| 206 | + } else if (p->query_type == segment_v2::InvertedIndexQueryType::LESS_EQUAL_QUERY) { |
| 207 | + p->roaring->addRange(15, 25); |
| 208 | + } |
| 209 | + return Status::OK(); |
| 210 | + } |
| 211 | + Status read_null_bitmap(segment_v2::InvertedIndexQueryCacheHandle* cache_handle) override { |
| 212 | + return Status::OK(); |
| 213 | + } |
| 214 | + Result<bool> has_null() override { return false; } |
| 215 | + |
| 216 | +private: |
| 217 | + std::shared_ptr<MockIndexReader> _reader; |
| 218 | +}; |
| 219 | + |
| 220 | +TEST(FunctionIpTest, evaluate_inverted_index) { |
| 221 | + FunctionIsIPAddressInRange func; |
| 222 | + |
| 223 | + // IPv4 test |
| 224 | + { |
| 225 | + auto cidr_col = ColumnString::create(); |
| 226 | + cidr_col->insert_data("127.0.0.0/8", 11); |
| 227 | + auto const_cidr_col = ColumnConst::create(std::move(cidr_col), 1); |
| 228 | + |
| 229 | + ColumnsWithTypeAndName arguments = { |
| 230 | + {std::move(const_cidr_col), std::make_shared<DataTypeString>(), "cidr"}}; |
| 231 | + |
| 232 | + std::vector<IndexFieldNameAndTypePair> data_type_with_names = { |
| 233 | + {"ip_addr", std::make_shared<DataTypeIPv4>()}}; |
| 234 | + |
| 235 | + TabletIndex index_meta; |
| 236 | + auto reader = std::make_shared<MockIndexReader>(index_meta); |
| 237 | + auto iter = std::make_unique<MockIndexIterator>(reader); |
| 238 | + std::vector<segment_v2::IndexIterator*> iterators = {iter.get()}; |
| 239 | + |
| 240 | + segment_v2::InvertedIndexResultBitmap bitmap_result; |
| 241 | + auto status = func.evaluate_inverted_index(arguments, data_type_with_names, iterators, 100, |
| 242 | + bitmap_result); |
| 243 | + ASSERT_TRUE(status.ok()); |
| 244 | + |
| 245 | + // min_param: [10, 20), max_param: [15, 25) |
| 246 | + // intersection: [15, 20) -> 15, 16, 17, 18, 19 |
| 247 | + ASSERT_EQ(bitmap_result.get_data_bitmap()->cardinality(), 5); |
| 248 | + for (int i = 15; i < 20; ++i) { |
| 249 | + ASSERT_TRUE(bitmap_result.get_data_bitmap()->contains(i)); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + // IPv6 test |
| 254 | + { |
| 255 | + auto cidr_col = ColumnString::create(); |
| 256 | + cidr_col->insert_data("ffff::/16", 9); |
| 257 | + auto const_cidr_col = ColumnConst::create(std::move(cidr_col), 1); |
| 258 | + |
| 259 | + ColumnsWithTypeAndName arguments = { |
| 260 | + {std::move(const_cidr_col), std::make_shared<DataTypeString>(), "cidr"}}; |
| 261 | + |
| 262 | + std::vector<IndexFieldNameAndTypePair> data_type_with_names = { |
| 263 | + {"ip_addr", std::make_shared<DataTypeIPv6>()}}; |
| 264 | + |
| 265 | + TabletIndex index_meta; |
| 266 | + auto reader = std::make_shared<MockIndexReader>(index_meta); |
| 267 | + auto iter = std::make_unique<MockIndexIterator>(reader); |
| 268 | + std::vector<segment_v2::IndexIterator*> iterators = {iter.get()}; |
| 269 | + |
| 270 | + segment_v2::InvertedIndexResultBitmap bitmap_result; |
| 271 | + auto status = func.evaluate_inverted_index(arguments, data_type_with_names, iterators, 100, |
| 272 | + bitmap_result); |
| 273 | + ASSERT_TRUE(status.ok()); |
| 274 | + |
| 275 | + ASSERT_EQ(bitmap_result.get_data_bitmap()->cardinality(), 5); |
| 276 | + for (int i = 15; i < 20; ++i) { |
| 277 | + ASSERT_TRUE(bitmap_result.get_data_bitmap()->contains(i)); |
| 278 | + } |
| 279 | + } |
| 280 | +} |
| 281 | + |
161 | 282 | } // namespace doris::vectorized |
0 commit comments