Coverage Report

Created: 2026-01-27 18:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/runtime/runtime_predicate.cpp
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
#include "runtime/runtime_predicate.h"
19
20
#include <memory>
21
22
#include "common/compiler_util.h" // IWYU pragma: keep
23
#include "common/exception.h"
24
#include "common/status.h"
25
#include "olap/accept_null_predicate.h"
26
#include "olap/column_predicate.h"
27
#include "olap/predicate_creator.h"
28
#include "runtime/define_primitive_type.h"
29
30
namespace doris::vectorized {
31
32
RuntimePredicate::RuntimePredicate(const TTopnFilterDesc& desc)
33
0
        : _nulls_first(desc.null_first), _is_asc(desc.is_asc) {
34
0
    DCHECK(!desc.target_node_id_to_target_expr.empty());
35
0
    for (auto p : desc.target_node_id_to_target_expr) {
36
0
        _contexts[p.first].expr = p.second;
37
0
    }
38
39
0
    _type = thrift_to_type(desc.target_node_id_to_target_expr.begin()
40
0
                                   ->second.nodes[0]
41
0
                                   .type.types[0]
42
0
                                   .scalar_type.type);
43
0
    if (!_init(_type)) {
44
0
        std::stringstream ss;
45
0
        desc.target_node_id_to_target_expr.begin()->second.nodes[0].printTo(ss);
46
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid type, type={}, expr={}",
47
0
                        type_to_string(_type), ss.str());
48
0
    }
49
50
    // For ASC  sort, create runtime predicate col_name <= max_top_value
51
    // since values that > min_top_value are large than any value in current topn values
52
    // For DESC sort, create runtime predicate col_name >= min_top_value
53
    // since values that < min_top_value are less than any value in current topn values
54
0
    _pred_constructor = _is_asc ? create_comparison_predicate0<PredicateType::LE>
55
0
                                : create_comparison_predicate0<PredicateType::GE>;
56
0
}
57
58
Status RuntimePredicate::init_target(
59
        int32_t target_node_id, phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc,
60
0
        const int column_id) {
61
0
    if (column_id < 0) {
62
0
        return Status::OK();
63
0
    }
64
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
65
0
    check_target_node_id(target_node_id);
66
0
    if (target_is_slot(target_node_id)) {
67
0
        _contexts[target_node_id].col_name =
68
0
                slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id]
69
0
                        ->col_name();
70
0
        _contexts[target_node_id].col_data_type =
71
0
                slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id]->type();
72
0
        _contexts[target_node_id].predicate = SharedPredicate::create_shared(
73
0
                cast_set<uint32_t>(column_id), _contexts[target_node_id].col_name);
74
0
    }
75
0
    _detected_target = true;
76
0
    return Status::OK();
77
0
}
78
79
0
StringRef RuntimePredicate::_get_string_ref(const Field& field, const PrimitiveType type) {
80
0
    switch (type) {
81
0
    case PrimitiveType::TYPE_BOOLEAN: {
82
0
        const auto& v = field.get<TYPE_BOOLEAN>();
83
0
        return StringRef((char*)&v, sizeof(v));
84
0
    }
85
0
    case PrimitiveType::TYPE_TINYINT: {
86
0
        const auto& v = field.get<TYPE_TINYINT>();
87
0
        return StringRef((char*)&v, sizeof(v));
88
0
    }
89
0
    case PrimitiveType::TYPE_SMALLINT: {
90
0
        const auto& v = field.get<TYPE_SMALLINT>();
91
0
        return StringRef((char*)&v, sizeof(v));
92
0
    }
93
0
    case PrimitiveType::TYPE_INT: {
94
0
        const auto& v = field.get<TYPE_INT>();
95
0
        return StringRef((char*)&v, sizeof(v));
96
0
    }
97
0
    case PrimitiveType::TYPE_BIGINT: {
98
0
        const auto& v = field.get<TYPE_BIGINT>();
99
0
        return StringRef((char*)&v, sizeof(v));
100
0
    }
101
0
    case PrimitiveType::TYPE_LARGEINT: {
102
0
        const auto& v = field.get<TYPE_LARGEINT>();
103
0
        return StringRef((char*)&v, sizeof(v));
104
0
    }
105
0
    case PrimitiveType::TYPE_CHAR:
106
0
    case PrimitiveType::TYPE_VARCHAR:
107
0
    case PrimitiveType::TYPE_STRING: {
108
0
        const auto& v = field.get<TYPE_STRING>();
109
0
        auto length = v.size();
110
0
        char* buffer = _predicate_arena.alloc(length);
111
0
        memset(buffer, 0, length);
112
0
        memcpy(buffer, v.data(), v.length());
113
114
0
        return {buffer, length};
115
0
    }
116
0
    case PrimitiveType::TYPE_DATEV2: {
117
0
        const auto& v = field.get<TYPE_DATEV2>();
118
0
        return StringRef((char*)&v, sizeof(v));
119
0
    }
120
0
    case PrimitiveType::TYPE_DATETIMEV2: {
121
0
        const auto& v = field.get<TYPE_DATETIMEV2>();
122
0
        return StringRef((char*)&v, sizeof(v));
123
0
    }
124
0
    case PrimitiveType::TYPE_TIMESTAMPTZ: {
125
0
        const auto& v = field.get<TYPE_TIMESTAMPTZ>();
126
0
        return StringRef((char*)&v, sizeof(v));
127
0
        break;
128
0
    }
129
0
    case PrimitiveType::TYPE_DATE: {
130
0
        const auto& v = field.get<TYPE_DATE>();
131
0
        return StringRef((char*)&v, sizeof(v));
132
0
    }
133
0
    case PrimitiveType::TYPE_DATETIME: {
134
0
        const auto& v = field.get<TYPE_DATETIME>();
135
0
        return StringRef((char*)&v, sizeof(v));
136
0
    }
137
0
    case PrimitiveType::TYPE_TIMEV2: {
138
0
        const auto& v = field.get<TYPE_TIMEV2>();
139
0
        return StringRef((char*)&v, sizeof(v));
140
0
    }
141
0
    case PrimitiveType::TYPE_DECIMAL32: {
142
0
        const auto& v = field.get<TYPE_DECIMAL32>();
143
0
        return StringRef((char*)&v, sizeof(v));
144
0
    }
145
0
    case PrimitiveType::TYPE_DECIMAL64: {
146
0
        const auto& v = field.get<TYPE_DECIMAL64>();
147
0
        return StringRef((char*)&v, sizeof(v));
148
0
    }
149
0
    case PrimitiveType::TYPE_DECIMALV2: {
150
0
        const auto& v = field.get<TYPE_DECIMALV2>();
151
0
        return StringRef((char*)&v, sizeof(v));
152
0
    }
153
0
    case PrimitiveType::TYPE_DECIMAL128I: {
154
0
        const auto& v = field.get<TYPE_DECIMAL128I>();
155
0
        return StringRef((char*)&v, sizeof(v));
156
0
    }
157
0
    case PrimitiveType::TYPE_DECIMAL256: {
158
0
        const auto& v = field.get<TYPE_DECIMAL256>();
159
0
        return StringRef((char*)&v, sizeof(v));
160
0
    }
161
0
    case PrimitiveType::TYPE_IPV4: {
162
0
        const auto& v = field.get<TYPE_IPV4>();
163
0
        return StringRef((char*)&v, sizeof(v));
164
0
    }
165
0
    case PrimitiveType::TYPE_IPV6: {
166
0
        const auto& v = field.get<TYPE_IPV6>();
167
0
        return StringRef((char*)&v, sizeof(v));
168
0
    }
169
0
    case doris::PrimitiveType::TYPE_VARBINARY: {
170
0
        const auto& v = field.get<TYPE_VARBINARY>();
171
0
        auto length = v.size();
172
0
        char* buffer = _predicate_arena.alloc(length);
173
0
        memset(buffer, 0, length);
174
0
        memcpy(buffer, v.data(), length);
175
0
        return {buffer, length};
176
0
    }
177
0
    default:
178
0
        break;
179
0
    }
180
181
0
    throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid type, type={}", type_to_string(type));
182
0
    return {};
183
0
}
184
185
0
bool RuntimePredicate::_init(PrimitiveType type) {
186
0
    return is_int_or_bool(type) || is_decimal(type) || is_string_type(type) || is_date_type(type) ||
187
0
           is_time_type(type) || is_ip(type) || is_varbinary(type);
188
0
}
189
190
0
Status RuntimePredicate::update(const Field& value) {
191
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
192
    // skip null value
193
0
    if (value.is_null()) {
194
0
        return Status::OK();
195
0
    }
196
197
0
    bool updated = false;
198
199
0
    if (UNLIKELY(_orderby_extrem.is_null())) {
200
0
        _orderby_extrem = value;
201
0
        updated = true;
202
0
    } else {
203
0
        if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) {
204
0
            _orderby_extrem = value;
205
0
            updated = true;
206
0
        }
207
0
    }
208
209
0
    _has_value = true;
210
211
0
    if (!updated) {
212
0
        return Status::OK();
213
0
    }
214
0
    for (auto p : _contexts) {
215
0
        auto ctx = p.second;
216
0
        auto str_ref = _get_string_ref(_orderby_extrem, _type);
217
0
        std::shared_ptr<ColumnPredicate> pred =
218
0
                _pred_constructor(ctx.predicate->column_id(), ctx.col_name, ctx.col_data_type,
219
0
                                  str_ref, false, _predicate_arena);
220
221
        // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL
222
        // since ORDER BY ASC/DESC should get NULL first but pred returns NULL
223
        // and NULL in where predicate will be treated as FALSE
224
0
        if (_nulls_first) {
225
0
            pred = AcceptNullPredicate::create_shared(pred);
226
0
        }
227
228
0
        ((SharedPredicate*)ctx.predicate.get())->set_nested(pred);
229
0
    }
230
0
    return Status::OK();
231
0
}
232
233
} // namespace doris::vectorized