Coverage Report

Created: 2025-12-31 20:40

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(const vectorized::IndexFieldNameAndTypePair& name_with_type,
56
                    IndexIterator* iterator, uint32_t num_rows,
57
0
                    roaring::Roaring* bitmap) const override {
58
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
59
0
        if (!_nested) {
60
0
            return Status::OK();
61
0
        }
62
0
        return _nested->evaluate(name_with_type, iterator, num_rows, bitmap);
63
0
    }
64
65
    void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
66
0
                      bool* flags) const override {
67
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
68
0
        if (!_nested) {
69
0
            return;
70
0
        }
71
0
        return _nested->evaluate_and(column, sel, size, flags);
72
0
    }
73
74
    void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
75
0
                     bool* flags) const override {
76
0
        DCHECK(false) << "should not reach here";
77
0
    }
78
79
0
    bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const override {
80
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
81
0
        if (!_nested) {
82
0
            return ColumnPredicate::evaluate_and(statistic);
83
0
        }
84
0
        return _nested->evaluate_and(statistic);
85
0
    }
86
87
0
    bool evaluate_del(const std::pair<WrapperField*, WrapperField*>& statistic) const override {
88
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
89
0
        if (!_nested) {
90
0
            return ColumnPredicate::evaluate_del(statistic);
91
0
        }
92
0
        return _nested->evaluate_del(statistic);
93
0
    }
94
95
0
    bool evaluate_and(const BloomFilter* bf) const override {
96
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
97
0
        if (!_nested) {
98
0
            return ColumnPredicate::evaluate_and(bf);
99
0
        }
100
0
        return _nested->evaluate_and(bf);
101
0
    }
102
103
0
    bool can_do_bloom_filter(bool ngram) const override {
104
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
105
0
        if (!_nested) {
106
0
            return ColumnPredicate::can_do_bloom_filter(ngram);
107
0
        }
108
0
        return _nested->can_do_bloom_filter(ngram);
109
0
    }
110
111
    void evaluate_vec(const vectorized::IColumn& column, uint16_t size,
112
0
                      bool* flags) const override {
113
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
114
0
        if (!_nested) {
115
0
            for (uint16_t i = 0; i < size; ++i) {
116
0
                flags[i] = true;
117
0
            }
118
0
            return;
119
0
        }
120
0
        _nested->evaluate_vec(column, size, flags);
121
0
    }
122
123
    void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size,
124
0
                          bool* flags) const override {
125
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
126
0
        if (!_nested) {
127
0
            return;
128
0
        }
129
0
        _nested->evaluate_and_vec(column, size, flags);
130
0
    }
131
132
0
    std::string get_search_str() const override {
133
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
134
0
        if (!_nested) {
135
0
            DCHECK(false) << "should not reach here";
136
0
        }
137
0
        return _nested->get_search_str();
138
0
    }
139
140
private:
141
    uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel,
142
0
                             uint16_t size) const override {
143
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
144
0
        if (!_nested) {
145
0
            return size;
146
0
        }
147
0
        return _nested->evaluate(column, sel, size);
148
0
    }
149
150
0
    std::string _debug_string() const override {
151
0
        std::shared_lock<std::shared_mutex> lock(_mtx);
152
0
        if (!_nested) {
153
0
            return "shared_predicate(unknow)";
154
0
        }
155
0
        return "shared_predicate(" + _nested->debug_string() + ")";
156
0
    }
157
158
    mutable std::shared_mutex _mtx;
159
    std::shared_ptr<ColumnPredicate> _nested;
160
};
161
162
} //namespace doris