| 56 | : profiler_(std::move(profiler)) {} |
| 57 | |
| 58 | Result<DocPtrList> SQLEngineImpl::execute( |
| 59 | CollectionSchema::Ptr collection, SearchQuery query, |
| 60 | const std::vector<Segment::Ptr> &segments) { |
| 61 | if (segments.empty()) { |
| 62 | return DocPtrList{}; |
| 63 | } |
| 64 | |
| 65 | // Check filter satisfiability once before the loop (result depends only on |
| 66 | // the query, not on segment data). |
| 67 | auto first_query_info = build_query_info(collection, query, nullptr); |
| 68 | if (!first_query_info) { |
| 69 | return tl::make_unexpected(first_query_info.error()); |
| 70 | } |
| 71 | if (first_query_info.value()->is_filter_unsatisfiable()) { |
| 72 | LOG_WARN("filter is unsatisfiable: %s", |
| 73 | first_query_info.value()->to_string().c_str()); |
| 74 | return {}; |
| 75 | } |
| 76 | // Capture output field schema before query_infos are moved into the planner. |
| 77 | auto select_item_meta_ptrs = |
| 78 | first_query_info.value()->select_item_schema_ptrs(); |
| 79 | |
| 80 | // Build a separate QueryInfo per segment so the optimizer can mutate each |
| 81 | // independently. Reuse first_query_info for segment 0. |
| 82 | std::vector<QueryInfo::Ptr> query_infos; |
| 83 | query_infos.reserve(segments.size()); |
| 84 | query_infos.emplace_back(std::move(first_query_info.value())); |
| 85 | for (size_t i = 1; i < segments.size(); ++i) { |
| 86 | auto query_info = build_query_info(collection, query, nullptr); |
| 87 | if (!query_info) { |
| 88 | return tl::make_unexpected(query_info.error()); |
| 89 | } |
| 90 | query_infos.emplace_back(std::move(query_info.value())); |
| 91 | } |
| 92 | auto reader = search_by_query_info(collection, segments, &query_infos); |
| 93 | if (!reader) { |
| 94 | return tl::make_unexpected(Status::InternalError( |
| 95 | "Execute plan failed (query): ", reader.error().c_str())); |
| 96 | } |
| 97 | return fill_result(select_item_meta_ptrs, reader.value().get()); |
| 98 | } |
| 99 | |
| 100 | SearchQuery from_group_by(const GroupByVectorQuery &gq) { |
| 101 | SearchQuery sq; |