Coverage Report

Created: 2026-03-11 04:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/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 "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 "core/column/column_const.h"
26
#include "exprs/function/function_search.h"
27
#include "exprs/vexpr_context.h"
28
#include "exprs/vliteral.h"
29
#include "exprs/vslot_ref.h"
30
#include "glog/logging.h"
31
#include "runtime/runtime_state.h"
32
#include "storage/index/inverted/inverted_index_reader.h"
33
#include "storage/segment/segment.h"
34
35
namespace doris {
36
using namespace segment_v2;
37
38
namespace {
39
40
struct SearchInputBundle {
41
    std::unordered_map<std::string, IndexIterator*> iterators;
42
    std::unordered_map<std::string, IndexFieldNameAndTypePair> field_types;
43
    std::unordered_map<std::string, int> field_name_to_column_id;
44
    std::vector<int> column_ids;
45
    ColumnsWithTypeAndName literal_args;
46
};
47
48
Status collect_search_inputs(const VSearchExpr& expr, VExprContext* context,
49
1.16k
                             SearchInputBundle* bundle) {
50
1.16k
    DCHECK(bundle != nullptr);
51
52
1.16k
    auto index_context = context->get_index_context();
53
1.16k
    if (index_context == nullptr) {
54
0
        LOG(WARNING) << "collect_search_inputs: No inverted index context available";
55
0
        return Status::InternalError("No inverted index context available");
56
0
    }
57
58
    // Get field bindings for variant subcolumn support
59
1.16k
    const auto& search_param = expr.get_search_param();
60
1.16k
    const auto& field_bindings = search_param.field_bindings;
61
62
1.16k
    std::unordered_map<std::string, ColumnId> parent_to_base_column_id;
63
1.16k
    std::unordered_map<std::string, std::string> parent_to_storage_field_prefix;
64
65
    // Resolve and cache the base (parent) column id for a variant field binding.
66
    // This avoids repeated schema lookups when multiple subcolumns share the same parent column.
67
1.16k
    auto resolve_parent_column_id = [&](const std::string& parent_field, ColumnId* column_id) {
68
        // Guard against invalid inputs: variant bindings may miss parent_field, and callers must
69
        // provide a valid output pointer to receive the resolved id.
70
14
        if (parent_field.empty() || column_id == nullptr) {
71
0
            return false;
72
0
        }
73
14
        auto it = parent_to_base_column_id.find(parent_field);
74
14
        if (it != parent_to_base_column_id.end()) {
75
0
            *column_id = it->second;
76
0
            return true;
77
0
        }
78
14
        if (index_context == nullptr || index_context->segment() == nullptr) {
79
0
            return false;
80
0
        }
81
14
        const int32_t ordinal =
82
14
                index_context->segment()->tablet_schema()->field_index(parent_field);
83
14
        if (ordinal < 0) {
84
0
            return false;
85
0
        }
86
14
        ColumnId resolved_id = static_cast<ColumnId>(ordinal);
87
14
        parent_to_base_column_id.emplace(parent_field, resolved_id);
88
14
        if (auto* storage_name_type = index_context->get_storage_name_and_type_by_id(resolved_id);
89
14
            storage_name_type != nullptr) {
90
14
            parent_to_storage_field_prefix[parent_field] = storage_name_type->first;
91
14
        }
92
14
        *column_id = resolved_id;
93
14
        return true;
94
14
    };
95
96
1.16k
    int child_index = 0; // Index for iterating through children
97
1.71k
    for (const auto& child : expr.children()) {
98
1.71k
        if (child->is_slot_ref()) {
99
1.71k
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
100
1.71k
            int column_id = column_slot_ref->column_id();
101
102
            // Determine the field_name from field_bindings (for variant subcolumns)
103
            // field_bindings and children should have the same order
104
1.71k
            std::string field_name;
105
1.71k
            const TSearchFieldBinding* binding = nullptr;
106
1.71k
            if (child_index < field_bindings.size()) {
107
                // Use field_name from binding (may include "parent.subcolumn" for variant)
108
1.69k
                binding = &field_bindings[child_index];
109
1.69k
                field_name = binding->field_name;
110
1.69k
            } else {
111
                // Fallback to column_name if binding not found
112
18
                field_name = column_slot_ref->column_name();
113
18
            }
114
115
1.71k
            bundle->field_name_to_column_id[field_name] = column_id;
116
117
1.71k
            auto* iterator = index_context->get_inverted_index_iterator_by_column_id(column_id);
118
1.71k
            const auto* storage_name_type =
119
1.71k
                    index_context->get_storage_name_and_type_by_column_id(column_id);
120
1.71k
            bool field_added = false;
121
            // For variant subcolumns, slot_ref might not map to a real indexed column in the scan schema.
122
            // Fall back to the parent variant column's iterator and synthesize lucene field name.
123
1.71k
            if (iterator == nullptr && binding != nullptr &&
124
1.71k
                binding->__isset.is_variant_subcolumn && binding->is_variant_subcolumn &&
125
1.71k
                binding->__isset.parent_field_name && !binding->parent_field_name.empty()) {
126
14
                ColumnId base_column_id = 0;
127
14
                if (resolve_parent_column_id(binding->parent_field_name, &base_column_id)) {
128
14
                    iterator = index_context->get_inverted_index_iterator_by_id(base_column_id);
129
14
                    const auto* base_storage_name_type =
130
14
                            index_context->get_storage_name_and_type_by_id(base_column_id);
131
14
                    if (iterator != nullptr && base_storage_name_type != nullptr) {
132
0
                        std::string prefix = base_storage_name_type->first;
133
0
                        if (auto pit =
134
0
                                    parent_to_storage_field_prefix.find(binding->parent_field_name);
135
0
                            pit != parent_to_storage_field_prefix.end() && !pit->second.empty()) {
136
0
                            prefix = pit->second;
137
0
                        } else {
138
0
                            parent_to_storage_field_prefix[binding->parent_field_name] = prefix;
139
0
                        }
140
141
0
                        std::string sub_path;
142
0
                        if (binding->__isset.subcolumn_path) {
143
0
                            sub_path = binding->subcolumn_path;
144
0
                        }
145
0
                        if (sub_path.empty()) {
146
                            // Fallback: strip "parent." prefix from logical field name
147
0
                            std::string pfx = binding->parent_field_name + ".";
148
0
                            if (field_name.starts_with(pfx)) {
149
0
                                sub_path = field_name.substr(pfx.size());
150
0
                            }
151
0
                        }
152
0
                        if (!sub_path.empty()) {
153
0
                            bundle->iterators[field_name] = iterator;
154
0
                            bundle->field_types[field_name] =
155
0
                                    std::make_pair(prefix + "." + sub_path, nullptr);
156
0
                            int base_column_index =
157
0
                                    index_context->column_index_by_id(base_column_id);
158
0
                            if (base_column_index >= 0) {
159
0
                                bundle->column_ids.emplace_back(base_column_index);
160
0
                            }
161
0
                            field_added = true;
162
0
                        }
163
0
                    }
164
14
                }
165
14
            }
166
167
            // Only collect fields that have iterators (materialized columns with indexes)
168
1.72k
            if (!field_added && iterator != nullptr) {
169
1.69k
                if (storage_name_type == nullptr) {
170
1
                    return Status::InternalError("storage_name_type not found for column {} in {}",
171
1
                                                 column_id, expr.expr_name());
172
1
                }
173
174
1.69k
                bundle->iterators.emplace(field_name, iterator);
175
1.69k
                bundle->field_types.emplace(field_name, *storage_name_type);
176
1.69k
                bundle->column_ids.emplace_back(column_id);
177
1.69k
            }
178
179
1.71k
            child_index++;
180
18.4E
        } else if (child->is_literal()) {
181
0
            auto* literal = assert_cast<VLiteral*>(child.get());
182
0
            bundle->literal_args.emplace_back(literal->get_column_ptr(), literal->get_data_type(),
183
0
                                              literal->expr_name());
184
18.4E
        } else {
185
            // Check if this is ElementAt expression (for variant subcolumn access)
186
18.4E
            if (child->expr_name() == "element_at" && child_index < field_bindings.size() &&
187
18.4E
                field_bindings[child_index].__isset.is_variant_subcolumn &&
188
18.4E
                field_bindings[child_index].is_variant_subcolumn) {
189
                // Variant subcolumn not materialized - skip, will create empty BitSetQuery in function_search
190
0
                child_index++;
191
0
                continue;
192
0
            }
193
194
            // Not a supported child type
195
18.4E
            return Status::InvalidArgument("Unsupported child node type: {}", child->expr_name());
196
18.4E
        }
197
1.71k
    }
198
199
1.16k
    return Status::OK();
200
1.16k
}
201
202
} // namespace
203
204
517
VSearchExpr::VSearchExpr(const TExprNode& node) : VExpr(node) {
205
517
    if (node.__isset.search_param) {
206
514
        _search_param = node.search_param;
207
514
        _original_dsl = _search_param.original_dsl;
208
514
    }
209
517
}
210
211
Status VSearchExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc,
212
440
                            VExprContext* context) {
213
440
    RETURN_IF_ERROR(VExpr::prepare(state, row_desc, context));
214
440
    const auto& query_options = state->query_options();
215
440
    if (query_options.__isset.enable_inverted_index_query_cache) {
216
440
        _enable_cache = query_options.enable_inverted_index_query_cache;
217
440
    }
218
440
    return Status::OK();
219
440
}
220
221
74
const std::string& VSearchExpr::expr_name() const {
222
74
    static const std::string name = "VSearchExpr";
223
74
    return name;
224
74
}
225
226
Status VSearchExpr::execute_column(VExprContext* context, const Block* block, Selector* selector,
227
6
                                   size_t count, ColumnPtr& result_column) const {
228
6
    if (fast_execute(context, selector, count, result_column)) {
229
4
        return Status::OK();
230
4
    }
231
232
2
    return Status::InternalError("SearchExpr should not be executed without inverted index");
233
6
}
234
235
1.21k
Status VSearchExpr::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) {
236
1.21k
    if (_search_param.original_dsl.empty()) {
237
3
        return Status::InvalidArgument("search DSL is empty");
238
3
    }
239
240
1.20k
    auto index_context = context->get_index_context();
241
1.20k
    if (!index_context) {
242
15
        LOG(WARNING) << "VSearchExpr: No inverted index context available";
243
15
        return Status::OK();
244
15
    }
245
246
1.19k
    SearchInputBundle bundle;
247
1.19k
    RETURN_IF_ERROR(collect_search_inputs(*this, context, &bundle));
248
249
1.19k
    VLOG_DEBUG << "VSearchExpr: bundle.iterators.size()=" << bundle.iterators.size();
250
251
1.19k
    const bool is_nested_query = _search_param.root.clause_type == "NESTED";
252
1.19k
    if (bundle.iterators.empty() && !is_nested_query) {
253
19
        LOG(WARNING) << "VSearchExpr: No indexed columns available for evaluation, DSL: "
254
19
                     << _original_dsl;
255
19
        auto empty_bitmap = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(),
256
19
                                                      std::make_shared<roaring::Roaring>());
257
19
        index_context->set_index_result_for_expr(this, std::move(empty_bitmap));
258
19
        return Status::OK();
259
19
    }
260
261
1.17k
    auto function = std::make_shared<FunctionSearch>();
262
1.17k
    auto result_bitmap = InvertedIndexResultBitmap();
263
1.17k
    auto status = function->evaluate_inverted_index_with_search_param(
264
1.17k
            _search_param, bundle.field_types, bundle.iterators, segment_num_rows, result_bitmap,
265
1.17k
            _enable_cache, index_context.get(), bundle.field_name_to_column_id);
266
267
1.17k
    if (!status.ok()) {
268
2
        LOG(WARNING) << "VSearchExpr: Function evaluation failed: " << status.to_string();
269
2
        return status;
270
2
    }
271
272
1.17k
    index_context->set_index_result_for_expr(this, result_bitmap);
273
1.73k
    for (int column_id : bundle.column_ids) {
274
1.73k
        index_context->set_true_for_index_status(this, column_id);
275
1.73k
    }
276
277
1.17k
    return Status::OK();
278
1.17k
}
279
280
} // namespace doris