/root/doris/be/src/olap/column_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 <memory> |
21 | | #include <roaring/roaring.hh> |
22 | | |
23 | | #include "common/exception.h" |
24 | | #include "olap/rowset/segment_v2/bloom_filter.h" |
25 | | #include "olap/rowset/segment_v2/inverted_index_iterator.h" |
26 | | #include "runtime/define_primitive_type.h" |
27 | | #include "runtime_filter/runtime_filter_selectivity.h" |
28 | | #include "util/defer_op.h" |
29 | | #include "util/runtime_profile.h" |
30 | | #include "vec/columns/column.h" |
31 | | #include "vec/exec/format/parquet/parquet_predicate.h" |
32 | | #include "vec/exprs/vruntimefilter_wrapper.h" |
33 | | |
34 | | using namespace doris::segment_v2; |
35 | | |
36 | | namespace doris { |
37 | | |
38 | | enum class PredicateType { |
39 | | UNKNOWN = 0, |
40 | | EQ = 1, |
41 | | NE = 2, |
42 | | LT = 3, |
43 | | LE = 4, |
44 | | GT = 5, |
45 | | GE = 6, |
46 | | IN_LIST = 7, |
47 | | NOT_IN_LIST = 8, |
48 | | IS_NULL = 9, |
49 | | IS_NOT_NULL = 10, |
50 | | BF = 11, // BloomFilter |
51 | | BITMAP_FILTER = 12, // BitmapFilter |
52 | | MATCH = 13, // fulltext match |
53 | | }; |
54 | | |
55 | | template <PrimitiveType primitive_type, typename ResultType> |
56 | | ResultType get_zone_map_value(void* data_ptr) { |
57 | | ResultType res; |
58 | | // DecimalV2's storage value is different from predicate or compute value type |
59 | | // need convert it to DecimalV2Value |
60 | | if constexpr (primitive_type == PrimitiveType::TYPE_DECIMALV2) { |
61 | | decimal12_t decimal_12_t_value; |
62 | | memcpy((char*)(&decimal_12_t_value), data_ptr, sizeof(decimal12_t)); |
63 | | res.from_olap_decimal(decimal_12_t_value.integer, decimal_12_t_value.fraction); |
64 | | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATE) { |
65 | | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); |
66 | | uint24_t date; |
67 | | memcpy(&date, data_ptr, sizeof(uint24_t)); |
68 | | res.from_olap_date(date); |
69 | | } else if constexpr (primitive_type == PrimitiveType::TYPE_DATETIME) { |
70 | | static_assert(std::is_same_v<ResultType, VecDateTimeValue>); |
71 | | uint64_t datetime; |
72 | | memcpy(&datetime, data_ptr, sizeof(uint64_t)); |
73 | | res.from_olap_datetime(datetime); |
74 | | } else { |
75 | | memcpy(reinterpret_cast<void*>(&res), data_ptr, sizeof(ResultType)); |
76 | | } |
77 | | return res; |
78 | | } |
79 | | |
80 | 0 | inline std::string type_to_string(PredicateType type) { |
81 | 0 | switch (type) { |
82 | 0 | case PredicateType::UNKNOWN: |
83 | 0 | return "UNKNOWN"; |
84 | 0 |
|
85 | 0 | case PredicateType::EQ: |
86 | 0 | return "EQ"; |
87 | 0 |
|
88 | 0 | case PredicateType::NE: |
89 | 0 | return "NE"; |
90 | 0 |
|
91 | 0 | case PredicateType::LT: |
92 | 0 | return "LT"; |
93 | 0 |
|
94 | 0 | case PredicateType::LE: |
95 | 0 | return "LE"; |
96 | 0 |
|
97 | 0 | case PredicateType::GT: |
98 | 0 | return "GT"; |
99 | 0 |
|
100 | 0 | case PredicateType::GE: |
101 | 0 | return "GE"; |
102 | 0 |
|
103 | 0 | case PredicateType::IN_LIST: |
104 | 0 | return "IN_LIST"; |
105 | 0 |
|
106 | 0 | case PredicateType::NOT_IN_LIST: |
107 | 0 | return "NOT_IN_LIST"; |
108 | 0 |
|
109 | 0 | case PredicateType::IS_NULL: |
110 | 0 | return "IS_NULL"; |
111 | 0 |
|
112 | 0 | case PredicateType::IS_NOT_NULL: |
113 | 0 | return "IS_NOT_NULL"; |
114 | 0 |
|
115 | 0 | case PredicateType::BF: |
116 | 0 | return "BF"; |
117 | 0 | default: |
118 | 0 | return ""; |
119 | 0 | }; |
120 | 0 |
|
121 | 0 | return ""; |
122 | 0 | } |
123 | | |
124 | 0 | inline std::string type_to_op_str(PredicateType type) { |
125 | 0 | switch (type) { |
126 | 0 | case PredicateType::EQ: |
127 | 0 | return "="; |
128 | | |
129 | 0 | case PredicateType::NE: |
130 | 0 | return "!="; |
131 | | |
132 | 0 | case PredicateType::LT: |
133 | 0 | return "<<"; |
134 | | |
135 | 0 | case PredicateType::LE: |
136 | 0 | return "<="; |
137 | | |
138 | 0 | case PredicateType::GT: |
139 | 0 | return ">>"; |
140 | | |
141 | 0 | case PredicateType::GE: |
142 | 0 | return ">="; |
143 | | |
144 | 0 | case PredicateType::IN_LIST: |
145 | 0 | return "*="; |
146 | | |
147 | 0 | case PredicateType::NOT_IN_LIST: |
148 | 0 | return "!*="; |
149 | | |
150 | 0 | case PredicateType::IS_NULL: |
151 | 0 | case PredicateType::IS_NOT_NULL: |
152 | 0 | return "is"; |
153 | | |
154 | 0 | default: |
155 | 0 | break; |
156 | 0 | }; |
157 | |
|
158 | 0 | return ""; |
159 | 0 | } |
160 | | |
161 | | struct PredicateTypeTraits { |
162 | 467 | static constexpr bool is_range(PredicateType type) { |
163 | 467 | return (type == PredicateType::LT || type == PredicateType::LE || |
164 | 467 | type == PredicateType::GT || type == PredicateType::GE); |
165 | 467 | } |
166 | | |
167 | 140 | static constexpr bool is_bloom_filter(PredicateType type) { return type == PredicateType::BF; } |
168 | | |
169 | 0 | static constexpr bool is_list(PredicateType type) { |
170 | 0 | return (type == PredicateType::IN_LIST || type == PredicateType::NOT_IN_LIST); |
171 | 0 | } |
172 | | |
173 | 0 | static constexpr bool is_equal_or_list(PredicateType type) { |
174 | 0 | return (type == PredicateType::EQ || type == PredicateType::IN_LIST); |
175 | 0 | } |
176 | | |
177 | 0 | static constexpr bool is_comparison(PredicateType type) { |
178 | 0 | return (type == PredicateType::EQ || type == PredicateType::NE || |
179 | 0 | type == PredicateType::LT || type == PredicateType::LE || |
180 | 0 | type == PredicateType::GT || type == PredicateType::GE); |
181 | 0 | } |
182 | | }; |
183 | | |
184 | | #define EVALUATE_BY_SELECTOR(EVALUATE_IMPL_WITH_NULL_MAP, EVALUATE_IMPL_WITHOUT_NULL_MAP) \ |
185 | 4.19k | const bool is_dense_column = pred_col.size() == size; \ |
186 | 3.90M | for (uint16_t i = 0; i < size; i++) { \ |
187 | 3.90M | uint16_t idx = is_dense_column ? i : sel[i]; \ |
188 | 3.90M | if constexpr (is_nullable) { \ |
189 | 9.40k | if (EVALUATE_IMPL_WITH_NULL_MAP(idx)) { \ |
190 | 9.01k | sel[new_size++] = idx; \ |
191 | 9.01k | } \ |
192 | 3.89M | } else { \ |
193 | 3.89M | if (EVALUATE_IMPL_WITHOUT_NULL_MAP(idx)) { \ |
194 | 3.68M | sel[new_size++] = idx; \ |
195 | 3.68M | } \ |
196 | 3.89M | } \ |
197 | 3.90M | } |
198 | | |
199 | | struct ZoneMapInfo { |
200 | | vectorized::Field min_value; |
201 | | vectorized::Field max_value; |
202 | | bool has_null = false; |
203 | | bool is_all_null = false; |
204 | | }; |
205 | | |
206 | | class ColumnPredicate : public std::enable_shared_from_this<ColumnPredicate> { |
207 | | public: |
208 | | explicit ColumnPredicate(uint32_t column_id, std::string col_name, PrimitiveType primitive_type, |
209 | | bool opposite = false) |
210 | 944 | : _column_id(column_id), |
211 | 944 | _col_name(col_name), |
212 | 944 | _primitive_type(primitive_type), |
213 | 944 | _opposite(opposite) { |
214 | 944 | reset_judge_selectivity(); |
215 | 944 | } |
216 | 1 | ColumnPredicate(const ColumnPredicate& other, uint32_t col_id) : ColumnPredicate(other) { |
217 | 1 | _column_id = col_id; |
218 | 1 | } |
219 | | |
220 | 945 | virtual ~ColumnPredicate() = default; |
221 | | |
222 | | virtual PredicateType type() const = 0; |
223 | 8 | virtual PrimitiveType primitive_type() const { return _primitive_type; } |
224 | | virtual std::shared_ptr<ColumnPredicate> clone(uint32_t col_id) const = 0; |
225 | | |
226 | | //evaluate predicate on inverted |
227 | | virtual Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type, |
228 | | IndexIterator* iterator, uint32_t num_rows, |
229 | 0 | roaring::Roaring* bitmap) const { |
230 | 0 | return Status::NotSupported( |
231 | 0 | "Not Implemented evaluate with inverted index, please check the predicate"); |
232 | 0 | } |
233 | | |
234 | 0 | virtual double get_ignore_threshold() const { return 0; } |
235 | | // If this predicate acts on the key column, this predicate should be erased. |
236 | 0 | virtual bool could_be_erased() const { return false; } |
237 | | // Return the size of value set for IN/NOT IN predicates and 0 for others. |
238 | 2 | virtual std::string debug_string() const { |
239 | 2 | fmt::memory_buffer debug_string_buffer; |
240 | 2 | fmt::format_to(debug_string_buffer, |
241 | 2 | "Column ID: {}, Data Type: {}, PredicateType: {}, opposite: {}, Runtime " |
242 | 2 | "Filter ID: {}", |
243 | 2 | _column_id, type_to_string(primitive_type()), pred_type_string(type()), |
244 | 2 | _opposite, _runtime_filter_id); |
245 | 2 | return fmt::to_string(debug_string_buffer); |
246 | 2 | } |
247 | | |
248 | | // evaluate predicate on IColumn |
249 | | // a short circuit eval way |
250 | 4.20k | uint16_t evaluate(const vectorized::IColumn& column, uint16_t* sel, uint16_t size) const { |
251 | 4.20k | Defer defer([&] { try_reset_judge_selectivity(); }); |
252 | | |
253 | 4.20k | if (always_true()) { |
254 | 0 | update_filter_info(0, 0, size); |
255 | 0 | return size; |
256 | 0 | } |
257 | | |
258 | 4.20k | uint16_t new_size = _evaluate_inner(column, sel, size); |
259 | 4.20k | if (_can_ignore()) { |
260 | 0 | do_judge_selectivity(size - new_size, size); |
261 | 0 | } |
262 | 4.20k | update_filter_info(size - new_size, size, 0); |
263 | 4.20k | return new_size; |
264 | 4.20k | } |
265 | | virtual void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
266 | 0 | bool* flags) const {} |
267 | | virtual void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
268 | 0 | bool* flags) const {} |
269 | | |
270 | 0 | virtual bool support_zonemap() const { return true; } |
271 | | |
272 | 0 | virtual bool evaluate_and(const ZoneMapInfo& zone_map_info) const { return true; } |
273 | | |
274 | 0 | virtual bool is_always_true(const ZoneMapInfo& zone_map_info) const { return false; } |
275 | | |
276 | 0 | virtual bool evaluate_del(const ZoneMapInfo& zone_map_info) const { return false; } |
277 | | |
278 | 0 | virtual bool evaluate_and(const vectorized::ParquetBlockSplitBloomFilter* bf) const { |
279 | 0 | return true; |
280 | 0 | } |
281 | | |
282 | 0 | virtual bool evaluate_and(const BloomFilter* bf) const { return true; } |
283 | | |
284 | 0 | virtual bool evaluate_and(const StringRef* dict_words, const size_t dict_count) const { |
285 | 0 | return true; |
286 | 0 | } |
287 | | |
288 | 0 | virtual bool can_do_bloom_filter(bool ngram) const { return false; } |
289 | | |
290 | | /** |
291 | | * Figure out whether this page is matched partially or completely. |
292 | | */ |
293 | 0 | virtual bool evaluate_and(vectorized::ParquetPredicate::ColumnStat* statistic) const { |
294 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, |
295 | 0 | "ParquetPredicate is not supported by this predicate!"); |
296 | 0 | return true; |
297 | 0 | } |
298 | | |
299 | | virtual bool evaluate_and(vectorized::ParquetPredicate::CachedPageIndexStat* statistic, |
300 | 0 | RowRanges* row_ranges) const { |
301 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, |
302 | 0 | "ParquetPredicate is not supported by this predicate!"); |
303 | 0 | return true; |
304 | 0 | } |
305 | | |
306 | | // used to evaluate pre read column in lazy materialization |
307 | | // now only support integer/float |
308 | | // a vectorized eval way |
309 | 0 | virtual void evaluate_vec(const vectorized::IColumn& column, uint16_t size, bool* flags) const { |
310 | 0 | DCHECK(false) << "should not reach here"; |
311 | 0 | } |
312 | | virtual void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size, |
313 | 0 | bool* flags) const { |
314 | 0 | DCHECK(false) << "should not reach here"; |
315 | 0 | } |
316 | | |
317 | 0 | virtual std::string get_search_str() const { |
318 | 0 | DCHECK(false) << "should not reach here"; |
319 | 0 | return ""; |
320 | 0 | } |
321 | | |
322 | 0 | virtual void set_page_ng_bf(std::unique_ptr<segment_v2::BloomFilter>) { |
323 | 0 | DCHECK(false) << "should not reach here"; |
324 | 0 | } |
325 | 15.2k | uint32_t column_id() const { return _column_id; } |
326 | 6 | std::string col_name() const { return _col_name; } |
327 | | |
328 | 6 | bool opposite() const { return _opposite; } |
329 | | |
330 | | void attach_profile_counter( |
331 | | int filter_id, std::shared_ptr<RuntimeProfile::Counter> predicate_filtered_rows_counter, |
332 | | std::shared_ptr<RuntimeProfile::Counter> predicate_input_rows_counter, |
333 | 0 | std::shared_ptr<RuntimeProfile::Counter> predicate_always_true_rows_counter) { |
334 | 0 | _runtime_filter_id = filter_id; |
335 | 0 | DCHECK(predicate_filtered_rows_counter != nullptr); |
336 | 0 | DCHECK(predicate_input_rows_counter != nullptr); |
337 | |
|
338 | 0 | if (predicate_filtered_rows_counter != nullptr) { |
339 | 0 | _predicate_filtered_rows_counter = predicate_filtered_rows_counter; |
340 | 0 | } |
341 | 0 | if (predicate_input_rows_counter != nullptr) { |
342 | 0 | _predicate_input_rows_counter = predicate_input_rows_counter; |
343 | 0 | } |
344 | 0 | if (predicate_always_true_rows_counter != nullptr) { |
345 | 0 | _predicate_always_true_rows_counter = predicate_always_true_rows_counter; |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | | /// TODO: Currently we only record statistics for runtime filters, in the future we should record for all predicates |
350 | | void update_filter_info(int64_t filter_rows, int64_t input_rows, |
351 | 4.20k | int64_t always_true_rows) const { |
352 | 4.20k | if (_predicate_input_rows_counter == nullptr || |
353 | 4.20k | _predicate_filtered_rows_counter == nullptr || |
354 | 4.20k | _predicate_always_true_rows_counter == nullptr) { |
355 | 0 | throw Exception(INTERNAL_ERROR, "Predicate profile counters are not initialized"); |
356 | 0 | } |
357 | 4.20k | COUNTER_UPDATE(_predicate_input_rows_counter, input_rows); |
358 | 4.20k | COUNTER_UPDATE(_predicate_filtered_rows_counter, filter_rows); |
359 | 4.20k | COUNTER_UPDATE(_predicate_always_true_rows_counter, always_true_rows); |
360 | 4.20k | } |
361 | | |
362 | 2 | static std::string pred_type_string(PredicateType type) { |
363 | 2 | switch (type) { |
364 | 2 | case PredicateType::EQ: |
365 | 2 | return "eq"; |
366 | 0 | case PredicateType::NE: |
367 | 0 | return "ne"; |
368 | 0 | case PredicateType::LT: |
369 | 0 | return "lt"; |
370 | 0 | case PredicateType::LE: |
371 | 0 | return "le"; |
372 | 0 | case PredicateType::GT: |
373 | 0 | return "gt"; |
374 | 0 | case PredicateType::GE: |
375 | 0 | return "ge"; |
376 | 0 | case PredicateType::IN_LIST: |
377 | 0 | return "in"; |
378 | 0 | case PredicateType::NOT_IN_LIST: |
379 | 0 | return "not_in"; |
380 | 0 | case PredicateType::IS_NULL: |
381 | 0 | return "is_null"; |
382 | 0 | case PredicateType::IS_NOT_NULL: |
383 | 0 | return "is_not_null"; |
384 | 0 | case PredicateType::BF: |
385 | 0 | return "bf"; |
386 | 0 | case PredicateType::MATCH: |
387 | 0 | return "match"; |
388 | 0 | default: |
389 | 0 | return "unknown"; |
390 | 2 | } |
391 | 2 | } |
392 | | |
393 | 4.20k | bool always_true() const { return _always_true; } |
394 | | // Return whether the ColumnPredicate was created by a runtime filter. |
395 | | // If true, it was definitely created by a runtime filter. |
396 | | // If false, it may still have been created by a runtime filter, |
397 | | // as certain filters like "in filter" generate key ranges instead of ColumnPredicate. |
398 | | // is_runtime_filter uses _can_ignore, except for BitmapFilter, |
399 | | // as BitmapFilter cannot ignore data. |
400 | 0 | virtual bool is_runtime_filter() const { return _can_ignore(); } |
401 | | |
402 | | protected: |
403 | 8.40k | virtual bool _can_ignore() const { return _runtime_filter_id != -1; } |
404 | | virtual uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel, |
405 | 0 | uint16_t size) const { |
406 | 0 | throw Exception(INTERNAL_ERROR, "Not Implemented _evaluate_inner"); |
407 | 0 | } |
408 | | |
409 | 944 | void reset_judge_selectivity() const { |
410 | 944 | _always_true = false; |
411 | 944 | _judge_counter = config::runtime_filter_sampling_frequency; |
412 | 944 | _judge_input_rows = 0; |
413 | 944 | _judge_filter_rows = 0; |
414 | 944 | } |
415 | | |
416 | 4.20k | void try_reset_judge_selectivity() const { |
417 | 4.20k | if (_can_ignore() && ((_judge_counter--) == 0)) { |
418 | 0 | reset_judge_selectivity(); |
419 | 0 | } |
420 | 4.20k | } |
421 | | |
422 | 0 | void do_judge_selectivity(uint64_t filter_rows, uint64_t input_rows) const { |
423 | 0 | if (!_always_true) { |
424 | 0 | _judge_filter_rows += filter_rows; |
425 | 0 | _judge_input_rows += input_rows; |
426 | 0 | RuntimeFilterSelectivity::judge_selectivity(get_ignore_threshold(), _judge_filter_rows, |
427 | 0 | _judge_input_rows, _always_true); |
428 | 0 | } |
429 | 0 | } |
430 | | |
431 | | uint32_t _column_id; |
432 | | const std::string _col_name; |
433 | | PrimitiveType _primitive_type; |
434 | | // TODO: the value is only in delete condition, better be template value |
435 | | bool _opposite; |
436 | | int _runtime_filter_id = -1; |
437 | | // VRuntimeFilterWrapper and ColumnPredicate share the same logic, |
438 | | // but it's challenging to unify them, so the code is duplicated. |
439 | | // _judge_counter, _judge_input_rows, _judge_filter_rows, and _always_true |
440 | | // are variables used to implement the _always_true logic, calculated periodically |
441 | | // based on runtime_filter_sampling_frequency. During each period, if _always_true |
442 | | // is evaluated as true, the logic for always_true is applied for the rest of that period |
443 | | // without recalculating. At the beginning of the next period, |
444 | | // reset_judge_selectivity is used to reset these variables. |
445 | | mutable int _judge_counter = 0; |
446 | | mutable uint64_t _judge_input_rows = 0; |
447 | | mutable uint64_t _judge_filter_rows = 0; |
448 | | mutable bool _always_true = false; |
449 | | |
450 | | std::shared_ptr<RuntimeProfile::Counter> _predicate_filtered_rows_counter = |
451 | | std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0); |
452 | | std::shared_ptr<RuntimeProfile::Counter> _predicate_input_rows_counter = |
453 | | std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0); |
454 | | std::shared_ptr<RuntimeProfile::Counter> _predicate_always_true_rows_counter = |
455 | | std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0); |
456 | | |
457 | | private: |
458 | 1 | ColumnPredicate(const ColumnPredicate& other) = default; |
459 | | }; |
460 | | |
461 | | } //namespace doris |