HuaHuaY commented on code in PR #435:
URL: https://github.com/apache/iceberg-cpp/pull/435#discussion_r2645816226


##########
src/iceberg/delete_file_index.cc:
##########
@@ -0,0 +1,775 @@
+/*
+ * 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 "iceberg/delete_file_index.h"
+
+#include <algorithm>
+#include <iterator>
+#include <ranges>
+#include <vector>
+
+#include "iceberg/expression/expression.h"
+#include "iceberg/expression/manifest_evaluator.h"
+#include "iceberg/expression/projections.h"
+#include "iceberg/file_io.h"
+#include "iceberg/manifest/manifest_list.h"
+#include "iceberg/manifest/manifest_reader.h"
+#include "iceberg/partition_spec.h"
+#include "iceberg/schema.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/content_file_util.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace internal {
+
+Status EqualityDeleteFile::ConvertBoundsIfNeeded() const {
+  if (bounds_converted) {
+    return {};
+  }
+
+  // Convert bounds for equality field IDs only
+  for (int32_t field_id : wrapped.data_file->equality_ids) {
+    ICEBERG_ASSIGN_OR_RAISE(auto field, schema->FindFieldById(field_id));
+    if (!field.has_value()) {
+      continue;
+    }
+
+    const auto& schema_field = field.value().get();
+    if (schema_field.type()->is_nested()) {
+      continue;
+    }
+
+    const auto primitive_type = 
checked_pointer_cast<PrimitiveType>(schema_field.type());
+
+    // Convert lower bound
+    if (auto it = wrapped.data_file->lower_bounds.find(field_id);
+        it != wrapped.data_file->lower_bounds.cend() && !it->second.empty()) {
+      ICEBERG_ASSIGN_OR_RAISE(auto lower,
+                              Literal::Deserialize(it->second, 
primitive_type));
+      lower_bounds.emplace(field_id, std::move(lower));
+    }
+
+    // Convert upper bound
+    if (auto it = wrapped.data_file->upper_bounds.find(field_id);
+        it != wrapped.data_file->upper_bounds.cend() && !it->second.empty()) {
+      ICEBERG_ASSIGN_OR_RAISE(auto upper,
+                              Literal::Deserialize(it->second, 
primitive_type));
+      upper_bounds.emplace(field_id, std::move(upper));
+    }
+  }
+
+  bounds_converted = true;
+  return {};
+}
+
+// Check if an equality delete file can contain deletes for a data file.
+Result<bool> CanContainEqDeletesForFile(const DataFile& data_file,
+                                        const EqualityDeleteFile& delete_file) 
{
+  // Whether to check data ranges or to assume that the ranges match.  If 
upper/lower
+  // bounds are missing, null counts may still be used to determine delete 
files can be
+  // skipped.
+  bool check_ranges = !data_file.lower_bounds.empty() &&
+                      !data_file.upper_bounds.empty() &&
+                      delete_file.HasLowerAndUpperBounds();
+
+  const auto* wrapped_delete_file = delete_file.wrapped.data_file.get();
+
+  for (int32_t field_id : wrapped_delete_file->equality_ids) {
+    ICEBERG_ASSIGN_OR_RAISE(auto found_field,
+                            delete_file.schema->FindFieldById(field_id));
+    if (!found_field.has_value()) {
+      continue;
+    }
+
+    const auto& field = found_field.value().get();
+    if (field.type()->is_nested()) {
+      continue;
+    }
+
+    bool is_required = !field.optional();
+    bool data_contains_null =
+        ContainsNull(data_file.null_value_counts, field_id, is_required);
+    bool delete_contains_null =
+        ContainsNull(wrapped_delete_file->null_value_counts, field_id, 
is_required);
+
+    if (data_contains_null && delete_contains_null) {
+      // Both have nulls - delete may apply
+      continue;
+    }
+
+    if (AllNull(data_file.null_value_counts, data_file.value_counts, field_id,
+                is_required) &&
+        AllNonNull(wrapped_delete_file->null_value_counts, field_id, 
is_required)) {
+      return false;  // Data is all null, delete has no nulls - cannot match
+    }
+
+    if (AllNull(wrapped_delete_file->null_value_counts, 
wrapped_delete_file->value_counts,
+                field_id, is_required) &&
+        AllNonNull(data_file.null_value_counts, field_id, is_required)) {
+      return false;  // Delete is all null, data has no nulls - cannot match
+    }
+
+    if (!check_ranges) {
+      continue;
+    }
+
+    // Check range overlap
+    auto data_lower_it = data_file.lower_bounds.find(field_id);
+    auto data_upper_it = data_file.upper_bounds.find(field_id);
+    if (data_lower_it == data_file.lower_bounds.cend() || 
data_lower_it->second.empty() ||
+        data_upper_it == data_file.upper_bounds.cend() || 
data_upper_it->second.empty()) {
+      continue;  // Missing bounds, assume may match
+    }
+
+    auto delete_lower = delete_file.LowerBound(field_id);
+    auto delete_upper = delete_file.UpperBound(field_id);
+    if (!delete_lower.has_value() || !delete_upper.has_value()) {
+      continue;  // Missing bounds, assume may match
+    }
+
+    // Convert data bounds
+    auto primitive_type = checked_pointer_cast<PrimitiveType>(field.type());
+    ICEBERG_ASSIGN_OR_RAISE(auto data_lower,
+                            Literal::Deserialize(data_lower_it->second, 
primitive_type));
+    ICEBERG_ASSIGN_OR_RAISE(auto data_upper,
+                            Literal::Deserialize(data_upper_it->second, 
primitive_type));
+
+    if (!RangesOverlap(data_lower, data_upper, delete_lower->value().get(),
+                       delete_upper->value().get())) {
+      return false;  // Ranges don't overlap - cannot match
+    }
+  }
+
+  return true;
+}
+
+// PositionDeletes implementation
+
+Status PositionDeletes::Add(ManifestEntry&& entry) {
+  ICEBERG_PRECHECK(entry.sequence_number.has_value(),
+                   "Missing sequence number for position delete: {}",
+                   entry.data_file->file_path);
+  files_.emplace_back(std::move(entry));
+  indexed_ = false;
+  return {};
+}
+
+std::vector<std::shared_ptr<DataFile>> PositionDeletes::Filter(int64_t seq) {
+  IndexIfNeeded();
+
+  size_t start = FindStartIndex(seqs_, seq);
+  if (start >= files_.size()) {
+    return {};
+  }
+
+  return std::ranges::transform_view(files_ | std::views::drop(start),

Review Comment:
   ```cpp
   return files_ 
     | std::views::drop(start) 
     | std::views::transform(&ManifestEntry::data_file) 
     | std::ranges::to<std::vector<std::shared_ptr<DataFile>>>();
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to