/root/doris/be/src/olap/shared_predicate.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 | | |
23 | | #include "common/factory_creator.h" |
24 | | #include "olap/column_predicate.h" |
25 | | #include "olap/rowset/segment_v2/bloom_filter.h" |
26 | | #include "olap/rowset/segment_v2/inverted_index_reader.h" |
27 | | #include "olap/wrapper_field.h" |
28 | | #include "vec/columns/column_dictionary.h" |
29 | | |
30 | | namespace doris { |
31 | | |
32 | | // SharedPredicate only used on topn runtime predicate. |
33 | | // Runtime predicate globally share one predicate, to ensure that updates can be real-time. |
34 | | // At the beginning nested predicate may be nullptr, in which case predicate always returns true. |
35 | | class SharedPredicate final : public ColumnPredicate { |
36 | | ENABLE_FACTORY_CREATOR(SharedPredicate); |
37 | | |
38 | | public: |
39 | | SharedPredicate(uint32_t column_id) |
40 | 0 | : ColumnPredicate(column_id, PrimitiveType::INVALID_TYPE), |
41 | 0 | _mtx(std::make_shared<std::shared_mutex>()) {} |
42 | | SharedPredicate(const ColumnPredicate& other) = delete; |
43 | | SharedPredicate(const SharedPredicate& other, uint32_t column_id) |
44 | | : ColumnPredicate(other, column_id), |
45 | | _mtx(std::make_shared<std::shared_mutex>()), |
46 | | _nested(assert_cast<const SharedPredicate&>(other)._nested |
47 | | ? other._nested->clone(column_id) |
48 | 0 | : nullptr) {} |
49 | 0 | ~SharedPredicate() override = default; |
50 | 0 | std::string debug_string() const override { |
51 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
52 | 0 | fmt::memory_buffer debug_string_buffer; |
53 | 0 | fmt::format_to(debug_string_buffer, "SharedPredicate({}, nested={})", |
54 | 0 | ColumnPredicate::debug_string(), _nested ? _nested->debug_string() : "null"); |
55 | 0 | return fmt::to_string(debug_string_buffer); |
56 | 0 | } |
57 | 0 | std::shared_ptr<ColumnPredicate> clone(uint32_t column_id) const override { |
58 | | // All scanner thread should share the same SharedPredicate object. |
59 | 0 | return std::const_pointer_cast<ColumnPredicate>(shared_from_this()); |
60 | 0 | } |
61 | | |
62 | 0 | PredicateType type() const override { |
63 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
64 | 0 | if (!_nested) { |
65 | | // topn filter is le or ge |
66 | 0 | return PredicateType::LE; |
67 | 0 | } |
68 | 0 | return _nested->type(); |
69 | 0 | } |
70 | 0 | PrimitiveType primitive_type() const override { |
71 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
72 | 0 | if (!_nested) { |
73 | 0 | return PrimitiveType::INVALID_TYPE; |
74 | 0 | } |
75 | 0 | return _nested->primitive_type(); |
76 | 0 | } |
77 | | |
78 | 0 | void set_nested(const std::shared_ptr<ColumnPredicate>& nested) { |
79 | 0 | std::unique_lock<std::shared_mutex> lock(*_mtx); |
80 | 0 | _nested = nested; |
81 | 0 | } |
82 | | |
83 | | Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type, |
84 | | IndexIterator* iterator, uint32_t num_rows, |
85 | 0 | roaring::Roaring* bitmap) const override { |
86 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
87 | 0 | if (!_nested) { |
88 | 0 | return Status::OK(); |
89 | 0 | } |
90 | 0 | return _nested->evaluate(name_with_type, iterator, num_rows, bitmap); |
91 | 0 | } |
92 | | |
93 | | void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
94 | 0 | bool* flags) const override { |
95 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
96 | 0 | if (!_nested) { |
97 | 0 | return; |
98 | 0 | } |
99 | 0 | return _nested->evaluate_and(column, sel, size, flags); |
100 | 0 | } |
101 | | |
102 | | void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
103 | 0 | bool* flags) const override { |
104 | 0 | DCHECK(false) << "should not reach here"; |
105 | 0 | } |
106 | | |
107 | 0 | bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const override { |
108 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
109 | 0 | if (!_nested) { |
110 | 0 | return ColumnPredicate::evaluate_and(statistic); |
111 | 0 | } |
112 | 0 | return _nested->evaluate_and(statistic); |
113 | 0 | } |
114 | | |
115 | 0 | bool evaluate_del(const std::pair<WrapperField*, WrapperField*>& statistic) const override { |
116 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
117 | 0 | if (!_nested) { |
118 | 0 | return ColumnPredicate::evaluate_del(statistic); |
119 | 0 | } |
120 | 0 | return _nested->evaluate_del(statistic); |
121 | 0 | } |
122 | | |
123 | 0 | bool evaluate_and(const BloomFilter* bf) const override { |
124 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
125 | 0 | if (!_nested) { |
126 | 0 | return ColumnPredicate::evaluate_and(bf); |
127 | 0 | } |
128 | 0 | return _nested->evaluate_and(bf); |
129 | 0 | } |
130 | | |
131 | 0 | bool can_do_bloom_filter(bool ngram) const override { |
132 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
133 | 0 | if (!_nested) { |
134 | 0 | return ColumnPredicate::can_do_bloom_filter(ngram); |
135 | 0 | } |
136 | 0 | return _nested->can_do_bloom_filter(ngram); |
137 | 0 | } |
138 | | |
139 | | void evaluate_vec(const vectorized::IColumn& column, uint16_t size, |
140 | 0 | bool* flags) const override { |
141 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
142 | 0 | if (!_nested) { |
143 | 0 | for (uint16_t i = 0; i < size; ++i) { |
144 | 0 | flags[i] = true; |
145 | 0 | } |
146 | 0 | return; |
147 | 0 | } |
148 | 0 | _nested->evaluate_vec(column, size, flags); |
149 | 0 | } |
150 | | |
151 | | void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size, |
152 | 0 | bool* flags) const override { |
153 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
154 | 0 | if (!_nested) { |
155 | 0 | return; |
156 | 0 | } |
157 | 0 | _nested->evaluate_and_vec(column, size, flags); |
158 | 0 | } |
159 | | |
160 | 0 | std::string get_search_str() const override { |
161 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
162 | 0 | if (!_nested) { |
163 | 0 | DCHECK(false) << "should not reach here"; |
164 | 0 | } |
165 | 0 | return _nested->get_search_str(); |
166 | 0 | } |
167 | | |
168 | | private: |
169 | | uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel, |
170 | 0 | uint16_t size) const override { |
171 | 0 | std::shared_lock<std::shared_mutex> lock(*_mtx); |
172 | 0 | if (!_nested) { |
173 | 0 | return size; |
174 | 0 | } |
175 | 0 | return _nested->evaluate(column, sel, size); |
176 | 0 | } |
177 | | |
178 | | mutable std::shared_ptr<std::shared_mutex> _mtx; |
179 | | std::shared_ptr<ColumnPredicate> _nested; |
180 | | }; |
181 | | |
182 | | } //namespace doris |