Coverage Report

Created: 2025-03-27 13:57

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