lszskye commented on code in PR #490:
URL: https://github.com/apache/paimon-cpp/pull/490#discussion_r4476427295


##########
src/paimon/core/operation/precise_file_index_read.cpp:
##########
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "paimon/core/operation/precise_file_index_read.h"
+
+#include <algorithm>
+#include <cmath>
+#include <map>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/c/abi.h"
+#include "arrow/type.h"
+#include "fmt/format.h"
+#include "paimon/common/reader/complete_index_score_file_batch_reader.h"
+#include "paimon/common/reader/predicate_batch_reader.h"
+#include "paimon/common/reader/reader_utils.h"
+#include "paimon/common/types/data_field.h"
+#include "paimon/common/utils/object_utils.h"
+#include "paimon/common/utils/scope_guard.h"
+#include "paimon/core/deletionvectors/bitmap64_deletion_vector.h"
+#include "paimon/core/deletionvectors/bitmap_deletion_vector.h"
+#include "paimon/core/io/data_file_meta.h"
+#include "paimon/core/io/data_file_path_factory.h"
+#include "paimon/core/io/file_index_evaluator.h"
+#include "paimon/core/io/file_index_options.h"
+#include "paimon/core/operation/internal_read_context.h"
+#include "paimon/core/operation/raw_file_split_read.h"
+#include "paimon/core/schema/schema_manager.h"
+#include "paimon/core/schema/table_schema.h"
+#include "paimon/core/utils/field_mapping.h"
+#include "paimon/file_index/bitmap_index_result.h"
+#include "paimon/file_index/file_index_result.h"
+#include "paimon/file_index/scored_file_index_result.h"
+#include "paimon/format/reader_builder.h"
+#include "paimon/predicate/compound_predicate.h"
+#include "paimon/predicate/full_text_search.h"
+#include "paimon/predicate/function.h"
+#include "paimon/predicate/leaf_predicate.h"
+#include "paimon/predicate/vector_search.h"
+#include "paimon/utils/roaring_bitmap64.h"
+
+namespace paimon {
+namespace {
+
+Result<std::string> ResolveSearchFieldName(
+    const std::string& field_name, const std::map<std::string, std::string>& 
field_name_mapping,
+    const std::string& file_name) {
+    auto iter = field_name_mapping.find(field_name);
+    if (iter == field_name_mapping.end()) {
+        return Status::Invalid(
+            fmt::format("Search field '{}' has no matching field ID in data 
schema for file '{}'",
+                        field_name, file_name));
+    }
+    return iter->second;
+}
+
+Status PreparePredicate(
+    const std::shared_ptr<Predicate>& predicate,
+    const std::map<std::string, DataField>& read_by_name,
+    const std::map<int32_t, std::pair<int32_t, DataField>>& data_by_id,
+    const std::vector<std::string>& partition_keys, FileIndexEvaluator* 
indexes,
+    std::map<std::shared_ptr<LeafPredicate>, std::shared_ptr<LeafPredicate>>* 
mapped_leaves) {
+    if (!predicate) {
+        return Status::OK();
+    }
+    if (auto compound = 
std::dynamic_pointer_cast<CompoundPredicate>(predicate)) {
+        const Function::Type type = compound->GetFunction().GetType();
+        if (type != Function::Type::AND && type != Function::Type::OR) {
+            return Status::Invalid(
+                fmt::format("unsupported compound file predicate {}", 
predicate->ToString()));
+        }
+        for (const auto& child : compound->Children()) {
+            PAIMON_RETURN_NOT_OK(PreparePredicate(child, read_by_name, 
data_by_id, partition_keys,
+                                                  indexes, mapped_leaves));
+        }
+        return Status::OK();
+    }
+    auto leaf = std::dynamic_pointer_cast<LeafPredicate>(predicate);
+    if (!leaf) {
+        return Status::Invalid("File predicate must contain leaf or compound 
predicates");
+    }
+    bool full_text = leaf->GetFunction().GetType() == 
Function::Type::FULL_TEXT_MATCH;
+    if (!full_text && ObjectUtils::Contains(partition_keys, 
leaf->FieldName())) {
+        mapped_leaves->emplace(leaf, nullptr);
+        return Status::OK();
+    }
+    PAIMON_ASSIGN_OR_RAISE(
+        std::optional<std::shared_ptr<Predicate>> mapped,
+        FieldMappingBuilder::ReconstructPredicateWithDataFields(leaf, 
read_by_name, data_by_id));
+    std::shared_ptr<LeafPredicate> mapped_leaf;
+    if (mapped) {
+        mapped_leaf = std::dynamic_pointer_cast<LeafPredicate>(*mapped);
+        if (!mapped_leaf) {
+            return Status::Invalid("Mapped file predicate must be a leaf");
+        }
+        if (full_text) {
+            PAIMON_ASSIGN_OR_RAISE(
+                [[maybe_unused]] std::shared_ptr<FileIndexReader> reader,
+                indexes->GetSearchReader(mapped_leaf->FieldName(),
+                                         
FileIndexReader::SearchType::FULL_TEXT_MATCH));
+        }
+    }
+    mapped_leaves->emplace(leaf, std::move(mapped_leaf));
+    return Status::OK();
+}
+
+Status PrepareSearch(const std::shared_ptr<InternalReadContext>& context,
+                     const std::map<std::string, std::string>& field_names,
+                     const std::string& file_name, FileIndexEvaluator* indexes,
+                     std::shared_ptr<VectorSearch>* vector_search,
+                     std::shared_ptr<FullTextSearch>* full_text_search) {
+    if (context->GetVectorSearch()) {
+        *vector_search = 
std::make_shared<VectorSearch>(*context->GetVectorSearch());
+        PAIMON_ASSIGN_OR_RAISE(
+            (*vector_search)->field_name,
+            ResolveSearchFieldName((*vector_search)->field_name, field_names, 
file_name));
+        PAIMON_ASSIGN_OR_RAISE(
+            [[maybe_unused]] std::shared_ptr<FileIndexReader> reader,
+            indexes->GetSearchReader((*vector_search)->field_name,
+                                     
FileIndexReader::SearchType::VECTOR_SEARCH));
+    }
+    if (context->GetFullTextSearch()) {
+        *full_text_search = 
std::make_shared<FullTextSearch>(*context->GetFullTextSearch());
+        PAIMON_ASSIGN_OR_RAISE(
+            (*full_text_search)->field_name,
+            ResolveSearchFieldName((*full_text_search)->field_name, 
field_names, file_name));
+        PAIMON_ASSIGN_OR_RAISE(
+            [[maybe_unused]] std::shared_ptr<FileIndexReader> reader,
+            indexes->GetSearchReader((*full_text_search)->field_name,
+                                     
FileIndexReader::SearchType::FULL_TEXT_SEARCH));
+    }
+    return Status::OK();
+}
+
+}  // namespace
+
+Result<std::unique_ptr<PreciseFileIndexRead>> PreciseFileIndexRead::Create(
+    const RawFileSplitRead& split_read, const BinaryRow& partition,
+    const std::shared_ptr<DataFileMeta>& file, const FieldMappingBuilder* 
field_mapping_builder,
+    const std::shared_ptr<DataFilePathFactory>& path_factory,
+    const std::map<std::string, std::string>& extra_format_options) {
+    std::shared_ptr<TableSchema> table_schema = 
split_read.context_->GetTableSchema();

Review Comment:
   ok



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to