Coverage Report

Created: 2026-11-05 15:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/runtime_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 <functional>
21
#include <memory>
22
#include <mutex>
23
#include <shared_mutex>
24
#include <string>
25
26
#include "common/status.h"
27
#include "core/arena.h"
28
#include "core/binary_cast.hpp"
29
#include "core/data_type/define_primitive_type.h"
30
#include "core/data_type/primitive_type.h"
31
#include "core/field.h"
32
#include "core/types.h"
33
#include "core/value/vdatetime_value.h"
34
#include "storage/olap_scan_common.h"
35
#include "storage/predicate/shared_predicate.h"
36
#include "storage/tablet/tablet_schema.h"
37
38
namespace doris {
39
class ColumnPredicate;
40
41
class RuntimePredicate {
42
public:
43
    RuntimePredicate(const TTopnFilterDesc& desc, bool is_strict);
44
45
    Status init_target(int32_t target_node_id,
46
                       phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc,
47
                       const int column_id);
48
49
17.9k
    bool enable() const {
50
        // when sort node and scan node are not in the same fragment, predicate will be disabled
51
17.9k
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
52
17.9k
        return _detected_source && _detected_target;
53
17.9k
    }
54
55
838
    void set_detected_source() {
56
838
        std::unique_lock<std::shared_mutex> wlock(_rwlock);
57
        // Source detection does not reset bounds. Recursive rebuilds call reset_bounds().
58
838
        _detected_source = true;
59
838
    }
60
61
    // Recursive fragment rebuild calls this after the old tasks finish, before new tasks run.
62
    void reset_bounds();
63
64
1.58k
    std::shared_ptr<ColumnPredicate> get_predicate(int32_t target_node_id) {
65
1.58k
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
66
1.58k
        check_target_node_id(target_node_id);
67
1.58k
        return _contexts.find(target_node_id)->second.predicate;
68
1.58k
    }
69
70
    Status update(const Field& value);
71
72
1.39k
    bool has_value() const {
73
1.39k
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
74
1.39k
        return _has_value;
75
1.39k
    }
76
77
1.19k
    Field get_value() const {
78
1.19k
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
79
1.19k
        return _orderby_extrem;
80
1.19k
    }
81
82
11.2k
    std::string get_col_name(int32_t target_node_id) const {
83
11.2k
        check_target_node_id(target_node_id);
84
11.2k
        return _contexts.find(target_node_id)->second.col_name;
85
11.2k
    }
86
87
444
    bool is_asc() const { return _is_asc; }
88
89
103
    bool is_strict() const { return _is_strict; }
90
91
1.83k
    const char* comparison_function() const {
92
1.83k
        if (_is_asc) {
93
1.46k
            return _is_strict ? "lt" : "le";
94
1.46k
        }
95
374
        return _is_strict ? "gt" : "ge";
96
1.83k
    }
97
98
2
    TExprOpcode::type comparison_opcode() const {
99
2
        if (_is_asc) {
100
1
            return is_strict() ? TExprOpcode::LT : TExprOpcode::LE;
101
1
        }
102
1
        return is_strict() ? TExprOpcode::GT : TExprOpcode::GE;
103
2
    }
104
105
611
    bool nulls_first() const { return _nulls_first; }
106
107
13.3k
    bool target_is_slot(int32_t target_node_id) const {
108
13.3k
        check_target_node_id(target_node_id);
109
13.3k
        return _contexts.find(target_node_id)->second.target_is_slot();
110
13.3k
    }
111
112
3.67k
    const TExpr& get_texpr(int32_t target_node_id) const {
113
3.67k
        check_target_node_id(target_node_id);
114
3.67k
        return _contexts.find(target_node_id)->second.expr;
115
3.67k
    }
116
117
private:
118
30.3k
    void check_target_node_id(int32_t target_node_id) const {
119
30.3k
        if (!_contexts.contains(target_node_id)) {
120
0
            std::string msg = "context target node ids: [";
121
0
            bool first = true;
122
0
            for (auto p : _contexts) {
123
0
                if (first) {
124
0
                    first = false;
125
0
                } else {
126
0
                    msg += ',';
127
0
                }
128
0
                msg += std::to_string(p.first);
129
0
            }
130
0
            msg += "], input target node is: " + std::to_string(target_node_id);
131
0
            DCHECK(false) << msg;
132
0
        }
133
30.3k
    }
134
    struct TargetContext {
135
        TExpr expr;
136
        std::string col_name;
137
        DataTypePtr col_data_type;
138
        std::shared_ptr<ColumnPredicate> predicate;
139
140
13.4k
        bool target_is_slot() const {
141
13.4k
            return expr.nodes[0].node_type == TExprNodeType::SLOT_REF &&
142
13.4k
                   expr.nodes[0].slot_ref.is_virtual_slot == false;
143
13.4k
        }
144
    };
145
146
    bool _init(PrimitiveType type);
147
148
    mutable std::shared_mutex _rwlock;
149
150
    bool _nulls_first;
151
    bool _is_asc;
152
    const bool _is_strict;
153
    std::map<int32_t, TargetContext> _contexts;
154
155
    Field _orderby_extrem {PrimitiveType::TYPE_NULL};
156
    std::function<std::shared_ptr<ColumnPredicate>(const int cid, const std::string& col_name,
157
                                                   const DataTypePtr& data_type, const Field& value,
158
                                                   bool opposite)>
159
            _pred_constructor;
160
    bool _detected_source = false;
161
    bool _detected_target = false;
162
    bool _has_value = false;
163
    PrimitiveType _type;
164
};
165
166
} // namespace doris