Coverage Report

Created: 2026-11-05 15:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/predicate/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 "core/column/column_dictionary.h"
25
#include "storage/index/bloom_filter/bloom_filter.h"
26
#include "storage/index/inverted/inverted_index_reader.h"
27
#include "storage/predicate/column_predicate.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
654
            : ColumnPredicate(column_id, col_name, PrimitiveType::INVALID_TYPE),
40
654
              _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
0
              _nested(other._nested ? other._nested->clone(column_id) : nullptr) {}
46
654
    ~SharedPredicate() override = default;
47
2.07k
    std::string debug_string() const override {
48
2.07k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
49
2.07k
        fmt::memory_buffer debug_string_buffer;
50
2.07k
        fmt::format_to(debug_string_buffer, "SharedPredicate({}, nested={})",
51
2.07k
                       ColumnPredicate::debug_string(), _nested ? _nested->debug_string() : "null");
52
2.07k
        return fmt::to_string(debug_string_buffer);
53
2.07k
    }
54
3.22k
    std::shared_ptr<ColumnPredicate> clone(uint32_t column_id) const override {
55
        // All scanner thread should share the same SharedPredicate object.
56
3.22k
        return std::const_pointer_cast<ColumnPredicate>(shared_from_this());
57
3.22k
    }
58
59
13.6k
    PredicateType type() const override {
60
13.6k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
61
13.6k
        if (!_nested) {
62
            // topn filter is le or ge
63
6.29k
            return PredicateType::LE;
64
6.29k
        }
65
7.32k
        return _nested->type();
66
13.6k
    }
67
2.07k
    PrimitiveType primitive_type() const override {
68
2.07k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
69
2.07k
        if (!_nested) {
70
452
            return PrimitiveType::INVALID_TYPE;
71
452
        }
72
1.61k
        return _nested->primitive_type();
73
2.07k
    }
74
75
2.43k
    void set_nested(const std::shared_ptr<ColumnPredicate>& nested) {
76
2.43k
        std::unique_lock<std::shared_mutex> lock(*_mtx);
77
2.43k
        _nested = nested;
78
2.43k
    }
79
80
    // Only reset between executions, after the old scanners have finished.
81
4
    void reset_for_new_execution() {
82
4
        std::unique_lock<std::shared_mutex> lock(*_mtx);
83
4
        _nested.reset();
84
4
    }
85
86
    Status evaluate(const IndexFieldNameAndTypePair& name_with_type, IndexIterator* iterator,
87
0
                    uint32_t num_rows, roaring::Roaring* bitmap) const override {
88
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
89
0
        if (!_nested) {
90
0
            return Status::OK();
91
0
        }
92
0
        return _nested->evaluate(name_with_type, iterator, num_rows, bitmap);
93
0
    }
94
95
    void evaluate_and(const IColumn& column, const uint16_t* sel, uint16_t size,
96
0
                      bool* flags) const override {
97
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
98
0
        if (!_nested) {
99
0
            return;
100
0
        }
101
0
        return _nested->evaluate_and(column, sel, size, flags);
102
0
    }
103
104
    void evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
105
0
                     bool* flags) const override {
106
0
        DCHECK(false) << "should not reach here";
107
0
    }
108
109
14.8k
    bool evaluate_and(const segment_v2::ZoneMap& zone_map) const override {
110
14.8k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
111
14.8k
        if (!_nested) {
112
5.40k
            return ColumnPredicate::evaluate_and(zone_map);
113
5.40k
        }
114
9.48k
        return _nested->evaluate_and(zone_map);
115
14.8k
    }
116
117
0
    bool evaluate_del(const segment_v2::ZoneMap& zone_map) const override {
118
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
119
0
        if (!_nested) {
120
0
            return ColumnPredicate::evaluate_del(zone_map);
121
0
        }
122
0
        return _nested->evaluate_del(zone_map);
123
0
    }
124
125
0
    bool evaluate_and(const BloomFilter* bf) const override {
126
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
127
0
        if (!_nested) {
128
0
            return ColumnPredicate::evaluate_and(bf);
129
0
        }
130
0
        return _nested->evaluate_and(bf);
131
0
    }
132
133
4.96k
    bool can_do_bloom_filter(bool ngram) const override {
134
4.96k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
135
4.96k
        if (!_nested) {
136
2.84k
            return ColumnPredicate::can_do_bloom_filter(ngram);
137
2.84k
        }
138
2.12k
        return _nested->can_do_bloom_filter(ngram);
139
4.96k
    }
140
141
1.97k
    void evaluate_vec(const IColumn& column, uint16_t size, bool* flags) const override {
142
1.97k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
143
1.97k
        if (!_nested) {
144
2.46M
            for (uint16_t i = 0; i < size; ++i) {
145
2.46M
                flags[i] = true;
146
2.46M
            }
147
883
            return;
148
883
        }
149
1.08k
        _nested->evaluate_vec(column, size, flags);
150
1.08k
    }
151
152
1.40k
    void evaluate_and_vec(const IColumn& column, uint16_t size, bool* flags) const override {
153
1.40k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
154
1.40k
        if (!_nested) {
155
526
            return;
156
526
        }
157
881
        _nested->evaluate_and_vec(column, size, flags);
158
881
    }
159
160
0
    std::string get_search_str() const override {
161
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
162
0
        if (!_nested) {
163
0
            DCHECK(false) << "should not reach here";
164
0
        }
165
0
        return _nested->get_search_str();
166
0
    }
167
168
0
    bool evaluate_and(ParquetPredicate::ColumnStat* statistic) const override {
169
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
170
0
        if (!_nested) {
171
            // at the begining _nested will be null, so return true.
172
0
            return true;
173
0
        }
174
0
        return _nested->evaluate_and(statistic);
175
0
    }
176
177
    bool evaluate_and(ParquetPredicate::CachedPageIndexStat* statistic,
178
0
                      RowRanges* row_ranges) const override {
179
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
180
181
0
        if (!_nested) {
182
            // at the begining _nested will be null, so return true.
183
0
            row_ranges->add(statistic->row_group_range);
184
0
            return true;
185
0
        }
186
0
        return _nested->evaluate_and(statistic, row_ranges);
187
0
    }
188
189
private:
190
340
    uint16_t _evaluate_inner(const IColumn& column, uint16_t* sel, uint16_t size) const override {
191
340
        std::shared_lock<std::shared_mutex> lock(*_mtx);
192
340
        if (!_nested) {
193
109
            return size;
194
109
        }
195
231
        return _nested->evaluate(column, sel, size);
196
340
    }
197
198
    mutable std::shared_ptr<std::shared_mutex> _mtx;
199
    std::shared_ptr<ColumnPredicate> _nested;
200
};
201
202
} //namespace doris