/root/doris/be/src/olap/column_predicate.h
Line | Count | Source (jump to first uncovered line) |
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 <memory> |
21 | | #include <roaring/roaring.hh> |
22 | | |
23 | | #include "common/exception.h" |
24 | | #include "olap/rowset/segment_v2/bitmap_index_reader.h" |
25 | | #include "olap/rowset/segment_v2/bloom_filter.h" |
26 | | #include "olap/rowset/segment_v2/inverted_index_reader.h" |
27 | | #include "runtime/define_primitive_type.h" |
28 | | #include "util/runtime_profile.h" |
29 | | #include "vec/columns/column.h" |
30 | | #include "vec/exprs/vruntimefilter_wrapper.h" |
31 | | |
32 | | using namespace doris::segment_v2; |
33 | | |
34 | | namespace doris { |
35 | | |
36 | | enum class PredicateType { |
37 | | UNKNOWN = 0, |
38 | | EQ = 1, |
39 | | NE = 2, |
40 | | LT = 3, |
41 | | LE = 4, |
42 | | GT = 5, |
43 | | GE = 6, |
44 | | IN_LIST = 7, |
45 | | NOT_IN_LIST = 8, |
46 | | IS_NULL = 9, |
47 | | IS_NOT_NULL = 10, |
48 | | BF = 11, // BloomFilter |
49 | | BITMAP_FILTER = 12, // BitmapFilter |
50 | | MATCH = 13, // fulltext match |
51 | | }; |
52 | | |
53 | | template <PrimitiveType primitive_type, typename ResultType> |
54 | 4 | ResultType get_zone_map_value(void* data_ptr) { |
55 | 4 | ResultType res; |
56 | | // DecimalV2's storage value is different from predicate or compute value type |
57 | | // need convert it to DecimalV2Value |
58 | 4 | if constexpr (primitive_type == PrimitiveType::TYPE_DECIMALV2) { |
59 | 4 | decimal12_t decimal_12_t_value; |
60 | 4 | memcpy((char*)(&decimal_12_t_value), data_ptr, sizeof(decimal12_t)); |
61 | 4 | res.from_olap_decimal(decimal_12_t_value.integer, decimal_12_t_value.fraction); |
62 | 4 | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATE) { |
63 | 4 | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); |
64 | 4 | res.from_olap_date(*reinterpret_cast<uint24_t*>(data_ptr)); |
65 | 4 | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATETIME) { |
66 | 4 | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); |
67 | 4 | res.from_olap_datetime(*reinterpret_cast<uint64_t*>(data_ptr)); |
68 | 4 | } else { |
69 | 4 | memcpy(reinterpret_cast<void*>(&res), data_ptr, sizeof(ResultType)); |
70 | 4 | } |
71 | 4 | return res; |
72 | 4 | } _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE5EiEET0_Pv Line | Count | Source | 54 | 4 | ResultType get_zone_map_value(void* data_ptr) { | 55 | 4 | ResultType res; | 56 | | // DecimalV2's storage value is different from predicate or compute value type | 57 | | // need convert it to DecimalV2Value | 58 | 4 | if constexpr (primitive_type == PrimitiveType::TYPE_DECIMALV2) { | 59 | 4 | decimal12_t decimal_12_t_value; | 60 | 4 | memcpy((char*)(&decimal_12_t_value), data_ptr, sizeof(decimal12_t)); | 61 | 4 | res.from_olap_decimal(decimal_12_t_value.integer, decimal_12_t_value.fraction); | 62 | 4 | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATE) { | 63 | 4 | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); | 64 | 4 | res.from_olap_date(*reinterpret_cast<uint24_t*>(data_ptr)); | 65 | 4 | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATETIME) { | 66 | 4 | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); | 67 | 4 | res.from_olap_datetime(*reinterpret_cast<uint64_t*>(data_ptr)); | 68 | 4 | } else { | 69 | 4 | memcpy(reinterpret_cast<void*>(&res), data_ptr, sizeof(ResultType)); | 70 | 4 | } | 71 | 4 | return res; | 72 | 4 | } |
Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE11ENS_16VecDateTimeValueEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE12ENS_16VecDateTimeValueEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE3EaEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE4EsEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE6ElEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE7EnEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE8EfEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE9EdEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE20ENS_14DecimalV2ValueEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE28ENS_10vectorized7DecimalIiEEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE29ENS_10vectorized7DecimalIlEEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE30ENS_10vectorized12Decimal128V3EEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE35ENS_10vectorized7DecimalIN4wide7integerILm256EiEEEEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE15ENS_9StringRefEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE23ENS_9StringRefEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE25ENS_11DateV2ValueINS_15DateV2ValueTypeEEEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE26ENS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE2EbEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE36EjEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE37EoEET0_Pv Unexecuted instantiation: _ZN5doris18get_zone_map_valueILNS_13PrimitiveTypeE10ENS_9StringRefEEET0_Pv |
73 | | |
74 | 0 | inline std::string type_to_string(PredicateType type) { |
75 | 0 | switch (type) { |
76 | 0 | case PredicateType::UNKNOWN: |
77 | 0 | return "UNKNOWN"; |
78 | | |
79 | 0 | case PredicateType::EQ: |
80 | 0 | return "EQ"; |
81 | | |
82 | 0 | case PredicateType::NE: |
83 | 0 | return "NE"; |
84 | | |
85 | 0 | case PredicateType::LT: |
86 | 0 | return "LT"; |
87 | | |
88 | 0 | case PredicateType::LE: |
89 | 0 | return "LE"; |
90 | | |
91 | 0 | case PredicateType::GT: |
92 | 0 | return "GT"; |
93 | | |
94 | 0 | case PredicateType::GE: |
95 | 0 | return "GE"; |
96 | | |
97 | 0 | case PredicateType::IN_LIST: |
98 | 0 | return "IN_LIST"; |
99 | | |
100 | 0 | case PredicateType::NOT_IN_LIST: |
101 | 0 | return "NOT_IN_LIST"; |
102 | | |
103 | 0 | case PredicateType::IS_NULL: |
104 | 0 | return "IS_NULL"; |
105 | | |
106 | 0 | case PredicateType::IS_NOT_NULL: |
107 | 0 | return "IS_NOT_NULL"; |
108 | | |
109 | 0 | case PredicateType::BF: |
110 | 0 | return "BF"; |
111 | 0 | default: |
112 | 0 | return ""; |
113 | 0 | }; |
114 | |
|
115 | 0 | return ""; |
116 | 0 | } |
117 | | |
118 | | struct PredicateTypeTraits { |
119 | 467 | static constexpr bool is_range(PredicateType type) { |
120 | 467 | return (type == PredicateType::LT || type == PredicateType::LE || |
121 | 467 | type == PredicateType::GT || type == PredicateType::GE); |
122 | 467 | } |
123 | | |
124 | 140 | static constexpr bool is_bloom_filter(PredicateType type) { return type == PredicateType::BF; } |
125 | | |
126 | 0 | static constexpr bool is_list(PredicateType type) { |
127 | 0 | return (type == PredicateType::IN_LIST || type == PredicateType::NOT_IN_LIST); |
128 | 0 | } |
129 | | |
130 | 0 | static constexpr bool is_equal_or_list(PredicateType type) { |
131 | 0 | return (type == PredicateType::EQ || type == PredicateType::IN_LIST); |
132 | 0 | } |
133 | | |
134 | 0 | static constexpr bool is_comparison(PredicateType type) { |
135 | 0 | return (type == PredicateType::EQ || type == PredicateType::NE || |
136 | 0 | type == PredicateType::LT || type == PredicateType::LE || |
137 | 0 | type == PredicateType::GT || type == PredicateType::GE); |
138 | 0 | } |
139 | | }; |
140 | | |
141 | | #define EVALUATE_BY_SELECTOR(EVALUATE_IMPL_WITH_NULL_MAP, EVALUATE_IMPL_WITHOUT_NULL_MAP) \ |
142 | 4.19k | const bool is_dense_column = pred_col.size() == size; \ |
143 | 3.90M | for (uint16_t i = 0; i < size; i++) { \ |
144 | 3.90M | uint16_t idx = is_dense_column ? i : sel[i]; \ |
145 | 3.90M | if constexpr (is_nullable) { \ |
146 | 9.40k | if (EVALUATE_IMPL_WITH_NULL_MAP(idx)) { \ |
147 | 9.01k | sel[new_size++] = idx; \ |
148 | 9.01k | } \ |
149 | 3.89M | } else { \ |
150 | 3.89M | if (EVALUATE_IMPL_WITHOUT_NULL_MAP(idx)) { \ |
151 | 3.68M | sel[new_size++] = idx; \ |
152 | 3.68M | } \ |
153 | 3.89M | } \ |
154 | 3.90M | } |
155 | | |
156 | | class ColumnPredicate { |
157 | | public: |
158 | | explicit ColumnPredicate(uint32_t column_id, bool opposite = false) |
159 | 205 | : _column_id(column_id), _opposite(opposite) { |
160 | 205 | reset_judge_selectivity(); |
161 | 205 | } |
162 | | |
163 | 205 | virtual ~ColumnPredicate() = default; |
164 | | |
165 | | virtual PredicateType type() const = 0; |
166 | | |
167 | | //evaluate predicate on Bitmap |
168 | | virtual Status evaluate(BitmapIndexIterator* iterator, uint32_t num_rows, |
169 | | roaring::Roaring* roaring) const = 0; |
170 | | |
171 | | //evaluate predicate on inverted |
172 | | virtual Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type, |
173 | | InvertedIndexIterator* iterator, uint32_t num_rows, |
174 | 0 | roaring::Roaring* bitmap) const { |
175 | 0 | return Status::NotSupported( |
176 | 0 | "Not Implemented evaluate with inverted index, please check the predicate"); |
177 | 0 | } |
178 | | |
179 | 0 | virtual double get_ignore_threshold() const { return 0; } |
180 | | |
181 | | // evaluate predicate on IColumn |
182 | | // a short circuit eval way |
183 | 4.20k | uint16_t evaluate(const vectorized::IColumn& column, uint16_t* sel, uint16_t size) const { |
184 | 4.20k | if (always_true()) { |
185 | 0 | return size; |
186 | 0 | } |
187 | | |
188 | 4.20k | uint16_t new_size = _evaluate_inner(column, sel, size); |
189 | 4.20k | if (_can_ignore()) { |
190 | 0 | do_judge_selectivity(size - new_size, size); |
191 | 0 | } |
192 | 4.20k | update_filter_info(size - new_size, size); |
193 | 4.20k | return new_size; |
194 | 4.20k | } |
195 | | virtual void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
196 | 0 | bool* flags) const {} |
197 | | virtual void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
198 | 0 | bool* flags) const {} |
199 | | |
200 | 0 | virtual bool support_zonemap() const { return true; } |
201 | | |
202 | 0 | virtual bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const { |
203 | 0 | return true; |
204 | 0 | } |
205 | | |
206 | 0 | virtual bool is_always_true(const std::pair<WrapperField*, WrapperField*>& statistic) const { |
207 | 0 | return false; |
208 | 0 | } |
209 | | |
210 | 0 | virtual bool evaluate_del(const std::pair<WrapperField*, WrapperField*>& statistic) const { |
211 | 0 | return false; |
212 | 0 | } |
213 | | |
214 | 0 | virtual bool evaluate_and(const BloomFilter* bf) const { return true; } |
215 | | |
216 | 0 | virtual bool evaluate_and(const StringRef* dict_words, const size_t dict_count) const { |
217 | 0 | return true; |
218 | 0 | } |
219 | | |
220 | 0 | virtual bool can_do_bloom_filter(bool ngram) const { return false; } |
221 | | |
222 | | // Check input type could apply safely. |
223 | | // Note: Currenly ColumnPredicate is not include complex type, so use PrimitiveType |
224 | | // is simple and intuitive |
225 | | virtual bool can_do_apply_safely(PrimitiveType input_type, bool is_null) const = 0; |
226 | | |
227 | | // used to evaluate pre read column in lazy materialization |
228 | | // now only support integer/float |
229 | | // a vectorized eval way |
230 | 0 | virtual void evaluate_vec(const vectorized::IColumn& column, uint16_t size, bool* flags) const { |
231 | 0 | DCHECK(false) << "should not reach here"; |
232 | 0 | } |
233 | | virtual void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size, |
234 | 0 | bool* flags) const { |
235 | 0 | DCHECK(false) << "should not reach here"; |
236 | 0 | } |
237 | | |
238 | 0 | virtual std::string get_search_str() const { |
239 | 0 | DCHECK(false) << "should not reach here"; |
240 | 0 | return ""; |
241 | 0 | } |
242 | | |
243 | 0 | virtual void set_page_ng_bf(std::unique_ptr<segment_v2::BloomFilter>) { |
244 | 0 | DCHECK(false) << "should not reach here"; |
245 | 0 | } |
246 | 15.1k | uint32_t column_id() const { return _column_id; } |
247 | | |
248 | 0 | bool opposite() const { return _opposite; } |
249 | | |
250 | 0 | std::string debug_string() const { |
251 | 0 | return _debug_string() + |
252 | 0 | fmt::format(", column_id={}, opposite={}, can_ignore={}, runtime_filter_id={}", |
253 | 0 | _column_id, _opposite, _can_ignore(), _runtime_filter_id); |
254 | 0 | } |
255 | | |
256 | 0 | int get_runtime_filter_id() const { return _runtime_filter_id; } |
257 | | |
258 | | void attach_profile_counter( |
259 | | int filter_id, std::shared_ptr<RuntimeProfile::Counter> predicate_filtered_rows_counter, |
260 | 0 | std::shared_ptr<RuntimeProfile::Counter> predicate_input_rows_counter) { |
261 | 0 | _runtime_filter_id = filter_id; |
262 | 0 | DCHECK(predicate_filtered_rows_counter != nullptr); |
263 | 0 | DCHECK(predicate_input_rows_counter != nullptr); |
264 | |
|
265 | 0 | if (predicate_filtered_rows_counter != nullptr) { |
266 | 0 | _predicate_filtered_rows_counter = predicate_filtered_rows_counter; |
267 | 0 | } |
268 | 0 | if (predicate_input_rows_counter != nullptr) { |
269 | 0 | _predicate_input_rows_counter = predicate_input_rows_counter; |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | | /// TODO: Currently we only record statistics for runtime filters, in the future we should record for all predicates |
274 | 4.20k | void update_filter_info(int64_t filter_rows, int64_t input_rows) const { |
275 | 4.20k | COUNTER_UPDATE(_predicate_input_rows_counter, input_rows); |
276 | 4.20k | COUNTER_UPDATE(_predicate_filtered_rows_counter, filter_rows); |
277 | 4.20k | } |
278 | | |
279 | 0 | static std::string pred_type_string(PredicateType type) { |
280 | 0 | switch (type) { |
281 | 0 | case PredicateType::EQ: |
282 | 0 | return "eq"; |
283 | 0 | case PredicateType::NE: |
284 | 0 | return "ne"; |
285 | 0 | case PredicateType::LT: |
286 | 0 | return "lt"; |
287 | 0 | case PredicateType::LE: |
288 | 0 | return "le"; |
289 | 0 | case PredicateType::GT: |
290 | 0 | return "gt"; |
291 | 0 | case PredicateType::GE: |
292 | 0 | return "ge"; |
293 | 0 | case PredicateType::IN_LIST: |
294 | 0 | return "in"; |
295 | 0 | case PredicateType::NOT_IN_LIST: |
296 | 0 | return "not_in"; |
297 | 0 | case PredicateType::IS_NULL: |
298 | 0 | return "is_null"; |
299 | 0 | case PredicateType::IS_NOT_NULL: |
300 | 0 | return "is_not_null"; |
301 | 0 | case PredicateType::BF: |
302 | 0 | return "bf"; |
303 | 0 | case PredicateType::MATCH: |
304 | 0 | return "match"; |
305 | 0 | default: |
306 | 0 | return "unknown"; |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | 4.20k | bool always_true() const { return _always_true; } |
311 | | // Return whether the ColumnPredicate was created by a runtime filter. |
312 | | // If true, it was definitely created by a runtime filter. |
313 | | // If false, it may still have been created by a runtime filter, |
314 | | // as certain filters like "in filter" generate key ranges instead of ColumnPredicate. |
315 | | // is_runtime_filter uses _can_ignore, except for BitmapFilter, |
316 | | // as BitmapFilter cannot ignore data. |
317 | 0 | virtual bool is_runtime_filter() const { return _can_ignore(); } |
318 | | |
319 | | protected: |
320 | | virtual std::string _debug_string() const = 0; |
321 | 4.20k | virtual bool _can_ignore() const { return _runtime_filter_id != -1; } |
322 | | virtual uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel, |
323 | 0 | uint16_t size) const { |
324 | 0 | throw Exception(INTERNAL_ERROR, "Not Implemented _evaluate_inner"); |
325 | 0 | } |
326 | | |
327 | 205 | void reset_judge_selectivity() const { |
328 | 205 | _always_true = false; |
329 | 205 | _judge_counter = config::runtime_filter_sampling_frequency; |
330 | 205 | _judge_input_rows = 0; |
331 | 205 | _judge_filter_rows = 0; |
332 | 205 | } |
333 | | |
334 | 0 | void do_judge_selectivity(uint64_t filter_rows, uint64_t input_rows) const { |
335 | 0 | if ((_judge_counter--) == 0) { |
336 | 0 | reset_judge_selectivity(); |
337 | 0 | } |
338 | 0 | if (!_always_true) { |
339 | 0 | _judge_filter_rows += filter_rows; |
340 | 0 | _judge_input_rows += input_rows; |
341 | 0 | vectorized::VRuntimeFilterWrapper::judge_selectivity( |
342 | 0 | get_ignore_threshold(), _judge_filter_rows, _judge_input_rows, _always_true); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | uint32_t _column_id; |
347 | | // TODO: the value is only in delete condition, better be template value |
348 | | bool _opposite; |
349 | | int _runtime_filter_id = -1; |
350 | | // VRuntimeFilterWrapper and ColumnPredicate share the same logic, |
351 | | // but it's challenging to unify them, so the code is duplicated. |
352 | | // _judge_counter, _judge_input_rows, _judge_filter_rows, and _always_true |
353 | | // are variables used to implement the _always_true logic, calculated periodically |
354 | | // based on runtime_filter_sampling_frequency. During each period, if _always_true |
355 | | // is evaluated as true, the logic for always_true is applied for the rest of that period |
356 | | // without recalculating. At the beginning of the next period, |
357 | | // reset_judge_selectivity is used to reset these variables. |
358 | | mutable int _judge_counter = 0; |
359 | | mutable uint64_t _judge_input_rows = 0; |
360 | | mutable uint64_t _judge_filter_rows = 0; |
361 | | mutable bool _always_true = false; |
362 | | |
363 | | std::shared_ptr<RuntimeProfile::Counter> _predicate_filtered_rows_counter = |
364 | | std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0); |
365 | | |
366 | | std::shared_ptr<RuntimeProfile::Counter> _predicate_input_rows_counter = |
367 | | std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0); |
368 | | }; |
369 | | |
370 | | } //namespace doris |