Coverage Report

Created: 2026-03-11 11:20

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