diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 0065b838b..310f6af5a 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -86,6 +86,7 @@ set(ICEBERG_SOURCES manifest/v1_metadata.cc manifest/v2_metadata.cc manifest/v3_metadata.cc + manifest/v4_types.cc metadata_columns.cc metrics_config.cc metrics/commit_report.cc diff --git a/src/iceberg/cpp20_compatibility_internal.h b/src/iceberg/cpp20_compatibility_internal.h index d9c64d36a..79b1b3e0e 100644 --- a/src/iceberg/cpp20_compatibility_internal.h +++ b/src/iceberg/cpp20_compatibility_internal.h @@ -103,6 +103,7 @@ #include "iceberg/manifest/manifest_reader.h" #include "iceberg/manifest/manifest_writer.h" #include "iceberg/manifest/rolling_manifest_writer.h" +#include "iceberg/manifest/v4_types.h" #include "iceberg/metadata_columns.h" #include "iceberg/metrics.h" #include "iceberg/metrics/commit_report.h" diff --git a/src/iceberg/manifest/v4_types.cc b/src/iceberg/manifest/v4_types.cc new file mode 100644 index 000000000..e5802754c --- /dev/null +++ b/src/iceberg/manifest/v4_types.cc @@ -0,0 +1,76 @@ +/* + * 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/manifest/v4_types.h" + +#include "iceberg/schema_field.h" +#include "iceberg/type.h" + +namespace iceberg { + +Result FileTrackingStatusFromInt(int32_t status) { + if (status < static_cast(FileTrackingStatus::kExisting) || + status > static_cast(FileTrackingStatus::kModified)) { + return InvalidArgument("Invalid file tracking status: {}", status); + } + return static_cast(status); +} + +std::shared_ptr FileTracking::Type() { + static const auto kInstance = std::make_shared(std::vector{ + SchemaField::MakeRequired(0, "status", int32()), + SchemaField::MakeOptional(1, "snapshot_id", int64()), + SchemaField::MakeOptional(5, "modified_snapshot_id", int64()), + SchemaField::MakeOptional(3, "sequence_number", int64()), + SchemaField::MakeOptional(4, "file_sequence_number", int64()), + SchemaField::MakeOptional(142, "first_row_id", int64()), + SchemaField::MakeOptional(6, "deleted_positions", binary()), + SchemaField::MakeOptional(7, "replaced_positions", binary())}); + return kInstance; +} + +std::shared_ptr DeletionVectorMetadata::Type() { + static const auto kInstance = std::make_shared( + std::vector{SchemaField::MakeRequired(155, "location", string()), + SchemaField::MakeRequired(144, "offset", int64()), + SchemaField::MakeRequired(145, "size_in_bytes", int64()), + SchemaField::MakeRequired(156, "cardinality", int64()), + SchemaField::MakeOptional(149, "key_metadata", binary())}); + return kInstance; +} + +std::shared_ptr ManifestInfo::Type() { + static const auto kInstance = std::make_shared(std::vector{ + SchemaField::MakeRequired(521, "format_version", int32()), + SchemaField::MakeOptional(522, "dv", binary()), + SchemaField::MakeRequired(504, "added_files_count", int32()), + SchemaField::MakeRequired(505, "existing_files_count", int32()), + SchemaField::MakeRequired(525, "modified_files_count", int32()), + SchemaField::MakeRequired(506, "deleted_files_count", int32()), + SchemaField::MakeRequired(523, "replaced_files_count", int32()), + SchemaField::MakeRequired(512, "added_rows_count", int64()), + SchemaField::MakeRequired(513, "existing_rows_count", int64()), + SchemaField::MakeRequired(526, "modified_rows_count", int64()), + SchemaField::MakeRequired(514, "deleted_rows_count", int64()), + SchemaField::MakeRequired(524, "replaced_rows_count", int64()), + SchemaField::MakeRequired(516, "min_sequence_number", int64())}); + return kInstance; +} + +} // namespace iceberg diff --git a/src/iceberg/manifest/v4_types.h b/src/iceberg/manifest/v4_types.h new file mode 100644 index 000000000..b44e5d14a --- /dev/null +++ b/src/iceberg/manifest/v4_types.h @@ -0,0 +1,132 @@ +/* + * 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/manifest/v4_types.h +/// Foundational metadata types for the v4 manifest tree. These types do not enable +/// v4 table reads or writes. The v4 specification is still under development. +/// See https://iceberg.apache.org/spec/#manifest-schema. + +#include +#include +#include +#include +#include + +#include "iceberg/iceberg_export.h" +#include "iceberg/result.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Status of a tracked file in a v4 manifest. +/// Kept separate from ManifestStatus because v1-v3 manifests cannot contain +/// REPLACED or MODIFIED entries. +enum class FileTrackingStatus { + kExisting = 0, + kAdded = 1, + kDeleted = 2, + kReplaced = 3, + kModified = 4, +}; + +ICEBERG_EXPORT Result FileTrackingStatusFromInt(int32_t status); + +/// \brief The v4 tracking struct, shared by data files and manifests. +struct ICEBERG_EXPORT FileTracking { + /// Field id: 0. ADDED, EXISTING, and MODIFIED entries are live. + FileTrackingStatus status = FileTrackingStatus::kAdded; + /// Field id: 1. Snapshot where the file was added, replaced, or deleted. + /// Inherited when null; writers should persist a non-null value. + std::optional snapshot_id; + /// Field id: 5. Snapshot where the file was last modified. + std::optional modified_snapshot_id; + /// Field id: 3. Data sequence number; inherited when null. + std::optional sequence_number; + /// Field id: 4. File sequence number; inherited when null. + std::optional file_sequence_number; + /// Field id: 142. Base row ID; subject to first row ID inheritance. + std::optional first_row_id; + /// Field id: 6. Manifest-only bitmap of positions deleted in modified_snapshot_id. + std::optional> deleted_positions; + /// Field id: 7. Manifest-only bitmap of positions replaced in modified_snapshot_id. + std::optional> replaced_positions; + + constexpr bool IsAlive() const { + return status == FileTrackingStatus::kExisting || + status == FileTrackingStatus::kAdded || + status == FileTrackingStatus::kModified; + } + + static std::shared_ptr Type(); +}; + +/// \brief The v4 deletion_vector struct, colocated with a tracked data file. +/// This references a Puffin blob; it does not hold the bitmap itself. +struct ICEBERG_EXPORT DeletionVectorMetadata { + /// Field id: 155. Location of the file containing the deletion vector. + std::string location; + /// Field id: 144. Must match the blob offset in the Puffin footer. + int64_t offset = 0; + /// Field id: 145. Must match the blob length in the Puffin footer. + int64_t size_in_bytes = 0; + /// Field id: 156. Number of deleted rows (set bits). + int64_t cardinality = 0; + /// Field id: 149. Encryption key metadata, absent for unencrypted blobs. + std::optional> key_metadata; + + static std::shared_ptr Type(); +}; + +/// \brief The v4 manifest_info struct, present only for tracked manifests. +/// File counts include all five tracking statuses and sum to the manifest's +/// record_count. This data carrier does not validate tracked-file requirements. +struct ICEBERG_EXPORT ManifestInfo { + /// Field id: 521. 0 denotes a pre-v4 manifest; 4 denotes a v4 manifest. + int32_t format_version = 0; + /// Field id: 522. Bitmap of positions that are no longer live in a leaf manifest. + std::optional> dv; + /// Field id: 504. + int32_t added_files_count = 0; + /// Field id: 505. + int32_t existing_files_count = 0; + /// Field id: 525. + int32_t modified_files_count = 0; + /// Field id: 506. + int32_t deleted_files_count = 0; + /// Field id: 523. + int32_t replaced_files_count = 0; + /// Field id: 512. + int64_t added_rows_count = 0; + /// Field id: 513. + int64_t existing_rows_count = 0; + /// Field id: 526. + int64_t modified_rows_count = 0; + /// Field id: 514. + int64_t deleted_rows_count = 0; + /// Field id: 524. + int64_t replaced_rows_count = 0; + /// Field id: 516. Minimum data sequence number of live entries. + int64_t min_sequence_number = 0; + + static std::shared_ptr Type(); +}; + +} // namespace iceberg diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index a6f3d8ba1..875dfbddf 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -100,6 +100,8 @@ add_iceberg_test(schema_test transform_test.cc type_test.cc) +add_iceberg_test(v4_types_test SOURCES v4_types_test.cc) + add_iceberg_test(table_test SOURCES catalog_util_test.cc diff --git a/src/iceberg/test/v4_types_test.cc b/src/iceberg/test/v4_types_test.cc new file mode 100644 index 000000000..82adce9d3 --- /dev/null +++ b/src/iceberg/test/v4_types_test.cc @@ -0,0 +1,185 @@ +/* + * 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/manifest/v4_types.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include "iceberg/json_serde_internal.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/test/matchers.h" +#include "iceberg/type.h" + +namespace iceberg { +namespace { + +// Independent wire-schema fixtures from the v4 manifest schema in the spec. +// Check IDs, names, order, types, and requiredness through the schema serializer. +void CheckSchema(const StructType& type, const nlohmann::json& expected) { + ICEBERG_UNWRAP_OR_FAIL(auto actual, ToJson(type)); + EXPECT_EQ(actual, expected); + ICEBERG_UNWRAP_OR_FAIL(auto parsed, TypeFromJson(expected)); + EXPECT_EQ(type, *parsed); +} + +TEST(V4TypesTest, FileTrackingSchema) { + const auto expected = nlohmann::json::parse(R"({ + "type": "struct", + "fields": [ + {"id": 0, "name": "status", "type": "int", "required": true}, + {"id": 1, "name": "snapshot_id", "type": "long", "required": false}, + {"id": 5, "name": "modified_snapshot_id", "type": "long", "required": false}, + {"id": 3, "name": "sequence_number", "type": "long", "required": false}, + {"id": 4, "name": "file_sequence_number", "type": "long", "required": false}, + {"id": 142, "name": "first_row_id", "type": "long", "required": false}, + {"id": 6, "name": "deleted_positions", "type": "binary", "required": false}, + {"id": 7, "name": "replaced_positions", "type": "binary", "required": false} + ] + })"); + auto type = FileTracking::Type(); + EXPECT_EQ(type, FileTracking::Type()); + CheckSchema(*type, expected); +} + +TEST(V4TypesTest, DeletionVectorMetadataSchema) { + const auto expected = nlohmann::json::parse(R"({ + "type": "struct", + "fields": [ + {"id": 155, "name": "location", "type": "string", "required": true}, + {"id": 144, "name": "offset", "type": "long", "required": true}, + {"id": 145, "name": "size_in_bytes", "type": "long", "required": true}, + {"id": 156, "name": "cardinality", "type": "long", "required": true}, + {"id": 149, "name": "key_metadata", "type": "binary", "required": false} + ] + })"); + auto type = DeletionVectorMetadata::Type(); + EXPECT_EQ(type, DeletionVectorMetadata::Type()); + CheckSchema(*type, expected); +} + +TEST(V4TypesTest, ManifestInfoSchema) { + const auto expected = nlohmann::json::parse(R"({ + "type": "struct", + "fields": [ + {"id": 521, "name": "format_version", "type": "int", "required": true}, + {"id": 522, "name": "dv", "type": "binary", "required": false}, + {"id": 504, "name": "added_files_count", "type": "int", "required": true}, + {"id": 505, "name": "existing_files_count", "type": "int", "required": true}, + {"id": 525, "name": "modified_files_count", "type": "int", "required": true}, + {"id": 506, "name": "deleted_files_count", "type": "int", "required": true}, + {"id": 523, "name": "replaced_files_count", "type": "int", "required": true}, + {"id": 512, "name": "added_rows_count", "type": "long", "required": true}, + {"id": 513, "name": "existing_rows_count", "type": "long", "required": true}, + {"id": 526, "name": "modified_rows_count", "type": "long", "required": true}, + {"id": 514, "name": "deleted_rows_count", "type": "long", "required": true}, + {"id": 524, "name": "replaced_rows_count", "type": "long", "required": true}, + {"id": 516, "name": "min_sequence_number", "type": "long", "required": true} + ] + })"); + auto type = ManifestInfo::Type(); + EXPECT_EQ(type, ManifestInfo::Type()); + CheckSchema(*type, expected); +} + +struct TrackingStatusCase { + int32_t value; + FileTrackingStatus status; + bool alive; +}; + +class FileTrackingStatusTest : public ::testing::TestWithParam {}; + +TEST_P(FileTrackingStatusTest, DecodeAndPlanLiveEntries) { + const auto& test_case = GetParam(); + ICEBERG_UNWRAP_OR_FAIL(auto status, FileTrackingStatusFromInt(test_case.value)); + EXPECT_EQ(status, test_case.status); + EXPECT_EQ(static_cast(status), test_case.value); + EXPECT_EQ(FileTracking{.status = status}.IsAlive(), test_case.alive); +} + +INSTANTIATE_TEST_SUITE_P( + AllStatuses, FileTrackingStatusTest, + ::testing::Values(TrackingStatusCase{0, FileTrackingStatus::kExisting, true}, + TrackingStatusCase{1, FileTrackingStatus::kAdded, true}, + TrackingStatusCase{2, FileTrackingStatus::kDeleted, false}, + TrackingStatusCase{3, FileTrackingStatus::kReplaced, false}, + TrackingStatusCase{4, FileTrackingStatus::kModified, true})); + +TEST(V4TypesTest, InvalidTrackingStatuses) { + for (int32_t value : {-1, 5, std::numeric_limits::min(), + std::numeric_limits::max()}) { + SCOPED_TRACE(value); + EXPECT_THAT(FileTrackingStatusFromInt(value), IsError(ErrorKind::kInvalidArgument)); + } +} + +TEST(V4TypesTest, LegacyStatusDecoderRejectsV4Statuses) { + for (int32_t value : {0, 1, 2}) { + EXPECT_THAT(ManifestStatusFromInt(value), IsOk()); + } + for (int32_t value : {3, 4}) { + EXPECT_THAT(ManifestStatusFromInt(value), IsError(ErrorKind::kInvalidArgument)); + } +} + +TEST(V4TypesTest, TrackingPreservesMissingInheritanceFieldsAndBitmaps) { + FileTracking tracking; + EXPECT_FALSE(tracking.snapshot_id.has_value()); + EXPECT_FALSE(tracking.modified_snapshot_id.has_value()); + EXPECT_FALSE(tracking.sequence_number.has_value()); + EXPECT_FALSE(tracking.file_sequence_number.has_value()); + EXPECT_FALSE(tracking.first_row_id.has_value()); + EXPECT_FALSE(tracking.deleted_positions.has_value()); + EXPECT_FALSE(tracking.replaced_positions.has_value()); + + // Missing sequence numbers must remain distinguishable from sequence number 0. + tracking.sequence_number = 0; + EXPECT_TRUE(tracking.sequence_number.has_value()); + EXPECT_EQ(tracking.sequence_number.value(), 0); + + tracking.deleted_positions = std::vector{}; + const auto copy = tracking; + ASSERT_TRUE(copy.deleted_positions.has_value()); + EXPECT_TRUE(copy.deleted_positions->empty()); + EXPECT_FALSE(copy.replaced_positions.has_value()); +} + +TEST(V4TypesTest, OptionalBinaryMetadataPreservesAbsence) { + DeletionVectorMetadata dv; + ManifestInfo manifest; + EXPECT_FALSE(dv.key_metadata.has_value()); + EXPECT_FALSE(manifest.dv.has_value()); + dv.key_metadata = std::vector{0, 255}; + manifest.dv = std::vector{}; + const auto dv_copy = dv; + const auto manifest_copy = manifest; + EXPECT_EQ(dv_copy.key_metadata, dv.key_metadata); + ASSERT_TRUE(manifest_copy.dv.has_value()); + EXPECT_TRUE(manifest_copy.dv->empty()); +} + +} // namespace +} // namespace iceberg