Coverage Report

Created: 2026-02-25 19:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/vec/exprs/vsearch.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 "vec/exprs/vsearch.h"
19
20
#include <memory>
21
#include <roaring/roaring.hh>
22
23
#include "common/logging.h"
24
#include "common/status.h"
25
#include "glog/logging.h"
26
#include "olap/rowset/segment_v2/inverted_index_reader.h"
27
#include "runtime/runtime_state.h"
28
#include "vec/columns/column_const.h"
29
#include "vec/exprs/vexpr_context.h"
30
#include "vec/exprs/vliteral.h"
31
#include "vec/exprs/vslot_ref.h"
32
#include "vec/functions/function_search.h"
33
34
namespace doris::vectorized {
35
using namespace segment_v2;
36
37
namespace {
38
39
struct SearchInputBundle {
40
    std::unordered_map<std::string, IndexIterator*> iterators;
41
    std::unordered_map<std::string, vectorized::IndexFieldNameAndTypePair> field_types;
42
    std::vector<int> column_ids;
43
    vectorized::ColumnsWithTypeAndName literal_args;
44
};
45
46
Status collect_search_inputs(const VSearchExpr& expr, VExprContext* context,
47
4
                             SearchInputBundle* bundle) {
48
4
    DCHECK(bundle != nullptr);
49
50
4
    auto index_context = context->get_index_context();
51
4
    if (index_context == nullptr) {
52
0
        LOG(WARNING) << "collect_search_inputs: No inverted index context available";
53
0
        return Status::InternalError("No inverted index context available");
54
0
    }
55
56
    // Get field bindings for variant subcolumn support
57
4
    const auto& search_param = expr.get_search_param();
58
4
    const auto& field_bindings = search_param.field_bindings;
59
60
4
    int child_index = 0; // Index for iterating through children
61
4
    for (const auto& child : expr.children()) {
62
4
        if (child->is_slot_ref()) {
63
3
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
64
3
            int column_id = column_slot_ref->column_id();
65
3
            auto* iterator = index_context->get_inverted_index_iterator_by_column_id(column_id);
66
67
            // Determine the field_name from field_bindings (for variant subcolumns)
68
            // field_bindings and children should have the same order
69
3
            std::string field_name;
70
3
            if (child_index < field_bindings.size()) {
71
                // Use field_name from binding (may include "parent.subcolumn" for variant)
72
3
                field_name = field_bindings[child_index].field_name;
73
3
            } else {
74
                // Fallback to column_name if binding not found
75
0
                field_name = column_slot_ref->column_name();
76
0
            }
77
78
            // Only collect fields that have iterators (materialized columns with indexes)
79
3
            if (iterator != nullptr) {
80
2
                const auto* storage_name_type =
81
2
                        index_context->get_storage_name_and_type_by_column_id(column_id);
82
2
                if (storage_name_type == nullptr) {
83
1
                    return Status::InternalError("storage_name_type not found for column {} in {}",
84
1
                                                 column_id, expr.expr_name());
85
1
                }
86
87
1
                bundle->iterators.emplace(field_name, iterator);
88
1
                bundle->field_types.emplace(field_name, *storage_name_type);
89
1
                bundle->column_ids.emplace_back(column_id);
90
1
            }
91
92
2
            child_index++;
93
2
        } else if (child->is_literal()) {
94
0
            auto* literal = assert_cast<VLiteral*>(child.get());
95
0
            bundle->literal_args.emplace_back(literal->get_column_ptr(), literal->get_data_type(),
96
0
                                              literal->expr_name());
97
1
        } else {
98
            // Check if this is ElementAt expression (for variant subcolumn access)
99
1
            if (child->expr_name() == "element_at" && child_index < field_bindings.size() &&
100
1
                field_bindings[child_index].__isset.is_variant_subcolumn &&
101
1
                field_bindings[child_index].is_variant_subcolumn) {
102
                // Variant subcolumn not materialized - skip, will create empty BitSetQuery in function_search
103
0
                child_index++;
104
0
                continue;
105
0
            }
106
107
            // Not a supported child type
108
1
            return Status::InvalidArgument("Unsupported child node type: {}", child->expr_name());
109
1
        }
110
4
    }
111
112
2
    return Status::OK();
113
4
}
114
115
} // namespace
116
117
77
VSearchExpr::VSearchExpr(const TExprNode& node) : VExpr(node) {
118
77
    if (node.__isset.search_param) {
119
73
        _search_param = node.search_param;
120
73
        _original_dsl = _search_param.original_dsl;
121
73
    }
122
77
}
123
124
Status VSearchExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc,
125
0
                            VExprContext* context) {
126
0
    RETURN_IF_ERROR(VExpr::prepare(state, row_desc, context));
127
0
    const auto& query_options = state->query_options();
128
0
    if (query_options.__isset.enable_inverted_index_query_cache) {
129
0
        _enable_cache = query_options.enable_inverted_index_query_cache;
130
0
    }
131
0
    return Status::OK();
132
0
}
133
134
4
const std::string& VSearchExpr::expr_name() const {
135
4
    static const std::string name = "VSearchExpr";
136
4
    return name;
137
4
}
138
139
Status VSearchExpr::execute_column(VExprContext* context, const Block* block, Selector* selector,
140
3
                                   size_t count, ColumnPtr& result_column) const {
141
3
    if (fast_execute(context, selector, count, result_column)) {
142
1
        return Status::OK();
143
1
    }
144
145
2
    return Status::InternalError("SearchExpr should not be executed without inverted index");
146
3
}
147
148
22
Status VSearchExpr::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
149
22
    LOG(INFO) << "VSearchExpr::evaluate_inverted_index called, DSL: " << _search_param.original_dsl;
150
151
22
    if (_search_param.original_dsl.empty()) {
152
3
        return Status::InvalidArgument("search DSL is empty");
153
3
    }
154
155
19
    auto index_context = context->get_index_context();
156
19
    if (!index_context) {
157
15
        LOG(WARNING) << "VSearchExpr: No inverted index context available";
158
15
        return Status::OK();
159
15
    }
160
161
4
    SearchInputBundle bundle;
162
4
    RETURN_IF_ERROR(collect_search_inputs(*this, context, &bundle));
163
164
2
    VLOG_DEBUG << "VSearchExpr: bundle.iterators.size()=" << bundle.iterators.size();
165
166
2
    if (bundle.iterators.empty()) {
167
1
        LOG(WARNING) << "VSearchExpr: No indexed columns available for evaluation, DSL: "
168
1
                     << _original_dsl;
169
1
        auto empty_bitmap = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(),
170
1
                                                      std::make_shared<roaring::Roaring>());
171
1
        index_context->set_index_result_for_expr(this, std::move(empty_bitmap));
172
1
        return Status::OK();
173
1
    }
174
175
1
    auto function = std::make_shared<FunctionSearch>();
176
1
    auto result_bitmap = InvertedIndexResultBitmap();
177
1
    auto status = function->evaluate_inverted_index_with_search_param(
178
1
            _search_param, bundle.field_types, bundle.iterators, segment_num_rows, result_bitmap,
179
1
            _enable_cache);
180
181
1
    if (!status.ok()) {
182
1
        LOG(WARNING) << "VSearchExpr: Function evaluation failed: " << status.to_string();
183
1
        return status;
184
1
    }
185
186
0
    index_context->set_index_result_for_expr(this, result_bitmap);
187
0
    for (int column_id : bundle.column_ids) {
188
0
        index_context->set_true_for_index_status(this, column_id);
189
0
    }
190
191
0
    return Status::OK();
192
1
}
193
194
} // namespace doris::vectorized