Coverage Report

Created: 2026-03-13 09:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vtopn_pred.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 <gen_cpp/types.pb.h>
21
22
#include <utility>
23
24
#include "core/block/column_numbers.h"
25
#include "core/data_type/data_type.h"
26
#include "exec/common/util.hpp"
27
#include "exprs/function/simple_function_factory.h"
28
#include "exprs/vectorized_fn_call.h"
29
#include "exprs/vexpr.h"
30
#include "exprs/vslot_ref.h"
31
#include "runtime/query_context.h"
32
#include "runtime/runtime_predicate.h"
33
#include "runtime/runtime_state.h"
34
35
namespace doris {
36
#include "common/compile_check_begin.h"
37
38
// only used for dynamic topn filter
39
class VTopNPred : public VExpr {
40
    ENABLE_FACTORY_CREATOR(VTopNPred);
41
42
public:
43
    VTopNPred(const TExprNode& node, int source_node_id, VExprContextSPtr target_ctx)
44
4.84k
            : VExpr(node),
45
4.84k
              _source_node_id(source_node_id),
46
4.84k
              _expr_name(fmt::format("VTopNPred(source_node_id={})", _source_node_id)),
47
4.84k
              _target_ctx(std::move(target_ctx)) {}
48
9.68k
    bool is_topn_filter() const override { return true; }
49
50
4.86k
    static Status create_vtopn_pred(const TExpr& target_expr, int source_node_id, VExprSPtr& expr) {
51
4.86k
        VExprContextSPtr target_ctx;
52
4.86k
        RETURN_IF_ERROR(VExpr::create_expr_tree(target_expr, target_ctx));
53
54
4.86k
        TExprNode node;
55
4.86k
        node.__set_node_type(TExprNodeType::FUNCTION_CALL);
56
4.86k
        node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
57
4.86k
        node.__set_is_nullable(target_ctx->root()->is_nullable());
58
4.86k
        expr = VTopNPred::create_shared(node, source_node_id, target_ctx);
59
60
4.86k
        DCHECK(target_ctx->root() != nullptr);
61
4.86k
        expr->add_child(target_ctx->root());
62
63
4.86k
        return Status::OK();
64
4.86k
    }
65
66
4.80k
    int source_node_id() const { return _source_node_id; }
67
68
4.83k
    Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override {
69
4.83k
        _predicate = &state->get_query_ctx()->get_runtime_predicate(_source_node_id);
70
4.83k
        RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
71
72
4.83k
        ColumnsWithTypeAndName argument_template;
73
4.83k
        argument_template.emplace_back(nullptr, _children[0]->data_type(),
74
4.83k
                                       _children[0]->expr_name());
75
4.83k
        argument_template.emplace_back(nullptr, _children[0]->data_type(), "topn value");
76
77
4.83k
        _function = SimpleFunctionFactory::instance().get_function(
78
4.83k
                _predicate->is_asc() ? "le" : "ge", argument_template, _data_type, {},
79
4.83k
                state->be_exec_version());
80
4.83k
        if (!_function) {
81
0
            return Status::InternalError("get function failed");
82
0
        }
83
4.83k
        return Status::OK();
84
4.83k
    }
85
86
    Status execute_column(VExprContext* context, const Block* block, Selector* selector,
87
15.6k
                          size_t count, ColumnPtr& result_column) const override {
88
15.6k
        if (!_predicate->has_value()) {
89
12.6k
            result_column = create_always_true_column(count, _data_type->is_nullable());
90
12.6k
            return Status::OK();
91
12.6k
        }
92
93
2.96k
        Block temp_block;
94
95
        // slot
96
2.96k
        ColumnPtr slot_column;
97
2.96k
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, slot_column));
98
2.96k
        auto slot_type = _children[0]->execute_type(block);
99
2.96k
        temp_block.insert({slot_column, slot_type, _children[0]->expr_name()});
100
2.96k
        int slot_id = 0;
101
102
        // topn value
103
2.96k
        Field field = _predicate->get_value();
104
2.96k
        auto column_ptr = _children[0]->data_type()->create_column_const(1, field);
105
2.96k
        int topn_value_id = VExpr::insert_param(&temp_block,
106
2.96k
                                                {column_ptr, _children[0]->data_type(), _expr_name},
107
2.96k
                                                std::max(count, column_ptr->size()));
108
109
        // if error(slot_id == -1), will return.
110
2.96k
        ColumnNumbers arguments = {static_cast<uint32_t>(slot_id),
111
2.96k
                                   static_cast<uint32_t>(topn_value_id)};
112
113
2.96k
        uint32_t num_columns_without_result = temp_block.columns();
114
        // prepare a column to save result
115
2.96k
        temp_block.insert({nullptr, _data_type, _expr_name});
116
117
2.96k
        RETURN_IF_ERROR(_function->execute(nullptr, temp_block, arguments,
118
2.96k
                                           num_columns_without_result, temp_block.rows()));
119
2.96k
        result_column = std::move(temp_block.get_by_position(num_columns_without_result).column);
120
2.96k
        if (is_nullable() && _predicate->nulls_first()) {
121
            // null values ​​are always not filtered
122
2.67k
            change_null_to_true(result_column->assume_mutable());
123
2.67k
        }
124
2.96k
        DCHECK_EQ(result_column->size(), count);
125
2.96k
        return Status::OK();
126
2.96k
    }
127
128
0
    const std::string& expr_name() const override { return _expr_name; }
129
130
    // only used in external table (for min-max filter). get `slot > xxx`, not `function(slot) > xxx`.
131
6.73k
    bool get_binary_expr(VExprSPtr& new_root) const {
132
6.73k
        if (!get_child(0)->is_slot_ref()) {
133
            // top rf maybe is `xxx order by abs(column) limit xxx`.
134
0
            return false;
135
0
        }
136
137
6.73k
        if (!_predicate->has_value()) {
138
6.64k
            return false;
139
6.64k
        }
140
141
86
        auto* slot_ref = assert_cast<VSlotRef*>(get_child(0).get());
142
86
        auto slot_data_type = remove_nullable(slot_ref->data_type());
143
86
        {
144
86
            TFunction fn;
145
86
            TFunctionName fn_name;
146
86
            fn_name.__set_db_name("");
147
18.4E
            fn_name.__set_function_name(_predicate->is_asc() ? "le" : "ge");
148
86
            fn.__set_name(fn_name);
149
86
            fn.__set_binary_type(TFunctionBinaryType::BUILTIN);
150
86
            std::vector<TTypeDesc> arg_types;
151
86
            arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(),
152
86
                                                 slot_data_type->get_precision(),
153
86
                                                 slot_data_type->get_scale()));
154
155
86
            arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(),
156
86
                                                 slot_data_type->get_precision(),
157
86
                                                 slot_data_type->get_scale()));
158
86
            fn.__set_arg_types(arg_types);
159
86
            fn.__set_ret_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
160
86
            fn.__set_has_var_args(false);
161
162
86
            TExprNode texpr_node;
163
86
            texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
164
86
            texpr_node.__set_node_type(TExprNodeType::BINARY_PRED);
165
18.4E
            texpr_node.__set_opcode(_predicate->is_asc() ? TExprOpcode::LE : TExprOpcode::GE);
166
86
            texpr_node.__set_fn(fn);
167
86
            texpr_node.__set_num_children(2);
168
86
            texpr_node.__set_is_nullable(is_nullable());
169
86
            new_root = VectorizedFnCall::create_shared(texpr_node);
170
86
        }
171
172
86
        {
173
            // add slot
174
86
            new_root->add_child(children().at(0));
175
86
        }
176
        // add Literal
177
86
        {
178
86
            Field field = _predicate->get_value();
179
86
            TExprNode node = create_texpr_node_from(field, slot_data_type->get_primitive_type(),
180
86
                                                    slot_data_type->get_precision(),
181
86
                                                    slot_data_type->get_scale());
182
86
            new_root->add_child(VLiteral::create_shared(node));
183
86
        }
184
185
        // Since the normal greater than or less than relationship does not consider the relationship of null values, the generated `col >=/<= xxx OR col is null.`
186
88
        if (_predicate->nulls_first()) {
187
88
            VExprSPtr col_is_null_node;
188
88
            {
189
88
                TFunction fn;
190
88
                TFunctionName fn_name;
191
88
                fn_name.__set_db_name("");
192
88
                fn_name.__set_function_name("is_null_pred");
193
88
                fn.__set_name(fn_name);
194
88
                fn.__set_binary_type(TFunctionBinaryType::BUILTIN);
195
88
                std::vector<TTypeDesc> arg_types;
196
88
                arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(),
197
88
                                                     slot_data_type->get_precision(),
198
88
                                                     slot_data_type->get_scale()));
199
88
                fn.__set_arg_types(arg_types);
200
88
                fn.__set_ret_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
201
88
                fn.__set_has_var_args(false);
202
203
88
                TExprNode texpr_node;
204
88
                texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
205
88
                texpr_node.__set_node_type(TExprNodeType::FUNCTION_CALL);
206
88
                texpr_node.__set_fn(fn);
207
88
                texpr_node.__set_num_children(1);
208
88
                col_is_null_node = VectorizedFnCall::create_shared(texpr_node);
209
210
                // add slot.
211
88
                col_is_null_node->add_child(children().at(0));
212
88
            }
213
214
88
            VExprSPtr or_node;
215
88
            {
216
88
                TExprNode texpr_node;
217
88
                texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN));
218
88
                texpr_node.__set_node_type(TExprNodeType::COMPOUND_PRED);
219
88
                texpr_node.__set_opcode(TExprOpcode::COMPOUND_OR);
220
88
                texpr_node.__set_num_children(2);
221
88
                or_node = VectorizedFnCall::create_shared(texpr_node);
222
88
            }
223
224
88
            or_node->add_child(col_is_null_node);
225
88
            or_node->add_child(new_root);
226
88
            new_root = or_node;
227
88
        }
228
229
86
        return true;
230
6.73k
    }
231
232
private:
233
    int _source_node_id;
234
    std::string _expr_name;
235
    RuntimePredicate* _predicate = nullptr;
236
    FunctionBasePtr _function;
237
    VExprContextSPtr _target_ctx;
238
};
239
240
#include "common/compile_check_end.h"
241
} // namespace doris