This is an automated email from the ASF dual-hosted git repository.
zhjwpku pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 55a2894b fix: fail scans when position deletes/DVs don't match data
file partition (#1159)
55a2894b is described below
commit 55a2894b6342357c0b5f04a72f310b34c6a31b7f
Author: Gang Wu <[email protected]>
AuthorDate: Tue Nov 24 07:44:11 2026 +0800
fix: fail scans when position deletes/DVs don't match data file partition
(#1159)
---
src/iceberg/delete_file_index.cc | 43 ++++++++++++++++++++-
src/iceberg/test/delete_file_index_test.cc | 35 +++++++++++++++++
src/iceberg/test/table_scan_test.cc | 60 ++++++++++++++++++++++++++++++
3 files changed, 137 insertions(+), 1 deletion(-)
diff --git a/src/iceberg/delete_file_index.cc b/src/iceberg/delete_file_index.cc
index a8c4ef12..0f604402 100644
--- a/src/iceberg/delete_file_index.cc
+++ b/src/iceberg/delete_file_index.cc
@@ -44,6 +44,42 @@
namespace iceberg {
+namespace {
+// TODO(gangwu): Use PartitionSpec to include field names, as Java's
+// PartitionData.toString() does.
+std::string DescribePartition(const PartitionValues& partition) {
+ std::string result = "(";
+ for (const auto& value : partition.values()) {
+ if (result.size() > 1) {
+ result += ", ";
+ }
+ result += value.ToString();
+ }
+ return result + ")";
+}
+
+Status ValidatePartitionMatch(const DataFile& deletion, const DataFile& data) {
+ ICEBERG_PRECHECK(data.partition_spec_id.has_value(),
+ "Missing partition spec id from data file {}",
data.file_path);
+ ICEBERG_CHECK(deletion.partition_spec_id == data.partition_spec_id,
+ "Mismatched partition specs ({}, {}) for delete file {} and
data file "
+ "{}: metadata is corrupted",
+ deletion.partition_spec_id.value_or(-1),
data.partition_spec_id.value(),
+ deletion.file_path, data.file_path);
+ // Partition identity treats two nulls as equal and canonicalizes
floating-point NaNs.
+ auto equal = [](const Literal& lhs, const Literal& rhs) {
+ return (lhs.IsNull() && rhs.IsNull()) || (lhs.IsNaN() && rhs.IsNaN()) ||
lhs == rhs;
+ };
+ ICEBERG_CHECK(
+ std::ranges::equal(deletion.partition.values(), data.partition.values(),
equal),
+ "Mismatched partition tuples ({}, {}) for delete file {} and data file
{}: "
+ "metadata is corrupted",
+ DescribePartition(deletion.partition), DescribePartition(data.partition),
+ deletion.file_path, data.file_path);
+ return {};
+}
+} // namespace
+
namespace internal {
Status EqualityDeleteFile::ConvertBoundsIfNeeded() const {
@@ -439,7 +475,11 @@ Result<std::vector<std::shared_ptr<DataFile>>>
DeleteFileIndex::FindPathDeletes(
return {};
}
- return it->second->Filter(seq);
+ auto deletes = it->second->Filter(seq);
+ for (const auto& deletion : deletes) {
+ ICEBERG_RETURN_UNEXPECTED(ValidatePartitionMatch(*deletion, data_file));
+ }
+ return deletes;
}
Result<std::shared_ptr<DataFile>> DeleteFileIndex::FindDV(
@@ -458,6 +498,7 @@ Result<std::shared_ptr<DataFile>> DeleteFileIndex::FindDV(
"sequence number {}",
it->second.sequence_number.value(), seq);
+ ICEBERG_RETURN_UNEXPECTED(ValidatePartitionMatch(*it->second.data_file,
data_file));
return it->second.data_file;
}
diff --git a/src/iceberg/test/delete_file_index_test.cc
b/src/iceberg/test/delete_file_index_test.cc
index 7f77c638..5cb58ea4 100644
--- a/src/iceberg/test/delete_file_index_test.cc
+++ b/src/iceberg/test/delete_file_index_test.cc
@@ -1114,6 +1114,41 @@ TEST_P(DeleteFileIndexTest, TestMultipleDVs) {
EXPECT_THAT(index_result, HasErrorMessage(file_a_->file_path));
}
+TEST_P(DeleteFileIndexTest, ReferencedDeleteMustMatchDataFilePartition) {
+ const auto spec_id = partitioned_spec_->spec_id();
+ auto deletion =
+ GetParam() == 3
+ ? MakeDV("/dv.puffin", file_a_->partition, spec_id,
file_a_->file_path)
+ : MakePositionDeleteFile("/pos.parquet", file_a_->partition,
spec_id);
+ if (GetParam() == 2) {
+ ICEBERG_UNWRAP_OR_FAIL(auto path,
Literal::String(file_a_->file_path).Serialize());
+ deletion->lower_bounds[MetadataColumns::kDeleteFilePathColumnId] = path;
+ deletion->upper_bounds[MetadataColumns::kDeleteFilePathColumnId] = path;
+ }
+ auto manifest = WriteDeleteManifest(
+ GetParam(), 1000, {MakeDeleteEntry(1000, 1, deletion)},
partitioned_spec_);
+ ICEBERG_UNWRAP_OR_FAIL(auto index, BuildIndex({manifest}));
+ ICEBERG_UNWRAP_OR_FAIL(auto matches, index->ForDataFile(1, *file_a_));
+ EXPECT_EQ(matches.size(), 1);
+
+ auto data = *file_a_;
+ data.partition = file_b_->partition;
+ auto result = index->ForDataFile(1, data);
+ EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed));
+ EXPECT_THAT(result, HasErrorMessage("Mismatched partition tuples ((0),
(1))"));
+ data.partition = file_a_->partition;
+ data.partition_spec_id = unpartitioned_spec_->spec_id();
+ result = index->ForDataFile(1, data);
+ EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed));
+ EXPECT_THAT(result, HasErrorMessage("Mismatched partition specs"));
+
+ data.partition_spec_id.reset();
+ result = index->ForDataFile(1, data);
+ EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
+ EXPECT_THAT(result, HasErrorMessage("Missing partition spec id from data
file"));
+ EXPECT_THAT(result, HasErrorMessage(data.file_path));
+}
+
TEST_P(DeleteFileIndexTest, TestInvalidDVSequenceNumber) {
auto version = GetParam();
if (version < 3) {
diff --git a/src/iceberg/test/table_scan_test.cc
b/src/iceberg/test/table_scan_test.cc
index a71031bd..33fe7bdf 100644
--- a/src/iceberg/test/table_scan_test.cc
+++ b/src/iceberg/test/table_scan_test.cc
@@ -38,6 +38,7 @@
#include "iceberg/data/file_scan_task_reader.h"
#include "iceberg/data_table_scan.h"
#include "iceberg/expression/expressions.h"
+#include "iceberg/metadata_columns.h"
#include "iceberg/parquet/parquet_register.h"
#include "iceberg/schema_internal.h"
#include "iceberg/snapshot.h"
@@ -828,6 +829,65 @@ TEST_P(TableScanTest, PlanFilesWithDeleteFiles) {
verify_tasks(streamed_tasks);
}
+TEST_P(TableScanTest, PlanFilesValidatesReferencedDeletePartition) {
+ const int version = GetParam();
+ if (version < 2) GTEST_SKIP() << "Requires row-level deletes";
+
+ auto check = [&](const PartitionValues& data_partition,
+ const PartitionValues& delete_partition,
+ const std::shared_ptr<PartitionSpec>& delete_spec,
+ std::string_view error) {
+ constexpr int64_t snapshot_id = 1000;
+ auto data =
+ MakeDataFile("/data.parquet", data_partition,
partitioned_spec_->spec_id());
+ auto deletion = MakePositionDeleteFile("/deletion", delete_partition,
+ delete_spec->spec_id(),
data->file_path);
+ if (version >= 3) {
+ deletion->file_format = FileFormatType::kPuffin;
+ deletion->content_offset = 4;
+ deletion->content_size_in_bytes = 6;
+ } else {
+ ICEBERG_UNWRAP_OR_FAIL(auto path,
Literal::String(data->file_path).Serialize());
+ deletion->lower_bounds[MetadataColumns::kDeleteFilePathColumnId] = path;
+ deletion->upper_bounds[MetadataColumns::kDeleteFilePathColumnId] = path;
+ }
+ auto added = [&](auto file) {
+ return MakeEntry(ManifestStatus::kAdded, snapshot_id, 1, file);
+ };
+ auto manifest =
+ WriteDataManifest(version, snapshot_id, {added(data)},
partitioned_spec_);
+ auto deletes =
+ WriteDeleteManifest(version, snapshot_id, {added(deletion)},
delete_spec);
+ auto snapshot = std::make_shared<Snapshot>(Snapshot{
+ .snapshot_id = snapshot_id,
+ .sequence_number = 1,
+ .manifest_list = WriteManifestList(version, snapshot_id, 1, {manifest,
deletes}),
+ .schema_id = schema_->schema_id()});
+ auto metadata =
+ ScanTestBase::MakeTableMetadata({snapshot}, snapshot_id, {},
delete_spec);
+ ICEBERG_UNWRAP_OR_FAIL(auto builder,
MakeScanBuilder<DataTableScan>(metadata));
+ ICEBERG_UNWRAP_OR_FAIL(auto scan, builder->Build());
+ auto planned = scan->PlanFiles();
+ if (error.empty()) {
+ ASSERT_TRUE(planned.has_value()) << planned.error().message;
+ ASSERT_EQ(planned->size(), 1U);
+ EXPECT_EQ(planned->front()->delete_files().size(), 1U);
+ } else {
+ ASSERT_FALSE(planned.has_value());
+ EXPECT_EQ(planned.error().kind, ErrorKind::kValidationFailed);
+ EXPECT_THAT(planned.error().message,
testing::HasSubstr(std::string(error)));
+ EXPECT_THAT(planned.error().message,
testing::HasSubstr(data->file_path));
+ EXPECT_THAT(planned.error().message,
testing::HasSubstr(deletion->file_path));
+ }
+ };
+ PartitionValues zero(Literal::Int(0));
+ check(zero, PartitionValues(Literal::Int(1)), partitioned_spec_,
+ "Mismatched partition tuples");
+ check(zero, PartitionValues{}, unpartitioned_spec_, "Mismatched partition
specs");
+ PartitionValues null_partition(Literal::Null(int32()));
+ check(null_partition, null_partition, partitioned_spec_, "");
+}
+
TEST_P(TableScanTest, SchemaWithSelectedColumnsAndFilter) {
auto schema = std::make_shared<Schema>(std::vector<SchemaField>{
SchemaField::MakeRequired(/*field_id=*/1, "id", int32()),