Coverage Report

Created: 2026-04-10 16:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vin_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 "exprs/vin_predicate.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Exprs_types.h>
22
#include <glog/logging.h>
23
24
#include <algorithm>
25
#include <cstddef>
26
#include <ostream>
27
28
#include "common/status.h"
29
#include "core/block/block.h"
30
#include "core/block/column_numbers.h"
31
#include "core/block/column_with_type_and_name.h"
32
#include "core/block/columns_with_type_and_name.h"
33
#include "exprs/function/simple_function_factory.h"
34
#include "exprs/vexpr_context.h"
35
#include "exprs/vliteral.h"
36
#include "exprs/vslot_ref.h"
37
#include "runtime/runtime_state.h"
38
39
namespace doris {
40
class RowDescriptor;
41
class RuntimeState;
42
} // namespace doris
43
44
namespace doris {
45
46
VInPredicate::VInPredicate(const TExprNode& node)
47
2.09k
        : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
48
49
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
50
1.85k
                             VExprContext* context) {
51
1.85k
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
52
53
1.85k
    if (_children.empty()) {
54
0
        return Status::InternalError("no Function operator in.");
55
0
    }
56
57
1.85k
    _expr_name =
58
1.85k
            fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
59
60
1.85k
    ColumnsWithTypeAndName argument_template;
61
1.85k
    argument_template.reserve(get_num_children());
62
6.39k
    for (auto child : _children) {
63
6.39k
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
64
6.39k
    }
65
66
    // construct the proper function_name
67
1.85k
    std::string head(_is_not_in ? "not_" : "");
68
1.85k
    std::string real_function_name = head + std::string(function_name);
69
1.85k
    auto arg_type = remove_nullable(argument_template[0].type);
70
1.85k
    if (is_complex_type(arg_type->get_primitive_type())) {
71
12
        real_function_name = "collection_" + real_function_name;
72
12
    }
73
1.85k
    _function = SimpleFunctionFactory::instance().get_function(real_function_name,
74
1.85k
                                                               argument_template, _data_type, {});
75
1.85k
    if (_function == nullptr) {
76
0
        return Status::NotSupported("Function {} is not implemented", real_function_name);
77
0
    }
78
79
1.85k
    VExpr::register_function_context(state, context);
80
1.85k
    _prepare_finished = true;
81
82
1.85k
    if (state->query_options().__isset.in_list_value_count_threshold) {
83
1.85k
        _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
84
1.85k
    }
85
1.85k
    return Status::OK();
86
1.85k
}
87
88
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
89
11.9k
                          FunctionContext::FunctionStateScope scope) {
90
11.9k
    DCHECK(_prepare_finished);
91
41.1k
    for (auto& child : _children) {
92
41.1k
        RETURN_IF_ERROR(child->open(state, context, scope));
93
41.1k
    }
94
11.9k
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
95
11.9k
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
96
1.84k
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
97
1.84k
    }
98
99
11.9k
    _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
100
29.2k
                                        [](const VExprSPtr& expr) { return expr->is_constant(); });
101
11.9k
    _open_finished = true;
102
11.9k
    return Status::OK();
103
11.9k
}
104
105
12.0k
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
106
12.0k
    VExpr::close_function_context(context, scope, _function);
107
12.0k
    VExpr::close(context, scope);
108
12.0k
}
109
110
945
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
111
945
    DCHECK_GE(get_num_children(), 2);
112
945
    return _evaluate_inverted_index(context, _function, segment_num_rows);
113
945
}
114
115
Status VInPredicate::execute_column(VExprContext* context, const Block* block, Selector* selector,
116
14.9k
                                    size_t count, ColumnPtr& result_column) const {
117
14.9k
    if (is_const_and_have_executed()) { // const have execute in open function
118
7
        result_column = get_result_from_const(count);
119
7
        return Status::OK();
120
7
    }
121
14.9k
    if (fast_execute(context, selector, count, result_column)) {
122
34
        return Status::OK();
123
34
    }
124
14.9k
    DCHECK(_open_finished || block == nullptr);
125
126
    // This is an optimization. For expressions like colA IN (1, 2, 3, 4),
127
    // where all values inside the IN clause are constants,
128
    // a hash set is created during open, and it will not be accessed again during execute
129
    //  Here, _children[0] is colA
130
14.9k
    const size_t args_size = _is_args_all_constant ? 1 : _children.size();
131
132
14.9k
    ColumnNumbers arguments;
133
14.9k
    arguments.reserve(args_size);
134
14.9k
    Block temp_block;
135
29.8k
    for (int i = 0; i < args_size; ++i) {
136
14.9k
        ColumnPtr column;
137
14.9k
        RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
138
14.9k
        arguments.push_back(i);
139
14.9k
        temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
140
14.9k
    }
141
142
14.9k
    int num_columns_without_result = temp_block.columns();
143
14.9k
    temp_block.insert({nullptr, _data_type, _expr_name});
144
145
14.9k
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
146
14.9k
                                       arguments, num_columns_without_result, temp_block.rows()));
147
14.9k
    result_column = temp_block.get_by_position(num_columns_without_result).column;
148
14.9k
    DCHECK_EQ(result_column->size(), count);
149
14.9k
    return Status::OK();
150
14.9k
}
151
152
0
size_t VInPredicate::estimate_memory(const size_t rows) {
153
0
    if (is_const_and_have_executed()) {
154
0
        return 0;
155
0
    }
156
157
0
    size_t estimate_size = 0;
158
159
0
    for (int i = 0; i < _children.size(); ++i) {
160
0
        estimate_size += _children[i]->estimate_memory(rows);
161
0
    }
162
163
0
    if (_data_type->is_nullable()) {
164
0
        estimate_size += rows * sizeof(uint8_t);
165
0
    }
166
167
0
    estimate_size += rows * sizeof(uint8_t);
168
169
0
    return estimate_size;
170
0
}
171
172
1.30k
const std::string& VInPredicate::expr_name() const {
173
1.30k
    return _expr_name;
174
1.30k
}
175
176
18
std::string VInPredicate::debug_string() const {
177
18
    std::stringstream out;
178
18
    out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
179
18
    int num_children = get_num_children();
180
181
57
    for (int i = 1; i < num_children; ++i) {
182
39
        out << (i == 1 ? "" : " ") << children()[i]->debug_string();
183
39
    }
184
185
18
    out << "])";
186
18
    return out.str();
187
18
}
188
189
} // namespace doris