Coverage Report

Created: 2026-02-09 16:34

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 "vec/columns/column_dictionary.h"
28
29
namespace doris {
30
31
// SharedPredicate only used on topn runtime predicate.
32
// Runtime predicate globally share one predicate, to ensure that updates can be real-time.
33
// At the beginning nested predicate may be nullptr, in which case predicate always returns true.
34
class SharedPredicate final : public ColumnPredicate {
35
    ENABLE_FACTORY_CREATOR(SharedPredicate);
36
37
public:
38
    SharedPredicate(uint32_t column_id, std::string col_name)
39
0
            : ColumnPredicate(column_id, col_name, PrimitiveType::INVALID_TYPE),
40
0
              _mtx(std::make_shared<std::shared_mutex>()) {}
41
    SharedPredicate(const ColumnPredicate& other) = delete;
42
    SharedPredicate(const SharedPredicate& other, uint32_t column_id)
43
            : ColumnPredicate(other, column_id),
44
              _mtx(std::make_shared<std::shared_mutex>()),
45
              _nested(assert_cast<const SharedPredicate&>(other)._nested
46
                              ? other._nested->clone(column_id)
47
0
                              : nullptr) {}
48
0
    ~SharedPredicate() override = default;
49
0
    std::string debug_string() const override {
50
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
51
0
        fmt::memory_buffer debug_string_buffer;
52
0
        fmt::format_to(debug_string_buffer, "SharedPredicate({}, nested={})",
53
0
                       ColumnPredicate::debug_string(), _nested ? _nested->debug_string() : "null");
54
0
        return fmt::to_string(debug_string_buffer);
55
0
    }
56
0
    std::shared_ptr<ColumnPredicate> clone(uint32_t column_id) const override {
57
        // All scanner thread should share the same SharedPredicate object.
58
0
        return std::const_pointer_cast<ColumnPredicate>(shared_from_this());
59
0
    }
60
61
0
    PredicateType type() const override {
62
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
63
0
        if (!_nested) {
64
            // topn filter is le or ge
65
0
            return PredicateType::LE;
66
0
        }
67
0
        return _nested->type();
68
0
    }
69
0
    PrimitiveType primitive_type() const override {
70
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
71
0
        if (!_nested) {
72
0
            return PrimitiveType::INVALID_TYPE;
73
0
        }
74
0
        return _nested->primitive_type();
75
0
    }
76
77
0
    void set_nested(const std::shared_ptr<ColumnPredicate>& nested) {
78
0
        std::unique_lock<std::shared_mutex> lock(*_mtx);
79
0
        _nested = nested;
80
0
    }
81
82
    Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type,
83
                    IndexIterator* iterator, uint32_t num_rows,
84
0
                    roaring::Roaring* bitmap) const override {
85
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
86
0
        if (!_nested) {
87
0
            return Status::OK();
88
0
        }
89
0
        return _nested->evaluate(name_with_type, iterator, num_rows, bitmap);
90
0
    }
91
92
    void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
93
0
                      bool* flags) const override {
94
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
95
0
        if (!_nested) {
96
0
            return;
97
0
        }
98
0
        return _nested->evaluate_and(column, sel, size, flags);
99
0
    }
100
101
    void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size,
102
0
                     bool* flags) const override {
103
0
        DCHECK(false) << "should not reach here";
104
0
    }
105
106
0
    bool evaluate_and(const ZoneMapInfo& zone_map_info) const override {
107
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
108
0
        if (!_nested) {
109
0
            return ColumnPredicate::evaluate_and(zone_map_info);
110
0
        }
111
0
        return _nested->evaluate_and(zone_map_info);
112
0
    }
113
114
0
    bool evaluate_del(const ZoneMapInfo& zone_map_info) const override {
115
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
116
0
        if (!_nested) {
117
0
            return ColumnPredicate::evaluate_del(zone_map_info);
118
0
        }
119
0
        return _nested->evaluate_del(zone_map_info);
120
0
    }
121
122
0
    bool evaluate_and(const BloomFilter* bf) const override {
123
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
124
0
        if (!_nested) {
125
0
            return ColumnPredicate::evaluate_and(bf);
126
0
        }
127
0
        return _nested->evaluate_and(bf);
128
0
    }
129
130
0
    bool can_do_bloom_filter(bool ngram) const override {
131
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
132
0
        if (!_nested) {
133
0
            return ColumnPredicate::can_do_bloom_filter(ngram);
134
0
        }
135
0
        return _nested->can_do_bloom_filter(ngram);
136
0
    }
137
138
    void evaluate_vec(const vectorized::IColumn& column, uint16_t size,
139
0
                      bool* flags) const override {
140
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
141
0
        if (!_nested) {
142
0
            for (uint16_t i = 0; i < size; ++i) {
143
0
                flags[i] = true;
144
0
            }
145
0
            return;
146
0
        }
147
0
        _nested->evaluate_vec(column, size, flags);
148
0
    }
149
150
    void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size,
151
0
                          bool* flags) const override {
152
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
153
0
        if (!_nested) {
154
0
            return;
155
0
        }
156
0
        _nested->evaluate_and_vec(column, size, flags);
157
0
    }
158
159
0
    std::string get_search_str() const override {
160
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
161
0
        if (!_nested) {
162
0
            DCHECK(false) << "should not reach here";
163
0
        }
164
0
        return _nested->get_search_str();
165
0
    }
166
167
0
    bool evaluate_and(vectorized::ParquetPredicate::ColumnStat* statistic) const override {
168
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
169
0
        if (!_nested) {
170
            // at the begining _nested will be null, so return true.
171
0
            return true;
172
0
        }
173
0
        return _nested->evaluate_and(statistic);
174
0
    }
175
176
    bool evaluate_and(vectorized::ParquetPredicate::CachedPageIndexStat* statistic,
177
0
                      RowRanges* row_ranges) const override {
178
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
179
180
0
        if (!_nested) {
181
            // at the begining _nested will be null, so return true.
182
0
            row_ranges->add(statistic->row_group_range);
183
0
            return true;
184
0
        }
185
0
        return _nested->evaluate_and(statistic, row_ranges);
186
0
    }
187
188
private:
189
    uint16_t _evaluate_inner(const vectorized::IColumn& column, uint16_t* sel,
190
0
                             uint16_t size) const override {
191
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
192
0
        if (!_nested) {
193
0
            return size;
194
0
        }
195
0
        return _nested->evaluate(column, sel, size);
196
0
    }
197
198
    mutable std::shared_ptr<std::shared_mutex> _mtx;
199
    std::shared_ptr<ColumnPredicate> _nested;
200
};
201
202
} //namespace doris