Coverage Report

Created: 2026-03-13 09:37

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
2.13k
            : ColumnPredicate(column_id, col_name, PrimitiveType::INVALID_TYPE),
40
2.13k
              _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
2.13k
    ~SharedPredicate() override = default;
49
3.01k
    std::string debug_string() const override {
50
3.01k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
51
3.01k
        fmt::memory_buffer debug_string_buffer;
52
3.01k
        fmt::format_to(debug_string_buffer, "SharedPredicate({}, nested={})",
53
3.01k
                       ColumnPredicate::debug_string(), _nested ? _nested->debug_string() : "null");
54
3.01k
        return fmt::to_string(debug_string_buffer);
55
3.01k
    }
56
9.76k
    std::shared_ptr<ColumnPredicate> clone(uint32_t column_id) const override {
57
        // All scanner thread should share the same SharedPredicate object.
58
9.76k
        return std::const_pointer_cast<ColumnPredicate>(shared_from_this());
59
9.76k
    }
60
61
15.3k
    PredicateType type() const override {
62
15.3k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
63
15.3k
        if (!_nested) {
64
            // topn filter is le or ge
65
7.77k
            return PredicateType::LE;
66
7.77k
        }
67
7.53k
        return _nested->type();
68
15.3k
    }
69
3.01k
    PrimitiveType primitive_type() const override {
70
3.01k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
71
3.01k
        if (!_nested) {
72
1.29k
            return PrimitiveType::INVALID_TYPE;
73
1.29k
        }
74
1.71k
        return _nested->primitive_type();
75
3.01k
    }
76
77
3.76k
    void set_nested(const std::shared_ptr<ColumnPredicate>& nested) {
78
3.76k
        std::unique_lock<std::shared_mutex> lock(*_mtx);
79
3.76k
        _nested = nested;
80
3.76k
    }
81
82
    Status evaluate(const IndexFieldNameAndTypePair& name_with_type, IndexIterator* iterator,
83
0
                    uint32_t num_rows, roaring::Roaring* bitmap) const override {
84
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
85
0
        if (!_nested) {
86
0
            return Status::OK();
87
0
        }
88
0
        return _nested->evaluate(name_with_type, iterator, num_rows, bitmap);
89
0
    }
90
91
    void evaluate_and(const IColumn& column, const uint16_t* sel, uint16_t size,
92
0
                      bool* flags) const override {
93
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
94
0
        if (!_nested) {
95
0
            return;
96
0
        }
97
0
        return _nested->evaluate_and(column, sel, size, flags);
98
0
    }
99
100
    void evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
101
0
                     bool* flags) const override {
102
0
        DCHECK(false) << "should not reach here";
103
0
    }
104
105
21.6k
    bool evaluate_and(const segment_v2::ZoneMap& zone_map) const override {
106
21.6k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
107
21.6k
        if (!_nested) {
108
9.88k
            return ColumnPredicate::evaluate_and(zone_map);
109
9.88k
        }
110
11.7k
        return _nested->evaluate_and(zone_map);
111
21.6k
    }
112
113
0
    bool evaluate_del(const segment_v2::ZoneMap& zone_map) const override {
114
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
115
0
        if (!_nested) {
116
0
            return ColumnPredicate::evaluate_del(zone_map);
117
0
        }
118
0
        return _nested->evaluate_del(zone_map);
119
0
    }
120
121
0
    bool evaluate_and(const BloomFilter* bf) const override {
122
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
123
0
        if (!_nested) {
124
0
            return ColumnPredicate::evaluate_and(bf);
125
0
        }
126
0
        return _nested->evaluate_and(bf);
127
0
    }
128
129
12.4k
    bool can_do_bloom_filter(bool ngram) const override {
130
12.4k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
131
12.4k
        if (!_nested) {
132
6.18k
            return ColumnPredicate::can_do_bloom_filter(ngram);
133
6.18k
        }
134
6.26k
        return _nested->can_do_bloom_filter(ngram);
135
12.4k
    }
136
137
4.25k
    void evaluate_vec(const IColumn& column, uint16_t size, bool* flags) const override {
138
4.25k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
139
4.25k
        if (!_nested) {
140
10.3M
            for (uint16_t i = 0; i < size; ++i) {
141
10.3M
                flags[i] = true;
142
10.3M
            }
143
2.55k
            return;
144
2.55k
        }
145
1.69k
        _nested->evaluate_vec(column, size, flags);
146
1.69k
    }
147
148
1.38k
    void evaluate_and_vec(const IColumn& column, uint16_t size, bool* flags) const override {
149
1.38k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
150
1.38k
        if (!_nested) {
151
726
            return;
152
726
        }
153
660
        _nested->evaluate_and_vec(column, size, flags);
154
660
    }
155
156
0
    std::string get_search_str() const override {
157
0
        std::shared_lock<std::shared_mutex> lock(*_mtx);
158
0
        if (!_nested) {
159
0
            DCHECK(false) << "should not reach here";
160
0
        }
161
0
        return _nested->get_search_str();
162
0
    }
163
164
6.33k
    bool evaluate_and(ParquetPredicate::ColumnStat* statistic) const override {
165
6.33k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
166
6.33k
        if (!_nested) {
167
            // at the begining _nested will be null, so return true.
168
5.57k
            return true;
169
5.57k
        }
170
766
        return _nested->evaluate_and(statistic);
171
6.33k
    }
172
173
    bool evaluate_and(ParquetPredicate::CachedPageIndexStat* statistic,
174
3.01k
                      RowRanges* row_ranges) const override {
175
3.01k
        std::shared_lock<std::shared_mutex> lock(*_mtx);
176
177
3.01k
        if (!_nested) {
178
            // at the begining _nested will be null, so return true.
179
2.96k
            row_ranges->add(statistic->row_group_range);
180
2.96k
            return true;
181
2.96k
        }
182
52
        return _nested->evaluate_and(statistic, row_ranges);
183
3.01k
    }
184
185
private:
186
380
    uint16_t _evaluate_inner(const IColumn& column, uint16_t* sel, uint16_t size) const override {
187
380
        std::shared_lock<std::shared_mutex> lock(*_mtx);
188
380
        if (!_nested) {
189
151
            return size;
190
151
        }
191
229
        return _nested->evaluate(column, sel, size);
192
380
    }
193
194
    mutable std::shared_ptr<std::shared_mutex> _mtx;
195
    std::shared_ptr<ColumnPredicate> _nested;
196
};
197
198
} //namespace doris