Coverage Report

Created: 2026-07-14 18:50

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/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
#include "common/compile_check_begin.h"
47
48
VInPredicate::VInPredicate(const TExprNode& node)
49
22
        : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
50
51
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
52
8
                             VExprContext* context) {
53
8
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
54
55
8
    if (_children.empty()) {
56
0
        return Status::InternalError("no Function operator in.");
57
0
    }
58
59
8
    _expr_name =
60
8
            fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
61
62
8
    ColumnsWithTypeAndName argument_template;
63
8
    argument_template.reserve(get_num_children());
64
26
    for (auto child : _children) {
65
26
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
66
26
    }
67
68
    // construct the proper function_name
69
8
    std::string head(_is_not_in ? "not_" : "");
70
8
    std::string real_function_name = head + std::string(function_name);
71
8
    auto arg_type = remove_nullable(argument_template[0].type);
72
8
    if (is_complex_type(arg_type->get_primitive_type())) {
73
0
        real_function_name = "collection_" + real_function_name;
74
0
    }
75
8
    _function = SimpleFunctionFactory::instance().get_function(real_function_name,
76
8
                                                               argument_template, _data_type, {});
77
8
    if (_function == nullptr) {
78
0
        return Status::NotSupported("Function {} is not implemented", real_function_name);
79
0
    }
80
81
8
    VExpr::register_function_context(state, context);
82
8
    _prepare_finished = true;
83
84
8
    if (state->query_options().__isset.in_list_value_count_threshold) {
85
8
        _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
86
8
    }
87
8
    return Status::OK();
88
8
}
89
90
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
91
3
                          FunctionContext::FunctionStateScope scope) {
92
3
    DCHECK(_prepare_finished);
93
9
    for (auto& child : _children) {
94
9
        RETURN_IF_ERROR(child->open(state, context, scope));
95
9
    }
96
3
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
97
3
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
98
3
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
99
3
    }
100
101
3
    _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
102
6
                                        [](const VExprSPtr& expr) { return expr->is_constant(); });
103
3
    if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant &&
104
3
        !_zonemap_materialized) {
105
3
        RETURN_IF_ERROR(_materialize_for_zonemap_filter(context));
106
3
    }
107
3
    _open_finished = true;
108
3
    return Status::OK();
109
3
}
110
111
13
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
112
13
    VExpr::close_function_context(context, scope, _function);
113
13
    VExpr::close(context, scope);
114
13
}
115
116
1
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
117
1
    DCHECK_GE(get_num_children(), 2);
118
1
    return _evaluate_inverted_index(context, _function, segment_num_rows);
119
1
}
120
121
3
Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) {
122
3
    _seg_filter_values.clear();
123
3
    _seg_filter_contains_null = false;
124
3
    _zonemap_materialized = false;
125
3
    if (_children.size() < 2 || !_children[0]->is_slot_ref()) {
126
0
        return Status::OK();
127
0
    }
128
129
3
    const auto data_type = remove_nullable(_children[0]->data_type());
130
3
    DORIS_CHECK(data_type != nullptr);
131
3
    if (is_complex_type(data_type->get_primitive_type())) {
132
0
        return Status::OK();
133
0
    }
134
135
3
    DORIS_CHECK(context != nullptr);
136
3
    auto* fn_ctx = context->fn_context(_fn_context_index);
137
3
    DORIS_CHECK(fn_ctx != nullptr);
138
3
    auto* in_state =
139
3
            reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL));
140
3
    DORIS_CHECK(in_state != nullptr);
141
3
    DORIS_CHECK(in_state->use_set);
142
3
    DORIS_CHECK(in_state->hybrid_set != nullptr);
143
144
3
    expr_zonemap::InZonemapMaterializedSet materialized;
145
3
    RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter(
146
3
            *in_state->hybrid_set, data_type, &materialized));
147
3
    _seg_filter_contains_null = materialized.contains_null;
148
3
    _seg_filter_values = std::move(materialized.values);
149
3
    _seg_filter_min = std::move(materialized.min_value);
150
3
    _seg_filter_max = std::move(materialized.max_value);
151
3
    _zonemap_materialized = true;
152
3
    return Status::OK();
153
3
}
154
155
2
ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const {
156
2
    if (_is_not_in && _seg_filter_contains_null) {
157
1
        return ZoneMapFilterResult::kNoMatch;
158
1
    }
159
1
    return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values,
160
1
                                         _seg_filter_min, _seg_filter_max);
161
2
}
162
163
0
bool VInPredicate::can_evaluate_zonemap_filter() const {
164
0
    return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
165
0
}
166
167
ZoneMapFilterResult VInPredicate::evaluate_dictionary_filter(
168
2
        const DictionaryEvalContext& ctx) const {
169
2
    return expr_zonemap::eval_in_dictionary(ctx, get_child(0), _is_not_in, _seg_filter_values);
170
2
}
171
172
1
bool VInPredicate::can_evaluate_dictionary_filter() const {
173
1
    return _zonemap_materialized && !_is_not_in &&
174
1
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
175
1
}
176
177
2
ZoneMapFilterResult VInPredicate::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const {
178
2
    return expr_zonemap::eval_in_bloom_filter(ctx, get_child(0), _is_not_in, _seg_filter_values);
179
2
}
180
181
1
bool VInPredicate::can_evaluate_bloom_filter() const {
182
1
    return _zonemap_materialized && !_is_not_in &&
183
1
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
184
1
}
185
186
Status VInPredicate::execute_column(VExprContext* context, const Block* block, Selector* selector,
187
0
                                    size_t count, ColumnPtr& result_column) const {
188
0
    if (is_const_and_have_executed()) { // const have execute in open function
189
0
        result_column = get_result_from_const(count);
190
0
        return Status::OK();
191
0
    }
192
0
    if (fast_execute(context, selector, count, result_column)) {
193
0
        return Status::OK();
194
0
    }
195
0
    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
0
    const size_t args_size = _is_args_all_constant ? 1 : _children.size();
202
203
0
    ColumnNumbers arguments;
204
0
    arguments.reserve(args_size);
205
0
    Block temp_block;
206
0
    for (int i = 0; i < args_size; ++i) {
207
0
        ColumnPtr column;
208
0
        RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
209
0
        arguments.push_back(i);
210
0
        temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
211
0
    }
212
213
0
    int num_columns_without_result = temp_block.columns();
214
0
    temp_block.insert({nullptr, _data_type, _expr_name});
215
216
0
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
217
0
                                       arguments, num_columns_without_result, temp_block.rows()));
218
0
    result_column = temp_block.get_by_position(num_columns_without_result).column;
219
0
    DCHECK_EQ(result_column->size(), count);
220
0
    return Status::OK();
221
0
}
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
6
const std::string& VInPredicate::expr_name() const {
244
6
    return _expr_name;
245
6
}
246
247
5
std::string VInPredicate::debug_string() const {
248
5
    std::stringstream out;
249
5
    out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
250
5
    int num_children = get_num_children();
251
252
17
    for (int i = 1; i < num_children; ++i) {
253
12
        out << (i == 1 ? "" : " ") << children()[i]->debug_string();
254
12
    }
255
256
5
    out << "])";
257
5
    return out.str();
258
5
}
259
260
#include "common/compile_check_end.h"
261
} // namespace doris