Coverage Report

Created: 2025-12-27 03:15

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