diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 0768f1aaf..39d1b0ad9 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -59,6 +59,7 @@ set(ICEBERG_SOURCES incremental_append_scan.cc incremental_changelog_scan.cc inspect/history_table.cc + inspect/manifests_table.cc inspect/metadata_scan.cc inspect/metadata_table.cc inspect/metadata_scan_task.cc diff --git a/src/iceberg/cpp20_compatibility_internal.h b/src/iceberg/cpp20_compatibility_internal.h index d9c64d36a..ef7ac765d 100644 --- a/src/iceberg/cpp20_compatibility_internal.h +++ b/src/iceberg/cpp20_compatibility_internal.h @@ -81,6 +81,7 @@ #include "iceberg/incremental_changelog_scan.h" #include "iceberg/inheritable_metadata.h" #include "iceberg/inspect/history_table.h" +#include "iceberg/inspect/manifests_table.h" #include "iceberg/inspect/metadata_scan.h" #include "iceberg/inspect/metadata_scan_task.h" #include "iceberg/inspect/metadata_scan_task_reader.h" diff --git a/src/iceberg/inspect/manifests_table.cc b/src/iceberg/inspect/manifests_table.cc new file mode 100644 index 000000000..67b069a6b --- /dev/null +++ b/src/iceberg/inspect/manifests_table.cc @@ -0,0 +1,295 @@ +/* + * 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/inspect/manifests_table.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "iceberg/arrow_row_builder_internal.h" +#include "iceberg/expression/literal.h" +#include "iceberg/inspect/metadata_scan_util_internal.h" +#include "iceberg/inspect/metadata_table_util_internal.h" +#include "iceberg/logging/log_macros.h" +#include "iceberg/manifest/manifest_list.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/partition_field.h" +#include "iceberg/partition_spec.h" +#include "iceberg/schema.h" +#include "iceberg/schema_field.h" +#include "iceberg/snapshot.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transform.h" +#include "iceberg/type.h" +#include "iceberg/util/checked_cast.h" +#include "iceberg/util/conversions.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/type_util.h" + +namespace iceberg { +namespace { + +struct ManifestRow { + ManifestFile manifest; + std::shared_ptr spec; + std::shared_ptr partition_type; +}; + +Result> ManifestPartitionType(const TableMetadata& metadata, + const PartitionSpec& spec) { + ICEBERG_ASSIGN_OR_RAISE(auto current_schema, metadata.Schema()); + std::vector fields; + fields.reserve(spec.fields().size()); + for (const auto& partition_field : spec.fields()) { + ICEBERG_ASSIGN_OR_RAISE(auto source, + current_schema->FindFieldById(partition_field.source_id())); + // A retained spec can reference a dropped column. The widest compatible type + // can decode both original and promoted bounds, regardless of schema IDs. + if (!source) { + for (const auto& schema : metadata.schemas) { + ICEBERG_PRECHECK(schema != nullptr, "Table schema cannot be null"); + ICEBERG_ASSIGN_OR_RAISE(auto historical, + schema->FindFieldById(partition_field.source_id())); + if (historical && (!source || IsPromotionAllowed(source->get().type(), + historical->get().type()))) { + source = historical; + } + } + } + auto source_type = source ? source->get().type() : unknown(); + fields.push_back(SchemaField::MakeOptional( + partition_field.field_id(), std::string(partition_field.name()), + partition_field.transform()->ResultType(source_type))); + } + return std::make_shared(std::move(fields)); +} + +Status AppendPartitionBound(ArrowArray* array, const PartitionField& field, + const std::shared_ptr& type, + const std::optional>& bound) { + if (!bound.has_value() || type->type_id() == TypeId::kUnknown) { + return AppendString(array, "null"); + } + auto primitive = internal::checked_pointer_cast(type); + ICEBERG_ASSIGN_OR_RAISE(auto literal, + Conversions::FromBytes(std::move(primitive), *bound)); + // The default floating-point format preserves enough digits to round trip. + switch (type->type_id()) { + case TypeId::kFloat: + return AppendString(array, std::format("{}", std::get(literal.value()))); + case TypeId::kDouble: + return AppendString(array, std::format("{}", std::get(literal.value()))); + default: + break; + } + ICEBERG_ASSIGN_OR_RAISE(auto text, field.transform()->ToHumanString(literal)); + return AppendString(array, text); +} + +Status AppendPartitionSummaries(ArrowArray* array, const ManifestRow& row) { + const auto spec_fields = row.spec->fields(); + const auto& result_fields = row.partition_type->fields(); + ICEBERG_PRECHECK(row.manifest.partitions.size() <= spec_fields.size() && + row.manifest.partitions.size() <= result_fields.size(), + "Manifest '{}' has more partition summaries than spec {} fields", + row.manifest.manifest_path, row.spec->spec_id()); + auto* entries = array->children[0]; + ICEBERG_PRECHECK(entries != nullptr && entries->n_children == 4, + "Partition summaries must contain four fields"); + + for (size_t index = 0; index < row.manifest.partitions.size(); ++index) { + const auto& summary = row.manifest.partitions[index]; + ICEBERG_RETURN_UNEXPECTED(AppendBoolean(entries->children[0], summary.contains_null)); + if (summary.contains_nan.has_value()) { + ICEBERG_RETURN_UNEXPECTED( + AppendBoolean(entries->children[1], *summary.contains_nan)); + } else { + ICEBERG_RETURN_UNEXPECTED(AppendNull(entries->children[1])); + } + + const auto& field = spec_fields[index]; + const auto& type = result_fields[index].type(); + if (type->type_id() == TypeId::kUnknown && + (summary.lower_bound.has_value() || summary.upper_bound.has_value())) { + ICEBERG_LOG_WARN( + "Cannot render partition bounds with unknown type in manifest '{}' " + "(spec {}, field '{}' ({}), source column {})", + row.manifest.manifest_path, row.spec->spec_id(), field.name(), field.field_id(), + field.source_id()); + } + ICEBERG_RETURN_UNEXPECTED( + AppendPartitionBound(entries->children[2], field, type, summary.lower_bound)); + ICEBERG_RETURN_UNEXPECTED( + AppendPartitionBound(entries->children[3], field, type, summary.upper_bound)); + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(entries)); + } + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(array)); + return {}; +} + +template ManifestFile::* CountField, ManifestContent Content> +Status AppendManifestCount(ArrowArray* array, const ManifestRow& row) { + const auto& manifest = row.manifest; + if (manifest.content == Content) { + const auto& count = manifest.*CountField; + return count.has_value() ? AppendInt(array, *count) : AppendNull(array); + } else { + return AppendInt(array, 0); + } +} + +constexpr std::array, 12> kManifestColumns = {{ + {.field_id = 14, + .append = + [](ArrowArray* array, const ManifestRow& row) { + return AppendInt(array, static_cast(row.manifest.content)); + }}, + {.field_id = 1, + .append = + [](ArrowArray* array, const ManifestRow& row) { + return AppendString(array, row.manifest.manifest_path); + }}, + {.field_id = 2, + .append = + [](ArrowArray* array, const ManifestRow& row) { + return AppendInt(array, row.manifest.manifest_length); + }}, + {.field_id = 3, + .append = + [](ArrowArray* array, const ManifestRow& row) { + return AppendInt(array, row.manifest.partition_spec_id); + }}, + {.field_id = 4, + .append = [](ArrowArray* array, const ManifestRow& row) -> Status { + ICEBERG_PRECHECK(row.manifest.added_snapshot_id.has_value(), + "Manifest '{}' has no added snapshot ID", + row.manifest.manifest_path); + return AppendInt(array, *row.manifest.added_snapshot_id); + }}, + {.field_id = 5, + .append = + AppendManifestCount<&ManifestFile::added_files_count, ManifestContent::kData>}, + {.field_id = 6, + .append = AppendManifestCount<&ManifestFile::existing_files_count, + ManifestContent::kData>}, + {.field_id = 7, + .append = + AppendManifestCount<&ManifestFile::deleted_files_count, ManifestContent::kData>}, + {.field_id = 15, + .append = AppendManifestCount<&ManifestFile::added_files_count, + ManifestContent::kDeletes>}, + {.field_id = 16, + .append = AppendManifestCount<&ManifestFile::existing_files_count, + ManifestContent::kDeletes>}, + {.field_id = 17, + .append = AppendManifestCount<&ManifestFile::deleted_files_count, + ManifestContent::kDeletes>}, + {.field_id = 8, .append = AppendPartitionSummaries}, +}}; + +} // namespace + +ManifestsTable::ManifestsTable(std::shared_ptr table) + : MetadataTable(std::move(table)) {} + +ManifestsTable::~ManifestsTable() = default; + +std::shared_ptr ManifestsTable::schema() const { + static const auto schema = std::make_shared(std::vector{ + SchemaField::MakeRequired(14, "content", int32()), + SchemaField::MakeRequired(1, "path", string()), + SchemaField::MakeRequired(2, "length", int64()), + SchemaField::MakeRequired(3, "partition_spec_id", int32()), + SchemaField::MakeRequired(4, "added_snapshot_id", int64()), + // Legacy manifests can omit counts; unknown counts must stay null. + SchemaField::MakeOptional(5, "added_data_files_count", int32()), + SchemaField::MakeOptional(6, "existing_data_files_count", int32()), + SchemaField::MakeOptional(7, "deleted_data_files_count", int32()), + SchemaField::MakeOptional(15, "added_delete_files_count", int32()), + SchemaField::MakeOptional(16, "existing_delete_files_count", int32()), + SchemaField::MakeOptional(17, "deleted_delete_files_count", int32()), + SchemaField::MakeRequired( + 8, "partition_summaries", + list(SchemaField::MakeRequired( + 9, std::string(ListType::kElementName), + struct_({SchemaField::MakeRequired(10, "contains_null", boolean()), + SchemaField::MakeOptional(11, "contains_nan", boolean()), + SchemaField::MakeOptional(12, "lower_bound", string()), + SchemaField::MakeOptional(13, "upper_bound", string())})))), + }); + return schema; +} + +Result> ManifestsTable::Make( + std::shared_ptr table) { + ICEBERG_PRECHECK(table != nullptr, "Table cannot be null"); + return std::unique_ptr(new ManifestsTable(std::move(table))); +} + +Result> ManifestsTable::MakeScan( + internal::MetadataScanContext context, std::shared_ptr io) const { + return internal::MakeSnapshotMetadataScan(std::move(context), std::move(io), ReadRows); +} + +Result ManifestsTable::ReadRows( + const internal::MetadataScanContext& context, const std::shared_ptr& io, + int64_t batch_size) { + ICEBERG_ASSIGN_OR_RAISE(auto snapshot, internal::ResolveMetadataTableSnapshot( + *context.table_metadata, context)); + std::vector rows; + std::unordered_map> partition_types; + if (snapshot != nullptr) { + SnapshotReader snapshot_reader(snapshot.get()); + ICEBERG_ASSIGN_OR_RAISE(auto manifests, snapshot_reader.Manifests(io)); + rows.reserve(manifests.size()); + for (const auto& manifest : manifests) { + ICEBERG_ASSIGN_OR_RAISE(auto spec, context.table_metadata->PartitionSpecById( + manifest.partition_spec_id)); + auto [entry, inserted] = partition_types.try_emplace(spec->spec_id()); + if (inserted) { + ICEBERG_ASSIGN_OR_RAISE(entry->second, + ManifestPartitionType(*context.table_metadata, *spec)); + } + rows.push_back(ManifestRow{.manifest = manifest, + .spec = std::move(spec), + .partition_type = entry->second}); + } + } + + const size_t num_rows = rows.size(); + return internal::MakeMetadataBatchStream( + context.schema, context.projected_schema, kManifestColumns, num_rows, batch_size, + [rows = std::move(rows)](size_t index) -> const ManifestRow& { + return rows[index]; + }); +} + +} // namespace iceberg diff --git a/src/iceberg/inspect/manifests_table.h b/src/iceberg/inspect/manifests_table.h new file mode 100644 index 000000000..750deb12c --- /dev/null +++ b/src/iceberg/inspect/manifests_table.h @@ -0,0 +1,58 @@ +/* + * 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/inspect/manifests_table.h +/// \brief Define the manifests metadata table. + +#include + +#include "iceberg/arrow_c_data.h" +#include "iceberg/iceberg_export.h" +#include "iceberg/inspect/metadata_table.h" +#include "iceberg/result.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Metadata table containing manifest-list entries for a snapshot. +class ICEBERG_EXPORT ManifestsTable : public MetadataTable { + public: + static Result> Make(std::shared_ptr table); + + ~ManifestsTable() override; + + Kind kind() const noexcept override { return Kind::kManifests; } + + std::shared_ptr schema() const override; + + protected: + Result> MakeScan( + internal::MetadataScanContext context, std::shared_ptr io) const override; + + private: + static Result ReadRows(const internal::MetadataScanContext& context, + const std::shared_ptr& io, + int64_t batch_size); + + explicit ManifestsTable(std::shared_ptr table); +}; + +} // namespace iceberg diff --git a/src/iceberg/inspect/metadata_table.cc b/src/iceberg/inspect/metadata_table.cc index 57d8cdad3..26ec23b36 100644 --- a/src/iceberg/inspect/metadata_table.cc +++ b/src/iceberg/inspect/metadata_table.cc @@ -23,6 +23,7 @@ #include #include "iceberg/inspect/history_table.h" +#include "iceberg/inspect/manifests_table.h" #include "iceberg/inspect/metadata_scan.h" #include "iceberg/inspect/refs_table.h" #include "iceberg/inspect/snapshots_table.h" @@ -43,6 +44,8 @@ Result> MetadataTable::Make( return RefsTable::Make(std::move(table)); case MetadataTableKind::kHistory: return HistoryTable::Make(std::move(table)); + case MetadataTableKind::kManifests: + return ManifestsTable::Make(std::move(table)); default: break; } diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index 59506b166..107d63941 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -210,7 +210,8 @@ if(ICEBERG_BUILD_BUNDLE) inspect/history_table_test.cc inspect/metadata_table_test.cc inspect/refs_table_test.cc - inspect/snapshots_table_test.cc) + inspect/snapshots_table_test.cc + inspect/manifests_table_test.cc) add_iceberg_test(eval_expr_test USE_BUNDLE diff --git a/src/iceberg/test/inspect/manifests_table_test.cc b/src/iceberg/test/inspect/manifests_table_test.cc new file mode 100644 index 000000000..6573917f3 --- /dev/null +++ b/src/iceberg/test/inspect/manifests_table_test.cc @@ -0,0 +1,499 @@ +/* + * 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/inspect/manifests_table.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "iceberg/arrow/arrow_io_util.h" +#include "iceberg/constants.h" +#include "iceberg/inspect/metadata_scan.h" +#include "iceberg/inspect/metadata_scan_task_reader.h" +#include "iceberg/inspect/metadata_table.h" +#include "iceberg/logging/logger.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/manifest/manifest_list.h" +#include "iceberg/partition_spec.h" +#include "iceberg/row/partition_values.h" +#include "iceberg/schema.h" +#include "iceberg/schema_field.h" +#include "iceberg/snapshot.h" +#include "iceberg/table.h" +#include "iceberg/table_identifier.h" +#include "iceberg/table_metadata.h" +#include "iceberg/test/logging_test_helpers.h" +#include "iceberg/test/matchers.h" +#include "iceberg/test/mock_catalog.h" +#include "iceberg/test/scan_test_base.h" +#include "iceberg/transform.h" +#include "iceberg/type.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/timepoint.h" + +namespace iceberg { +namespace { + +class ManifestsTableTest : public ScanTestBase { + protected: + Result> MakeTable( + const std::vector>& snapshots, + int64_t current_snapshot_id, std::shared_ptr spec = nullptr) { + auto metadata = + MakeTableMetadata(snapshots, current_snapshot_id, {}, std::move(spec)); + return Table::Make( + TableIdentifier{.ns = Namespace{.levels = {"db"}}, .name = "table"}, + std::move(metadata), "s3://bucket/metadata.json", file_io_, catalog_); + } + + static Result Scan(const MetadataTable& table) { + ICEBERG_ASSIGN_OR_RAISE(auto builder, table.NewScan()); + ICEBERG_ASSIGN_OR_RAISE(auto scan, builder->Build()); + return scan->ToArrowArrayStream(); + } + + static Result>> ReadAllBatches( + ArrowArrayStream&& stream) { + auto reader = ::arrow::ImportRecordBatchReader(&stream); + if (!reader.ok()) { + return InvalidArrowData(reader.status().ToString()); + } + auto batches = reader.ValueUnsafe()->ToRecordBatches(); + if (!batches.ok()) { + return InvalidArrowData(batches.status().ToString()); + } + return std::move(batches).MoveValueUnsafe(); + } + + Status RefreshTableSchemaAndPartitionSpec(const std::shared_ptr& table, + const std::shared_ptr& first) { + auto old_schema = schema_; + schema_ = std::make_shared( + std::vector{SchemaField::MakeRequired(1, "id", int32()), + SchemaField::MakeRequired(2, "data", string()), + SchemaField::MakeOptional(3, "extra", int64())}, + 1); + std::shared_ptr new_spec; + ICEBERG_ASSIGN_OR_RAISE( + new_spec, + PartitionSpec::Make( + 2, {PartitionField(2, 1000, "data_bucket_16_2", Transform::Bucket(16)), + PartitionField(1, 1001, "id", Transform::Identity())})); + auto second = MakeAppendSnapshotWithPartitionValues( + GetParam(), 20, 10, 2, + {{"one.parquet", PartitionValues({Literal::Int(7), Literal::Int(1)})}, + {"two.parquet", PartitionValues({Literal::Int(7), Literal::Int(2)})}}, + new_spec); + auto metadata = MakeTableMetadata({first, second}, 20, {}, new_spec); + metadata->schemas.insert(metadata->schemas.begin(), old_schema); + metadata->partition_specs.push_back(new_spec); + metadata->last_column_id = 3; + metadata->last_partition_id = 1001; + ICEBERG_ASSIGN_OR_RAISE( + auto refreshed, + Table::Make(table->name(), std::move(metadata), "s3://bucket/refreshed.json", + arrow::MakeMockFileIO(), catalog_)); + EXPECT_CALL(*catalog_, LoadTable(::testing::_)) + .WillOnce(::testing::Return(refreshed)); + return table->Refresh(); + } + + const std::shared_ptr catalog_ = std::make_shared(); +}; + +TEST_P(ManifestsTableTest, Scan) { + auto snapshot = MakeAppendSnapshotWithPartitionValues( + GetParam(), 10, std::nullopt, 1, + {{"s3://bucket/data.parquet", PartitionValues(Literal::Int(7))}}, + partitioned_spec_); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({snapshot}, 10, partitioned_spec_)); + + ICEBERG_UNWRAP_OR_FAIL(auto manifests, MetadataTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests_stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests_batches, + ReadAllBatches(std::move(manifests_stream))); + ASSERT_EQ(manifests_batches.size(), 1); + ASSERT_EQ(manifests_batches[0]->num_rows(), 1); + auto manifest_paths = std::static_pointer_cast<::arrow::StringArray>( + manifests_batches[0]->GetColumnByName("path")); + auto added_files = std::static_pointer_cast<::arrow::Int32Array>( + manifests_batches[0]->GetColumnByName("added_data_files_count")); + EXPECT_FALSE(manifest_paths->GetString(0).empty()); + EXPECT_EQ(added_files->Value(0), 1); +} + +TEST_P(ManifestsTableTest, CountsMatchManifestContent) { + auto data_manifest = WriteDataManifest( + GetParam(), 10, + {MakeEntry(ManifestStatus::kAdded, 10, 1, MakeDataFile("data.parquet"))}); + auto delete_file = MakeDataFile("delete.parquet"); + delete_file->content = DataFile::Content::kEqualityDeletes; + delete_file->equality_ids = {1}; + auto delete_manifest = WriteDeleteManifest( + GetParam(), 10, {MakeEntry(ManifestStatus::kAdded, 10, 1, std::move(delete_file))}, + unpartitioned_spec_); + auto snapshot = std::make_shared(Snapshot{ + .snapshot_id = 10, + .sequence_number = 1, + .timestamp_ms = TimePointMsFromUnixMs(1000), + .manifest_list = + WriteManifestList(GetParam(), 10, 0, 1, {data_manifest, delete_manifest}), + }); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({snapshot}, 10)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, ManifestsTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + const auto& batch = batches.front(); + ASSERT_EQ(batch->num_rows(), 2); + auto content = std::static_pointer_cast<::arrow::Int32Array>(batch->column(0)); + EXPECT_EQ(content->Value(0), static_cast(ManifestContent::kData)); + EXPECT_EQ(content->Value(1), static_cast(ManifestContent::kDeletes)); + for (int index = 5; index <= 10; ++index) { + SCOPED_TRACE(batch->schema()->field(index)->name()); + auto counts = std::static_pointer_cast<::arrow::Int32Array>(batch->column(index)); + EXPECT_EQ(counts->null_count(), 0); + EXPECT_EQ(counts->Value(0), index == 5 ? 1 : 0); + EXPECT_EQ(counts->Value(1), index == 8 ? 1 : 0); + } +} + +TEST_P(ManifestsTableTest, PreservesUnknownManifestCounts) { + // Upgraded tables can still reference v1 manifest lists, where counts are optional. + auto unknown = WriteDataManifest( + 1, 10, {MakeEntry(ManifestStatus::kAdded, 10, 0, MakeDataFile("data.parquet"))}); + unknown.added_files_count.reset(); + unknown.existing_files_count.reset(); + unknown.deleted_files_count.reset(); + auto empty = WriteDataManifest(1, 10, {}); + auto snapshot = std::make_shared(Snapshot{ + .snapshot_id = 10, + .sequence_number = 0, + .timestamp_ms = TimePointMsFromUnixMs(1000), + .manifest_list = WriteManifestList(1, 10, 0, 0, {unknown, empty}), + }); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({snapshot}, 10)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, MetadataTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + ASSERT_EQ(batches[0]->num_rows(), 2); + for (int index = 5; index <= 10; ++index) { + auto counts = + std::static_pointer_cast<::arrow::Int32Array>(batches[0]->column(index)); + if (index < 8) { + EXPECT_TRUE(counts->IsNull(0)); + } else { + // Delete counts are definitively zero in a data manifest. + EXPECT_FALSE(counts->IsNull(0)); + EXPECT_EQ(counts->Value(0), 0); + } + // A known zero must remain distinguishable from an unknown count. + EXPECT_FALSE(counts->IsNull(1)); + EXPECT_EQ(counts->Value(1), 0); + EXPECT_TRUE(batches[0]->schema()->field(index)->nullable()); + EXPECT_TRUE(manifests->schema()->fields()[index].optional()); + } +} + +TEST_P(ManifestsTableTest, ManifestNullBoundsUseJavaRendering) { + auto snapshot = MakeAppendSnapshotWithPartitionValues( + GetParam(), 10, std::nullopt, 1, + {{"null.parquet", PartitionValues(Literal::Null(int32()))}}, partitioned_spec_); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({snapshot}, 10, partitioned_spec_)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, MetadataTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + auto summaries = std::static_pointer_cast<::arrow::ListArray>( + batches[0]->GetColumnByName("partition_summaries")); + auto values = std::static_pointer_cast<::arrow::StructArray>(summaries->values()); + ASSERT_EQ(values->length(), 1); + for (int index : {2, 3}) { + auto bound = std::static_pointer_cast<::arrow::StringArray>(values->field(index)); + ASSERT_FALSE(bound->IsNull(0)); + EXPECT_EQ(bound->GetString(0), "null"); + } +} + +TEST_P(ManifestsTableTest, FloatingPointBoundsRoundTrip) { + schema_ = std::make_shared(std::vector{ + SchemaField::MakeRequired(1, "f32", float32()), + SchemaField::MakeRequired(2, "f64", float64()), + }); + ICEBERG_UNWRAP_OR_FAIL( + partitioned_spec_, + PartitionSpec::Make(1, {PartitionField(1, 1000, "f32", Transform::Identity()), + PartitionField(2, 1001, "f64", Transform::Identity())})); + auto snapshot = MakeAppendSnapshotWithPartitionValues( + GetParam(), 10, std::nullopt, 1, + {{"low.parquet", PartitionValues({Literal::Float(1e-7F), Literal::Double(1e-7)})}, + {"high.parquet", PartitionValues({Literal::Float(2e-7F), Literal::Double(2e-7)})}}, + partitioned_spec_); + auto metadata = MakeTableMetadata({snapshot}, 10, {}, partitioned_spec_); + metadata->last_partition_id = 1001; + ICEBERG_UNWRAP_OR_FAIL( + auto table, Table::Make(TableIdentifier{.name = "table"}, std::move(metadata), + "s3://bucket/metadata.json", file_io_, catalog_)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, ManifestsTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + auto summaries = std::static_pointer_cast<::arrow::ListArray>( + batches[0]->GetColumnByName("partition_summaries")); + auto values = std::static_pointer_cast<::arrow::StructArray>(summaries->values()); + ASSERT_EQ(values->length(), 2); + auto lower = std::static_pointer_cast<::arrow::StringArray>(values->field(2)); + auto upper = std::static_pointer_cast<::arrow::StringArray>(values->field(3)); + EXPECT_EQ(std::stof(lower->GetString(0)), 1e-7F); + EXPECT_EQ(std::stof(upper->GetString(0)), 2e-7F); + EXPECT_EQ(std::stod(lower->GetString(1)), 1e-7); + EXPECT_EQ(std::stod(upper->GetString(1)), 2e-7); +} + +TEST_P(ManifestsTableTest, ManifestBoundsSurviveDroppedBucketSource) { + auto snapshot = MakeAppendSnapshotWithPartitionValues( + GetParam(), 10, std::nullopt, 1, + {{"data.parquet", PartitionValues(Literal::Int(7))}}, partitioned_spec_); + auto metadata = MakeTableMetadata({snapshot}, 10); + metadata->schemas.push_back(std::make_shared( + std::vector{SchemaField::MakeRequired(1, "id", int32())}, 1)); + metadata->current_schema_id = 1; + ICEBERG_UNWRAP_OR_FAIL( + auto table, Table::Make(TableIdentifier{.name = "table"}, std::move(metadata), + "s3://bucket/metadata.json", file_io_, catalog_)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, MetadataTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + auto summaries = std::static_pointer_cast<::arrow::ListArray>( + batches[0]->GetColumnByName("partition_summaries")); + auto values = std::static_pointer_cast<::arrow::StructArray>(summaries->values()); + ASSERT_EQ(values->length(), 1); + for (int index : {2, 3}) { + auto bound = std::static_pointer_cast<::arrow::StringArray>(values->field(index)); + ASSERT_FALSE(bound->IsNull(0)); + EXPECT_EQ(bound->GetString(0), "7"); + } +} + +TEST_P(ManifestsTableTest, ManifestBoundsUseHistoricalTypesForDroppedSources) { + std::shared_ptr spec; + ICEBERG_UNWRAP_OR_FAIL( + spec, PartitionSpec::Make( + 1, {PartitionField(1, 1000, "id", Transform::Identity()), + PartitionField(2, 1001, "data_prefix", Transform::Truncate(3))})); + auto write_manifest = [&](const std::string& path, Literal id) { + return WriteDataManifest( + GetParam(), 10, + {MakeEntry( + ManifestStatus::kAdded, 10, 1, + MakeDataFile(path, PartitionValues({std::move(id), Literal::String("abc")}), + spec))}, + spec); + }; + auto original = std::make_shared( + std::vector{SchemaField::MakeRequired(1, "id", int32()), + SchemaField::MakeRequired(2, "data", string())}, + 3); + schema_ = original; + auto old_manifest = write_manifest("int.parquet", Literal::Int(7)); + schema_ = std::make_shared( + std::vector{SchemaField::MakeRequired(1, "id", int64()), + SchemaField::MakeRequired(2, "data", string())}, + 1); + auto new_manifest = write_manifest("long.parquet", Literal::Long(4294967296LL)); + auto snapshot = std::make_shared(Snapshot{ + .snapshot_id = 10, + .sequence_number = 1, + .timestamp_ms = TimePointMsFromUnixMs(1000), + .manifest_list = + WriteManifestList(GetParam(), 10, 0, 1, {old_manifest, new_manifest}), + }); + auto metadata = MakeTableMetadata({snapshot}, 10); + auto current = std::make_shared( + std::vector{SchemaField::MakeOptional(3, "extra", string())}, 2); + // The schema ID decreased on promotion. The manifest list contains both encodings. + metadata->schemas = {original, schema_, current}; + metadata->current_schema_id = 2; + metadata->last_column_id = 3; + metadata->partition_specs = {unpartitioned_spec_, spec}; + metadata->last_partition_id = 1001; + ICEBERG_UNWRAP_OR_FAIL( + auto table, Table::Make(TableIdentifier{.name = "table"}, std::move(metadata), + "s3://bucket/metadata.json", file_io_, catalog_)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, ManifestsTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + auto summaries = std::static_pointer_cast<::arrow::ListArray>( + batches[0]->GetColumnByName("partition_summaries")); + auto values = std::static_pointer_cast<::arrow::StructArray>(summaries->values()); + ASSERT_EQ(values->length(), 4); + for (int index : {2, 3}) { + auto bounds = std::static_pointer_cast<::arrow::StringArray>(values->field(index)); + EXPECT_EQ(bounds->GetString(0), "7"); + EXPECT_EQ(bounds->GetString(1), "abc"); + EXPECT_EQ(bounds->GetString(2), "4294967296"); + EXPECT_EQ(bounds->GetString(3), "abc"); + } +} + +TEST_P(ManifestsTableTest, ManifestBoundsWithMissingSourceUseJavaRendering) { + std::shared_ptr spec; + ICEBERG_UNWRAP_OR_FAIL( + spec, PartitionSpec::Make( + 1, {PartitionField(1, 1000, "id", Transform::Identity()), + PartitionField(1, 1001, "id_bucket", Transform::Bucket(16)), + PartitionField(2, 1002, "data_prefix", Transform::Truncate(3))})); + auto snapshot = MakeAppendSnapshotWithPartitionValues( + GetParam(), 10, std::nullopt, 1, + {{"data.parquet", + PartitionValues({Literal::Int(7), Literal::Int(3), Literal::String("abc")})}}, + spec); + auto metadata = MakeTableMetadata({snapshot}, 10); + metadata->schemas = {std::make_shared( + std::vector{SchemaField::MakeRequired(2, "data", string())}, 1)}; + metadata->current_schema_id = 1; + metadata->partition_specs = {unpartitioned_spec_, spec}; + metadata->last_partition_id = 1002; + ICEBERG_UNWRAP_OR_FAIL( + auto table, Table::Make(TableIdentifier{.name = "table"}, std::move(metadata), + "s3://bucket/metadata.json", file_io_, catalog_)); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, ManifestsTable::Make(table)); + + auto logger = std::make_shared(); + logger->SetLevel(LogLevel::kWarn); + ScopedLogger bind(logger); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*manifests)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + auto summaries = std::static_pointer_cast<::arrow::ListArray>( + batches[0]->GetColumnByName("partition_summaries")); + auto values = std::static_pointer_cast<::arrow::StructArray>(summaries->values()); + ASSERT_EQ(values->length(), 3); + for (int index : {2, 3}) { + auto bounds = std::static_pointer_cast<::arrow::StringArray>(values->field(index)); + ASSERT_EQ(bounds->null_count(), 0); + EXPECT_EQ(bounds->GetString(0), "null"); + EXPECT_EQ(bounds->GetString(1), "3"); + EXPECT_EQ(bounds->GetString(2), "abc"); + } + + const auto warnings = logger->records(); + ASSERT_EQ(warnings.size(), 1); + EXPECT_EQ(warnings[0].level, LogLevel::kWarn); + EXPECT_THAT(warnings[0].message, + ::testing::HasSubstr("spec 1, field 'id' (1000), source column 1")); +} + +TEST_P(ManifestsTableTest, ScanEmpty) { + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({}, kInvalidSnapshotId)); + ICEBERG_UNWRAP_OR_FAIL(auto metadata_table, MetadataTable::Make(table)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, Scan(*metadata_table)); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + EXPECT_TRUE(batches.empty()); +} + +TEST_P(ManifestsTableTest, TasksRetainProjectionAndMetadataWithCustomBatchSize) { + std::vector manifests; + for (int32_t index = 0; index < 3; ++index) { + manifests.push_back(WriteDataManifest( + GetParam(), 10, + {MakeEntry( + ManifestStatus::kAdded, 10, 1, + MakeDataFile(std::format("file-{}.parquet", index), + PartitionValues(Literal::Int(index)), partitioned_spec_))}, + partitioned_spec_)); + } + auto first = std::make_shared( + Snapshot{.snapshot_id = 10, + .sequence_number = 1, + .timestamp_ms = TimePointMsFromUnixMs(1000), + .manifest_list = WriteManifestList(GetParam(), 10, 0, 1, manifests)}); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeTable({first}, 10, partitioned_spec_)); + ICEBERG_UNWRAP_OR_FAIL(auto metadata_table, + table->Inspect(MetadataTableKind::kManifests)); + const auto canonical_schema = metadata_table->schema(); + const auto& fields = canonical_schema->fields(); + auto projection = + std::make_shared(std::vector{fields[1], fields[0]}); + const std::string first_column(projection->fields()[0].name()); + const std::string second_column(projection->fields()[1].name()); + ICEBERG_UNWRAP_OR_FAIL(auto builder, metadata_table->NewScan()); + ICEBERG_UNWRAP_OR_FAIL(auto empty_builder, metadata_table->NewScan()); + ICEBERG_UNWRAP_OR_FAIL(auto invalid_builder, metadata_table->NewScan()); + auto invalid_projection = std::make_shared(std::vector{ + SchemaField::MakeRequired(fields[0].field_id(), "wrong_type", int64())}); + EXPECT_FALSE(invalid_builder->Project(invalid_projection).Build().has_value()); + + // Builders retain metadata even when the source refreshes before Build(). + ASSERT_THAT(RefreshTableSchemaAndPartitionSpec(table, first), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto scan, builder->Project(projection).Build()); + ICEBERG_UNWRAP_OR_FAIL(auto empty_scan, empty_builder->Select({}).Build()); + ICEBERG_UNWRAP_OR_FAIL(auto tasks, scan->PlanTasks()); + ICEBERG_UNWRAP_OR_FAIL(auto empty_tasks, empty_scan->PlanTasks()); + ASSERT_EQ(tasks.size(), 1); + ASSERT_EQ(empty_tasks.size(), 1); + builder.reset(); + empty_builder.reset(); + invalid_builder.reset(); + scan.reset(); + empty_scan.reset(); + metadata_table.reset(); + table.reset(); + + ICEBERG_UNWRAP_OR_FAIL(auto reader, MetadataScanTaskReader::Make({.batch_size = 2})); + ICEBERG_UNWRAP_OR_FAIL(auto stream, reader->Open(*tasks[0])); + ICEBERG_UNWRAP_OR_FAIL(auto repeat_stream, reader->Open(*tasks[0])); + ICEBERG_UNWRAP_OR_FAIL(auto empty_stream, reader->Open(*empty_tasks[0])); + reader.reset(); + tasks.clear(); + empty_tasks.clear(); + for (auto* output : {&stream, &repeat_stream}) { + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(*output))); + ASSERT_EQ(batches.size(), 2); + EXPECT_EQ(batches[0]->num_rows(), 2); + EXPECT_EQ(batches[1]->num_rows(), 1); + ASSERT_EQ(batches[0]->num_columns(), 2); + EXPECT_EQ(batches[0]->schema()->field(0)->name(), first_column); + EXPECT_EQ(batches[0]->schema()->field(1)->name(), second_column); + } + ICEBERG_UNWRAP_OR_FAIL(auto empty_batches, ReadAllBatches(std::move(empty_stream))); + ASSERT_EQ(empty_batches.size(), 2); + EXPECT_EQ(empty_batches[0]->num_rows(), 2); + EXPECT_EQ(empty_batches[1]->num_rows(), 1); + EXPECT_EQ(empty_batches[0]->num_columns(), 0); +} + +INSTANTIATE_TEST_SUITE_P(FormatVersions, ManifestsTableTest, ::testing::Values(2, 3)); + +} // namespace +} // namespace iceberg diff --git a/src/iceberg/type_fwd.h b/src/iceberg/type_fwd.h index 8e441d874..354495e38 100644 --- a/src/iceberg/type_fwd.h +++ b/src/iceberg/type_fwd.h @@ -286,6 +286,7 @@ class PositionDeleteIndex; enum class MetadataTableKind : uint8_t; class HistoryTable; +class ManifestsTable; class MetadataScanBuilder; class MetadataTable; class MetadataScan;