Coverage Report

Created: 2026-06-09 15:00

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/runtime_filter_expr.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
    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
178k
    static constexpr bool is_range(PredicateType type) {
163
178k
        return (type == PredicateType::LT || type == PredicateType::LE ||
164
178k
                type == PredicateType::GT || type == PredicateType::GE);
165
178k
    }
166
167
139k
    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
414
    static constexpr bool is_equal_or_list(PredicateType type) {
174
414
        return (type == PredicateType::EQ || type == PredicateType::IN_LIST);
175
414
    }
176
177
300k
    static constexpr bool is_comparison(PredicateType type) {
178
300k
        return (type == PredicateType::EQ || type == PredicateType::NE ||
179
300k
                type == PredicateType::LT || type == PredicateType::LE ||
180
300k
                type == PredicateType::GT || type == PredicateType::GE);
181
300k
    }
182
};
183
184
#define EVALUATE_BY_SELECTOR(EVALUATE_IMPL_WITH_NULL_MAP, EVALUATE_IMPL_WITHOUT_NULL_MAP) \
185
20.1k
    const bool is_dense_column = pred_col.size() == size;                                 \
186
12.0M
    for (uint16_t i = 0; i < size; i++) {                                                 \
187
12.0M
        uint16_t idx = is_dense_column ? i : sel[i];                                      \
188
12.0M
        if constexpr (is_nullable) {                                                      \
189
2.45M
            if (EVALUATE_IMPL_WITH_NULL_MAP(idx)) {                                       \
190
885k
                sel[new_size++] = idx;                                                    \
191
885k
            }                                                                             \
192
9.59M
        } else {                                                                          \
193
9.59M
            if (EVALUATE_IMPL_WITHOUT_NULL_MAP(idx)) {                                    \
194
5.34M
                sel[new_size++] = idx;                                                    \
195
5.34M
            }                                                                             \
196
9.59M
        }                                                                                 \
197
12.0M
    }
198
199
class ColumnPredicate : public std::enable_shared_from_this<ColumnPredicate> {
200
public:
201
    explicit ColumnPredicate(uint32_t column_id, std::string col_name, PrimitiveType primitive_type,
202
                             bool opposite = false)
203
500k
            : _column_id(column_id),
204
500k
              _col_name(col_name),
205
500k
              _primitive_type(primitive_type),
206
500k
              _opposite(opposite) {
207
500k
        reset_judge_selectivity();
208
500k
    }
209
983k
    ColumnPredicate(const ColumnPredicate& other, uint32_t col_id) : ColumnPredicate(other) {
210
983k
        _column_id = col_id;
211
983k
    }
212
213
1.48M
    virtual ~ColumnPredicate() = default;
214
215
    virtual PredicateType type() const = 0;
216
74.7k
    virtual PrimitiveType primitive_type() const { return _primitive_type; }
217
    virtual std::shared_ptr<ColumnPredicate> clone(uint32_t col_id) const = 0;
218
219
    //evaluate predicate on inverted
220
    virtual Status evaluate(const IndexFieldNameAndTypePair& name_with_type,
221
                            IndexIterator* iterator, uint32_t num_rows,
222
0
                            roaring::Roaring* bitmap) const {
223
0
        return Status::NotSupported(
224
0
                "Not Implemented evaluate with inverted index, please check the predicate");
225
0
    }
226
227
0
    virtual double get_ignore_threshold() const { return 0; }
228
229
    // Return the size of value set for IN/NOT IN predicates and 0 for others.
230
73.4k
    virtual std::string debug_string() const {
231
73.4k
        fmt::memory_buffer debug_string_buffer;
232
73.4k
        fmt::format_to(debug_string_buffer,
233
73.4k
                       "Column ID: {}, Data Type: {}, PredicateType: {}, opposite: {}, Runtime "
234
73.4k
                       "Filter ID: {}",
235
73.4k
                       _column_id, type_to_string(primitive_type()), pred_type_string(type()),
236
73.4k
                       _opposite, _runtime_filter_id);
237
73.4k
        return fmt::to_string(debug_string_buffer);
238
73.4k
    }
239
240
    // evaluate predicate on IColumn
241
    // a short circuit eval way
242
38.1k
    uint16_t evaluate(const IColumn& column, uint16_t* sel, uint16_t size) const {
243
38.1k
        Defer defer([&] { try_reset_judge_selectivity(); });
244
245
38.1k
        if (always_true()) {
246
251
            update_filter_info(0, 0, size);
247
251
            return size;
248
251
        }
249
250
37.9k
        uint16_t new_size = _evaluate_inner(column, sel, size);
251
37.9k
        if (_can_ignore()) {
252
19.1k
            do_judge_selectivity(size - new_size, size);
253
19.1k
        }
254
37.9k
        update_filter_info(size - new_size, size, 0);
255
37.9k
        return new_size;
256
38.1k
    }
257
    virtual void evaluate_and(const IColumn& column, const uint16_t* sel, uint16_t size,
258
0
                              bool* flags) const {}
259
    virtual void evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
260
0
                             bool* flags) const {}
261
262
134k
    virtual bool support_zonemap() const { return true; }
263
264
27.8k
    virtual bool evaluate_and(const segment_v2::ZoneMap& zone_map) const { return true; }
265
266
20.8k
    virtual bool is_always_true(const segment_v2::ZoneMap& zone_map) const { return false; }
267
268
0
    virtual bool evaluate_del(const segment_v2::ZoneMap& zone_map) const { return false; }
269
270
0
    virtual bool evaluate_and(const ParquetBlockSplitBloomFilter* bf) const { return true; }
271
272
0
    virtual bool evaluate_and(const BloomFilter* bf) const { return true; }
273
274
2.34k
    virtual bool evaluate_and(const StringRef* dict_words, const size_t dict_count) const {
275
2.34k
        return true;
276
2.34k
    }
277
278
18.3k
    virtual bool can_do_bloom_filter(bool ngram) const { return false; }
279
280
    /**
281
     * Figure out whether this page is matched partially or completely.
282
     */
283
0
    virtual bool evaluate_and(ParquetPredicate::ColumnStat* statistic) const {
284
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
285
0
                        "ParquetPredicate is not supported by this predicate!");
286
0
        return true;
287
0
    }
288
289
    virtual bool evaluate_and(ParquetPredicate::CachedPageIndexStat* statistic,
290
0
                              RowRanges* row_ranges) const {
291
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
292
0
                        "ParquetPredicate is not supported by this predicate!");
293
0
        return true;
294
0
    }
295
296
    // used to evaluate pre read column in lazy materialization
297
    // now only support integer/float
298
    // a vectorized eval way
299
0
    virtual void evaluate_vec(const IColumn& column, uint16_t size, bool* flags) const {
300
0
        DCHECK(false) << "should not reach here";
301
0
    }
302
0
    virtual void evaluate_and_vec(const IColumn& column, uint16_t size, bool* flags) const {
303
0
        DCHECK(false) << "should not reach here";
304
0
    }
305
306
0
    virtual std::string get_search_str() const {
307
0
        DCHECK(false) << "should not reach here";
308
0
        return "";
309
0
    }
310
311
0
    virtual void set_page_ng_bf(std::unique_ptr<segment_v2::BloomFilter>) {
312
0
        DCHECK(false) << "should not reach here";
313
0
    }
314
54.8M
    uint32_t column_id() const { return _column_id; }
315
31.2k
    std::string col_name() const { return _col_name; }
316
317
303k
    bool opposite() const { return _opposite; }
318
319
    void attach_profile_counter(
320
            int filter_id, std::shared_ptr<RuntimeProfile::Counter> predicate_filtered_rows_counter,
321
            std::shared_ptr<RuntimeProfile::Counter> predicate_input_rows_counter,
322
            std::shared_ptr<RuntimeProfile::Counter> predicate_always_true_rows_counter,
323
16.6k
            const RuntimeFilterSelectivity& rf_selectivity) {
324
16.6k
        _runtime_filter_id = filter_id;
325
16.6k
        _rf_selectivity = rf_selectivity;
326
16.6k
        DCHECK(predicate_filtered_rows_counter != nullptr);
327
16.6k
        DCHECK(predicate_input_rows_counter != nullptr);
328
329
16.6k
        if (predicate_filtered_rows_counter != nullptr) {
330
16.6k
            _predicate_filtered_rows_counter = predicate_filtered_rows_counter;
331
16.6k
        }
332
16.6k
        if (predicate_input_rows_counter != nullptr) {
333
16.6k
            _predicate_input_rows_counter = predicate_input_rows_counter;
334
16.6k
        }
335
16.6k
        if (predicate_always_true_rows_counter != nullptr) {
336
16.6k
            _predicate_always_true_rows_counter = predicate_always_true_rows_counter;
337
16.6k
        }
338
16.6k
    }
339
340
    /// TODO: Currently we only record statistics for runtime filters, in the future we should record for all predicates
341
    void update_filter_info(int64_t filter_rows, int64_t input_rows,
342
167k
                            int64_t always_true_rows) const {
343
167k
        if (_predicate_input_rows_counter == nullptr ||
344
167k
            _predicate_filtered_rows_counter == nullptr ||
345
167k
            _predicate_always_true_rows_counter == nullptr) {
346
0
            throw Exception(INTERNAL_ERROR, "Predicate profile counters are not initialized");
347
0
        }
348
167k
        COUNTER_UPDATE(_predicate_input_rows_counter, input_rows);
349
167k
        COUNTER_UPDATE(_predicate_filtered_rows_counter, filter_rows);
350
167k
        COUNTER_UPDATE(_predicate_always_true_rows_counter, always_true_rows);
351
167k
    }
352
353
73.4k
    static std::string pred_type_string(PredicateType type) {
354
73.4k
        switch (type) {
355
47.8k
        case PredicateType::EQ:
356
47.8k
            return "eq";
357
1.81k
        case PredicateType::NE:
358
1.81k
            return "ne";
359
819
        case PredicateType::LT:
360
819
            return "lt";
361
5.92k
        case PredicateType::LE:
362
5.92k
            return "le";
363
1.88k
        case PredicateType::GT:
364
1.88k
            return "gt";
365
4.93k
        case PredicateType::GE:
366
4.93k
            return "ge";
367
2.68k
        case PredicateType::IN_LIST:
368
2.68k
            return "in";
369
135
        case PredicateType::NOT_IN_LIST:
370
135
            return "not_in";
371
409
        case PredicateType::IS_NULL:
372
409
            return "is_null";
373
643
        case PredicateType::IS_NOT_NULL:
374
643
            return "is_not_null";
375
6.14k
        case PredicateType::BF:
376
6.14k
            return "bf";
377
0
        case PredicateType::MATCH:
378
0
            return "match";
379
0
        default:
380
0
            return "unknown";
381
73.4k
        }
382
73.4k
    }
383
384
299k
    bool always_true() const { return _rf_selectivity.maybe_always_true_can_ignore(); }
385
    // Return whether the ColumnPredicate was created by a runtime filter.
386
    // If true, it was definitely created by a runtime filter.
387
    // If false, it may still have been created by a runtime filter,
388
    // as certain filters like "in filter" generate key ranges instead of ColumnPredicate.
389
1.76M
    virtual bool is_runtime_filter() const { return _can_ignore(); }
390
391
protected:
392
2.22M
    virtual bool _can_ignore() const { return _runtime_filter_id != -1; }
393
0
    virtual uint16_t _evaluate_inner(const IColumn& column, uint16_t* sel, uint16_t size) const {
394
0
        throw Exception(INTERNAL_ERROR, "Not Implemented _evaluate_inner");
395
0
    }
396
397
500k
    void reset_judge_selectivity() const { _rf_selectivity.reset_judge_selectivity(); }
398
399
166k
    void try_reset_judge_selectivity() const {
400
166k
        if (_can_ignore()) {
401
28.5k
            _rf_selectivity.update_judge_counter();
402
28.5k
        }
403
166k
    }
404
405
28.2k
    void do_judge_selectivity(uint64_t filter_rows, uint64_t input_rows) const {
406
28.2k
        _rf_selectivity.update_judge_selectivity(_runtime_filter_id, filter_rows, input_rows,
407
28.2k
                                                 get_ignore_threshold());
408
28.2k
    }
409
410
    uint32_t _column_id;
411
    const std::string _col_name;
412
    PrimitiveType _primitive_type;
413
    // TODO: the value is only in delete condition, better be template value
414
    bool _opposite;
415
    int _runtime_filter_id = -1;
416
    // RuntimeFilterExpr and ColumnPredicate share the same logic,
417
    // but it's challenging to unify them, so the code is duplicated.
418
    // _judge_counter, _judge_input_rows, _judge_filter_rows, and _always_true
419
    // are variables used to implement the _always_true logic, calculated periodically
420
    // based on runtime_filter_sampling_frequency. During each period, if _always_true
421
    // is evaluated as true, the logic for always_true is applied for the rest of that period
422
    // without recalculating. At the beginning of the next period,
423
    // reset_judge_selectivity is used to reset these variables.
424
    mutable RuntimeFilterSelectivity _rf_selectivity;
425
426
    std::shared_ptr<RuntimeProfile::Counter> _predicate_filtered_rows_counter =
427
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
428
    std::shared_ptr<RuntimeProfile::Counter> _predicate_input_rows_counter =
429
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
430
    std::shared_ptr<RuntimeProfile::Counter> _predicate_always_true_rows_counter =
431
            std::make_shared<RuntimeProfile::Counter>(TUnit::UNIT, 0);
432
433
private:
434
983k
    ColumnPredicate(const ColumnPredicate& other) = default;
435
};
436
437
} //namespace doris