Coverage Report

Created: 2025-09-17 00:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/shared_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 <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
                    IndexIterator* 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
    void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
75
0
                      bool* flags) const override {
76
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
77
0
        if (!_nested) {
78
0
            return;
79
0
        }
80
0
        return _nested->evaluate_and(column, sel, size, flags);
81
0
    }
82
83
    void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
84
0
                     bool* flags) const override {
85
0
        DCHECK(false) << "should not reach here";
86
0
    }
87
88
0
    bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const override {
89
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
90
0
        if (!_nested) {
91
0
            return ColumnPredicate::evaluate_and(statistic);
92
0
        }
93
0
        return _nested->evaluate_and(statistic);
94
0
    }
95
96
0
    bool evaluate_del(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_del(statistic);
100
0
        }
101
0
        return _nested->evaluate_del(statistic);
102
0
    }
103
104
0
    bool evaluate_and(const BloomFilter* bf) const override {
105
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
106
0
        if (!_nested) {
107
0
            return ColumnPredicate::evaluate_and(bf);
108
0
        }
109
0
        return _nested->evaluate_and(bf);
110
0
    }
111
112
0
    bool can_do_bloom_filter(bool ngram) const override {
113
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
114
0
        if (!_nested) {
115
0
            return ColumnPredicate::can_do_bloom_filter(ngram);
116
0
        }
117
0
        return _nested->can_do_bloom_filter(ngram);
118
0
    }
119
120
    void evaluate_vec(const vectorized::IColumn& column, uint16_t size,
121
0
                      bool* flags) const override {
122
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
123
0
        if (!_nested) {
124
0
            for (uint16_t i = 0; i < size; ++i) {
125
0
                flags[i] = true;
126
0
            }
127
0
            return;
128
0
        }
129
0
        _nested->evaluate_vec(column, size, flags);
130
0
    }
131
132
    void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size,
133
0
                          bool* flags) const override {
134
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
135
0
        if (!_nested) {
136
0
            return;
137
0
        }
138
0
        _nested->evaluate_and_vec(column, size, flags);
139
0
    }
140
141
0
    std::string get_search_str() const override {
142
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
143
0
        if (!_nested) {
144
0
            DCHECK(false) << "should not reach here";
145
0
        }
146
0
        return _nested->get_search_str();
147
0
    }
148
149
private:
150
    uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel,
151
0
                             uint16_t size) const override {
152
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
153
0
        if (!_nested) {
154
0
            return size;
155
0
        }
156
0
        return _nested->evaluate(column, sel, size);
157
0
    }
158
159
0
    std::string _debug_string() const override {
160
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
161
0
        if (!_nested) {
162
0
            return "shared_predicate(unknow)";
163
0
        }
164
0
        return "shared_predicate(" + _nested->debug_string() + ")";
165
0
    }
166
167
    mutable std::shared_mutex _mtx;
168
    std::shared_ptr<ColumnPredicate> _nested;
169
};
170
171
} //namespace doris