Coverage Report

Created: 2026-06-12 10:04

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