|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <initializer_list> |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "core/data_type/primitive_type.h" |
| 28 | +#include "exprs/score_runtime.h" |
| 29 | +#include "exprs/vcompound_pred.h" |
| 30 | +#include "exprs/vexpr_context.h" |
| 31 | +#include "storage/compaction/collection_similarity.h" |
| 32 | +#include "storage/index/index_iterator.h" |
| 33 | +#include "storage/index/index_query_context.h" |
| 34 | + |
| 35 | +namespace doris { |
| 36 | +namespace { |
| 37 | + |
| 38 | +class ScoredBitmapExpr final : public VExpr { |
| 39 | +public: |
| 40 | + ScoredBitmapExpr(std::initializer_list<uint32_t> rows, float score) : _score(score) { |
| 41 | + set_node_type(TExprNodeType::MATCH_PRED); |
| 42 | + for (uint32_t row : rows) { |
| 43 | + _rows.add(row); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const std::string& expr_name() const override { |
| 48 | + static const std::string name = "ScoredBitmapExpr"; |
| 49 | + return name; |
| 50 | + } |
| 51 | + |
| 52 | + Status execute(VExprContext*, Block*, int*) const override { return Status::OK(); } |
| 53 | + |
| 54 | + Status execute_column_impl(VExprContext*, const Block*, const Selector*, size_t, |
| 55 | + ColumnPtr&) const override { |
| 56 | + return Status::OK(); |
| 57 | + } |
| 58 | + |
| 59 | + Status evaluate_inverted_index(VExprContext* context, uint32_t) override { |
| 60 | + auto index_context = context->get_index_context(); |
| 61 | + auto query_context = index_context->get_index_query_context(); |
| 62 | + for (uint32_t row : _rows) { |
| 63 | + query_context->collection_similarity->collect(row, _score); |
| 64 | + } |
| 65 | + index_context->set_index_result_for_expr( |
| 66 | + this, |
| 67 | + segment_v2::InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(_rows), |
| 68 | + std::make_shared<roaring::Roaring>())); |
| 69 | + return Status::OK(); |
| 70 | + } |
| 71 | + |
| 72 | +private: |
| 73 | + roaring::Roaring _rows; |
| 74 | + float _score; |
| 75 | +}; |
| 76 | + |
| 77 | +class UnindexedExpr final : public VExpr { |
| 78 | +public: |
| 79 | + const std::string& expr_name() const override { |
| 80 | + static const std::string name = "UnindexedExpr"; |
| 81 | + return name; |
| 82 | + } |
| 83 | + |
| 84 | + Status execute(VExprContext*, Block*, int*) const override { return Status::OK(); } |
| 85 | + |
| 86 | + Status execute_column_impl(VExprContext*, const Block*, const Selector*, size_t, |
| 87 | + ColumnPtr&) const override { |
| 88 | + return Status::OK(); |
| 89 | + } |
| 90 | + |
| 91 | + Status evaluate_inverted_index(VExprContext*, uint32_t) override { return Status::OK(); } |
| 92 | +}; |
| 93 | + |
| 94 | +std::shared_ptr<VCompoundPred> make_compound(TExprOpcode::type op, VExprSPtr left, |
| 95 | + VExprSPtr right) { |
| 96 | + TExprNode node; |
| 97 | + node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
| 98 | + node.__set_node_type(TExprNodeType::COMPOUND_PRED); |
| 99 | + node.__set_opcode(op); |
| 100 | + node.__set_num_children(2); |
| 101 | + node.__set_is_nullable(false); |
| 102 | + auto expr = VCompoundPred::create_shared(node); |
| 103 | + expr->add_child(left); |
| 104 | + expr->add_child(right); |
| 105 | + return expr; |
| 106 | +} |
| 107 | + |
| 108 | +std::shared_ptr<VCompoundPred> make_not(VExprSPtr child) { |
| 109 | + TExprNode node; |
| 110 | + node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
| 111 | + node.__set_node_type(TExprNodeType::COMPOUND_PRED); |
| 112 | + node.__set_opcode(TExprOpcode::COMPOUND_NOT); |
| 113 | + node.__set_num_children(1); |
| 114 | + node.__set_is_nullable(false); |
| 115 | + auto expr = VCompoundPred::create_shared(node); |
| 116 | + expr->add_child(child); |
| 117 | + return expr; |
| 118 | +} |
| 119 | + |
| 120 | +TEST(VCompoundPredScoreTest, FailedAndBranchDoesNotContributeToOrScore) { |
| 121 | + auto first = make_compound( |
| 122 | + TExprOpcode::COMPOUND_AND, |
| 123 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 2, 4}, 1.0F), |
| 124 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 2, 5}, 2.0F)); |
| 125 | + auto second = make_compound( |
| 126 | + TExprOpcode::COMPOUND_AND, |
| 127 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 2, 3, 4, 5}, |
| 128 | + 3.0F), |
| 129 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 3, 4, 5}, 4.0F)); |
| 130 | + auto root = make_compound(TExprOpcode::COMPOUND_OR, first, second); |
| 131 | + |
| 132 | + std::vector<std::unique_ptr<segment_v2::IndexIterator>> iterators; |
| 133 | + std::vector<IndexFieldNameAndTypePair> storage_types; |
| 134 | + std::unordered_map<ColumnId, std::unordered_map<const VExpr*, bool>> status_map; |
| 135 | + segment_v2::ColumnIteratorOptions iterator_options; |
| 136 | + auto score_runtime = std::make_shared<ScoreRuntime>(nullptr, false, 0); |
| 137 | + auto index_context = std::make_shared<IndexExecContext>( |
| 138 | + iterators, storage_types, status_map, score_runtime, nullptr, iterator_options); |
| 139 | + auto query_context = std::make_shared<segment_v2::IndexQueryContext>(); |
| 140 | + query_context->collection_similarity = std::make_shared<CollectionSimilarity>(); |
| 141 | + auto root_similarity = query_context->collection_similarity; |
| 142 | + index_context->set_index_query_context(query_context); |
| 143 | + auto expr_context = VExprContext::create_shared(root); |
| 144 | + expr_context->set_index_context(index_context); |
| 145 | + |
| 146 | + ASSERT_TRUE(root->evaluate_inverted_index(expr_context.get(), 6).ok()); |
| 147 | + const auto* result = index_context->get_index_result_for_expr(root.get()); |
| 148 | + ASSERT_NE(result, nullptr); |
| 149 | + EXPECT_EQ(result->get_data_bitmap()->cardinality(), 5); |
| 150 | + EXPECT_EQ(query_context->collection_similarity, root_similarity); |
| 151 | + |
| 152 | + auto scores = root_similarity->release_scores(); |
| 153 | + ASSERT_EQ(scores.size(), 5); |
| 154 | + EXPECT_FLOAT_EQ(scores.at(1), 10.0F); |
| 155 | + EXPECT_FLOAT_EQ(scores.at(2), 3.0F); |
| 156 | + EXPECT_FLOAT_EQ(scores.at(3), 7.0F); |
| 157 | + EXPECT_FLOAT_EQ(scores.at(4), 7.0F); |
| 158 | + EXPECT_FLOAT_EQ(scores.at(5), 7.0F); |
| 159 | +} |
| 160 | + |
| 161 | +TEST(VCompoundPredScoreTest, NotBranchDoesNotContributeToOrScore) { |
| 162 | + auto negative = make_not( |
| 163 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 2}, 1.0F)); |
| 164 | + auto positive = |
| 165 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1, 3}, 5.0F); |
| 166 | + auto root = make_compound(TExprOpcode::COMPOUND_OR, negative, positive); |
| 167 | + |
| 168 | + std::vector<std::unique_ptr<segment_v2::IndexIterator>> iterators; |
| 169 | + std::vector<IndexFieldNameAndTypePair> storage_types; |
| 170 | + std::unordered_map<ColumnId, std::unordered_map<const VExpr*, bool>> status_map; |
| 171 | + segment_v2::ColumnIteratorOptions iterator_options; |
| 172 | + auto score_runtime = std::make_shared<ScoreRuntime>(nullptr, false, 0); |
| 173 | + auto index_context = std::make_shared<IndexExecContext>( |
| 174 | + iterators, storage_types, status_map, score_runtime, nullptr, iterator_options); |
| 175 | + auto query_context = std::make_shared<segment_v2::IndexQueryContext>(); |
| 176 | + query_context->collection_similarity = std::make_shared<CollectionSimilarity>(); |
| 177 | + index_context->set_index_query_context(query_context); |
| 178 | + auto expr_context = VExprContext::create_shared(root); |
| 179 | + expr_context->set_index_context(index_context); |
| 180 | + |
| 181 | + ASSERT_TRUE(root->evaluate_inverted_index(expr_context.get(), 4).ok()); |
| 182 | + const auto* result = index_context->get_index_result_for_expr(root.get()); |
| 183 | + ASSERT_NE(result, nullptr); |
| 184 | + EXPECT_EQ(result->get_data_bitmap()->cardinality(), 3); |
| 185 | + EXPECT_TRUE(result->get_data_bitmap()->contains(0)); |
| 186 | + EXPECT_TRUE(result->get_data_bitmap()->contains(1)); |
| 187 | + EXPECT_TRUE(result->get_data_bitmap()->contains(3)); |
| 188 | + |
| 189 | + auto scores = query_context->collection_similarity->release_scores(); |
| 190 | + ASSERT_EQ(scores.size(), 2); |
| 191 | + EXPECT_FLOAT_EQ(scores.at(1), 5.0F); |
| 192 | + EXPECT_FLOAT_EQ(scores.at(3), 5.0F); |
| 193 | +} |
| 194 | + |
| 195 | +TEST(VCompoundPredScoreTest, IncompleteScoredBranchReturnsErrorWithoutPublishingScores) { |
| 196 | + auto root = make_compound( |
| 197 | + TExprOpcode::COMPOUND_AND, |
| 198 | + std::make_shared<ScoredBitmapExpr>(std::initializer_list<uint32_t> {1}, 1.0F), |
| 199 | + std::make_shared<UnindexedExpr>()); |
| 200 | + |
| 201 | + std::vector<std::unique_ptr<segment_v2::IndexIterator>> iterators; |
| 202 | + std::vector<IndexFieldNameAndTypePair> storage_types; |
| 203 | + std::unordered_map<ColumnId, std::unordered_map<const VExpr*, bool>> status_map; |
| 204 | + segment_v2::ColumnIteratorOptions iterator_options; |
| 205 | + auto score_runtime = std::make_shared<ScoreRuntime>(nullptr, false, 0); |
| 206 | + auto index_context = std::make_shared<IndexExecContext>( |
| 207 | + iterators, storage_types, status_map, score_runtime, nullptr, iterator_options); |
| 208 | + auto query_context = std::make_shared<segment_v2::IndexQueryContext>(); |
| 209 | + query_context->collection_similarity = std::make_shared<CollectionSimilarity>(); |
| 210 | + auto parent_similarity = query_context->collection_similarity; |
| 211 | + index_context->set_index_query_context(query_context); |
| 212 | + auto expr_context = VExprContext::create_shared(root); |
| 213 | + expr_context->set_index_context(index_context); |
| 214 | + |
| 215 | + auto status = root->evaluate_inverted_index(expr_context.get(), 2); |
| 216 | + EXPECT_EQ(status.code(), ErrorCode::INVERTED_INDEX_NOT_SUPPORTED); |
| 217 | + EXPECT_EQ(query_context->collection_similarity, parent_similarity); |
| 218 | + EXPECT_TRUE(parent_similarity->release_scores().empty()); |
| 219 | +} |
| 220 | + |
| 221 | +} // namespace |
| 222 | +} // namespace doris |
0 commit comments