Coverage Report

Created: 2026-07-10 07:28

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/expr_zonemap_filter.h"
34
#include "exprs/function/in.h"
35
#include "exprs/function/simple_function_factory.h"
36
#include "exprs/vexpr_context.h"
37
#include "exprs/vslot_ref.h"
38
#include "runtime/runtime_state.h"
39
40
namespace doris {
41
class RowDescriptor;
42
class RuntimeState;
43
} // namespace doris
44
45
namespace doris {
46
47
VInPredicate::VInPredicate(const TExprNode& node)
48
7.68k
        : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
49
50
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
51
3.92k
                             VExprContext* context) {
52
3.92k
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
53
54
3.92k
    if (_children.empty()) {
55
0
        return Status::InternalError("no Function operator in.");
56
0
    }
57
58
3.92k
    _expr_name =
59
3.92k
            fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
60
61
3.92k
    ColumnsWithTypeAndName argument_template;
62
3.92k
    argument_template.reserve(get_num_children());
63
13.4k
    for (auto child : _children) {
64
13.4k
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
65
13.4k
    }
66
67
    // construct the proper function_name
68
3.92k
    std::string head(_is_not_in ? "not_" : "");
69
3.92k
    std::string real_function_name = head + std::string(function_name);
70
3.92k
    auto arg_type = remove_nullable(argument_template[0].type);
71
3.92k
    if (is_complex_type(arg_type->get_primitive_type())) {
72
12
        real_function_name = "collection_" + real_function_name;
73
12
    }
74
3.92k
    _function = SimpleFunctionFactory::instance().get_function(real_function_name,
75
3.92k
                                                               argument_template, _data_type, {});
76
3.92k
    if (_function == nullptr) {
77
0
        return Status::NotSupported("Function {} is not implemented", real_function_name);
78
0
    }
79
80
3.92k
    VExpr::register_function_context(state, context);
81
3.92k
    _prepare_finished = true;
82
83
3.92k
    if (state->query_options().__isset.in_list_value_count_threshold) {
84
3.90k
        _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
85
3.90k
    }
86
3.92k
    return Status::OK();
87
3.92k
}
88
89
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
90
14.6k
                          FunctionContext::FunctionStateScope scope) {
91
14.6k
    DCHECK(_prepare_finished);
92
50.2k
    for (auto& child : _children) {
93
50.2k
        RETURN_IF_ERROR(child->open(state, context, scope));
94
50.2k
    }
95
14.6k
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
96
14.6k
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
97
3.91k
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
98
3.91k
    }
99
100
14.6k
    _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
101
35.7k
                                        [](const VExprSPtr& expr) { return expr->is_constant(); });
102
14.6k
    if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant &&
103
14.6k
        !_zonemap_materialized) {
104
3.91k
        RETURN_IF_ERROR(_materialize_for_zonemap_filter(context));
105
3.91k
    }
106
14.6k
    _open_finished = true;
107
14.6k
    return Status::OK();
108
14.6k
}
109
110
14.7k
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
111
14.7k
    VExpr::close_function_context(context, scope, _function);
112
14.7k
    VExpr::close(context, scope);
113
14.7k
}
114
115
1.27k
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
116
1.27k
    DCHECK_GE(get_num_children(), 2);
117
1.27k
    return _evaluate_inverted_index(context, _function, segment_num_rows);
118
1.27k
}
119
120
3.91k
Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) {
121
3.91k
    _seg_filter_values.clear();
122
3.91k
    _seg_filter_contains_null = false;
123
3.91k
    _zonemap_materialized = false;
124
3.91k
    if (_children.size() < 2 || !_children[0]->is_slot_ref()) {
125
149
        return Status::OK();
126
149
    }
127
128
3.76k
    const auto data_type = remove_nullable(_children[0]->data_type());
129
3.76k
    DORIS_CHECK(data_type != nullptr);
130
3.76k
    if (is_complex_type(data_type->get_primitive_type())) {
131
6
        return Status::OK();
132
6
    }
133
134
3.76k
    DORIS_CHECK(context != nullptr);
135
3.76k
    auto* fn_ctx = context->fn_context(_fn_context_index);
136
3.76k
    DORIS_CHECK(fn_ctx != nullptr);
137
3.76k
    auto* in_state =
138
3.76k
            reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL));
139
3.76k
    DORIS_CHECK(in_state != nullptr);
140
3.76k
    DORIS_CHECK(in_state->use_set);
141
3.76k
    DORIS_CHECK(in_state->hybrid_set != nullptr);
142
143
3.76k
    expr_zonemap::InZonemapMaterializedSet materialized;
144
3.76k
    RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter(
145
3.76k
            *in_state->hybrid_set, data_type, &materialized));
146
3.76k
    _seg_filter_contains_null = materialized.contains_null;
147
3.76k
    _seg_filter_values = std::move(materialized.values);
148
3.76k
    _seg_filter_min = std::move(materialized.min_value);
149
3.76k
    _seg_filter_max = std::move(materialized.max_value);
150
3.76k
    _zonemap_materialized = true;
151
3.76k
    return Status::OK();
152
3.76k
}
153
154
3.47k
ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const {
155
3.47k
    if (_is_not_in && _seg_filter_contains_null) {
156
1.00k
        return ZoneMapFilterResult::kNoMatch;
157
1.00k
    }
158
2.46k
    return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values,
159
2.46k
                                         _seg_filter_min, _seg_filter_max);
160
3.47k
}
161
162
8.98k
bool VInPredicate::can_evaluate_zonemap_filter() const {
163
8.98k
    return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
164
8.98k
}
165
166
ZoneMapFilterResult VInPredicate::evaluate_dictionary_filter(
167
72
        const DictionaryEvalContext& ctx) const {
168
72
    return expr_zonemap::eval_in_dictionary(ctx, get_child(0), _is_not_in, _seg_filter_values);
169
72
}
170
171
333
bool VInPredicate::can_evaluate_dictionary_filter() const {
172
333
    return _zonemap_materialized && !_is_not_in &&
173
333
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
174
333
}
175
176
2
ZoneMapFilterResult VInPredicate::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const {
177
2
    return expr_zonemap::eval_in_bloom_filter(ctx, get_child(0), _is_not_in, _seg_filter_values);
178
2
}
179
180
263
bool VInPredicate::can_evaluate_bloom_filter() const {
181
263
    return _zonemap_materialized && !_is_not_in &&
182
263
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
183
263
}
184
185
Status VInPredicate::execute_column_impl(VExprContext* context, const Block* block,
186
                                         const Selector* selector, size_t count,
187
17.3k
                                         ColumnPtr& result_column) const {
188
17.3k
    if (is_const_and_have_executed()) { // const have execute in open function
189
7
        result_column = get_result_from_const(count);
190
7
        return Status::OK();
191
7
    }
192
17.3k
    if (fast_execute(context, selector, count, result_column)) {
193
15
        return Status::OK();
194
15
    }
195
17.3k
    DCHECK(_open_finished || block == nullptr);
196
197
    // This is an optimization. For expressions like colA IN (1, 2, 3, 4),
198
    // where all values inside the IN clause are constants,
199
    // a hash set is created during open, and it will not be accessed again during execute
200
    //  Here, _children[0] is colA
201
17.3k
    const size_t args_size = _is_args_all_constant ? 1 : _children.size();
202
203
17.3k
    ColumnNumbers arguments;
204
17.3k
    arguments.reserve(args_size);
205
17.3k
    Block temp_block;
206
34.6k
    for (int i = 0; i < args_size; ++i) {
207
17.3k
        ColumnPtr column;
208
17.3k
        RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
209
17.3k
        arguments.push_back(i);
210
17.3k
        temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
211
17.3k
    }
212
213
17.3k
    int num_columns_without_result = temp_block.columns();
214
17.3k
    temp_block.insert({nullptr, _data_type, _expr_name});
215
216
17.3k
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
217
17.3k
                                       arguments, num_columns_without_result, temp_block.rows()));
218
17.3k
    result_column = temp_block.get_by_position(num_columns_without_result).column;
219
17.3k
    DCHECK_EQ(result_column->size(), count);
220
17.3k
    return Status::OK();
221
17.3k
}
222
223
0
size_t VInPredicate::estimate_memory(const size_t rows) {
224
0
    if (is_const_and_have_executed()) {
225
0
        return 0;
226
0
    }
227
228
0
    size_t estimate_size = 0;
229
230
0
    for (int i = 0; i < _children.size(); ++i) {
231
0
        estimate_size += _children[i]->estimate_memory(rows);
232
0
    }
233
234
0
    if (_data_type->is_nullable()) {
235
0
        estimate_size += rows * sizeof(uint8_t);
236
0
    }
237
238
0
    estimate_size += rows * sizeof(uint8_t);
239
240
0
    return estimate_size;
241
0
}
242
243
1.36k
const std::string& VInPredicate::expr_name() const {
244
1.36k
    return _expr_name;
245
1.36k
}
246
247
19
std::string VInPredicate::debug_string() const {
248
19
    std::stringstream out;
249
19
    out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
250
19
    int num_children = get_num_children();
251
252
59
    for (int i = 1; i < num_children; ++i) {
253
40
        out << (i == 1 ? "" : " ") << children()[i]->debug_string();
254
40
    }
255
256
19
    out << "])";
257
19
    return out.str();
258
19
}
259
260
} // namespace doris