Coverage Report

Created: 2026-04-11 00:05

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
9
        : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
48
49
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
50
5
                             VExprContext* context) {
51
5
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
52
53
5
    if (_children.empty()) {
54
0
        return Status::InternalError("no Function operator in.");
55
0
    }
56
57
5
    _expr_name =
58
5
            fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
59
60
5
    ColumnsWithTypeAndName argument_template;
61
5
    argument_template.reserve(get_num_children());
62
17
    for (auto child : _children) {
63
17
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
64
17
    }
65
66
    // construct the proper function_name
67
5
    std::string head(_is_not_in ? "not_" : "");
68
5
    std::string real_function_name = head + std::string(function_name);
69
5
    auto arg_type = remove_nullable(argument_template[0].type);
70
5
    if (is_complex_type(arg_type->get_primitive_type())) {
71
0
        real_function_name = "collection_" + real_function_name;
72
0
    }
73
5
    _function = SimpleFunctionFactory::instance().get_function(real_function_name,
74
5
                                                               argument_template, _data_type, {});
75
5
    if (_function == nullptr) {
76
0
        return Status::NotSupported("Function {} is not implemented", real_function_name);
77
0
    }
78
79
5
    VExpr::register_function_context(state, context);
80
5
    _prepare_finished = true;
81
82
5
    if (state->query_options().__isset.in_list_value_count_threshold) {
83
5
        _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
84
5
    }
85
5
    return Status::OK();
86
5
}
87
88
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
89
0
                          FunctionContext::FunctionStateScope scope) {
90
0
    DCHECK(_prepare_finished);
91
0
    for (auto& child : _children) {
92
0
        RETURN_IF_ERROR(child->open(state, context, scope));
93
0
    }
94
0
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
95
0
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
96
0
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
97
0
    }
98
99
0
    _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
100
0
                                        [](const VExprSPtr& expr) { return expr->is_constant(); });
101
0
    _open_finished = true;
102
0
    return Status::OK();
103
0
}
104
105
10
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
106
10
    VExpr::close_function_context(context, scope, _function);
107
10
    VExpr::close(context, scope);
108
10
}
109
110
1
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
111
1
    DCHECK_GE(get_num_children(), 2);
112
1
    return _evaluate_inverted_index(context, _function, segment_num_rows);
113
1
}
114
115
Status VInPredicate::execute_column(VExprContext* context, const Block* block, Selector* selector,
116
0
                                    size_t count, ColumnPtr& result_column) const {
117
0
    if (is_const_and_have_executed()) { // const have execute in open function
118
0
        result_column = get_result_from_const(count);
119
0
        return Status::OK();
120
0
    }
121
0
    if (fast_execute(context, selector, count, result_column)) {
122
0
        return Status::OK();
123
0
    }
124
0
    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
0
    const size_t args_size = _is_args_all_constant ? 1 : _children.size();
131
132
0
    ColumnNumbers arguments;
133
0
    arguments.reserve(args_size);
134
0
    Block temp_block;
135
0
    for (int i = 0; i < args_size; ++i) {
136
0
        ColumnPtr column;
137
0
        RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
138
0
        arguments.push_back(i);
139
0
        temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
140
0
    }
141
142
0
    int num_columns_without_result = temp_block.columns();
143
0
    temp_block.insert({nullptr, _data_type, _expr_name});
144
145
0
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
146
0
                                       arguments, num_columns_without_result, temp_block.rows()));
147
0
    result_column = temp_block.get_by_position(num_columns_without_result).column;
148
0
    DCHECK_EQ(result_column->size(), count);
149
0
    return Status::OK();
150
0
}
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
6
const std::string& VInPredicate::expr_name() const {
173
6
    return _expr_name;
174
6
}
175
176
4
std::string VInPredicate::debug_string() const {
177
4
    std::stringstream out;
178
4
    out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
179
4
    int num_children = get_num_children();
180
181
15
    for (int i = 1; i < num_children; ++i) {
182
11
        out << (i == 1 ? "" : " ") << children()[i]->debug_string();
183
11
    }
184
185
4
    out << "])";
186
4
    return out.str();
187
4
}
188
189
} // namespace doris