Copilot commented on code in PR #958: URL: https://github.com/apache/iceberg-cpp/pull/958#discussion_r4237184396
########## src/iceberg/changelog_dv_planner_internal.cc: ########## @@ -0,0 +1,252 @@ +/* + * 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/changelog_dv_planner_internal.h" + +#include <algorithm> +#include <utility> + +#include "iceberg/expression/manifest_evaluator.h" +#include "iceberg/expression/projections.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/manifest/manifest_reader.h" +#include "iceberg/partition_spec.h" +#include "iceberg/util/executor_util_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +Result<std::unique_ptr<DeletionVectorChangelogPlanner>> +DeletionVectorChangelogPlanner::Make( + std::shared_ptr<FileIO> io, std::shared_ptr<Schema> schema, + const std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>>& specs_by_id, + std::shared_ptr<Expression> filter, bool case_sensitive, OptionalExecutor executor, + const std::vector<ManifestFile>& delete_manifests, + const std::unordered_map<int64_t, int64_t>& sequence_numbers) { + ICEBERG_ASSIGN_OR_RAISE( + auto entries, + ParallelCollect( + executor, delete_manifests, + [&](const ManifestFile& manifest) -> Result<std::vector<ManifestEntry>> { + auto spec_it = specs_by_id.find(manifest.partition_spec_id); + ICEBERG_CHECK(spec_it != specs_by_id.end(), + "Partition spec ID {} not found when reading delete manifest", + manifest.partition_spec_id); + std::shared_ptr<Expression> partition_filter; + if (filter != nullptr) { + auto projector = + Projections::Inclusive(*spec_it->second, *schema, case_sensitive); + ICEBERG_ASSIGN_OR_RAISE(partition_filter, projector->Project(filter)); + ICEBERG_ASSIGN_OR_RAISE( + auto evaluator, + ManifestEvaluator::MakePartitionFilter( + partition_filter, spec_it->second, *schema, case_sensitive)); + ICEBERG_ASSIGN_OR_RAISE(bool may_match, evaluator->Evaluate(manifest)); + if (!may_match) { + return std::vector<ManifestEntry>{}; + } + } + + ICEBERG_ASSIGN_OR_RAISE( + auto reader, ManifestReader::Make(manifest, io, schema, specs_by_id)); + if (partition_filter != nullptr) { + reader->FilterPartitions(partition_filter).CaseSensitive(case_sensitive); + } + reader->TryDropStats(); + ICEBERG_ASSIGN_OR_RAISE(auto entries, reader->Entries()); + // A delete file removed before the range no longer applies to any data + // file. One removed by a snapshot in the range applied until then. + std::erase_if(entries, [&](const ManifestEntry& entry) { + return entry.status == ManifestStatus::kDeleted && + !(entry.snapshot_id.has_value() && + sequence_numbers.contains(entry.snapshot_id.value())); + }); + return entries; + })); + + auto planner = std::unique_ptr<DeletionVectorChangelogPlanner>( + new DeletionVectorChangelogPlanner()); + for (auto& entry : entries) { + ICEBERG_PRECHECK(entry.data_file != nullptr, + "Invalid manifest entry with missing delete file"); + const DataFile& file = *entry.data_file; + // The file sequence number belongs to the snapshot that committed the file, unlike + // the data sequence number, which a rewrite can carry over from an older file. + ICEBERG_PRECHECK( + entry.file_sequence_number.has_value() && entry.sequence_number.has_value(), + "Missing sequence number from delete file {}", file.file_path); + ICEBERG_PRECHECK(file.partition_spec_id.has_value(), + "Missing partition spec ID from delete file {}", file.file_path); + + if (file.content == DataFile::Content::kEqualityDeletes) { + auto spec_it = specs_by_id.find(file.partition_spec_id.value()); + ICEBERG_CHECK(spec_it != specs_by_id.end(), + "Partition spec ID {} not found for delete file {}", + file.partition_spec_id.value(), file.file_path); + KeepLatest(spec_it->second->IsUnpartitioned() + ? planner->global_equality_deletes_ + : LatestFor(planner->equality_deletes_by_partition_, file), + entry.sequence_number.value(), file.file_path); + continue; + } + + if (!file.IsDeletionVector()) { + KeepLatest( + file.referenced_data_file.has_value() + ? planner->position_deletes_by_path_[file.referenced_data_file.value()] + : LatestFor(planner->position_deletes_by_partition_, file), Review Comment: This only checks `referenced_data_file`, but legacy position-delete files may encode a file-scoped delete through equal lower/upper bounds on the metadata file-path column. `DeleteFileIndex` resolves both forms via `ContentFileUtil::ReferencedDataFile` (see `delete_file_index.cc:701-705`), so this planner will misclassify such deletes as partition-scoped and can either reject unrelated files or miss an applicable path-scoped delete. Resolve the reference with the same helper before choosing `position_deletes_by_path_` versus the partition map. -- 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]
