Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/iceberg/cpp20_compatibility_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
295 changes: 295 additions & 0 deletions src/iceberg/inspect/manifests_table.cc
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <cstddef>
#include <cstdint>
#include <format>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>

#include <nanoarrow/nanoarrow.h>

#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<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. 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<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() || type->type_id() == TypeId::kUnknown) {
return AppendString(array, "null");
}
auto primitive = internal::checked_pointer_cast<PrimitiveType>(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<float>(literal.value())));
case TypeId::kDouble:
return AppendString(array, std::format("{}", std::get<double>(literal.value())));
default:
break;
}
ICEBERG_ASSIGN_OR_RAISE(auto text, field.transform()->ToHumanString(literal));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use round-trip formatting for float and double bounds? Right now both 1e-7 and 2e-7 show up as 0.000000.

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 <std::optional<int32_t> 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<internal::MetadataTableColumn<ManifestRow>, 12> kManifestColumns = {{
{.field_id = 14,
.append =
[](ArrowArray* array, const ManifestRow& row) {
return AppendInt(array, static_cast<int64_t>(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<const Table> table)
: MetadataTable(std::move(table)) {}

ManifestsTable::~ManifestsTable() = default;

std::shared_ptr<Schema> ManifestsTable::schema() const {
static const auto schema = std::make_shared<Schema>(std::vector<SchemaField>{
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java impl's ManifestsTable has these fields as required [1], but it would throw NullPointerException on a null count (cond ? Integer : int unboxes) [2], I'm not sure if this is a bug from java side, and should we treat the unknown counts as 0.

[1] https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/ManifestsTable.java#L38-L43
[2] https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/ManifestsTable.java#L108-L113

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should unknown counts as null, to differentiate from real zero count.

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<std::unique_ptr<ManifestsTable>> ManifestsTable::Make(
std::shared_ptr<const Table> table) {
ICEBERG_PRECHECK(table != nullptr, "Table cannot be null");
return std::unique_ptr<ManifestsTable>(new ManifestsTable(std::move(table)));
}

Result<std::unique_ptr<MetadataScan>> ManifestsTable::MakeScan(
internal::MetadataScanContext context, std::shared_ptr<FileIO> io) const {
return internal::MakeSnapshotMetadataScan(std::move(context), std::move(io), ReadRows);
}

Result<ArrowArrayStream> ManifestsTable::ReadRows(
const internal::MetadataScanContext& context, const std::shared_ptr<FileIO>& io,
int64_t batch_size) {
ICEBERG_ASSIGN_OR_RAISE(auto snapshot, internal::ResolveMetadataTableSnapshot(
*context.table_metadata, context));
std::vector<ManifestRow> rows;
std::unordered_map<int32_t, std::shared_ptr<StructType>> 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<ManifestRow>(
context.schema, context.projected_schema, kManifestColumns, num_rows, batch_size,
[rows = std::move(rows)](size_t index) -> const ManifestRow& {
return rows[index];
});
}

} // namespace iceberg
58 changes: 58 additions & 0 deletions src/iceberg/inspect/manifests_table.h
Original file line number Diff line number Diff line change
@@ -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 <memory>

#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<std::unique_ptr<ManifestsTable>> Make(std::shared_ptr<const Table> table);

~ManifestsTable() override;

Kind kind() const noexcept override { return Kind::kManifests; }

std::shared_ptr<Schema> schema() const override;

protected:
Result<std::unique_ptr<MetadataScan>> MakeScan(
internal::MetadataScanContext context, std::shared_ptr<FileIO> io) const override;

private:
static Result<ArrowArrayStream> ReadRows(const internal::MetadataScanContext& context,
const std::shared_ptr<FileIO>& io,
int64_t batch_size);

explicit ManifestsTable(std::shared_ptr<const Table> table);
};

} // namespace iceberg
3 changes: 3 additions & 0 deletions src/iceberg/inspect/metadata_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>

#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"
Expand All @@ -43,6 +44,8 @@ Result<std::unique_ptr<MetadataTable>> 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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading