Coverage Report

Created: 2025-12-26 22:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/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 "exec/olap_common.h"
28
#include "olap/shared_predicate.h"
29
#include "olap/tablet_schema.h"
30
#include "runtime/define_primitive_type.h"
31
#include "runtime/primitive_type.h"
32
#include "util/binary_cast.hpp"
33
#include "vec/common/arena.h"
34
#include "vec/core/field.h"
35
#include "vec/core/types.h"
36
#include "vec/runtime/vdatetime_value.h"
37
38
namespace doris {
39
class ColumnPredicate;
40
41
namespace vectorized {
42
43
class RuntimePredicate {
44
public:
45
    RuntimePredicate(const TTopnFilterDesc& desc);
46
47
    Status init_target(int32_t target_node_id,
48
                       phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc,
49
                       const int column_id);
50
51
0
    bool enable() const {
52
        // when sort node and scan node are not in the same fragment, predicate will be disabled
53
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
54
0
        return _detected_source && _detected_target;
55
0
    }
56
57
0
    void set_detected_source() {
58
0
        std::unique_lock<std::shared_mutex> wlock(_rwlock);
59
0
        _detected_source = true;
60
0
    }
61
62
0
    Status set_tablet_schema(int32_t target_node_id, TabletSchemaSPtr tablet_schema) {
63
0
        std::unique_lock<std::shared_mutex> wlock(_rwlock);
64
0
        check_target_node_id(target_node_id);
65
0
        if (_contexts[target_node_id].tablet_schema) {
66
0
            return Status::OK();
67
0
        }
68
0
        RETURN_IF_ERROR(tablet_schema->have_column(_contexts[target_node_id].col_name));
69
0
        _contexts[target_node_id].tablet_schema = tablet_schema;
70
0
        DCHECK(_contexts[target_node_id].predicate != nullptr);
71
0
        return Status::OK();
72
0
    }
73
74
0
    std::shared_ptr<ColumnPredicate> get_predicate(int32_t target_node_id) {
75
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
76
0
        check_target_node_id(target_node_id);
77
0
        return _contexts.find(target_node_id)->second.predicate;
78
0
    }
79
80
    Status update(const Field& value);
81
82
0
    bool has_value() const {
83
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
84
0
        return _has_value;
85
0
    }
86
87
0
    Field get_value() const {
88
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
89
0
        return _orderby_extrem;
90
0
    }
91
92
0
    std::string get_col_name(int32_t target_node_id) const {
93
0
        check_target_node_id(target_node_id);
94
0
        return _contexts.find(target_node_id)->second.col_name;
95
0
    }
96
97
0
    bool is_asc() const { return _is_asc; }
98
99
0
    bool nulls_first() const { return _nulls_first; }
100
101
0
    bool target_is_slot(int32_t target_node_id) const {
102
0
        check_target_node_id(target_node_id);
103
0
        return _contexts.find(target_node_id)->second.target_is_slot();
104
0
    }
105
106
0
    const TExpr& get_texpr(int32_t target_node_id) const {
107
0
        check_target_node_id(target_node_id);
108
0
        return _contexts.find(target_node_id)->second.expr;
109
0
    }
110
111
private:
112
    StringRef _get_string_ref(const Field& field, const PrimitiveType type);
113
0
    void check_target_node_id(int32_t target_node_id) const {
114
0
        if (!_contexts.contains(target_node_id)) {
115
0
            std::string msg = "context target node ids: [";
116
0
            bool first = true;
117
0
            for (auto p : _contexts) {
118
0
                if (first) {
119
0
                    first = false;
120
0
                } else {
121
0
                    msg += ',';
122
0
                }
123
0
                msg += std::to_string(p.first);
124
0
            }
125
0
            msg += "], input target node is: " + std::to_string(target_node_id);
126
0
            DCHECK(false) << msg;
127
0
        }
128
0
    }
129
    struct TargetContext {
130
        TExpr expr;
131
        std::string col_name;
132
        // TODO(gabriel): remove this
133
        TabletSchemaSPtr tablet_schema;
134
        std::shared_ptr<ColumnPredicate> predicate;
135
136
0
        Result<int32_t> get_field_index() {
137
0
            const auto& column = *DORIS_TRY(tablet_schema->column(col_name));
138
0
            return tablet_schema->field_index(column.unique_id());
139
0
        }
140
141
0
        bool target_is_slot() const {
142
0
            return expr.nodes[0].node_type == TExprNodeType::SLOT_REF &&
143
0
                   expr.nodes[0].slot_ref.is_virtual_slot == false;
144
0
        }
145
    };
146
147
    bool _init(PrimitiveType type);
148
149
    mutable std::shared_mutex _rwlock;
150
151
    bool _nulls_first;
152
    bool _is_asc;
153
    std::map<int32_t, TargetContext> _contexts;
154
155
    Field _orderby_extrem {PrimitiveType::TYPE_NULL};
156
    Arena _predicate_arena;
157
    std::function<std::shared_ptr<ColumnPredicate>(
158
            const int cid, const vectorized::DataTypePtr& data_type, StringRef& value,
159
            bool opposite, vectorized::Arena& arena)>
160
            _pred_constructor;
161
    bool _detected_source = false;
162
    bool _detected_target = false;
163
    bool _has_value = false;
164
    PrimitiveType _type;
165
};
166
167
} // namespace vectorized
168
} // namespace doris