be/src/exprs/function/search/function_search_leaf.h
Line | Count | Source |
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 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <memory> |
22 | | #include <roaring/roaring.hh> |
23 | | #include <string> |
24 | | #include <utility> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/data_type/data_type.h" |
28 | | #include "storage/index/index_iterator.h" |
29 | | #include "storage/index/index_query_context.h" |
30 | | #include "storage/index/inverted/query/engine/bit_set_query/bit_set_query.h" |
31 | | #include "storage/index/inverted/query/engine/query.h" |
32 | | #include "storage/index/inverted/query/logical/node.h" |
33 | | #include "storage/index/inverted/query/spi/index_source.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | // What a leaf needs from the query it is part of. |
38 | | struct SearchLeafContext { |
39 | | std::shared_ptr<segment_v2::IndexQueryContext> context; |
40 | | uint32_t num_rows = 0; |
41 | | // Whether the query scores its rows; clauses that only add to the score matter only then. |
42 | | bool scoring = true; |
43 | | }; |
44 | | |
45 | | // Compiles one lowered leaf on its bound text or scalar index. |
46 | | class SearchLeafCompiler { |
47 | | public: |
48 | 100 | virtual ~SearchLeafCompiler() = default; |
49 | | |
50 | | virtual Status compile(const index_query::logical::Node& leaf, const SearchLeafContext& ctx, |
51 | | segment_v2::inverted_index::query_v2::QueryPtr* out) = 0; |
52 | | }; |
53 | | |
54 | | // UNKNOWN for every row: no row matches and every row counts as NULL. |
55 | 2 | inline segment_v2::inverted_index::query_v2::QueryPtr make_unknown_leaf_query(uint32_t num_rows) { |
56 | 2 | auto null_bitmap = std::make_shared<roaring::Roaring>(); |
57 | 2 | if (num_rows > 0) { |
58 | 2 | null_bitmap->addRange(0, num_rows); |
59 | 2 | } |
60 | 2 | return std::make_shared<segment_v2::inverted_index::query_v2::BitSetQuery>( |
61 | 2 | std::make_shared<roaring::Roaring>(), std::move(null_bitmap)); |
62 | 2 | } |
63 | | |
64 | | // Compiles a leaf into the lazy query_v2 query the engine evaluates on the field's bound source, |
65 | | // after the source resolved the leaf's terms in one dictionary batch. |
66 | | class LazyLeafCompiler final : public SearchLeafCompiler { |
67 | | public: |
68 | | LazyLeafCompiler(std::wstring field, std::string binding_key, |
69 | | index_query::IndexSourcePtr source); |
70 | | |
71 | | Status compile(const index_query::logical::Node& leaf, const SearchLeafContext& ctx, |
72 | | segment_v2::inverted_index::query_v2::QueryPtr* out) override; |
73 | | |
74 | | private: |
75 | | std::wstring _field; |
76 | | std::string _binding_key; |
77 | | index_query::IndexSourcePtr _source; |
78 | | }; |
79 | | |
80 | | // Answers a comparison leaf from a scalar (BKD) index through its iterator. |
81 | | // Every other leaf on such an index is UNKNOWN. |
82 | | class ScalarLeafCompiler final : public SearchLeafCompiler { |
83 | | public: |
84 | | ScalarLeafCompiler(segment_v2::IndexIterator* iterator, DataTypePtr column_type, |
85 | | std::string stored_field_name); |
86 | | |
87 | | Status compile(const index_query::logical::Node& leaf, const SearchLeafContext& ctx, |
88 | | segment_v2::inverted_index::query_v2::QueryPtr* out) override; |
89 | | |
90 | | private: |
91 | | segment_v2::IndexIterator* _iterator; |
92 | | DataTypePtr _column_type; |
93 | | std::string _stored_field_name; |
94 | | }; |
95 | | |
96 | | } // namespace doris |