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 <fmt/format.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <roaring/roaring.hh> |
24 | | |
25 | | #include "common/logging.h" |
26 | | #include "common/status.h" |
27 | | #include "core/column/column_const.h" |
28 | | #include "exprs/function/function_search.h" |
29 | | #include "exprs/vexpr_context.h" |
30 | | #include "exprs/vliteral.h" |
31 | | #include "exprs/vslot_ref.h" |
32 | | #include "glog/logging.h" |
33 | | #include "runtime/runtime_state.h" |
34 | | #include "storage/index/inverted/inverted_index_reader.h" |
35 | | |
36 | | namespace doris { |
37 | | using namespace segment_v2; |
38 | | |
39 | | namespace { |
40 | | |
41 | | struct SearchInputBundle { |
42 | | std::unordered_map<std::string, IndexIterator*> iterators; |
43 | | std::unordered_map<std::string, IndexFieldNameAndTypePair> field_types; |
44 | | std::unordered_map<std::string, int> field_name_to_column_id; |
45 | | std::vector<int> column_indexes; |
46 | | ColumnsWithTypeAndName literal_args; |
47 | | }; |
48 | | |
49 | | void add_search_binding_diagnostic(const IndexExecContext* index_context, |
50 | 90 | const std::string& diagnostic) { |
51 | 90 | VLOG_DEBUG << diagnostic; |
52 | 90 | if (index_context == nullptr) { |
53 | 0 | return; |
54 | 0 | } |
55 | 90 | const auto& index_query_context = index_context->get_index_query_context(); |
56 | 90 | if (index_query_context != nullptr && index_query_context->stats != nullptr) { |
57 | 87 | index_query_context->stats->inverted_index_stats.add_binding_diagnostic(diagnostic); |
58 | 87 | } |
59 | 90 | } |
60 | | |
61 | | Status collect_slot_search_input(const VSearchExpr& expr, const VSlotRef& slot_ref, |
62 | | const TSearchFieldBinding* binding, |
63 | 1.73k | IndexExecContext* index_context, SearchInputBundle* bundle) { |
64 | 1.73k | DCHECK(index_context != nullptr); |
65 | 1.73k | DCHECK(bundle != nullptr); |
66 | | |
67 | | // VSlotRef::column_id() is the scan-schema position used by IndexExecContext. |
68 | 1.73k | const int column_index = slot_ref.column_id(); |
69 | 1.73k | const std::string field_name = |
70 | 1.73k | binding != nullptr ? binding->field_name : slot_ref.column_name(); |
71 | 1.74k | const bool is_variant_subcolumn = binding != nullptr && binding->__isset.is_variant_subcolumn && |
72 | 1.74k | binding->is_variant_subcolumn; |
73 | | |
74 | 1.73k | bundle->field_name_to_column_id[field_name] = column_index; |
75 | | |
76 | 1.73k | auto* iterator = index_context->get_inverted_index_iterator_by_column_id(column_index); |
77 | 1.73k | if (iterator == nullptr) { |
78 | | // For example, `data.items.message` has its own SlotRef in the scan schema. The |
79 | | // storage layer may inherit index metadata from `data`, but it still constructs a |
80 | | // child iterator whose stored field name contains the complete Variant path. |
81 | 16 | if (is_variant_subcolumn) { |
82 | 15 | add_search_binding_diagnostic( |
83 | 15 | index_context, |
84 | 15 | fmt::format("[VariantSearchBinding] phase=collect_inputs " |
85 | 15 | "result=no_iterator logical_field={} column_index={} " |
86 | 15 | "reason=slot_iterator_missing", |
87 | 15 | field_name, column_index)); |
88 | 15 | } |
89 | 16 | return Status::OK(); |
90 | 16 | } |
91 | | |
92 | 1.71k | const auto* storage_name_type = |
93 | 1.71k | index_context->get_storage_name_and_type_by_column_id(column_index); |
94 | 1.71k | if (storage_name_type == nullptr) { |
95 | 1 | return Status::InternalError("storage_name_type not found for column {} in {}", |
96 | 1 | column_index, expr.expr_name()); |
97 | 1 | } |
98 | | |
99 | 1.71k | bundle->iterators.emplace(field_name, iterator); |
100 | 1.71k | bundle->field_types.emplace(field_name, *storage_name_type); |
101 | 1.71k | bundle->column_indexes.emplace_back(column_index); |
102 | 1.71k | if (is_variant_subcolumn) { |
103 | 62 | add_search_binding_diagnostic( |
104 | 62 | index_context, |
105 | 62 | fmt::format("[VariantSearchBinding] phase=collect_inputs " |
106 | 62 | "result=direct_iterator logical_field={} column_index={} " |
107 | 62 | "stored_field={}", |
108 | 62 | field_name, column_index, storage_name_type->first)); |
109 | 62 | } |
110 | 1.71k | return Status::OK(); |
111 | 1.71k | } |
112 | | |
113 | | Status collect_search_inputs(const VSearchExpr& expr, VExprContext* context, |
114 | 1.19k | SearchInputBundle* bundle) { |
115 | 1.19k | DCHECK(bundle != nullptr); |
116 | | |
117 | 1.19k | auto index_context = context->get_index_context(); |
118 | 1.19k | if (index_context == nullptr) { |
119 | 0 | LOG(WARNING) << "collect_search_inputs: No inverted index context available"; |
120 | 0 | return Status::InternalError("No inverted index context available"); |
121 | 0 | } |
122 | | |
123 | 1.19k | const auto& search_param = expr.get_search_param(); |
124 | 1.19k | const auto& field_bindings = search_param.field_bindings; |
125 | | |
126 | 1.19k | size_t child_index = 0; |
127 | 1.76k | for (const auto& child : expr.children()) { |
128 | 1.76k | if (child->is_slot_ref()) { |
129 | 1.75k | auto* column_slot_ref = assert_cast<VSlotRef*>(child.get()); |
130 | 1.75k | const TSearchFieldBinding* binding = |
131 | 1.75k | child_index < field_bindings.size() ? &field_bindings[child_index] : nullptr; |
132 | 1.75k | RETURN_IF_ERROR(collect_slot_search_input(expr, *column_slot_ref, binding, |
133 | 1.75k | index_context.get(), bundle)); |
134 | 1.75k | ++child_index; |
135 | 1.75k | } else if (child->is_literal()) { |
136 | 0 | auto* literal = assert_cast<VLiteral*>(child.get()); |
137 | 0 | bundle->literal_args.emplace_back(literal->get_column_ptr(), literal->get_data_type(), |
138 | 0 | literal->expr_name()); |
139 | 10 | } else { |
140 | | // Check if this is ElementAt expression (for variant subcolumn access) |
141 | 10 | if (child->expr_name() == "element_at" && child_index < field_bindings.size() && |
142 | 10 | field_bindings[child_index].__isset.is_variant_subcolumn && |
143 | 10 | field_bindings[child_index].is_variant_subcolumn) { |
144 | | // Variant subcolumn not materialized - skip, will create empty BitSetQuery in function_search |
145 | 0 | add_search_binding_diagnostic( |
146 | 0 | index_context.get(), |
147 | 0 | fmt::format("[VariantSearchBinding] phase=collect_inputs " |
148 | 0 | "result=unmaterialized_element_at logical_field={} " |
149 | 0 | "parent_field={} sub_path={} reason=no_slot_ref", |
150 | 0 | field_bindings[child_index].field_name, |
151 | 0 | field_bindings[child_index].__isset.parent_field_name |
152 | 0 | ? field_bindings[child_index].parent_field_name |
153 | 0 | : "", |
154 | 0 | field_bindings[child_index].__isset.subcolumn_path |
155 | 0 | ? field_bindings[child_index].subcolumn_path |
156 | 0 | : "")); |
157 | 0 | ++child_index; |
158 | 0 | continue; |
159 | 0 | } |
160 | | |
161 | | // Not a supported child type |
162 | 10 | return Status::InvalidArgument("Unsupported child node type: {}", child->expr_name()); |
163 | 10 | } |
164 | 1.76k | } |
165 | | |
166 | 1.18k | return Status::OK(); |
167 | 1.19k | } |
168 | | |
169 | | } // namespace |
170 | | |
171 | 547 | VSearchExpr::VSearchExpr(const TExprNode& node) : VExpr(node) { |
172 | 547 | if (node.__isset.search_param) { |
173 | 543 | _search_param = node.search_param; |
174 | 543 | _original_dsl = _search_param.original_dsl; |
175 | 543 | } |
176 | 547 | } |
177 | | |
178 | | Status VSearchExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, |
179 | 470 | VExprContext* context) { |
180 | 470 | RETURN_IF_ERROR(VExpr::prepare(state, row_desc, context)); |
181 | 470 | const auto& query_options = state->query_options(); |
182 | 470 | if (query_options.__isset.enable_inverted_index_query_cache) { |
183 | 470 | _enable_cache = query_options.enable_inverted_index_query_cache; |
184 | 470 | } |
185 | 470 | return Status::OK(); |
186 | 470 | } |
187 | | |
188 | 94 | const std::string& VSearchExpr::expr_name() const { |
189 | 94 | static const std::string name = "VSearchExpr"; |
190 | 94 | return name; |
191 | 94 | } |
192 | | |
193 | | Status VSearchExpr::execute_column_impl(VExprContext* context, const Block* block, |
194 | | const Selector* selector, size_t count, |
195 | 5 | ColumnPtr& result_column) const { |
196 | 5 | if (fast_execute(context, selector, count, result_column)) { |
197 | 4 | return Status::OK(); |
198 | 4 | } |
199 | | |
200 | 1 | return Status::InternalError("SearchExpr should not be executed without inverted index"); |
201 | 5 | } |
202 | | |
203 | 1.24k | Status VSearchExpr::evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) { |
204 | 1.24k | if (_search_param.original_dsl.empty()) { |
205 | 3 | return Status::InvalidArgument("search DSL is empty"); |
206 | 3 | } |
207 | | |
208 | 1.24k | auto index_context = context->get_index_context(); |
209 | 1.24k | if (!index_context) { |
210 | 15 | LOG(WARNING) << "VSearchExpr: No inverted index context available"; |
211 | 15 | return Status::OK(); |
212 | 15 | } |
213 | | |
214 | 1.22k | SearchInputBundle bundle; |
215 | 1.22k | RETURN_IF_ERROR(collect_search_inputs(*this, context, &bundle)); |
216 | | |
217 | 18.4E | VLOG_DEBUG << "VSearchExpr: bundle.iterators.size()=" << bundle.iterators.size(); |
218 | | |
219 | 1.22k | const bool is_nested_query = _search_param.root.clause_type == "NESTED"; |
220 | 1.22k | if (bundle.iterators.empty() && !is_nested_query) { |
221 | 13 | LOG(WARNING) << "VSearchExpr: No indexed columns available for evaluation, DSL: " |
222 | 13 | << _original_dsl; |
223 | 13 | add_search_binding_diagnostic( |
224 | 13 | index_context.get(), |
225 | 13 | fmt::format("[VariantSearchBinding] phase=evaluate_search result=no_iterator " |
226 | 13 | "dsl={} reason=no_indexed_columns", |
227 | 13 | _original_dsl)); |
228 | 13 | auto empty_bitmap = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(), |
229 | 13 | std::make_shared<roaring::Roaring>()); |
230 | 13 | index_context->set_index_result_for_expr(this, std::move(empty_bitmap)); |
231 | 13 | return Status::OK(); |
232 | 13 | } |
233 | | |
234 | 1.21k | auto index_query_context = index_context->get_index_query_context(); |
235 | | |
236 | 1.21k | auto function = std::make_shared<FunctionSearch>(); |
237 | 1.21k | auto result_bitmap = InvertedIndexResultBitmap(); |
238 | 1.21k | auto status = function->evaluate_inverted_index_with_search_param( |
239 | 1.21k | _search_param, bundle.field_types, bundle.iterators, segment_num_rows, result_bitmap, |
240 | 1.21k | _enable_cache, index_context.get(), bundle.field_name_to_column_id, |
241 | 1.21k | index_query_context); |
242 | | |
243 | 1.21k | if (!status.ok()) { |
244 | 2 | LOG(WARNING) << "VSearchExpr: Function evaluation failed: " << status.to_string(); |
245 | 2 | return status; |
246 | 2 | } |
247 | | |
248 | 1.21k | index_context->set_index_result_for_expr(this, result_bitmap); |
249 | 1.77k | for (int column_index : bundle.column_indexes) { |
250 | 1.77k | index_context->set_true_for_index_status(this, column_index); |
251 | 1.77k | } |
252 | | |
253 | 1.21k | return Status::OK(); |
254 | 1.21k | } |
255 | | |
256 | | } // namespace doris |