Coverage Report

Created: 2026-03-15 22:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/predicate/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 "core/column/column.h"
25
#include "core/data_type/define_primitive_type.h"
26
#include "exec/runtime_filter/runtime_filter_selectivity.h"
27
#include "exprs/vruntimefilter_wrapper.h"
28
#include "format/parquet/parquet_predicate.h"
29
#include "runtime/runtime_profile.h"
30
#include "storage/index/bloom_filter/bloom_filter.h"
31
#include "storage/index/inverted/inverted_index_iterator.h"
32
#include "storage/index/zone_map/zone_map_index.h"
33
#include "util/defer_op.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
129k
    static constexpr bool is_range(PredicateType type) {
164
129k
        return (type == PredicateType::LT || type == PredicateType::LE ||
165
129k
                type == PredicateType::GT || type == PredicateType::GE);
166
129k
    }
167
168
89.6k
    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
744
    static constexpr bool is_equal_or_list(PredicateType type) {
175
744
        return (type == PredicateType::EQ || type == PredicateType::IN_LIST);
176
744
    }
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
25.8k
    const bool is_dense_column = pred_col.size() == size;                                 \
187
11.4M
    for (uint16_t i = 0; i < size; i++) {                                                 \
188
11.4M
        uint16_t idx = is_dense_column ? i : sel[i];                                      \
189
11.4M
        if constexpr (is_nullable) {                                                      \
190
2.35M
            if (EVALUATE_IMPL_WITH_NULL_MAP(idx)) {                                       \
191
1.13M
                sel[new_size++] = idx;                                                    \
192
1.13M
            }                                                                             \
193
9.06M
        } else {                                                                          \
194
9.06M
            if (EVALUATE_IMPL_WITHOUT_NULL_MAP(idx)) {                                    \
195
4.74M
                sel[new_size++] = idx;                                                    \
196
4.74M
            }                                                                             \
197
9.06M
        }                                                                                 \
198
11.4M
    }
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
413k
            : _column_id(column_id),
205
413k
              _col_name(col_name),
206
413k
              _primitive_type(primitive_type),
207
413k
              _opposite(opposite) {
208
413k
        reset_judge_selectivity();
209
413k
    }
210
716k
    ColumnPredicate(const ColumnPredicate& other, uint32_t col_id) : ColumnPredicate(other) {
211
716k
        _column_id = col_id;
212
716k
    }
213
214
1.13M
    virtual ~ColumnPredicate() = default;
215
216
    virtual PredicateType type() const = 0;
217
54.7k
    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 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
6.90k
    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
54.9k
    virtual std::string debug_string() const {
233
54.9k
        fmt::memory_buffer debug_string_buffer;
234
54.9k
        fmt::format_to(debug_string_buffer,
235
54.9k
                       "Column ID: {}, Data Type: {}, PredicateType: {}, opposite: {}, Runtime "
236
54.9k
                       "Filter ID: {}",
237
54.9k
                       _column_id, type_to_string(primitive_type()), pred_type_string(type()),
238
54.9k
                       _opposite, _runtime_filter_id);
239
54.9k
        return fmt::to_string(debug_string_buffer);
240
54.9k
    }
241
242
    // evaluate predicate on IColumn
243
    // a short circuit eval way
244
42.0k
    uint16_t evaluate(const IColumn& column, uint16_t* sel, uint16_t size) const {
245
42.0k
        Defer defer([&] { try_reset_judge_selectivity(); });
246
247
42.0k
        if (always_true()) {
248
0
            update_filter_info(0, 0, size);
249
0
            return size;
250
0
        }
251
252
42.0k
        uint16_t new_size = _evaluate_inner(column, sel, size);
253
42.0k
        if (_can_ignore()) {
254
17.6k
            do_judge_selectivity(size - new_size, size);
255
17.6k
        }
256
42.0k
        update_filter_info(size - new_size, size, 0);
257
42.0k
        return new_size;
258
42.0k
    }
259
    virtual void evaluate_and(const IColumn& column, const uint16_t* sel, uint16_t size,
260
0
                              bool* flags) const {}
261
    virtual void evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
262
0
                             bool* flags) const {}
263
264
86.0k
    virtual bool support_zonemap() const { return true; }
265
266
35.9k
    virtual bool evaluate_and(const segment_v2::ZoneMap& zone_map) const { return true; }
267
268
23.6k
    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 ParquetBlockSplitBloomFilter* bf) const { return true; }
273
274
0
    virtual bool evaluate_and(const BloomFilter* bf) const { return true; }
275
276
2.02k
    virtual bool evaluate_and(const StringRef* dict_words, const size_t dict_count) const {
277
2.02k
        return true;
278
2.02k
    }
279
280
22.4k
    virtual bool can_do_bloom_filter(bool ngram) const { return false; }
281
282
    /**
283
     * Figure out whether this page is matched partially or completely.
284
     */
285
0
    virtual bool evaluate_and(ParquetPredicate::ColumnStat* statistic) const {
286
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
287
0
                        "ParquetPredicate is not supported by this predicate!");
288
0
        return true;
289
0
    }
290
291
    virtual bool evaluate_and(ParquetPredicate::CachedPageIndexStat* statistic,
292
0
                              RowRanges* row_ranges) const {
293
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
294
0
                        "ParquetPredicate is not supported by this predicate!");
295
0
        return true;
296
0
    }
297
298
    // used to evaluate pre read column in lazy materialization
299
    // now only support integer/float
300
    // a vectorized eval way
301
0
    virtual void evaluate_vec(const IColumn& column, uint16_t size, bool* flags) const {
302
0
        DCHECK(false) << "should not reach here";
303
0
    }
304
0
    virtual void evaluate_and_vec(const IColumn& column, uint16_t size, bool* flags) const {
305
0
        DCHECK(false) << "should not reach here";
306
0
    }
307
308
0
    virtual std::string get_search_str() const {
309
0
        DCHECK(false) << "should not reach here";
310
0
        return "";
311
0
    }
312
313
0
    virtual void set_page_ng_bf(std::unique_ptr<segment_v2::BloomFilter>) {
314
0
        DCHECK(false) << "should not reach here";
315
0
    }
316
33.8M
    uint32_t column_id() const { return _column_id; }
317
976
    std::string col_name() const { return _col_name; }
318
319
975
    bool opposite() const { return _opposite; }
320
321
    void attach_profile_counter(
322
            int filter_id, std::shared_ptr<RuntimeProfile::Counter> predicate_filtered_rows_counter,
323
            std::shared_ptr<RuntimeProfile::Counter> predicate_input_rows_counter,
324
            std::shared_ptr<RuntimeProfile::Counter> predicate_always_true_rows_counter,
325
92.2k
            const RuntimeFilterSelectivity& rf_selectivity) {
326
92.2k
        _runtime_filter_id = filter_id;
327
92.2k
        _rf_selectivity = rf_selectivity;
328
92.2k
        DCHECK(predicate_filtered_rows_counter != nullptr);
329
92.2k
        DCHECK(predicate_input_rows_counter != nullptr);
330
331
92.5k
        if (predicate_filtered_rows_counter != nullptr) {
332
92.5k
            _predicate_filtered_rows_counter = predicate_filtered_rows_counter;
333
92.5k
        }
334
92.3k
        if (predicate_input_rows_counter != nullptr) {
335
92.3k
            _predicate_input_rows_counter = predicate_input_rows_counter;
336
92.3k
        }
337
92.3k
        if (predicate_always_true_rows_counter != nullptr) {
338
92.3k
            _predicate_always_true_rows_counter = predicate_always_true_rows_counter;
339
92.3k
        }
340
92.2k
    }
341
342
    /// TODO: Currently we only record statistics for runtime filters, in the future we should record for all predicates
343
    void update_filter_info(int64_t filter_rows, int64_t input_rows,
344
120k
                            int64_t always_true_rows) const {
345
120k
        if (_predicate_input_rows_counter == nullptr ||
346
120k
            _predicate_filtered_rows_counter == nullptr ||
347
120k
            _predicate_always_true_rows_counter == nullptr) {
348
0
            throw Exception(INTERNAL_ERROR, "Predicate profile counters are not initialized");
349
0
        }
350
120k
        COUNTER_UPDATE(_predicate_input_rows_counter, input_rows);
351
120k
        COUNTER_UPDATE(_predicate_filtered_rows_counter, filter_rows);
352
120k
        COUNTER_UPDATE(_predicate_always_true_rows_counter, always_true_rows);
353
120k
    }
354
355
54.9k
    static std::string pred_type_string(PredicateType type) {
356
54.9k
        switch (type) {
357
29.5k
        case PredicateType::EQ:
358
29.5k
            return "eq";
359
1.83k
        case PredicateType::NE:
360
1.83k
            return "ne";
361
958
        case PredicateType::LT:
362
958
            return "lt";
363
6.36k
        case PredicateType::LE:
364
6.36k
            return "le";
365
2.03k
        case PredicateType::GT:
366
2.03k
            return "gt";
367
4.10k
        case PredicateType::GE:
368
4.10k
            return "ge";
369
2.82k
        case PredicateType::IN_LIST:
370
2.82k
            return "in";
371
138
        case PredicateType::NOT_IN_LIST:
372
138
            return "not_in";
373
338
        case PredicateType::IS_NULL:
374
338
            return "is_null";
375
491
        case PredicateType::IS_NOT_NULL:
376
491
            return "is_not_null";
377
6.18k
        case PredicateType::BF:
378
6.18k
            return "bf";
379
0
        case PredicateType::MATCH:
380
0
            return "match";
381
0
        default:
382
0
            return "unknown";
383
54.9k
        }
384
54.9k
    }
385
386
205k
    bool always_true() const { return _rf_selectivity.maybe_always_true_can_ignore(); }
387
    // Return whether the ColumnPredicate was created by a runtime filter.
388
    // If true, it was definitely created by a runtime filter.
389
    // If false, it may still have been created by a runtime filter,
390
    // as certain filters like "in filter" generate key ranges instead of ColumnPredicate.
391
    // is_runtime_filter uses _can_ignore, except for BitmapFilter,
392
    // as BitmapFilter cannot ignore data.
393
1.25M
    virtual bool is_runtime_filter() const { return _can_ignore(); }
394
395
protected:
396
1.57M
    virtual bool _can_ignore() const { return _runtime_filter_id != -1; }
397
0
    virtual uint16_t _evaluate_inner(const IColumn& column, uint16_t* sel, uint16_t size) const {
398
0
        throw Exception(INTERNAL_ERROR, "Not Implemented _evaluate_inner");
399
0
    }
400
401
413k
    void reset_judge_selectivity() const { _rf_selectivity.reset_judge_selectivity(); }
402
403
120k
    void try_reset_judge_selectivity() const {
404
120k
        if (_can_ignore()) {
405
28.2k
            _rf_selectivity.update_judge_counter();
406
28.2k
        }
407
120k
    }
408
409
28.2k
    void do_judge_selectivity(uint64_t filter_rows, uint64_t input_rows) const {
410
28.2k
        _rf_selectivity.update_judge_selectivity(_runtime_filter_id, filter_rows, input_rows,
411
28.2k
                                                 get_ignore_threshold());
412
28.2k
    }
413
414
    uint32_t _column_id;
415
    const std::string _col_name;
416
    PrimitiveType _primitive_type;
417
    // TODO: the value is only in delete condition, better be template value
418
    bool _opposite;
419
    int _runtime_filter_id = -1;
420
    // VRuntimeFilterWrapper and ColumnPredicate share the same logic,
421
    // but it's challenging to unify them, so the code is duplicated.
422
    // _judge_counter, _judge_input_rows, _judge_filter_rows, and _always_true
423
    // are variables used to implement the _always_true logic, calculated periodically
424
    // based on runtime_filter_sampling_frequency. During each period, if _always_true
425
    // is evaluated as true, the logic for always_true is applied for the rest of that period
426
    // without recalculating. At the beginning of the next period,
427
    // reset_judge_selectivity is used to reset these variables.
428
    mutable RuntimeFilterSelectivity _rf_selectivity;
429
430
    std::shared_ptr<RuntimeProfile::Counter> _predicate_filtered_rows_counter =
431
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
432
    std::shared_ptr<RuntimeProfile::Counter> _predicate_input_rows_counter =
433
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
434
    std::shared_ptr<RuntimeProfile::Counter> _predicate_always_true_rows_counter =
435
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
436
437
private:
438
716k
    ColumnPredicate(const ColumnPredicate& other) = default;
439
};
440
441
} //namespace doris