Coverage Report

Created: 2026-06-25 13:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vexpr_context.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/vexpr_context.h"
19
20
#include <algorithm>
21
#include <cstdint>
22
#include <string>
23
24
#include "common/compiler_util.h" // IWYU pragma: keep
25
#include "common/exception.h"
26
#include "common/status.h"
27
#include "core/block/column_numbers.h"
28
#include "core/block/column_with_type_and_name.h"
29
#include "core/block/columns_with_type_and_name.h"
30
#include "core/column/column.h"
31
#include "core/column/column_const.h"
32
#include "exec/common/util.hpp"
33
#include "exprs/function_context.h"
34
#include "exprs/vexpr.h"
35
#include "runtime/runtime_state.h"
36
#include "runtime/thread_context.h"
37
#include "storage/olap_common.h"
38
#include "storage/segment/column_reader.h"
39
#include "util/simd/bits.h"
40
41
namespace doris {
42
class RowDescriptor;
43
} // namespace doris
44
45
namespace doris {
46
47
18.8M
VExprContext::~VExprContext() {
48
    // In runtime filter, only create expr context to get expr root, will not call
49
    // prepare or open, so that it is not need to call close. And call close may core
50
    // because the function context in expr is not set.
51
18.8M
    if (!_prepared || !_opened) {
52
248k
        return;
53
248k
    }
54
18.5M
    try {
55
18.5M
        close();
56
18.5M
    } catch (const Exception& e) {
57
0
        LOG(WARNING) << "Exception occurs when expr context deconstruct: " << e.to_string();
58
0
    }
59
18.5M
}
60
61
1.11M
Status VExprContext::execute(Block* block, int* result_column_id) {
62
1.11M
    Status st;
63
1.11M
    RETURN_IF_CATCH_EXCEPTION({
64
1.11M
        st = _root->execute(this, block, result_column_id);
65
1.11M
        _last_result_column_id = *result_column_id;
66
        // We should first check the status, as some expressions might incorrectly set result_column_id, even if the st is not ok.
67
1.11M
        if (st.ok() && _last_result_column_id != -1) {
68
1.11M
            block->get_by_position(*result_column_id).column->sanity_check();
69
1.11M
            RETURN_IF_ERROR(
70
1.11M
                    block->get_by_position(*result_column_id).check_type_and_column_match());
71
1.11M
        }
72
1.11M
    });
73
1.11M
    return st;
74
1.11M
}
75
76
1.97M
Status VExprContext::execute(const Block* block, ColumnPtr& result_column) {
77
1.97M
    Status st;
78
1.97M
    RETURN_IF_CATCH_EXCEPTION(
79
1.97M
            { st = _root->execute_column(this, block, nullptr, block->rows(), result_column); });
80
1.97M
    return st;
81
1.97M
}
82
83
28.7k
Status VExprContext::execute(const Block* block, ColumnWithTypeAndName& result_data) {
84
28.7k
    Status st;
85
28.7k
    ColumnPtr result_column;
86
28.7k
    RETURN_IF_CATCH_EXCEPTION(
87
28.7k
            { st = _root->execute_column(this, block, nullptr, block->rows(), result_column); });
88
28.7k
    RETURN_IF_ERROR(st);
89
28.7k
    result_data.column = result_column;
90
28.7k
    result_data.type = execute_type(block);
91
28.7k
    result_data.name = _root->expr_name();
92
28.7k
    return Status::OK();
93
28.7k
}
94
95
812k
DataTypePtr VExprContext::execute_type(const Block* block) {
96
812k
    return _root->execute_type(block);
97
812k
}
98
99
1.03M
Status VExprContext::execute_const_expr(ColumnWithTypeAndName& result) {
100
1.03M
    Status st;
101
1.03M
    RETURN_IF_CATCH_EXCEPTION(
102
1.03M
            { st = _root->execute_column(this, nullptr, nullptr, 1, result.column); });
103
1.03M
    RETURN_IF_ERROR(st);
104
1.03M
    result.type = _root->execute_type(nullptr);
105
1.03M
    result.name = _root->expr_name();
106
1.03M
    return Status::OK();
107
1.03M
}
108
109
783k
[[nodiscard]] const std::string& VExprContext::expr_name() const {
110
783k
    return _root->expr_name();
111
783k
}
112
113
0
bool VExprContext::is_blockable() const {
114
0
    return _root->is_blockable();
115
0
}
116
117
4.98M
Status VExprContext::prepare(RuntimeState* state, const RowDescriptor& row_desc) {
118
4.98M
    _prepared = true;
119
4.98M
    Status st;
120
4.98M
    RETURN_IF_CATCH_EXCEPTION({ st = _root->prepare(state, row_desc, this); });
121
4.98M
    return st;
122
4.98M
}
123
124
4.98M
Status VExprContext::open(RuntimeState* state) {
125
4.98M
    DCHECK(_prepared);
126
4.98M
    if (_opened) {
127
47
        return Status::OK();
128
47
    }
129
4.98M
    _opened = true;
130
    // Fragment-local state is only initialized for original contexts. Clones inherit the
131
    // original's fragment state and only need to have thread-local state initialized.
132
4.98M
    FunctionContext::FunctionStateScope scope =
133
4.98M
            _is_clone ? FunctionContext::THREAD_LOCAL : FunctionContext::FRAGMENT_LOCAL;
134
4.98M
    Status st;
135
4.98M
    RETURN_IF_CATCH_EXCEPTION({ st = _root->open(state, this, scope); });
136
4.98M
    return st;
137
4.98M
}
138
139
18.5M
void VExprContext::close() {
140
    // Sometimes expr context may not have a root, then it need not call close
141
18.5M
    if (_root == nullptr) {
142
0
        return;
143
0
    }
144
18.5M
    FunctionContext::FunctionStateScope scope =
145
18.5M
            _is_clone ? FunctionContext::THREAD_LOCAL : FunctionContext::FRAGMENT_LOCAL;
146
18.5M
    _root->close(this, scope);
147
18.5M
}
148
149
13.5M
Status VExprContext::clone(RuntimeState* state, VExprContextSPtr& new_ctx) {
150
18.4E
    DCHECK(_prepared) << "expr context not prepared";
151
13.5M
    DCHECK(_opened);
152
13.5M
    DCHECK(new_ctx.get() == nullptr);
153
154
13.5M
    new_ctx = std::make_shared<VExprContext>(_root);
155
13.5M
    for (auto& _fn_context : _fn_contexts) {
156
1.16M
        new_ctx->_fn_contexts.push_back(_fn_context->clone());
157
1.16M
    }
158
159
13.5M
    new_ctx->_is_clone = true;
160
13.5M
    new_ctx->_prepared = true;
161
13.5M
    new_ctx->_opened = true;
162
    // segment_v2::AnnRangeSearchRuntime should be cloned as well.
163
    // The object of segment_v2::AnnRangeSearchRuntime is not shared by threads.
164
13.5M
    new_ctx->_ann_range_search_runtime = this->_ann_range_search_runtime;
165
166
13.5M
    return _root->open(state, new_ctx.get(), FunctionContext::THREAD_LOCAL);
167
13.5M
}
168
169
0
void VExprContext::clone_fn_contexts(VExprContext* other) {
170
0
    for (auto& _fn_context : _fn_contexts) {
171
0
        other->_fn_contexts.push_back(_fn_context->clone());
172
0
    }
173
0
}
174
175
int VExprContext::register_function_context(RuntimeState* state, const DataTypePtr& return_type,
176
607k
                                            const std::vector<DataTypePtr>& arg_types) {
177
607k
    _fn_contexts.push_back(FunctionContext::create_context(state, return_type, arg_types));
178
607k
    _fn_contexts.back()->set_check_overflow_for_decimal(state->check_overflow_for_decimal());
179
607k
    _fn_contexts.back()->set_enable_strict_mode(state->enable_strict_mode());
180
607k
    return static_cast<int>(_fn_contexts.size()) - 1;
181
607k
}
182
183
18.4k
Status VExprContext::evaluate_inverted_index(uint32_t segment_num_rows) {
184
18.4k
    Status st;
185
18.4k
    RETURN_IF_CATCH_EXCEPTION({ st = _root->evaluate_inverted_index(this, segment_num_rows); });
186
18.4k
    return st;
187
18.4k
}
188
189
18.1k
bool VExprContext::all_expr_inverted_index_evaluated() {
190
18.1k
    return _index_context->has_index_result_for_expr(_root.get());
191
18.1k
}
192
193
50
Status VExprContext::filter_block(VExprContext* vexpr_ctx, Block* block) {
194
50
    if (vexpr_ctx == nullptr || block->rows() == 0) {
195
0
        return Status::OK();
196
0
    }
197
50
    ColumnPtr filter_column;
198
50
    RETURN_IF_ERROR(vexpr_ctx->execute(block, filter_column));
199
50
    size_t filter_column_id = block->columns();
200
50
    block->insert({filter_column, vexpr_ctx->execute_type(block), "filter_column"});
201
50
    vexpr_ctx->_memory_usage = filter_column->allocated_bytes();
202
50
    return Block::filter_block(block, filter_column_id, filter_column_id);
203
50
}
204
205
Status VExprContext::filter_block(const VExprContextSPtrs& expr_contexts, Block* block,
206
1.30M
                                  size_t column_to_keep) {
207
1.30M
    if (expr_contexts.empty() || block->rows() == 0) {
208
1.29M
        return Status::OK();
209
1.29M
    }
210
211
16.3k
    ColumnNumbers columns_to_filter(column_to_keep);
212
16.3k
    std::iota(columns_to_filter.begin(), columns_to_filter.end(), 0);
213
214
16.3k
    return execute_conjuncts_and_filter_block(expr_contexts, block, columns_to_filter,
215
16.3k
                                              static_cast<int>(column_to_keep));
216
1.30M
}
217
218
Status VExprContext::execute_conjuncts(const VExprContextSPtrs& ctxs,
219
                                       const std::vector<IColumn::Filter*>* filters, Block* block,
220
2.82k
                                       IColumn::Filter* result_filter, bool* can_filter_all) {
221
2.82k
    return execute_conjuncts(ctxs, filters, false, block, result_filter, can_filter_all);
222
2.82k
}
223
224
Status VExprContext::execute_filter(const Block* block, uint8_t* __restrict result_filter_data,
225
118k
                                    size_t rows, bool accept_null, bool* can_filter_all) {
226
118k
    return _root->execute_filter(this, block, result_filter_data, rows, accept_null,
227
118k
                                 can_filter_all);
228
118k
}
229
230
Status VExprContext::execute_conjuncts(const VExprContextSPtrs& ctxs,
231
                                       const std::vector<IColumn::Filter*>* filters,
232
                                       bool accept_null, const Block* block,
233
316k
                                       IColumn::Filter* result_filter, bool* can_filter_all) {
234
316k
    size_t rows = block->rows();
235
316k
    DCHECK_EQ(result_filter->size(), rows);
236
316k
    *can_filter_all = false;
237
316k
    auto* __restrict result_filter_data = result_filter->data();
238
316k
    for (const auto& ctx : ctxs) {
239
118k
        RETURN_IF_ERROR(
240
118k
                ctx->execute_filter(block, result_filter_data, rows, accept_null, can_filter_all));
241
118k
        if (*can_filter_all) {
242
29.3k
            return Status::OK();
243
29.3k
        }
244
118k
    }
245
286k
    if (filters != nullptr) {
246
24
        for (auto* filter : *filters) {
247
0
            auto* __restrict filter_data = filter->data();
248
0
            const size_t size = filter->size();
249
0
            for (size_t i = 0; i < size; ++i) {
250
0
                result_filter_data[i] &= filter_data[i];
251
0
            }
252
0
            if (memchr(result_filter_data, 0x1, size) == nullptr) {
253
0
                *can_filter_all = true;
254
0
                return Status::OK();
255
0
            }
256
0
        }
257
24
    }
258
286k
    return Status::OK();
259
286k
}
260
261
Status VExprContext::execute_conjuncts(const VExprContextSPtrs& conjuncts, const Block* block,
262
384
                                       ColumnUInt8& null_map, IColumn::Filter& filter) {
263
384
    const auto& rows = block->rows();
264
384
    if (rows == 0) {
265
0
        return Status::OK();
266
0
    }
267
384
    if (null_map.size() != rows) {
268
0
        return Status::InternalError("null_map.size()!=rows, null_map.size()={}, rows={}",
269
0
                                     null_map.size(), rows);
270
0
    }
271
272
384
    auto* final_null_map = null_map.get_data().data();
273
384
    auto* final_filter_ptr = filter.data();
274
275
384
    for (const auto& conjunct : conjuncts) {
276
77
        ColumnPtr result_column;
277
77
        RETURN_IF_ERROR(conjunct->execute(block, result_column));
278
77
        auto [filter_column, is_const] = unpack_if_const(result_column);
279
77
        const auto* nullable_column = assert_cast<const ColumnNullable*>(filter_column.get());
280
77
        if (!is_const) {
281
65
            const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr();
282
65
            const IColumn::Filter& result =
283
65
                    assert_cast<const ColumnUInt8&>(*nested_column).get_data();
284
65
            const auto* __restrict filter_data = result.data();
285
65
            const auto* __restrict null_map_data = nullable_column->get_null_map_data().data();
286
65
            DCHECK_EQ(rows, nullable_column->size());
287
288
805
            for (size_t i = 0; i != rows; ++i) {
289
                // null and null    => null
290
                // null and true    => null
291
                // null and false   => false
292
740
                final_null_map[i] = (final_null_map[i] & (null_map_data[i] | filter_data[i])) |
293
740
                                    (null_map_data[i] & (final_null_map[i] | final_filter_ptr[i]));
294
740
                final_filter_ptr[i] = final_filter_ptr[i] & filter_data[i];
295
740
            }
296
65
        } else {
297
12
            bool filter_data = nullable_column->get_bool(0);
298
12
            bool null_map_data = nullable_column->is_null_at(0);
299
68
            for (size_t i = 0; i != rows; ++i) {
300
                // null and null    => null
301
                // null and true    => null
302
                // null and false   => false
303
56
                final_null_map[i] = (final_null_map[i] & (null_map_data | filter_data)) |
304
56
                                    (null_map_data & (final_null_map[i] | final_filter_ptr[i]));
305
56
                final_filter_ptr[i] = final_filter_ptr[i] & filter_data;
306
56
            }
307
12
        }
308
77
    }
309
384
    return Status::OK();
310
384
}
311
312
// TODO Performance Optimization
313
// need exception safety
314
Status VExprContext::execute_conjuncts_and_filter_block(const VExprContextSPtrs& ctxs, Block* block,
315
                                                        std::vector<uint32_t>& columns_to_filter,
316
17.6k
                                                        int column_to_keep) {
317
17.6k
    IColumn::Filter result_filter(block->rows(), 1);
318
17.6k
    bool can_filter_all;
319
320
17.6k
    _reset_memory_usage(ctxs);
321
322
17.6k
    RETURN_IF_ERROR(
323
17.6k
            execute_conjuncts(ctxs, nullptr, false, block, &result_filter, &can_filter_all));
324
325
    // Accumulate the usage of `result_filter` into the first context.
326
17.6k
    if (!ctxs.empty()) {
327
17.6k
        ctxs[0]->_memory_usage += result_filter.allocated_bytes();
328
17.6k
    }
329
17.6k
    if (can_filter_all) {
330
31.6k
        for (auto& col : columns_to_filter) {
331
31.6k
            auto& column = block->get_by_position(col).column;
332
31.6k
            if (column->is_exclusive()) {
333
28.8k
                column->assert_mutable()->clear();
334
28.8k
            } else {
335
2.77k
                column = column->clone_empty();
336
2.77k
            }
337
31.6k
        }
338
10.1k
    } else {
339
10.1k
        try {
340
10.1k
            Block::filter_block_internal(block, columns_to_filter, result_filter);
341
10.1k
        } catch (const Exception& e) {
342
0
            std::string str;
343
0
            for (auto ctx : ctxs) {
344
0
                if (str.length()) {
345
0
                    str += ",";
346
0
                }
347
0
                str += ctx->root()->debug_string();
348
0
            }
349
350
0
            return Status::InternalError(
351
0
                    "filter_block_internal meet exception, exprs=[{}], exception={}", str,
352
0
                    e.what());
353
0
        }
354
10.1k
    }
355
17.6k
    Block::erase_useless_column(block, column_to_keep);
356
17.6k
    return Status::OK();
357
17.6k
}
358
359
Status VExprContext::execute_conjuncts_and_filter_block(const VExprContextSPtrs& ctxs, Block* block,
360
                                                        std::vector<uint32_t>& columns_to_filter,
361
                                                        int column_to_keep,
362
11.5k
                                                        IColumn::Filter& filter) {
363
11.5k
    _reset_memory_usage(ctxs);
364
11.5k
    filter.resize_fill(block->rows(), 1);
365
11.5k
    bool can_filter_all;
366
11.5k
    RETURN_IF_ERROR(execute_conjuncts(ctxs, nullptr, false, block, &filter, &can_filter_all));
367
368
    // Accumulate the usage of `result_filter` into the first context.
369
11.5k
    if (!ctxs.empty()) {
370
11.5k
        ctxs[0]->_memory_usage += filter.allocated_bytes();
371
11.5k
    }
372
11.5k
    if (can_filter_all) {
373
6.29k
        for (auto& col : columns_to_filter) {
374
6.29k
            auto& column = block->get_by_position(col).column;
375
6.29k
            if (column->is_exclusive()) {
376
6.29k
                column->assert_mutable()->clear();
377
6.29k
            } else {
378
1
                column = column->clone_empty();
379
1
            }
380
6.29k
        }
381
8.15k
    } else {
382
8.15k
        RETURN_IF_CATCH_EXCEPTION(Block::filter_block_internal(block, columns_to_filter, filter));
383
8.15k
    }
384
385
11.5k
    Block::erase_useless_column(block, column_to_keep);
386
11.5k
    return Status::OK();
387
11.5k
}
388
389
Status VExprContext::get_output_block_after_execute_exprs(
390
163k
        const VExprContextSPtrs& output_vexpr_ctxs, const Block& input_block, Block* output_block) {
391
163k
    ColumnsWithTypeAndName result_columns;
392
163k
    _reset_memory_usage(output_vexpr_ctxs);
393
394
783k
    for (const auto& vexpr_ctx : output_vexpr_ctxs) {
395
783k
        ColumnPtr result_column;
396
783k
        RETURN_IF_ERROR(vexpr_ctx->execute(&input_block, result_column));
397
398
783k
        auto type = vexpr_ctx->execute_type(&input_block);
399
783k
        const auto& name = vexpr_ctx->expr_name();
400
401
783k
        vexpr_ctx->_memory_usage += result_column->allocated_bytes();
402
783k
        result_columns.emplace_back(result_column, type, name);
403
783k
    }
404
163k
    *output_block = {result_columns};
405
163k
    return Status::OK();
406
163k
}
407
408
192k
void VExprContext::_reset_memory_usage(const VExprContextSPtrs& contexts) {
409
192k
    std::for_each(contexts.begin(), contexts.end(),
410
822k
                  [](auto&& context) { context->_memory_usage = 0; });
411
192k
}
412
413
22.2k
void VExprContext::prepare_ann_range_search(const doris::VectorSearchUserParams& params) {
414
22.2k
    if (_root == nullptr) {
415
0
        return;
416
0
    }
417
418
22.2k
    _root->prepare_ann_range_search(params, _ann_range_search_runtime, _suitable_for_ann_index);
419
18.4E
    VLOG_DEBUG << fmt::format("Prepare ann range search result {}, _suitable_for_ann_index {}",
420
18.4E
                              this->_ann_range_search_runtime.to_string(),
421
18.4E
                              this->_suitable_for_ann_index);
422
22.2k
    return;
423
22.2k
}
424
425
Status VExprContext::evaluate_ann_range_search(
426
        const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& cid_to_index_iterators,
427
        const std::vector<ColumnId>& idx_to_cid,
428
        const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators,
429
        const std::unordered_map<VExprContext*, std::unordered_map<ColumnId, VExpr*>>&
430
                common_expr_to_slotref_map,
431
        size_t rows_of_segment, roaring::Roaring& row_bitmap,
432
        segment_v2::AnnIndexStats& ann_index_stats, bool enable_result_cache,
433
18.2k
        bool* ann_range_search_executed) {
434
18.2k
    if (ann_range_search_executed != nullptr) {
435
18.2k
        *ann_range_search_executed = false;
436
18.2k
    }
437
18.2k
    if (_root == nullptr) {
438
0
        return Status::OK();
439
0
    }
440
441
18.2k
    AnnRangeSearchEvaluationResult evaluation_result;
442
18.2k
    RETURN_IF_ERROR(_root->evaluate_ann_range_search(
443
18.2k
            _ann_range_search_runtime, cid_to_index_iterators, idx_to_cid, column_iterators,
444
18.2k
            rows_of_segment, row_bitmap, ann_index_stats, enable_result_cache, evaluation_result));
445
446
18.2k
    if (!evaluation_result.executed) {
447
18.2k
        return Status::OK();
448
18.2k
    }
449
37
    if (ann_range_search_executed != nullptr) {
450
27
        *ann_range_search_executed = true;
451
27
    }
452
453
37
    DCHECK(_index_context != nullptr);
454
37
    _index_context->set_index_result_for_expr(
455
37
            _root.get(),
456
37
            segment_v2::InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(row_bitmap),
457
37
                                                  std::make_shared<roaring::Roaring>()));
458
459
37
    if (!evaluation_result.dist_fulfilled) {
460
        // Do not perform index scan in this case.
461
2
        return Status::OK();
462
2
    }
463
464
37
    DCHECK_LT(_ann_range_search_runtime.src_col_idx, idx_to_cid.size());
465
35
    const auto src_col_idx = cast_set<int>(_ann_range_search_runtime.src_col_idx);
466
35
    const auto src_col_key = cast_set<ColumnId>(_ann_range_search_runtime.src_col_idx);
467
35
    auto slot_ref_map_it = common_expr_to_slotref_map.find(this);
468
35
    if (slot_ref_map_it == common_expr_to_slotref_map.end()) {
469
1
        return Status::OK();
470
1
    }
471
34
    auto& slot_ref_map = slot_ref_map_it->second;
472
34
    auto slot_ref_it = slot_ref_map.find(src_col_key);
473
34
    if (slot_ref_it == slot_ref_map.end()) {
474
0
        return Status::OK();
475
0
    }
476
34
    const VExpr* slot_ref_expr_addr = slot_ref_it->second;
477
34
    _index_context->set_true_for_index_status(slot_ref_expr_addr, src_col_idx);
478
479
34
    VLOG_DEBUG << fmt::format(
480
10
            "Evaluate ann range search for expr {}, src_col_idx {}, cid {}, row_bitmap "
481
10
            "cardinality {}",
482
10
            _root->debug_string(), src_col_idx, idx_to_cid[_ann_range_search_runtime.src_col_idx],
483
10
            row_bitmap.cardinality());
484
34
    return Status::OK();
485
34
}
486
487
286k
uint64_t VExprContext::get_digest(uint64_t seed) const {
488
286k
    return _root->get_digest(seed);
489
286k
}
490
491
649k
double VExprContext::execute_cost() const {
492
649k
    if (_root == nullptr) {
493
        // When there is no expression root, treat the cost as a base value.
494
        // This avoids null dereferences while keeping a deterministic cost.
495
0
        return 0.0;
496
0
    }
497
649k
    return _root->execute_cost();
498
649k
}
499
500
} // namespace doris