Coverage Report

Created: 2026-03-03 17:44

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