Coverage Report

Created: 2024-11-18 11:49

/root/doris/be/src/olap/shared_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 <cstdint>
21
#include <memory>
22
23
#include "common/factory_creator.h"
24
#include "olap/column_predicate.h"
25
#include "olap/rowset/segment_v2/bloom_filter.h"
26
#include "olap/rowset/segment_v2/inverted_index_reader.h"
27
#include "olap/wrapper_field.h"
28
#include "vec/columns/column_dictionary.h"
29
30
namespace doris {
31
32
// SharedPredicate only used on topn runtime predicate.
33
// Runtime predicate globally share one predicate, to ensure that updates can be real-time.
34
// At the beginning nested predicate may be nullptr, in which case predicate always returns true.
35
class SharedPredicate : public ColumnPredicate {
36
    ENABLE_FACTORY_CREATOR(SharedPredicate);
37
38
public:
39
0
    SharedPredicate(uint32_t column_id) : ColumnPredicate(column_id) {}
40
41
0
    PredicateType type() const override {
42
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
43
0
        if (!_nested) {
44
            // topn filter is le or ge
45
0
            return PredicateType::LE;
46
0
        }
47
0
        return _nested->type();
48
0
    }
49
50
0
    void set_nested(ColumnPredicate* nested) {
51
0
        std::unique_lock<std::shared_mutex> lock(_mtx);
52
0
        _nested.reset(nested);
53
0
    }
54
55
    Status evaluate(BitmapIndexIterator* iterator, uint32_t num_rows,
56
0
                    roaring::Roaring* roaring) const override {
57
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
58
0
        if (!_nested) {
59
0
            return Status::OK();
60
0
        }
61
0
        return _nested->evaluate(iterator, num_rows, roaring);
62
0
    }
63
64
    Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type,
65
                    InvertedIndexIterator* iterator, uint32_t num_rows,
66
0
                    roaring::Roaring* bitmap) const override {
67
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
68
0
        if (!_nested) {
69
0
            return Status::OK();
70
0
        }
71
0
        return _nested->evaluate(name_with_type, iterator, num_rows, bitmap);
72
0
    }
73
74
0
    bool can_do_apply_safely(PrimitiveType input_type, bool is_null) const override {
75
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
76
0
        if (!_nested) {
77
0
            return true;
78
0
        }
79
0
        return _nested->can_do_apply_safely(input_type, is_null);
80
0
    }
81
82
    void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
83
0
                      bool* flags) const override {
84
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
85
0
        if (!_nested) {
86
0
            return;
87
0
        }
88
0
        return _nested->evaluate_and(column, sel, size, flags);
89
0
    }
90
91
    void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
92
0
                     bool* flags) const override {
93
0
        DCHECK(false) << "should not reach here";
94
0
    }
95
96
0
    bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const override {
97
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
98
0
        if (!_nested) {
99
0
            return ColumnPredicate::evaluate_and(statistic);
100
0
        }
101
0
        return _nested->evaluate_and(statistic);
102
0
    }
103
104
0
    bool evaluate_del(const std::pair<WrapperField*, WrapperField*>& statistic) const override {
105
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
106
0
        if (!_nested) {
107
0
            return ColumnPredicate::evaluate_del(statistic);
108
0
        }
109
0
        return _nested->evaluate_del(statistic);
110
0
    }
111
112
0
    bool evaluate_and(const BloomFilter* bf) const override {
113
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
114
0
        if (!_nested) {
115
0
            return ColumnPredicate::evaluate_and(bf);
116
0
        }
117
0
        return _nested->evaluate_and(bf);
118
0
    }
119
120
0
    bool can_do_bloom_filter(bool ngram) const override {
121
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
122
0
        if (!_nested) {
123
0
            return ColumnPredicate::can_do_bloom_filter(ngram);
124
0
        }
125
0
        return _nested->can_do_bloom_filter(ngram);
126
0
    }
127
128
    void evaluate_vec(const vectorized::IColumn& column, uint16_t size,
129
0
                      bool* flags) const override {
130
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
131
0
        if (!_nested) {
132
0
            for (uint16_t i = 0; i < size; ++i) {
133
0
                flags[i] = true;
134
0
            }
135
0
            return;
136
0
        }
137
0
        _nested->evaluate_vec(column, size, flags);
138
0
    }
139
140
    void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size,
141
0
                          bool* flags) const override {
142
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
143
0
        if (!_nested) {
144
0
            return;
145
0
        }
146
0
        _nested->evaluate_and_vec(column, size, flags);
147
0
    }
148
149
0
    std::string get_search_str() const override {
150
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
151
0
        if (!_nested) {
152
0
            DCHECK(false) << "should not reach here";
153
0
        }
154
0
        return _nested->get_search_str();
155
0
    }
156
157
private:
158
    uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel,
159
0
                             uint16_t size) const override {
160
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
161
0
        if (!_nested) {
162
0
            return size;
163
0
        }
164
0
        return _nested->evaluate(column, sel, size);
165
0
    }
166
167
0
    std::string _debug_string() const override {
168
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
169
0
        if (!_nested) {
170
0
            return "shared_predicate(unknow)";
171
0
        }
172
0
        return "shared_predicate(" + _nested->debug_string() + ")";
173
0
    }
174
175
    mutable std::shared_mutex _mtx;
176
    std::shared_ptr<ColumnPredicate> _nested;
177
};
178
179
} //namespace doris