Coverage Report

Created: 2025-12-05 19:50

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