Coverage Report

Created: 2024-11-22 11:49

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