wgtmac commented on code in PR #1121: URL: https://github.com/apache/iceberg-cpp/pull/1121#discussion_r4661267962
########## src/iceberg/inspect/manifests_table.cc: ########## @@ -0,0 +1,288 @@ +/* + * 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 <array> +#include <cstddef> +#include <cstdint> +#include <memory> +#include <optional> +#include <string> +#include <unordered_map> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_row_builder_internal.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" + +namespace iceberg { +namespace { + +struct ManifestRow { + ManifestFile manifest; + std::shared_ptr<PartitionSpec> spec; + std::shared_ptr<StructType> partition_type; +}; + +Result<std::shared_ptr<StructType>> ManifestPartitionType(const TableMetadata& metadata, + const PartitionSpec& spec) { + ICEBERG_ASSIGN_OR_RAISE(auto current_schema, metadata.Schema()); + std::vector<SchemaField> 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. Recover its most recent + // known type by field ID, including any promotion before the column was dropped. + if (!source) { + int32_t latest_schema_id = -1; + for (const auto& schema : metadata.schemas) { + ICEBERG_PRECHECK(schema != nullptr, "Table schema cannot be null"); + if (schema->schema_id() > latest_schema_id) { Review Comment: Can we pick the widest compatible type from schema history? Schema IDs don't have to increase. This can pick `int` and reject a valid eight-byte `long` bound. ########## src/iceberg/inspect/manifests_table.cc: ########## @@ -0,0 +1,288 @@ +/* + * 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 <array> +#include <cstddef> +#include <cstdint> +#include <memory> +#include <optional> +#include <string> +#include <unordered_map> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_row_builder_internal.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" + +namespace iceberg { +namespace { + +struct ManifestRow { + ManifestFile manifest; + std::shared_ptr<PartitionSpec> spec; + std::shared_ptr<StructType> partition_type; +}; + +Result<std::shared_ptr<StructType>> ManifestPartitionType(const TableMetadata& metadata, + const PartitionSpec& spec) { + ICEBERG_ASSIGN_OR_RAISE(auto current_schema, metadata.Schema()); + std::vector<SchemaField> 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. Recover its most recent + // known type by field ID, including any promotion before the column was dropped. + if (!source) { + int32_t latest_schema_id = -1; + for (const auto& schema : metadata.schemas) { + ICEBERG_PRECHECK(schema != nullptr, "Table schema cannot be null"); + if (schema->schema_id() > latest_schema_id) { + ICEBERG_ASSIGN_OR_RAISE(auto historical, + schema->FindFieldById(partition_field.source_id())); + if (historical) { + source = historical; + latest_schema_id = schema->schema_id(); + } + } + } + } + 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<StructType>(std::move(fields)); +} + +Status AppendPartitionBound(ArrowArray* array, const PartitionField& field, + const std::shared_ptr<Type>& type, + const std::optional<std::vector<uint8_t>>& bound) { + if (!bound.has_value()) { + return AppendString(array, "null"); + } + if (type->type_id() == TypeId::kUnknown) { + return AppendNull(array); + } + auto primitive = internal::checked_pointer_cast<PrimitiveType>(type); + ICEBERG_ASSIGN_OR_RAISE(auto literal, + Conversions::FromBytes(std::move(primitive), *bound)); + ICEBERG_ASSIGN_OR_RAISE(auto text, field.transform()->ToHumanString(literal)); Review Comment: Can we use round-trip formatting for float and double bounds? Right now both `1e-7` and `2e-7` show up as `0.000000`. ########## src/iceberg/CMakeLists.txt: ########## @@ -58,6 +58,7 @@ set(ICEBERG_SOURCES geospatial.cc incremental_append_scan.cc incremental_changelog_scan.cc + inspect/manifests_table.cc Review Comment: please sort alphabetically ########## src/iceberg/test/inspect/manifests_table_test.cc: ########## @@ -0,0 +1,403 @@ +/* + * 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 <cstdint> +#include <format> +#include <memory> +#include <optional> +#include <string> +#include <utility> +#include <vector> + +#include <arrow/array.h> +#include <arrow/c/bridge.h> +#include <arrow/record_batch.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#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_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<std::shared_ptr<Table>> MakeTable( + const std::vector<std::shared_ptr<Snapshot>>& snapshots, + int64_t current_snapshot_id, std::shared_ptr<PartitionSpec> 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<ArrowArrayStream> 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<std::vector<std::shared_ptr<::arrow::RecordBatch>>> 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>& table, + const std::shared_ptr<Snapshot>& first) { + auto old_schema = schema_; + schema_ = std::make_shared<Schema>( + std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int32()), + SchemaField::MakeRequired(2, "data", string()), + SchemaField::MakeOptional(3, "extra", int64())}, + 1); + std::shared_ptr<PartitionSpec> 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<MockCatalog> catalog_ = std::make_shared<MockCatalog>(); +}; + +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<ManifestsTable>(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); Review Comment: Can we add a test with both data and delete manifests? Check for a nonzero delete count and zero counts in the opposite group for each row. -- 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]
