Coverage Report

Created: 2025-10-17 00:26

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