Coverage Report

Created: 2026-07-30 08:54

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 "core/data_type/define_primitive_type.h"
34
#include "exprs/expr_zonemap_filter.h"
35
#include "exprs/function/in.h"
36
#include "exprs/function/simple_function_factory.h"
37
#include "exprs/vexpr_context.h"
38
#include "exprs/vslot_ref.h"
39
#include "runtime/runtime_state.h"
40
41
namespace doris {
42
class RowDescriptor;
43
class RuntimeState;
44
} // namespace doris
45
46
namespace doris {
47
48
namespace {
49
50
506
size_t raw_in_value_size(PrimitiveType primitive_type) {
51
506
    switch (primitive_type) {
52
0
#define RETURN_RAW_IN_SIZE(TYPE) \
53
436
    case TYPE:                   \
54
436
        return sizeof(typename PrimitiveTypeTraits<TYPE>::CppType)
55
0
        RETURN_RAW_IN_SIZE(TYPE_BOOLEAN);
56
0
        RETURN_RAW_IN_SIZE(TYPE_TINYINT);
57
0
        RETURN_RAW_IN_SIZE(TYPE_SMALLINT);
58
322
        RETURN_RAW_IN_SIZE(TYPE_INT);
59
24
        RETURN_RAW_IN_SIZE(TYPE_BIGINT);
60
0
        RETURN_RAW_IN_SIZE(TYPE_LARGEINT);
61
0
        RETURN_RAW_IN_SIZE(TYPE_FLOAT);
62
0
        RETURN_RAW_IN_SIZE(TYPE_DOUBLE);
63
0
        RETURN_RAW_IN_SIZE(TYPE_DATE);
64
0
        RETURN_RAW_IN_SIZE(TYPE_DATETIME);
65
48
        RETURN_RAW_IN_SIZE(TYPE_DATEV2);
66
18
        RETURN_RAW_IN_SIZE(TYPE_DATETIMEV2);
67
0
        RETURN_RAW_IN_SIZE(TYPE_TIMESTAMPTZ);
68
0
        RETURN_RAW_IN_SIZE(TYPE_TIMEV2);
69
0
        RETURN_RAW_IN_SIZE(TYPE_DECIMAL32);
70
24
        RETURN_RAW_IN_SIZE(TYPE_DECIMAL64);
71
0
        RETURN_RAW_IN_SIZE(TYPE_DECIMALV2);
72
0
        RETURN_RAW_IN_SIZE(TYPE_DECIMAL128I);
73
0
        RETURN_RAW_IN_SIZE(TYPE_DECIMAL256);
74
0
        RETURN_RAW_IN_SIZE(TYPE_IPV4);
75
0
        RETURN_RAW_IN_SIZE(TYPE_IPV6);
76
0
#undef RETURN_RAW_IN_SIZE
77
70
    default:
78
70
        return 0;
79
506
    }
80
506
}
81
82
} // namespace
83
84
VInPredicate::VInPredicate(const TExprNode& node)
85
7.74k
        : VExpr(node), _is_not_in(node.in_predicate.is_not_in) {}
86
87
Status VInPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
88
3.02k
                             VExprContext* context) {
89
3.02k
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
90
91
3.02k
    if (_children.empty()) {
92
0
        return Status::InternalError("no Function operator in.");
93
0
    }
94
95
3.02k
    _expr_name =
96
3.02k
            fmt::format("({} {} set)", _children[0]->expr_name(), _is_not_in ? "not_in" : "in");
97
98
3.02k
    ColumnsWithTypeAndName argument_template;
99
3.02k
    argument_template.reserve(get_num_children());
100
10.6k
    for (auto child : _children) {
101
10.6k
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
102
10.6k
    }
103
104
    // construct the proper function_name
105
3.02k
    std::string head(_is_not_in ? "not_" : "");
106
3.02k
    std::string real_function_name = head + std::string(function_name);
107
3.02k
    auto arg_type = remove_nullable(argument_template[0].type);
108
3.02k
    if (is_complex_type(arg_type->get_primitive_type())) {
109
12
        real_function_name = "collection_" + real_function_name;
110
12
    }
111
3.02k
    _function = SimpleFunctionFactory::instance().get_function(real_function_name,
112
3.02k
                                                               argument_template, _data_type, {});
113
3.02k
    if (_function == nullptr) {
114
0
        return Status::NotSupported("Function {} is not implemented", real_function_name);
115
0
    }
116
117
3.02k
    VExpr::register_function_context(state, context);
118
3.02k
    _prepare_finished = true;
119
120
3.02k
    if (state->query_options().__isset.in_list_value_count_threshold) {
121
3.00k
        _in_list_value_count_threshold = state->query_options().in_list_value_count_threshold;
122
3.00k
    }
123
3.02k
    return Status::OK();
124
3.02k
}
125
126
Status VInPredicate::open(RuntimeState* state, VExprContext* context,
127
11.6k
                          FunctionContext::FunctionStateScope scope) {
128
11.6k
    DCHECK(_prepare_finished);
129
41.2k
    for (auto& child : _children) {
130
41.2k
        RETURN_IF_ERROR(child->open(state, context, scope));
131
41.2k
    }
132
11.6k
    RETURN_IF_ERROR(VExpr::init_function_context(state, context, scope, _function));
133
11.6k
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
134
3.03k
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
135
3.03k
    }
136
137
11.6k
    _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(),
138
29.7k
                                        [](const VExprSPtr& expr) { return expr->is_constant(); });
139
11.6k
    if (scope == FunctionContext::FRAGMENT_LOCAL && _is_args_all_constant &&
140
11.6k
        !_zonemap_materialized) {
141
3.03k
        RETURN_IF_ERROR(_materialize_for_zonemap_filter(context));
142
3.03k
    }
143
11.6k
    _open_finished = true;
144
11.6k
    return Status::OK();
145
11.6k
}
146
147
11.7k
void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
148
11.7k
    VExpr::close_function_context(context, scope, _function);
149
11.7k
    VExpr::close(context, scope);
150
11.7k
}
151
152
1.32k
Status VInPredicate::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
153
1.32k
    DCHECK_GE(get_num_children(), 2);
154
1.32k
    return _evaluate_inverted_index(context, _function, segment_num_rows);
155
1.32k
}
156
157
3.03k
Status VInPredicate::_materialize_for_zonemap_filter(VExprContext* context) {
158
3.03k
    _seg_filter_values.clear();
159
3.03k
    _seg_filter_contains_null = false;
160
3.03k
    _zonemap_materialized = false;
161
3.03k
    _direct_filter_set.reset();
162
3.03k
    if (_children.size() < 2 || !_children[0]->is_slot_ref()) {
163
106
        return Status::OK();
164
106
    }
165
166
2.92k
    const auto data_type = remove_nullable(_children[0]->data_type());
167
2.92k
    DORIS_CHECK(data_type != nullptr);
168
2.92k
    if (is_complex_type(data_type->get_primitive_type())) {
169
6
        return Status::OK();
170
6
    }
171
172
2.92k
    DORIS_CHECK(context != nullptr);
173
2.92k
    auto* fn_ctx = context->fn_context(_fn_context_index);
174
2.92k
    DORIS_CHECK(fn_ctx != nullptr);
175
2.92k
    auto* in_state =
176
2.92k
            reinterpret_cast<InState*>(fn_ctx->get_function_state(FunctionContext::FRAGMENT_LOCAL));
177
2.92k
    DORIS_CHECK(in_state != nullptr);
178
2.92k
    DORIS_CHECK(in_state->use_set);
179
2.92k
    DORIS_CHECK(in_state->hybrid_set != nullptr);
180
2.92k
    _direct_filter_set = in_state->hybrid_set;
181
182
2.92k
    expr_zonemap::InZonemapMaterializedSet materialized;
183
2.92k
    RETURN_IF_ERROR(expr_zonemap::materialize_hybrid_set_for_zonemap_filter(
184
2.92k
            *in_state->hybrid_set, data_type, &materialized));
185
2.92k
    _seg_filter_contains_null = materialized.contains_null;
186
2.92k
    _seg_filter_values = std::move(materialized.values);
187
2.92k
    _seg_filter_min = std::move(materialized.min_value);
188
2.92k
    _seg_filter_max = std::move(materialized.max_value);
189
2.92k
    _zonemap_materialized = true;
190
2.92k
    return Status::OK();
191
2.92k
}
192
193
2.53k
ZoneMapFilterResult VInPredicate::evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const {
194
2.53k
    if (_is_not_in && _seg_filter_contains_null) {
195
1.01k
        return ZoneMapFilterResult::kNoMatch;
196
1.01k
    }
197
1.52k
    return expr_zonemap::eval_in_zonemap(ctx, get_child(0), _is_not_in, _seg_filter_values,
198
1.52k
                                         _seg_filter_min, _seg_filter_max);
199
2.53k
}
200
201
7.98k
bool VInPredicate::can_evaluate_zonemap_filter() const {
202
7.98k
    return _zonemap_materialized && std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
203
7.98k
}
204
205
ZoneMapFilterResult VInPredicate::evaluate_dictionary_filter(
206
162
        const DictionaryEvalContext& ctx) const {
207
162
    return expr_zonemap::eval_in_dictionary(ctx, get_child(0), _is_not_in, _seg_filter_values);
208
162
}
209
210
735
bool VInPredicate::can_evaluate_dictionary_filter() const {
211
738
    return _zonemap_materialized && !_is_not_in &&
212
736
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
213
735
}
214
215
2
ZoneMapFilterResult VInPredicate::evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const {
216
2
    return expr_zonemap::eval_in_bloom_filter(ctx, get_child(0), _is_not_in, _seg_filter_values);
217
2
}
218
219
235
bool VInPredicate::can_evaluate_bloom_filter() const {
220
235
    return _zonemap_materialized && !_is_not_in &&
221
235
           std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
222
235
}
223
224
bool VInPredicate::can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
225
397
                                                   int column_id) const {
226
397
    if (!_zonemap_materialized || _direct_filter_set == nullptr || data_type == nullptr ||
227
397
        !_is_args_all_constant) {
228
0
        return false;
229
0
    }
230
397
    const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
231
397
    if (slot == nullptr || slot->column_id() != column_id || slot->data_type() == nullptr) {
232
0
        return false;
233
0
    }
234
397
    const auto raw_type = remove_nullable(data_type);
235
397
    return remove_nullable(slot->data_type())->equals(*raw_type) &&
236
397
           raw_in_value_size(raw_type->get_primitive_type()) != 0;
237
397
}
238
239
Status VInPredicate::execute_on_raw_fixed_values(const uint8_t* values, size_t num_values,
240
                                                 size_t value_width, const DataTypePtr& data_type,
241
109
                                                 int column_id, uint8_t* matches) const {
242
109
    if (!can_execute_on_raw_fixed_values(data_type, column_id)) {
243
0
        return Status::NotSupported("IN predicate cannot evaluate raw fixed-width values");
244
0
    }
245
109
    DORIS_CHECK(values != nullptr || num_values == 0);
246
109
    DORIS_CHECK(matches != nullptr || num_values == 0);
247
109
    const size_t expected_width =
248
109
            raw_in_value_size(remove_nullable(data_type)->get_primitive_type());
249
109
    if (value_width != expected_width) {
250
0
        return Status::Corruption("Raw IN width {} does not match expected {}", value_width,
251
0
                                  expected_width);
252
0
    }
253
    // NOT IN with a NULL literal is UNKNOWN for every non-null physical value. Definition levels
254
    // handle input NULLs, while this guard preserves the remaining three-valued SQL invariant.
255
109
    if (_is_not_in && _seg_filter_contains_null) {
256
0
        std::fill(matches, matches + num_values, uint8_t {0});
257
109
    } else if (_is_not_in) {
258
0
        _direct_filter_set->find_batch_raw_fixed_negative(values, num_values, value_width, matches);
259
109
    } else {
260
109
        _direct_filter_set->find_batch_raw_fixed(values, num_values, value_width, matches);
261
109
    }
262
109
    return Status::OK();
263
109
}
264
265
bool VInPredicate::can_execute_on_raw_binary_values(const DataTypePtr& data_type,
266
94
                                                    int column_id) const {
267
94
    if (!_zonemap_materialized || _direct_filter_set == nullptr || data_type == nullptr ||
268
94
        !_is_args_all_constant ||
269
94
        !is_string_type(remove_nullable(data_type)->get_primitive_type())) {
270
33
        return false;
271
33
    }
272
61
    const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
273
61
    return slot != nullptr && slot->column_id() == column_id && slot->data_type() != nullptr &&
274
61
           is_string_type(remove_nullable(slot->data_type())->get_primitive_type());
275
94
}
276
277
Status VInPredicate::execute_on_raw_binary_values(const StringRef* values, size_t num_values,
278
                                                  const DataTypePtr& data_type, int column_id,
279
11
                                                  uint8_t* matches) const {
280
11
    if (!can_execute_on_raw_binary_values(data_type, column_id)) {
281
0
        return Status::NotSupported("IN predicate cannot evaluate raw binary values");
282
0
    }
283
11
    DORIS_CHECK(values != nullptr || num_values == 0);
284
11
    DORIS_CHECK(matches != nullptr || num_values == 0);
285
11
    if (_is_not_in && _seg_filter_contains_null) {
286
0
        std::fill(matches, matches + num_values, uint8_t {0});
287
11
    } else if (_is_not_in) {
288
1
        _direct_filter_set->find_batch_raw_binary_negative(values, num_values, matches);
289
10
    } else {
290
        // Decoder-owned slices remain valid for the whole batch, so regular SQL IN/NOT IN can
291
        // probe the prepared hash set without constructing and then compacting a ColumnString.
292
10
        _direct_filter_set->find_batch_raw_binary(values, num_values, matches);
293
10
    }
294
11
    return Status::OK();
295
11
}
296
297
Status VInPredicate::execute_column_impl(VExprContext* context, const Block* block,
298
                                         const Selector* selector, size_t count,
299
10.0k
                                         ColumnPtr& result_column) const {
300
10.0k
    if (is_const_and_have_executed()) { // const have execute in open function
301
7
        result_column = get_result_from_const(count);
302
7
        return Status::OK();
303
7
    }
304
10.0k
    if (fast_execute(context, selector, count, result_column)) {
305
15
        return Status::OK();
306
15
    }
307
10.0k
    DCHECK(_open_finished || block == nullptr);
308
309
    // This is an optimization. For expressions like colA IN (1, 2, 3, 4),
310
    // where all values inside the IN clause are constants,
311
    // a hash set is created during open, and it will not be accessed again during execute
312
    //  Here, _children[0] is colA
313
10.0k
    const size_t args_size = _is_args_all_constant ? 1 : _children.size();
314
315
10.0k
    ColumnNumbers arguments;
316
10.0k
    arguments.reserve(args_size);
317
10.0k
    Block temp_block;
318
20.1k
    for (int i = 0; i < args_size; ++i) {
319
10.0k
        ColumnPtr column;
320
10.0k
        RETURN_IF_ERROR(_children[i]->execute_column(context, block, selector, count, column));
321
10.0k
        arguments.push_back(i);
322
10.0k
        temp_block.insert({column, _children[i]->execute_type(block), _children[i]->expr_name()});
323
10.0k
    }
324
325
10.0k
    int num_columns_without_result = temp_block.columns();
326
10.0k
    temp_block.insert({nullptr, _data_type, _expr_name});
327
328
10.0k
    RETURN_IF_ERROR(_function->execute(context->fn_context(_fn_context_index), temp_block,
329
10.0k
                                       arguments, num_columns_without_result, temp_block.rows()));
330
10.0k
    result_column = temp_block.get_by_position(num_columns_without_result).column;
331
10.0k
    DCHECK_EQ(result_column->size(), count);
332
10.0k
    return Status::OK();
333
10.0k
}
334
335
0
size_t VInPredicate::estimate_memory(const size_t rows) {
336
0
    if (is_const_and_have_executed()) {
337
0
        return 0;
338
0
    }
339
340
0
    size_t estimate_size = 0;
341
342
0
    for (int i = 0; i < _children.size(); ++i) {
343
0
        estimate_size += _children[i]->estimate_memory(rows);
344
0
    }
345
346
0
    if (_data_type->is_nullable()) {
347
0
        estimate_size += rows * sizeof(uint8_t);
348
0
    }
349
350
0
    estimate_size += rows * sizeof(uint8_t);
351
352
0
    return estimate_size;
353
0
}
354
355
1.52k
const std::string& VInPredicate::expr_name() const {
356
1.52k
    return _expr_name;
357
1.52k
}
358
359
38
std::string VInPredicate::debug_string() const {
360
38
    std::stringstream out;
361
38
    out << "InPredicate(" << children()[0]->debug_string() << " " << _is_not_in << ",[";
362
38
    int num_children = get_num_children();
363
364
120
    for (int i = 1; i < num_children; ++i) {
365
82
        out << (i == 1 ? "" : " ") << children()[i]->debug_string();
366
82
    }
367
368
38
    out << "])";
369
38
    return out.str();
370
38
}
371
372
} // namespace doris