HuaHuaY commented on code in PR #435: URL: https://github.com/apache/iceberg-cpp/pull/435#discussion_r2646491554
########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( Review Comment: ditto ########## src/iceberg/delete_file_index.h: ########## @@ -0,0 +1,406 @@ +/* + * 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. + */ + +#pragma once + +/// \file iceberg/delete_file_index.h +/// An index of delete files by sequence number. + +#include <algorithm> Review Comment: ```suggestion ``` ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); Review Comment: ```suggestion std::ranges::sort(files_, std::ranges::less{}, &ManifestEntry::sequence_number); ``` ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( + files_, [](const auto& entry) { return entry.sequence_number.value(); }) | + std::ranges::to<std::vector<int64_t>>(); + + indexed_ = true; +} + +// EqualityDeletes implementation + +Status EqualityDeletes::Add(ManifestEntry&& entry) { + ICEBERG_PRECHECK(entry.sequence_number.has_value(), + "Missing sequence number for equality delete: {}", + entry.data_file->file_path); + files_.emplace_back(&schema_, std::move(entry)); + indexed_ = false; + return {}; +} + +Result<std::vector<std::shared_ptr<DataFile>>> EqualityDeletes::Filter( + int64_t seq, const DataFile& data_file) { + IndexIfNeeded(); + + size_t start = FindStartIndex(seqs_, seq); + if (start >= files_.size()) { + return {}; + } + + std::vector<std::shared_ptr<DataFile>> result; + result.reserve(files_.size() - start); + for (size_t i = start; i < files_.size(); ++i) { + const auto& delete_file = files_[i]; + ICEBERG_ASSIGN_OR_RAISE(bool may_contain, + CanContainEqDeletesForFile(data_file, delete_file)); + if (may_contain) { + result.push_back(delete_file.wrapped.data_file); + } + } + + return result; +} + +std::vector<std::shared_ptr<DataFile>> EqualityDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view( + files_, [](const auto& file) { return file.wrapped.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void EqualityDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by apply sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.apply_sequence_number < b.apply_sequence_number; + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( Review Comment: ditto ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); Review Comment: ```suggestion auto iter = std::ranges::lower_bound(seqs_, seq); if (iter == seqs_.end()) { return {}; } return files_ | std::views::drop(iter - seqs_.begin()) | std::views::transform(&ManifestEntry::data_file) | std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); ``` I think adding `FindStartIndex` is not needed. ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( + files_, [](const auto& entry) { return entry.sequence_number.value(); }) | + std::ranges::to<std::vector<int64_t>>(); + + indexed_ = true; +} + +// EqualityDeletes implementation + +Status EqualityDeletes::Add(ManifestEntry&& entry) { + ICEBERG_PRECHECK(entry.sequence_number.has_value(), + "Missing sequence number for equality delete: {}", + entry.data_file->file_path); + files_.emplace_back(&schema_, std::move(entry)); + indexed_ = false; + return {}; +} + +Result<std::vector<std::shared_ptr<DataFile>>> EqualityDeletes::Filter( + int64_t seq, const DataFile& data_file) { + IndexIfNeeded(); + + size_t start = FindStartIndex(seqs_, seq); + if (start >= files_.size()) { + return {}; + } + + std::vector<std::shared_ptr<DataFile>> result; + result.reserve(files_.size() - start); + for (size_t i = start; i < files_.size(); ++i) { + const auto& delete_file = files_[i]; + ICEBERG_ASSIGN_OR_RAISE(bool may_contain, + CanContainEqDeletesForFile(data_file, delete_file)); + if (may_contain) { + result.push_back(delete_file.wrapped.data_file); + } + } + + return result; +} + +std::vector<std::shared_ptr<DataFile>> EqualityDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view( + files_, [](const auto& file) { return file.wrapped.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void EqualityDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by apply sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { Review Comment: ditto ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( + files_, [](const auto& entry) { return entry.sequence_number.value(); }) | + std::ranges::to<std::vector<int64_t>>(); + + indexed_ = true; +} + +// EqualityDeletes implementation + +Status EqualityDeletes::Add(ManifestEntry&& entry) { + ICEBERG_PRECHECK(entry.sequence_number.has_value(), + "Missing sequence number for equality delete: {}", + entry.data_file->file_path); + files_.emplace_back(&schema_, std::move(entry)); + indexed_ = false; + return {}; +} + +Result<std::vector<std::shared_ptr<DataFile>>> EqualityDeletes::Filter( + int64_t seq, const DataFile& data_file) { + IndexIfNeeded(); + + size_t start = FindStartIndex(seqs_, seq); + if (start >= files_.size()) { + return {}; + } + + std::vector<std::shared_ptr<DataFile>> result; + result.reserve(files_.size() - start); + for (size_t i = start; i < files_.size(); ++i) { + const auto& delete_file = files_[i]; + ICEBERG_ASSIGN_OR_RAISE(bool may_contain, + CanContainEqDeletesForFile(data_file, delete_file)); + if (may_contain) { + result.push_back(delete_file.wrapped.data_file); + } + } + + return result; +} + +std::vector<std::shared_ptr<DataFile>> EqualityDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view( Review Comment: ditto ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, + [](const auto& entry) { return entry.data_file; }) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +void PositionDeletes::IndexIfNeeded() { + if (indexed_) { + return; + } + + // Sort by data sequence number + std::ranges::sort(files_, [](const auto& a, const auto& b) { + return a.sequence_number.value() < b.sequence_number.value(); + }); + + // Build sequence number array for binary search + seqs_ = std::ranges::transform_view( + files_, [](const auto& entry) { return entry.sequence_number.value(); }) | + std::ranges::to<std::vector<int64_t>>(); + + indexed_ = true; +} + +// EqualityDeletes implementation + +Status EqualityDeletes::Add(ManifestEntry&& entry) { + ICEBERG_PRECHECK(entry.sequence_number.has_value(), + "Missing sequence number for equality delete: {}", + entry.data_file->file_path); + files_.emplace_back(&schema_, std::move(entry)); + indexed_ = false; + return {}; +} + +Result<std::vector<std::shared_ptr<DataFile>>> EqualityDeletes::Filter( + int64_t seq, const DataFile& data_file) { + IndexIfNeeded(); + + size_t start = FindStartIndex(seqs_, seq); + if (start >= files_.size()) { + return {}; + } + + std::vector<std::shared_ptr<DataFile>> result; + result.reserve(files_.size() - start); + for (size_t i = start; i < files_.size(); ++i) { + const auto& delete_file = files_[i]; + ICEBERG_ASSIGN_OR_RAISE(bool may_contain, + CanContainEqDeletesForFile(data_file, delete_file)); + if (may_contain) { + result.push_back(delete_file.wrapped.data_file); + } + } Review Comment: ```suggestion auto iter = std::ranges::lower_bound(seqs_, seq); if (iter == seqs_.end()) { return {}; } std::vector<std::shared_ptr<DataFile>> result; result.reserve(seqs_.end() - iter); for (auto& delete_file : files_ | std::views::drop(iter - seqs_.begin())) { ICEBERG_ASSIGN_OR_RAISE(bool may_contain, CanContainEqDeletesForFile(data_file, delete_file)); if (may_contain) { result.push_back(delete_file.wrapped.data_file); } } ``` ########## src/iceberg/delete_file_index.cc: ########## @@ -0,0 +1,776 @@ +/* + * 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_entry.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 files_ | std::views::drop(start) | + std::views::transform(&ManifestEntry::data_file) | + std::ranges::to<std::vector<std::shared_ptr<DataFile>>>(); +} + +std::vector<std::shared_ptr<DataFile>> PositionDeletes::ReferencedDeleteFiles() { + IndexIfNeeded(); + return std::ranges::transform_view(files_, Review Comment: Use `std::views::transform` -- 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]
