From 33c84fbf6790d2c1b2e80cadf9c98f43c7db23a6 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Sun, 15 Nov 2026 23:19:18 +0800 Subject: [PATCH 1/7] feat(inspect): capture scan inputs and open snapshot tasks Extract a focused piece from the metadata table implementation with its tests. Co-authored-by: Codex --- src/iceberg/CMakeLists.txt | 1 + src/iceberg/inspect/history_table.cc | 2 +- src/iceberg/inspect/history_table.h | 2 +- src/iceberg/inspect/metadata_scan.cc | 22 ++- src/iceberg/inspect/metadata_scan.h | 3 +- src/iceberg/inspect/metadata_scan_task.h | 2 + .../inspect/metadata_scan_task_reader.cc | 6 + src/iceberg/inspect/metadata_table.cc | 5 + src/iceberg/inspect/metadata_table.h | 8 +- .../inspect/metadata_table_util_internal.cc | 119 ++++++++++++++ .../inspect/metadata_table_util_internal.h | 63 +++++++ src/iceberg/inspect/refs_table.cc | 2 +- src/iceberg/inspect/refs_table.h | 2 +- src/iceberg/inspect/snapshots_table.cc | 4 +- src/iceberg/inspect/snapshots_table.h | 2 +- src/iceberg/test/CMakeLists.txt | 1 + .../inspect/metadata_snapshot_scan_test.cc | 155 ++++++++++++++++++ 17 files changed, 380 insertions(+), 19 deletions(-) create mode 100644 src/iceberg/inspect/metadata_table_util_internal.cc create mode 100644 src/iceberg/inspect/metadata_table_util_internal.h create mode 100644 src/iceberg/test/inspect/metadata_snapshot_scan_test.cc diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 4cde4fb23f..6bab2279ec 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -63,6 +63,7 @@ set(ICEBERG_SOURCES inspect/metadata_table.cc inspect/metadata_scan_task.cc inspect/metadata_scan_task_reader.cc + inspect/metadata_table_util_internal.cc inspect/refs_table.cc inspect/snapshots_table.cc inheritable_metadata.cc diff --git a/src/iceberg/inspect/history_table.cc b/src/iceberg/inspect/history_table.cc index a6f616fdb8..a6ffebbd86 100644 --- a/src/iceberg/inspect/history_table.cc +++ b/src/iceberg/inspect/history_table.cc @@ -77,7 +77,7 @@ HistoryTable::HistoryTable(std::shared_ptr table) HistoryTable::~HistoryTable() = default; -const std::shared_ptr& HistoryTable::schema() const { return CanonicalSchema(); } +std::shared_ptr HistoryTable::schema() const { return CanonicalSchema(); } const std::shared_ptr& HistoryTable::CanonicalSchema() { static const auto schema = std::make_shared(std::vector{ diff --git a/src/iceberg/inspect/history_table.h b/src/iceberg/inspect/history_table.h index ec18e36a4f..f1b949bf9e 100644 --- a/src/iceberg/inspect/history_table.h +++ b/src/iceberg/inspect/history_table.h @@ -46,7 +46,7 @@ class ICEBERG_EXPORT HistoryTable : public MetadataTable { MetadataTableKind kind() const noexcept override { return MetadataTableKind::kHistory; } - const std::shared_ptr& schema() const override; + std::shared_ptr schema() const override; protected: Result> MakeScan( diff --git a/src/iceberg/inspect/metadata_scan.cc b/src/iceberg/inspect/metadata_scan.cc index 2957a2ba44..03c03f567a 100644 --- a/src/iceberg/inspect/metadata_scan.cc +++ b/src/iceberg/inspect/metadata_scan.cc @@ -107,20 +107,26 @@ class TaskArrowStreamSource { } // namespace -MetadataScanBuilder::MetadataScanBuilder(const MetadataTable& table) - : table_(&table), - context_{.kind = table.kind(), - .table_metadata = table.source_table()->metadata(), - .schema = table.schema(), - .projected_schema = table.schema()} {} +MetadataScanBuilder::MetadataScanBuilder(const MetadataTable& table, + internal::MetadataScanContext context) + : table_(&table), context_(std::move(context)) {} MetadataScanBuilder::~MetadataScanBuilder() = default; Result> MetadataScanBuilder::Make( const MetadataTable& table) { ICEBERG_PRECHECK(table.source_table() != nullptr, "Source table cannot be null"); - ICEBERG_PRECHECK(table.schema() != nullptr, "Metadata table schema cannot be null"); - return std::unique_ptr(new MetadataScanBuilder(table)); + auto metadata = table.source_table()->metadata(); + ICEBERG_PRECHECK(metadata != nullptr, "Table metadata cannot be null"); + ICEBERG_ASSIGN_OR_RAISE(auto schema, table.SchemaForScan(*metadata)); + ICEBERG_PRECHECK(schema != nullptr, "Metadata table schema cannot be null"); + internal::MetadataScanContext context{.kind = table.kind(), + .table_metadata = std::move(metadata), + .schema = schema, + .projected_schema = std::move(schema), + .io = table.source_table()->io()}; + return std::unique_ptr( + new MetadataScanBuilder(table, std::move(context))); } MetadataScanBuilder& MetadataScanBuilder::Select( diff --git a/src/iceberg/inspect/metadata_scan.h b/src/iceberg/inspect/metadata_scan.h index 5df4cc0344..9289e65159 100644 --- a/src/iceberg/inspect/metadata_scan.h +++ b/src/iceberg/inspect/metadata_scan.h @@ -52,6 +52,7 @@ struct MetadataScanContext { std::optional snapshot_id; std::string ref_name; std::optional as_of_time_in_millis; + std::shared_ptr io; }; } // namespace internal @@ -97,7 +98,7 @@ class ICEBERG_EXPORT MetadataScanBuilder : public ErrorCollector { Result> Build(); private: - explicit MetadataScanBuilder(const MetadataTable& table); + MetadataScanBuilder(const MetadataTable& table, internal::MetadataScanContext context); const MetadataTable* table_; internal::MetadataScanContext context_; diff --git a/src/iceberg/inspect/metadata_scan_task.h b/src/iceberg/inspect/metadata_scan_task.h index 6e0dac54e8..fc746efcd2 100644 --- a/src/iceberg/inspect/metadata_scan_task.h +++ b/src/iceberg/inspect/metadata_scan_task.h @@ -37,6 +37,8 @@ class ICEBERG_EXPORT MetadataScanTask { enum class TaskKind { /// StaticMetadataTask reads in-memory table metadata (snapshots, history, refs). kStatic, + /// SnapshotMetadataTask reads snapshot manifests and their file entries. + kSnapshot, }; virtual ~MetadataScanTask() = default; diff --git a/src/iceberg/inspect/metadata_scan_task_reader.cc b/src/iceberg/inspect/metadata_scan_task_reader.cc index 74acaa8f33..abe69c4989 100644 --- a/src/iceberg/inspect/metadata_scan_task_reader.cc +++ b/src/iceberg/inspect/metadata_scan_task_reader.cc @@ -25,6 +25,7 @@ #include "iceberg/inspect/history_table.h" #include "iceberg/inspect/metadata_scan_task.h" #include "iceberg/inspect/metadata_table.h" +#include "iceberg/inspect/metadata_table_util_internal.h" #include "iceberg/inspect/refs_table.h" #include "iceberg/inspect/snapshots_table.h" #include "iceberg/util/checked_cast.h" @@ -47,6 +48,11 @@ Result> MetadataScanTaskReader::Make( Result MetadataScanTaskReader::Open( const MetadataScanTask& task) const { switch (task.task_kind()) { + case MetadataScanTask::TaskKind::kSnapshot: { + const auto& snapshot_task = + internal::checked_cast(task); + return snapshot_task.Open(options_.batch_size); + } case MetadataScanTask::TaskKind::kStatic: { const auto& static_task = internal::checked_cast(task); switch (static_task.table_kind()) { diff --git a/src/iceberg/inspect/metadata_table.cc b/src/iceberg/inspect/metadata_table.cc index 012ffaa995..57d8cdad3e 100644 --- a/src/iceberg/inspect/metadata_table.cc +++ b/src/iceberg/inspect/metadata_table.cc @@ -53,6 +53,11 @@ const std::shared_ptr& MetadataTable::source_table() const { return source_table_; } +Result> MetadataTable::SchemaForScan( + const TableMetadata& metadata) const { + return schema(); +} + Result> MetadataTable::NewScan() const { return MetadataScanBuilder::Make(*this); } diff --git a/src/iceberg/inspect/metadata_table.h b/src/iceberg/inspect/metadata_table.h index 83f44f5c17..99e98ef1ee 100644 --- a/src/iceberg/inspect/metadata_table.h +++ b/src/iceberg/inspect/metadata_table.h @@ -78,8 +78,8 @@ class ICEBERG_EXPORT MetadataTable { /// \brief Return this metadata table's kind. virtual Kind kind() const noexcept = 0; - /// \brief Return the complete canonical schema of this metadata table. - virtual const std::shared_ptr& schema() const = 0; + /// \brief Return shared ownership of this metadata table's canonical schema. + virtual std::shared_ptr schema() const = 0; /// \brief Return the source table whose metadata is exposed. const std::shared_ptr& source_table() const; @@ -93,6 +93,10 @@ class ICEBERG_EXPORT MetadataTable { protected: explicit MetadataTable(std::shared_ptr source_table); + /// \brief Derive a scan schema from the captured source metadata. + virtual Result> SchemaForScan( + const TableMetadata& metadata) const; + /// \brief Plan a scan using the captured metadata, projection, and snapshot selection. virtual Result> MakeScan( internal::MetadataScanContext context) const = 0; diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util_internal.cc new file mode 100644 index 0000000000..a599fd733e --- /dev/null +++ b/src/iceberg/inspect/metadata_table_util_internal.cc @@ -0,0 +1,119 @@ +/* + * 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/metadata_table_util_internal.h" + +#include +#include +#include +#include + +#include "iceberg/constants.h" +#include "iceberg/schema.h" +#include "iceberg/snapshot.h" +#include "iceberg/table_metadata.h" +#include "iceberg/type.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/snapshot_util.h" + +namespace iceberg::internal { + +namespace { + +Result> SnapshotAtRef(const TableMetadata& metadata, + std::string_view ref_name) { + if (ref_name.empty() || ref_name == SnapshotRef::kMainBranch) { + if (metadata.current_snapshot_id == kInvalidSnapshotId) { + return std::shared_ptr{nullptr}; + } + return metadata.SnapshotById(metadata.current_snapshot_id); + } + + auto ref = metadata.refs.find(std::string(ref_name)); + ICEBERG_PRECHECK(ref != metadata.refs.end(), "Cannot find snapshot reference '{}'", + ref_name); + ICEBERG_PRECHECK(ref->second != nullptr, "Snapshot reference '{}' is null", ref_name); + return metadata.SnapshotById(ref->second->snapshot_id); +} + +} // namespace + +namespace { +class SnapshotMetadataScan : public MetadataScan { + public: + SnapshotMetadataScan(MetadataScanContext context, + SnapshotMetadataTask::ReadRowsFunction read_rows) + : MetadataScan(context), + task_(std::make_shared(std::move(context), read_rows)) {} + + Result PlanTasksStream() const override { + return MetadataScanTaskStream::FromVector({task_}); + } + + private: + const std::shared_ptr task_; +}; +} // namespace + +Result> MakeSnapshotMetadataScan( + MetadataScanContext context, SnapshotMetadataTask::ReadRowsFunction read_rows) { + ICEBERG_PRECHECK(read_rows != nullptr, "Snapshot row reader cannot be null"); + ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); + ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); + ICEBERG_PRECHECK(context.projected_schema != nullptr, + "Projected schema cannot be null"); + ICEBERG_PRECHECK(context.io != nullptr, "FileIO cannot be null"); + for (const auto& projected : context.projected_schema->fields()) { + ICEBERG_ASSIGN_OR_RAISE(auto field, + context.schema->GetFieldById(projected.field_id())); + ICEBERG_PRECHECK(field.has_value(), "Unknown metadata column: {}", projected.name()); + if (*field->get().type() != *projected.type()) { + return NotSupported("Partial projection of metadata column is not supported: {}", + projected.name()); + } + ICEBERG_PRECHECK(!field->get().optional() || projected.optional(), + "Nullable metadata column cannot be projected as required: {}", + projected.name()); + } + ICEBERG_RETURN_UNEXPECTED( + ResolveMetadataTableSnapshot(*context.table_metadata, context)); + return std::make_unique(std::move(context), read_rows); +} + +Result> ResolveMetadataTableSnapshot( + const TableMetadata& metadata, const MetadataScanContext& selection) { + if (!selection.ref_name.empty() && selection.ref_name != SnapshotRef::kMainBranch) { + ICEBERG_PRECHECK( + !selection.snapshot_id.has_value() && !selection.as_of_time_in_millis.has_value(), + "Cannot combine a snapshot reference with a snapshot ID or timestamp"); + } + if (selection.snapshot_id.has_value()) { + return metadata.SnapshotById(*selection.snapshot_id); + } + if (selection.as_of_time_in_millis.has_value()) { + ICEBERG_ASSIGN_OR_RAISE( + auto snapshot_id, + SnapshotUtil::SnapshotIdAsOfTime( + metadata, TimePointMsFromUnixMs(*selection.as_of_time_in_millis))); + return metadata.SnapshotById(snapshot_id); + } + return SnapshotAtRef(metadata, selection.ref_name); +} + +} // namespace iceberg::internal diff --git a/src/iceberg/inspect/metadata_table_util_internal.h b/src/iceberg/inspect/metadata_table_util_internal.h new file mode 100644 index 0000000000..3fbc6816b6 --- /dev/null +++ b/src/iceberg/inspect/metadata_table_util_internal.h @@ -0,0 +1,63 @@ +/* + * 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 + +#include +#include +#include + +#include "iceberg/iceberg_export.h" +#include "iceberg/inspect/metadata_scan.h" +#include "iceberg/inspect/metadata_scan_task.h" +#include "iceberg/result.h" + +namespace iceberg::internal { + +/// A task retaining the captured metadata, projection, and manifest IO. +class SnapshotMetadataTask : public MetadataScanTask { + public: + using ReadRowsFunction = Result (*)(const MetadataScanContext&, + const std::shared_ptr&, + int64_t); + + SnapshotMetadataTask(MetadataScanContext context, ReadRowsFunction read_rows) + : context(std::move(context)), read_rows_(read_rows) {} + + Result Open(int64_t batch_size) const { + return read_rows_(context, context.io, batch_size); + } + + TaskKind task_kind() const override { return TaskKind::kSnapshot; } + MetadataTableKind table_kind() const override { return context.kind; } + + const MetadataScanContext context; + + private: + ReadRowsFunction read_rows_; +}; + +ICEBERG_EXPORT Result> MakeSnapshotMetadataScan( + MetadataScanContext context, SnapshotMetadataTask::ReadRowsFunction read_rows); + +/// \brief Resolve a time-travel selection to a snapshot. +ICEBERG_EXPORT Result> ResolveMetadataTableSnapshot( + const TableMetadata& metadata, const MetadataScanContext& selection); + +} // namespace iceberg::internal diff --git a/src/iceberg/inspect/refs_table.cc b/src/iceberg/inspect/refs_table.cc index c57e092bd0..25d064da3d 100644 --- a/src/iceberg/inspect/refs_table.cc +++ b/src/iceberg/inspect/refs_table.cc @@ -91,7 +91,7 @@ RefsTable::RefsTable(std::shared_ptr table) RefsTable::~RefsTable() = default; -const std::shared_ptr& RefsTable::schema() const { return CanonicalSchema(); } +std::shared_ptr RefsTable::schema() const { return CanonicalSchema(); } const std::shared_ptr& RefsTable::CanonicalSchema() { static const auto schema = std::make_shared(std::vector{ diff --git a/src/iceberg/inspect/refs_table.h b/src/iceberg/inspect/refs_table.h index baa11f1922..2628ffe6b3 100644 --- a/src/iceberg/inspect/refs_table.h +++ b/src/iceberg/inspect/refs_table.h @@ -46,7 +46,7 @@ class ICEBERG_EXPORT RefsTable : public MetadataTable { MetadataTableKind kind() const noexcept override { return MetadataTableKind::kRefs; } - const std::shared_ptr& schema() const override; + std::shared_ptr schema() const override; protected: Result> MakeScan( diff --git a/src/iceberg/inspect/snapshots_table.cc b/src/iceberg/inspect/snapshots_table.cc index 7eb204b43b..c54a71778b 100644 --- a/src/iceberg/inspect/snapshots_table.cc +++ b/src/iceberg/inspect/snapshots_table.cc @@ -82,9 +82,7 @@ SnapshotsTable::SnapshotsTable(std::shared_ptr table) SnapshotsTable::~SnapshotsTable() = default; -const std::shared_ptr& SnapshotsTable::schema() const { - return CanonicalSchema(); -} +std::shared_ptr SnapshotsTable::schema() const { return CanonicalSchema(); } const std::shared_ptr& SnapshotsTable::CanonicalSchema() { static const auto schema = std::make_shared(std::vector{ diff --git a/src/iceberg/inspect/snapshots_table.h b/src/iceberg/inspect/snapshots_table.h index 4bfaa554f3..2293ad929d 100644 --- a/src/iceberg/inspect/snapshots_table.h +++ b/src/iceberg/inspect/snapshots_table.h @@ -48,7 +48,7 @@ class ICEBERG_EXPORT SnapshotsTable : public MetadataTable { return MetadataTableKind::kSnapshots; } - const std::shared_ptr& schema() const override; + std::shared_ptr schema() const override; protected: Result> MakeScan( diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index 1874fc25e8..59506b1660 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -206,6 +206,7 @@ if(ICEBERG_BUILD_BUNDLE) add_iceberg_test(metadata_table_test USE_BUNDLE SOURCES + inspect/metadata_snapshot_scan_test.cc inspect/history_table_test.cc inspect/metadata_table_test.cc inspect/refs_table_test.cc diff --git a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc new file mode 100644 index 0000000000..eeb0627039 --- /dev/null +++ b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc @@ -0,0 +1,155 @@ +/* + * 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 +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "iceberg/inspect/metadata_scan_task_reader.h" +#include "iceberg/inspect/metadata_scan_util_internal.h" +#include "iceberg/inspect/metadata_table.h" +#include "iceberg/inspect/metadata_table_util_internal.h" +#include "iceberg/partition_spec.h" +#include "iceberg/schema.h" +#include "iceberg/schema_field.h" +#include "iceberg/test/inspect/metadata_table_test_base.h" +#include "iceberg/test/matchers.h" +#include "iceberg/transform.h" +#include "iceberg/type.h" + +namespace iceberg::internal { +namespace { + +std::shared_ptr IntSchema() { + return std::make_shared( + std::vector{SchemaField::MakeRequired(1, "id", int64())}); +} + +Result ReadIntTaskRows(const MetadataScanContext& context, + const std::shared_ptr& io, + int64_t batch_size) { + ICEBERG_PRECHECK(io != nullptr && context.table_metadata != nullptr, + "Task lost its input owners"); + const std::vector> columns{ + {.field_id = 1, .append = [](ArrowArray* array, const int64_t& row) { + return AppendInt(array, row); + }}}; + return MakeMetadataBatchStream( + context.schema, context.projected_schema, columns, 3, batch_size, + [metadata = context.table_metadata, io](size_t index) { + return static_cast(index + 1); + }); +} + +class SnapshotMetadataSupportTest : public MetadataTableTestBase { + protected: + MetadataScanContext Context() { + auto schema = IntSchema(); + return {.kind = MetadataTableKind::kFiles, + .table_metadata = metadata_, + .schema = schema, + .projected_schema = schema, + .io = io_}; + } +}; + +TEST_F(SnapshotMetadataSupportTest, TasksRetainOwnersAndCanBeReopened) { + ICEBERG_UNWRAP_OR_FAIL(auto scan, MakeSnapshotMetadataScan(Context(), ReadIntTaskRows)); + ICEBERG_UNWRAP_OR_FAIL(auto tasks, scan->PlanTasks()); + ASSERT_EQ(tasks.size(), 1); + scan.reset(); + table_.reset(); + metadata_.reset(); + io_.reset(); + for (int attempt = 0; attempt < 2; ++attempt) { + ICEBERG_UNWRAP_OR_FAIL(auto reader, MetadataScanTaskReader::Make({.batch_size = 2})); + ICEBERG_UNWRAP_OR_FAIL(auto stream, reader->Open(*tasks[0])); + reader.reset(); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 2); + EXPECT_EQ(batches[0]->num_rows(), 2); + EXPECT_EQ(batches[1]->num_rows(), 1); + ASSERT_EQ(batches[0]->column(0)->type_id(), ::arrow::Type::INT64); + auto ids = std::static_pointer_cast<::arrow::Int64Array>(batches[0]->column(0)); + EXPECT_EQ(ids->Value(0), 1); + } +} + +TEST_F(SnapshotMetadataSupportTest, RejectsInvalidProjectionAndReader) { + EXPECT_THAT(MakeSnapshotMetadataScan(Context(), nullptr), + IsError(ErrorKind::kInvalidArgument)); + auto context = Context(); + context.projected_schema = std::make_shared( + std::vector{SchemaField::MakeRequired(99, "missing", int64())}); + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); + context = Context(); + context.ref_name = "dev"; + context.snapshot_id = 1; + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); +} + +TEST_F(SnapshotMetadataSupportTest, ResolvesSnapshotIdsReferencesAndTimes) { + auto first = std::make_shared( + Snapshot{.snapshot_id = 10, .timestamp_ms = TimePointMsFromUnixMs(1000)}); + auto second = std::make_shared( + Snapshot{.snapshot_id = 20, .timestamp_ms = TimePointMsFromUnixMs(2000)}); + auto metadata = metadata_; + metadata->snapshots = {first, second}; + metadata->current_snapshot_id = 20; + metadata->refs["tag"] = std::make_shared( + SnapshotRef{.snapshot_id = 10, .retention = SnapshotRef::Tag{}}); + metadata->snapshot_log = { + {.timestamp_ms = TimePointMsFromUnixMs(1000), .snapshot_id = 10}, + {.timestamp_ms = TimePointMsFromUnixMs(2000), .snapshot_id = 20}}; + MetadataScanContext selection{.kind = MetadataTableKind::kFiles}; + ICEBERG_UNWRAP_OR_FAIL(auto current, + ResolveMetadataTableSnapshot(*metadata, selection)); + EXPECT_EQ(current, second); + selection.snapshot_id = 10; + ICEBERG_UNWRAP_OR_FAIL(auto by_id, ResolveMetadataTableSnapshot(*metadata, selection)); + EXPECT_EQ(by_id, first); + selection.snapshot_id.reset(); + selection.ref_name = "tag"; + ICEBERG_UNWRAP_OR_FAIL(auto by_ref, ResolveMetadataTableSnapshot(*metadata, selection)); + EXPECT_EQ(by_ref, first); + selection.ref_name.clear(); + selection.as_of_time_in_millis = 1500; + ICEBERG_UNWRAP_OR_FAIL(auto by_time, + ResolveMetadataTableSnapshot(*metadata, selection)); + EXPECT_EQ(by_time, first); + selection.ref_name = "tag"; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata, selection), + IsError(ErrorKind::kInvalidArgument)); + selection.as_of_time_in_millis.reset(); + selection.ref_name = "missing"; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata, selection), + IsError(ErrorKind::kInvalidArgument)); +} + +} // namespace +} // namespace iceberg::internal From ca715a53662789bfb67f13d08b2ce3b88e6e17d0 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Mon, 16 Nov 2026 22:26:40 +0800 Subject: [PATCH 2/7] refactor(inspect): reuse SnapshotUtil for reference lookup Remove the private SnapshotAtRef helper. Validate explicit references and handle an empty main snapshot before delegating to LatestSnapshot, so missing references and dangling snapshot IDs still fail. Cover empty tables, missing/null references, and dangling snapshot IDs. Co-authored-by: Codex --- .../inspect/metadata_table_util_internal.cc | 35 +++++++------------ .../inspect/metadata_snapshot_scan_test.cc | 28 +++++++++++++++ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util_internal.cc index a599fd733e..c7c2944120 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.cc +++ b/src/iceberg/inspect/metadata_table_util_internal.cc @@ -21,7 +21,6 @@ #include #include -#include #include #include "iceberg/constants.h" @@ -34,26 +33,6 @@ namespace iceberg::internal { -namespace { - -Result> SnapshotAtRef(const TableMetadata& metadata, - std::string_view ref_name) { - if (ref_name.empty() || ref_name == SnapshotRef::kMainBranch) { - if (metadata.current_snapshot_id == kInvalidSnapshotId) { - return std::shared_ptr{nullptr}; - } - return metadata.SnapshotById(metadata.current_snapshot_id); - } - - auto ref = metadata.refs.find(std::string(ref_name)); - ICEBERG_PRECHECK(ref != metadata.refs.end(), "Cannot find snapshot reference '{}'", - ref_name); - ICEBERG_PRECHECK(ref->second != nullptr, "Snapshot reference '{}' is null", ref_name); - return metadata.SnapshotById(ref->second->snapshot_id); -} - -} // namespace - namespace { class SnapshotMetadataScan : public MetadataScan { public: @@ -113,7 +92,19 @@ Result> ResolveMetadataTableSnapshot( metadata, TimePointMsFromUnixMs(*selection.as_of_time_in_millis))); return metadata.SnapshotById(snapshot_id); } - return SnapshotAtRef(metadata, selection.ref_name); + const bool is_main_ref = + selection.ref_name.empty() || selection.ref_name == SnapshotRef::kMainBranch; + if (is_main_ref && metadata.current_snapshot_id == kInvalidSnapshotId) { + return std::shared_ptr{nullptr}; + } + if (!is_main_ref) { + auto ref = metadata.refs.find(selection.ref_name); + ICEBERG_PRECHECK(ref != metadata.refs.end(), "Cannot find snapshot reference '{}'", + selection.ref_name); + ICEBERG_PRECHECK(ref->second != nullptr, "Snapshot reference '{}' is null", + selection.ref_name); + } + return SnapshotUtil::LatestSnapshot(metadata, selection.ref_name); } } // namespace iceberg::internal diff --git a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc index eeb0627039..148d64d3d0 100644 --- a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc +++ b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc @@ -151,5 +151,33 @@ TEST_F(SnapshotMetadataSupportTest, ResolvesSnapshotIdsReferencesAndTimes) { IsError(ErrorKind::kInvalidArgument)); } +TEST_F(SnapshotMetadataSupportTest, PreservesSnapshotLookupErrorsAndEmptyTables) { + auto selection = Context(); + for (const auto* ref : {"", "main"}) { + selection.ref_name = ref; + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, + ResolveMetadataTableSnapshot(*metadata_, selection)); + EXPECT_EQ(snapshot, nullptr); + } + + selection.ref_name = "missing"; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata_, selection), + IsError(ErrorKind::kInvalidArgument)); + metadata_->refs["null"] = nullptr; + selection.ref_name = "null"; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata_, selection), + IsError(ErrorKind::kInvalidArgument)); + + metadata_->refs["dangling"] = std::make_shared( + SnapshotRef{.snapshot_id = 999, .retention = SnapshotRef::Tag{}}); + selection.ref_name = "dangling"; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata_, selection), + IsError(ErrorKind::kNotFound)); + selection.ref_name = "main"; + metadata_->current_snapshot_id = 999; + EXPECT_THAT(ResolveMetadataTableSnapshot(*metadata_, selection), + IsError(ErrorKind::kNotFound)); +} + } // namespace } // namespace iceberg::internal From 800230f61bd62362bd836ffd543bb4dff0d8123a Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Tue, 17 Nov 2026 19:32:41 +0800 Subject: [PATCH 3/7] refactor(inspect): share main branch reference check Extract SnapshotUtil::IsMainBranch and reuse it for metadata snapshot selection, schema lookup, and latest snapshot lookup. Co-authored-by: Codex --- src/iceberg/inspect/metadata_table_util_internal.cc | 6 ++---- src/iceberg/util/snapshot_util.cc | 10 +++++++--- src/iceberg/util/snapshot_util.h | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util_internal.cc index c7c2944120..8679483d81 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.cc +++ b/src/iceberg/inspect/metadata_table_util_internal.cc @@ -25,7 +25,6 @@ #include "iceberg/constants.h" #include "iceberg/schema.h" -#include "iceberg/snapshot.h" #include "iceberg/table_metadata.h" #include "iceberg/type.h" #include "iceberg/util/macros.h" @@ -77,7 +76,8 @@ Result> MakeSnapshotMetadataScan( Result> ResolveMetadataTableSnapshot( const TableMetadata& metadata, const MetadataScanContext& selection) { - if (!selection.ref_name.empty() && selection.ref_name != SnapshotRef::kMainBranch) { + const bool is_main_ref = SnapshotUtil::IsMainBranch(selection.ref_name); + if (!is_main_ref) { ICEBERG_PRECHECK( !selection.snapshot_id.has_value() && !selection.as_of_time_in_millis.has_value(), "Cannot combine a snapshot reference with a snapshot ID or timestamp"); @@ -92,8 +92,6 @@ Result> ResolveMetadataTableSnapshot( metadata, TimePointMsFromUnixMs(*selection.as_of_time_in_millis))); return metadata.SnapshotById(snapshot_id); } - const bool is_main_ref = - selection.ref_name.empty() || selection.ref_name == SnapshotRef::kMainBranch; if (is_main_ref && metadata.current_snapshot_id == kInvalidSnapshotId) { return std::shared_ptr{nullptr}; } diff --git a/src/iceberg/util/snapshot_util.cc b/src/iceberg/util/snapshot_util.cc index 885189c2e0..32048b4f48 100644 --- a/src/iceberg/util/snapshot_util.cc +++ b/src/iceberg/util/snapshot_util.cc @@ -31,6 +31,10 @@ namespace iceberg { +bool SnapshotUtil::IsMainBranch(std::string_view ref) { + return ref.empty() || ref == SnapshotRef::kMainBranch; +} + // Shorthand to return for a NotFound error. #define ICEBERG_ACTION_FOR_NOT_FOUND(result, action) \ if (!result.has_value()) [[unlikely]] { \ @@ -372,7 +376,7 @@ Result> SnapshotUtil::SchemaFor(const Table& table, Result> SnapshotUtil::SchemaFor(const Table& table, const std::string& ref) { - if (ref.empty() || ref == SnapshotRef::kMainBranch) { + if (IsMainBranch(ref)) { return table.schema(); } @@ -387,7 +391,7 @@ Result> SnapshotUtil::SchemaFor(const Table& table, Result> SnapshotUtil::SchemaFor(const TableMetadata& metadata, const std::string& ref) { - if (ref.empty() || ref == SnapshotRef::kMainBranch) { + if (IsMainBranch(ref)) { return metadata.Schema(); } @@ -411,7 +415,7 @@ Result> SnapshotUtil::LatestSnapshot( Result> SnapshotUtil::LatestSnapshot( const TableMetadata& metadata, const std::string& branch) { - if (branch.empty() || branch == SnapshotRef::kMainBranch) { + if (IsMainBranch(branch)) { return metadata.Snapshot(); } diff --git a/src/iceberg/util/snapshot_util.h b/src/iceberg/util/snapshot_util.h index 5c832d65fe..f81c7b7b2b 100644 --- a/src/iceberg/util/snapshot_util.h +++ b/src/iceberg/util/snapshot_util.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "iceberg/iceberg_export.h" @@ -39,6 +40,10 @@ namespace iceberg { /// \note All the returned std::shared_ptr are guaranteed to be not null. class ICEBERG_EXPORT SnapshotUtil { public: + /// \brief Return whether a reference selects the main branch. + /// \param ref Reference name (empty string means main branch) + static bool IsMainBranch(std::string_view ref); + /// \brief Returns a vector of ancestors of the given snapshot. /// /// \param table The table From ee6918427eea4057de533d52c7def74cbbc64653 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Tue, 17 Nov 2026 21:59:26 +0800 Subject: [PATCH 4/7] docs(inspect): explain snapshot metadata scan ownership Document the retained snapshot task, deferred row-reader callback, and fresh task streams returned by repeated planning. Co-authored-by: Codex --- src/iceberg/inspect/metadata_table_util_internal.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util_internal.cc index 8679483d81..dd75395e28 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.cc +++ b/src/iceberg/inspect/metadata_table_util_internal.cc @@ -33,6 +33,11 @@ namespace iceberg::internal { namespace { +/// A scan that plans one task for a metadata table's selected snapshot. +/// +/// The task retains the captured metadata, schemas, and FileIO so it can outlive the +/// scan. Table-specific row reading is deferred to the supplied callback when +/// MetadataScanTaskReader opens the task. class SnapshotMetadataScan : public MetadataScan { public: SnapshotMetadataScan(MetadataScanContext context, @@ -41,6 +46,7 @@ class SnapshotMetadataScan : public MetadataScan { task_(std::make_shared(std::move(context), read_rows)) {} Result PlanTasksStream() const override { + // Each planning call gets a fresh stream over the same retained task. return MetadataScanTaskStream::FromVector({task_}); } From d64f4e543e7daa08764d264933bf063d4c9ebbce Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Tue, 17 Nov 2026 22:11:02 +0800 Subject: [PATCH 5/7] refactor(inspect): centralize metadata scan context validation Validate common metadata and schema inputs through MetadataScanContext::Validate at builder and scan factory boundaries. Keep FileIO validation specific to snapshot scans and cover direct factory input validation and static scans without FileIO. Co-authored-by: Codex --- src/iceberg/inspect/history_table.cc | 5 +---- src/iceberg/inspect/metadata_scan.cc | 12 +++++++++--- src/iceberg/inspect/metadata_scan.h | 4 ++++ .../inspect/metadata_table_util_internal.cc | 5 +---- src/iceberg/inspect/refs_table.cc | 5 +---- src/iceberg/inspect/snapshots_table.cc | 5 +---- .../inspect/metadata_snapshot_scan_test.cc | 19 +++++++++++++++++++ .../test/inspect/snapshots_table_test.cc | 13 +++++++++++++ 8 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/iceberg/inspect/history_table.cc b/src/iceberg/inspect/history_table.cc index a6ffebbd86..fb2fd8266d 100644 --- a/src/iceberg/inspect/history_table.cc +++ b/src/iceberg/inspect/history_table.cc @@ -151,10 +151,7 @@ Result> HistoryScan::Make( context.as_of_time_in_millis.has_value()) { return NotSupported("Metadata table does not support time travel"); } - ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); - ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); - ICEBERG_PRECHECK(context.projected_schema != nullptr, - "Projected schema cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context.Validate()); ICEBERG_RETURN_UNEXPECTED(internal::SelectAppendFns( *HistoryTable::CanonicalSchema(), *context.projected_schema, kHistoryColumns)); ICEBERG_ASSIGN_OR_RAISE(auto task, diff --git a/src/iceberg/inspect/metadata_scan.cc b/src/iceberg/inspect/metadata_scan.cc index 03c03f567a..19e7ff41bd 100644 --- a/src/iceberg/inspect/metadata_scan.cc +++ b/src/iceberg/inspect/metadata_scan.cc @@ -107,6 +107,13 @@ class TaskArrowStreamSource { } // namespace +Status internal::MetadataScanContext::Validate() const { + ICEBERG_PRECHECK(table_metadata != nullptr, "Table metadata cannot be null"); + ICEBERG_PRECHECK(schema != nullptr, "Metadata table schema cannot be null"); + ICEBERG_PRECHECK(projected_schema != nullptr, "Projected schema cannot be null"); + return {}; +} + MetadataScanBuilder::MetadataScanBuilder(const MetadataTable& table, internal::MetadataScanContext context) : table_(&table), context_(std::move(context)) {} @@ -119,12 +126,12 @@ Result> MetadataScanBuilder::Make( auto metadata = table.source_table()->metadata(); ICEBERG_PRECHECK(metadata != nullptr, "Table metadata cannot be null"); ICEBERG_ASSIGN_OR_RAISE(auto schema, table.SchemaForScan(*metadata)); - ICEBERG_PRECHECK(schema != nullptr, "Metadata table schema cannot be null"); internal::MetadataScanContext context{.kind = table.kind(), .table_metadata = std::move(metadata), .schema = schema, .projected_schema = std::move(schema), .io = table.source_table()->io()}; + ICEBERG_RETURN_UNEXPECTED(context.Validate()); return std::unique_ptr( new MetadataScanBuilder(table, std::move(context))); } @@ -176,8 +183,7 @@ MetadataScanBuilder& MetadataScanBuilder::AsOfTime(int64_t as_of_time_in_millis) Result> MetadataScanBuilder::Build() { ICEBERG_RETURN_UNEXPECTED(CheckErrors()); - ICEBERG_PRECHECK(context_.projected_schema != nullptr, - "Projected schema cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context_.Validate()); return table_->MakeScan(context_); } diff --git a/src/iceberg/inspect/metadata_scan.h b/src/iceberg/inspect/metadata_scan.h index 9289e65159..28f3eb742c 100644 --- a/src/iceberg/inspect/metadata_scan.h +++ b/src/iceberg/inspect/metadata_scan.h @@ -53,6 +53,10 @@ struct MetadataScanContext { std::string ref_name; std::optional as_of_time_in_millis; std::shared_ptr io; + + /// \brief Validate the metadata and schemas required by all scans. + /// FileIO is checked separately by scan factories that require it. + ICEBERG_EXPORT Status Validate() const; }; } // namespace internal diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util_internal.cc index dd75395e28..3e314bc30a 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.cc +++ b/src/iceberg/inspect/metadata_table_util_internal.cc @@ -58,10 +58,7 @@ class SnapshotMetadataScan : public MetadataScan { Result> MakeSnapshotMetadataScan( MetadataScanContext context, SnapshotMetadataTask::ReadRowsFunction read_rows) { ICEBERG_PRECHECK(read_rows != nullptr, "Snapshot row reader cannot be null"); - ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); - ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); - ICEBERG_PRECHECK(context.projected_schema != nullptr, - "Projected schema cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context.Validate()); ICEBERG_PRECHECK(context.io != nullptr, "FileIO cannot be null"); for (const auto& projected : context.projected_schema->fields()) { ICEBERG_ASSIGN_OR_RAISE(auto field, diff --git a/src/iceberg/inspect/refs_table.cc b/src/iceberg/inspect/refs_table.cc index 25d064da3d..ff12b79c01 100644 --- a/src/iceberg/inspect/refs_table.cc +++ b/src/iceberg/inspect/refs_table.cc @@ -160,10 +160,7 @@ Result> RefsScan::Make(internal::MetadataScanContext c context.as_of_time_in_millis.has_value()) { return NotSupported("Metadata table does not support time travel"); } - ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); - ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); - ICEBERG_PRECHECK(context.projected_schema != nullptr, - "Projected schema cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context.Validate()); ICEBERG_RETURN_UNEXPECTED(internal::SelectAppendFns( *RefsTable::CanonicalSchema(), *context.projected_schema, kRefColumns)); ICEBERG_ASSIGN_OR_RAISE(auto task, diff --git a/src/iceberg/inspect/snapshots_table.cc b/src/iceberg/inspect/snapshots_table.cc index c54a71778b..5bbdb19a5e 100644 --- a/src/iceberg/inspect/snapshots_table.cc +++ b/src/iceberg/inspect/snapshots_table.cc @@ -142,10 +142,7 @@ Result> SnapshotsScan::Make( context.as_of_time_in_millis.has_value()) { return NotSupported("Metadata table does not support time travel"); } - ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); - ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); - ICEBERG_PRECHECK(context.projected_schema != nullptr, - "Projected schema cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context.Validate()); ICEBERG_RETURN_UNEXPECTED(internal::SelectAppendFns( *SnapshotsTable::CanonicalSchema(), *context.projected_schema, kSnapshotColumns)); ICEBERG_ASSIGN_OR_RAISE(auto task, diff --git a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc index 148d64d3d0..3b5b97c398 100644 --- a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc +++ b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc @@ -98,6 +98,25 @@ TEST_F(SnapshotMetadataSupportTest, TasksRetainOwnersAndCanBeReopened) { } } +TEST_F(SnapshotMetadataSupportTest, RejectsMissingInputs) { + auto context = Context(); + context.table_metadata.reset(); + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); + context = Context(); + context.schema.reset(); + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); + context = Context(); + context.projected_schema.reset(); + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); + context = Context(); + context.io.reset(); + EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + IsError(ErrorKind::kInvalidArgument)); +} + TEST_F(SnapshotMetadataSupportTest, RejectsInvalidProjectionAndReader) { EXPECT_THAT(MakeSnapshotMetadataScan(Context(), nullptr), IsError(ErrorKind::kInvalidArgument)); diff --git a/src/iceberg/test/inspect/snapshots_table_test.cc b/src/iceberg/test/inspect/snapshots_table_test.cc index b5029dc441..54320a3b6b 100644 --- a/src/iceberg/test/inspect/snapshots_table_test.cc +++ b/src/iceberg/test/inspect/snapshots_table_test.cc @@ -241,6 +241,19 @@ TEST_F(SnapshotsTableTest, ScanProjectedColumns) { EXPECT_EQ(snapshot_ids->Value(1), 2); } +TEST_F(SnapshotsTableTest, DirectScanDoesNotRequireFileIO) { + internal::MetadataScanContext context{ + .kind = MetadataTableKind::kSnapshots, + .table_metadata = table_->metadata(), + .schema = SnapshotsTable::CanonicalSchema(), + .projected_schema = SnapshotsTable::CanonicalSchema()}; + ICEBERG_UNWRAP_OR_FAIL(auto scan, SnapshotsScan::Make(std::move(context))); + ICEBERG_UNWRAP_OR_FAIL(auto stream, scan->ToArrowArrayStream()); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 1); + EXPECT_EQ(batches.front()->num_rows(), 2); +} + TEST_F(SnapshotsTableTest, ScanRejectsInvalidProjection) { // An optional column cannot be projected as required. auto required_parent = std::make_shared( From 0562ede7fb0079cd396ec471440d12f3098a5b61 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 20 Nov 2026 11:21:01 +0800 Subject: [PATCH 6/7] refactor(inspect): address snapshot scan review feedback Keep FileIO on the metadata scan builder and retained snapshot tasks instead of scan configuration. Colocate snapshot and static task implementations, label the callback parameters, rename the metadata utility source, and document future task splitting and serialization. Verify captured FileIO survives source refresh and the builder, scan, task, and active stream lifetimes. Co-authored-by: Codex --- src/iceberg/CMakeLists.txt | 2 +- src/iceberg/inspect/history_table.cc | 2 +- src/iceberg/inspect/history_table.h | 2 +- src/iceberg/inspect/metadata_scan.cc | 13 ++-- src/iceberg/inspect/metadata_scan.h | 6 +- src/iceberg/inspect/metadata_scan_task.cc | 21 ++++++ src/iceberg/inspect/metadata_scan_task.h | 32 +++++++++ .../inspect/metadata_scan_task_reader.cc | 3 +- src/iceberg/inspect/metadata_table.h | 3 +- ...til_internal.cc => metadata_table_util.cc} | 18 +++-- .../inspect/metadata_table_util_internal.h | 28 +------- src/iceberg/inspect/refs_table.cc | 2 +- src/iceberg/inspect/refs_table.h | 2 +- src/iceberg/inspect/snapshots_table.cc | 2 +- src/iceberg/inspect/snapshots_table.h | 2 +- .../inspect/metadata_snapshot_scan_test.cc | 72 ++++++++++++++++--- 16 files changed, 144 insertions(+), 66 deletions(-) rename src/iceberg/inspect/{metadata_table_util_internal.cc => metadata_table_util.cc} (89%) diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 6bab2279ec..0768f1aafb 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -63,7 +63,7 @@ set(ICEBERG_SOURCES inspect/metadata_table.cc inspect/metadata_scan_task.cc inspect/metadata_scan_task_reader.cc - inspect/metadata_table_util_internal.cc + inspect/metadata_table_util.cc inspect/refs_table.cc inspect/snapshots_table.cc inheritable_metadata.cc diff --git a/src/iceberg/inspect/history_table.cc b/src/iceberg/inspect/history_table.cc index fb2fd8266d..3573b3b936 100644 --- a/src/iceberg/inspect/history_table.cc +++ b/src/iceberg/inspect/history_table.cc @@ -95,7 +95,7 @@ Result> HistoryTable::Make( } Result> HistoryTable::MakeScan( - internal::MetadataScanContext context) const { + internal::MetadataScanContext context, std::shared_ptr /*io*/) const { return HistoryScan::Make(std::move(context)); } diff --git a/src/iceberg/inspect/history_table.h b/src/iceberg/inspect/history_table.h index f1b949bf9e..f261bbddb8 100644 --- a/src/iceberg/inspect/history_table.h +++ b/src/iceberg/inspect/history_table.h @@ -50,7 +50,7 @@ class ICEBERG_EXPORT HistoryTable : public MetadataTable { protected: Result> MakeScan( - internal::MetadataScanContext context) const override; + internal::MetadataScanContext context, std::shared_ptr io) const override; private: friend class MetadataScanTaskReader; diff --git a/src/iceberg/inspect/metadata_scan.cc b/src/iceberg/inspect/metadata_scan.cc index 19e7ff41bd..d6cc6236f8 100644 --- a/src/iceberg/inspect/metadata_scan.cc +++ b/src/iceberg/inspect/metadata_scan.cc @@ -115,8 +115,9 @@ Status internal::MetadataScanContext::Validate() const { } MetadataScanBuilder::MetadataScanBuilder(const MetadataTable& table, - internal::MetadataScanContext context) - : table_(&table), context_(std::move(context)) {} + internal::MetadataScanContext context, + std::shared_ptr io) + : table_(&table), context_(std::move(context)), io_(std::move(io)) {} MetadataScanBuilder::~MetadataScanBuilder() = default; @@ -125,15 +126,15 @@ Result> MetadataScanBuilder::Make( ICEBERG_PRECHECK(table.source_table() != nullptr, "Source table cannot be null"); auto metadata = table.source_table()->metadata(); ICEBERG_PRECHECK(metadata != nullptr, "Table metadata cannot be null"); + auto io = table.source_table()->io(); ICEBERG_ASSIGN_OR_RAISE(auto schema, table.SchemaForScan(*metadata)); internal::MetadataScanContext context{.kind = table.kind(), .table_metadata = std::move(metadata), .schema = schema, - .projected_schema = std::move(schema), - .io = table.source_table()->io()}; + .projected_schema = std::move(schema)}; ICEBERG_RETURN_UNEXPECTED(context.Validate()); return std::unique_ptr( - new MetadataScanBuilder(table, std::move(context))); + new MetadataScanBuilder(table, std::move(context), std::move(io))); } MetadataScanBuilder& MetadataScanBuilder::Select( @@ -184,7 +185,7 @@ MetadataScanBuilder& MetadataScanBuilder::AsOfTime(int64_t as_of_time_in_millis) Result> MetadataScanBuilder::Build() { ICEBERG_RETURN_UNEXPECTED(CheckErrors()); ICEBERG_RETURN_UNEXPECTED(context_.Validate()); - return table_->MakeScan(context_); + return table_->MakeScan(context_, io_); } MetadataScan::MetadataScan(internal::MetadataScanContext context) diff --git a/src/iceberg/inspect/metadata_scan.h b/src/iceberg/inspect/metadata_scan.h index 28f3eb742c..8a53b47814 100644 --- a/src/iceberg/inspect/metadata_scan.h +++ b/src/iceberg/inspect/metadata_scan.h @@ -52,10 +52,8 @@ struct MetadataScanContext { std::optional snapshot_id; std::string ref_name; std::optional as_of_time_in_millis; - std::shared_ptr io; /// \brief Validate the metadata and schemas required by all scans. - /// FileIO is checked separately by scan factories that require it. ICEBERG_EXPORT Status Validate() const; }; @@ -102,10 +100,12 @@ class ICEBERG_EXPORT MetadataScanBuilder : public ErrorCollector { Result> Build(); private: - MetadataScanBuilder(const MetadataTable& table, internal::MetadataScanContext context); + MetadataScanBuilder(const MetadataTable& table, internal::MetadataScanContext context, + std::shared_ptr io); const MetadataTable* table_; internal::MetadataScanContext context_; + const std::shared_ptr io_; }; /// \brief A metadata scan that plans tasks or reads all of them as Arrow batches. diff --git a/src/iceberg/inspect/metadata_scan_task.cc b/src/iceberg/inspect/metadata_scan_task.cc index 07195e16fa..6d6b1c9d89 100644 --- a/src/iceberg/inspect/metadata_scan_task.cc +++ b/src/iceberg/inspect/metadata_scan_task.cc @@ -49,4 +49,25 @@ StaticMetadataTask::StaticMetadataTask(MetadataTableKind table_kind, metadata_(std::move(metadata)), projected_schema_(std::move(projected_schema)) {} +// SnapshotMetadataTask + +Result> SnapshotMetadataTask::Make( + internal::MetadataScanContext context, std::shared_ptr io, + ReadRowsFunction read_rows) { + ICEBERG_PRECHECK(read_rows != nullptr, "Snapshot row reader cannot be null"); + ICEBERG_RETURN_UNEXPECTED(context.Validate()); + ICEBERG_PRECHECK(io != nullptr, "FileIO cannot be null"); + return std::shared_ptr( + new SnapshotMetadataTask(std::move(context), std::move(io), read_rows)); +} + +SnapshotMetadataTask::SnapshotMetadataTask(internal::MetadataScanContext context, + std::shared_ptr io, + ReadRowsFunction read_rows) + : context_(std::move(context)), io_(std::move(io)), read_rows_(read_rows) {} + +Result SnapshotMetadataTask::Open(int64_t batch_size) const { + return read_rows_(context_, io_, batch_size); +} + } // namespace iceberg diff --git a/src/iceberg/inspect/metadata_scan_task.h b/src/iceberg/inspect/metadata_scan_task.h index fc746efcd2..eab2d26931 100644 --- a/src/iceberg/inspect/metadata_scan_task.h +++ b/src/iceberg/inspect/metadata_scan_task.h @@ -19,9 +19,12 @@ #pragma once +#include #include +#include "iceberg/arrow_c_data.h" #include "iceberg/iceberg_export.h" +#include "iceberg/inspect/metadata_scan.h" #include "iceberg/result.h" #include "iceberg/type_fwd.h" @@ -87,4 +90,33 @@ class ICEBERG_EXPORT StaticMetadataTask : public MetadataScanTask { const std::shared_ptr projected_schema_; }; +/// \brief Task retaining a snapshot scan's captured settings and FileIO. +/// +/// Rows are read through an in-process callback. Splitting work across multiple +/// tasks and serializable task descriptions are future work for parallel execution. +class ICEBERG_EXPORT SnapshotMetadataTask : public MetadataScanTask { + public: + using ReadRowsFunction = Result (*)( + const internal::MetadataScanContext& /*context*/, + const std::shared_ptr& /*io*/, int64_t /*batch_size*/); + + /// \brief Create a task owning the scan settings, FileIO, and row reader. + static Result> Make( + internal::MetadataScanContext context, std::shared_ptr io, + ReadRowsFunction read_rows); + + Result Open(int64_t batch_size) const; + + TaskKind task_kind() const override { return TaskKind::kSnapshot; } + MetadataTableKind table_kind() const override { return context_.kind; } + + private: + SnapshotMetadataTask(internal::MetadataScanContext context, std::shared_ptr io, + ReadRowsFunction read_rows); + + const internal::MetadataScanContext context_; + const std::shared_ptr io_; + const ReadRowsFunction read_rows_; +}; + } // namespace iceberg diff --git a/src/iceberg/inspect/metadata_scan_task_reader.cc b/src/iceberg/inspect/metadata_scan_task_reader.cc index abe69c4989..a43b4f26c9 100644 --- a/src/iceberg/inspect/metadata_scan_task_reader.cc +++ b/src/iceberg/inspect/metadata_scan_task_reader.cc @@ -25,7 +25,6 @@ #include "iceberg/inspect/history_table.h" #include "iceberg/inspect/metadata_scan_task.h" #include "iceberg/inspect/metadata_table.h" -#include "iceberg/inspect/metadata_table_util_internal.h" #include "iceberg/inspect/refs_table.h" #include "iceberg/inspect/snapshots_table.h" #include "iceberg/util/checked_cast.h" @@ -50,7 +49,7 @@ Result MetadataScanTaskReader::Open( switch (task.task_kind()) { case MetadataScanTask::TaskKind::kSnapshot: { const auto& snapshot_task = - internal::checked_cast(task); + internal::checked_cast(task); return snapshot_task.Open(options_.batch_size); } case MetadataScanTask::TaskKind::kStatic: { diff --git a/src/iceberg/inspect/metadata_table.h b/src/iceberg/inspect/metadata_table.h index 99e98ef1ee..2e610527a5 100644 --- a/src/iceberg/inspect/metadata_table.h +++ b/src/iceberg/inspect/metadata_table.h @@ -98,8 +98,9 @@ class ICEBERG_EXPORT MetadataTable { const TableMetadata& metadata) const; /// \brief Plan a scan using the captured metadata, projection, and snapshot selection. + /// \param io FileIO captured when the scan builder was created virtual Result> MakeScan( - internal::MetadataScanContext context) const = 0; + internal::MetadataScanContext context, std::shared_ptr io) const = 0; private: friend class MetadataScanBuilder; diff --git a/src/iceberg/inspect/metadata_table_util_internal.cc b/src/iceberg/inspect/metadata_table_util.cc similarity index 89% rename from src/iceberg/inspect/metadata_table_util_internal.cc rename to src/iceberg/inspect/metadata_table_util.cc index 3e314bc30a..16a063f41a 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.cc +++ b/src/iceberg/inspect/metadata_table_util.cc @@ -17,13 +17,12 @@ * under the License. */ -#include "iceberg/inspect/metadata_table_util_internal.h" - #include #include #include #include "iceberg/constants.h" +#include "iceberg/inspect/metadata_table_util_internal.h" #include "iceberg/schema.h" #include "iceberg/table_metadata.h" #include "iceberg/type.h" @@ -41,9 +40,8 @@ namespace { class SnapshotMetadataScan : public MetadataScan { public: SnapshotMetadataScan(MetadataScanContext context, - SnapshotMetadataTask::ReadRowsFunction read_rows) - : MetadataScan(context), - task_(std::make_shared(std::move(context), read_rows)) {} + std::shared_ptr task) + : MetadataScan(std::move(context)), task_(std::move(task)) {} Result PlanTasksStream() const override { // Each planning call gets a fresh stream over the same retained task. @@ -56,10 +54,10 @@ class SnapshotMetadataScan : public MetadataScan { } // namespace Result> MakeSnapshotMetadataScan( - MetadataScanContext context, SnapshotMetadataTask::ReadRowsFunction read_rows) { - ICEBERG_PRECHECK(read_rows != nullptr, "Snapshot row reader cannot be null"); - ICEBERG_RETURN_UNEXPECTED(context.Validate()); - ICEBERG_PRECHECK(context.io != nullptr, "FileIO cannot be null"); + MetadataScanContext context, std::shared_ptr io, + SnapshotMetadataTask::ReadRowsFunction read_rows) { + ICEBERG_ASSIGN_OR_RAISE(auto task, + SnapshotMetadataTask::Make(context, std::move(io), read_rows)); for (const auto& projected : context.projected_schema->fields()) { ICEBERG_ASSIGN_OR_RAISE(auto field, context.schema->GetFieldById(projected.field_id())); @@ -74,7 +72,7 @@ Result> MakeSnapshotMetadataScan( } ICEBERG_RETURN_UNEXPECTED( ResolveMetadataTableSnapshot(*context.table_metadata, context)); - return std::make_unique(std::move(context), read_rows); + return std::make_unique(std::move(context), std::move(task)); } Result> ResolveMetadataTableSnapshot( diff --git a/src/iceberg/inspect/metadata_table_util_internal.h b/src/iceberg/inspect/metadata_table_util_internal.h index 3fbc6816b6..81b03c1c4d 100644 --- a/src/iceberg/inspect/metadata_table_util_internal.h +++ b/src/iceberg/inspect/metadata_table_util_internal.h @@ -19,9 +19,7 @@ #pragma once -#include #include -#include #include "iceberg/iceberg_export.h" #include "iceberg/inspect/metadata_scan.h" @@ -30,31 +28,9 @@ namespace iceberg::internal { -/// A task retaining the captured metadata, projection, and manifest IO. -class SnapshotMetadataTask : public MetadataScanTask { - public: - using ReadRowsFunction = Result (*)(const MetadataScanContext&, - const std::shared_ptr&, - int64_t); - - SnapshotMetadataTask(MetadataScanContext context, ReadRowsFunction read_rows) - : context(std::move(context)), read_rows_(read_rows) {} - - Result Open(int64_t batch_size) const { - return read_rows_(context, context.io, batch_size); - } - - TaskKind task_kind() const override { return TaskKind::kSnapshot; } - MetadataTableKind table_kind() const override { return context.kind; } - - const MetadataScanContext context; - - private: - ReadRowsFunction read_rows_; -}; - ICEBERG_EXPORT Result> MakeSnapshotMetadataScan( - MetadataScanContext context, SnapshotMetadataTask::ReadRowsFunction read_rows); + MetadataScanContext context, std::shared_ptr io, + SnapshotMetadataTask::ReadRowsFunction read_rows); /// \brief Resolve a time-travel selection to a snapshot. ICEBERG_EXPORT Result> ResolveMetadataTableSnapshot( diff --git a/src/iceberg/inspect/refs_table.cc b/src/iceberg/inspect/refs_table.cc index ff12b79c01..2f20dc8155 100644 --- a/src/iceberg/inspect/refs_table.cc +++ b/src/iceberg/inspect/refs_table.cc @@ -111,7 +111,7 @@ Result> RefsTable::Make(std::shared_ptr } Result> RefsTable::MakeScan( - internal::MetadataScanContext context) const { + internal::MetadataScanContext context, std::shared_ptr /*io*/) const { return RefsScan::Make(std::move(context)); } diff --git a/src/iceberg/inspect/refs_table.h b/src/iceberg/inspect/refs_table.h index 2628ffe6b3..da7179d25c 100644 --- a/src/iceberg/inspect/refs_table.h +++ b/src/iceberg/inspect/refs_table.h @@ -50,7 +50,7 @@ class ICEBERG_EXPORT RefsTable : public MetadataTable { protected: Result> MakeScan( - internal::MetadataScanContext context) const override; + internal::MetadataScanContext context, std::shared_ptr io) const override; private: friend class MetadataScanTaskReader; diff --git a/src/iceberg/inspect/snapshots_table.cc b/src/iceberg/inspect/snapshots_table.cc index 5bbdb19a5e..91ded292b7 100644 --- a/src/iceberg/inspect/snapshots_table.cc +++ b/src/iceberg/inspect/snapshots_table.cc @@ -105,7 +105,7 @@ Result> SnapshotsTable::Make( } Result> SnapshotsTable::MakeScan( - internal::MetadataScanContext context) const { + internal::MetadataScanContext context, std::shared_ptr /*io*/) const { return SnapshotsScan::Make(std::move(context)); } diff --git a/src/iceberg/inspect/snapshots_table.h b/src/iceberg/inspect/snapshots_table.h index 2293ad929d..61fd20fe4a 100644 --- a/src/iceberg/inspect/snapshots_table.h +++ b/src/iceberg/inspect/snapshots_table.h @@ -52,7 +52,7 @@ class ICEBERG_EXPORT SnapshotsTable : public MetadataTable { protected: Result> MakeScan( - internal::MetadataScanContext context) const override; + internal::MetadataScanContext context, std::shared_ptr io) const override; private: friend class MetadataScanTaskReader; diff --git a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc index 3b5b97c398..e70e8f2d17 100644 --- a/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc +++ b/src/iceberg/test/inspect/metadata_snapshot_scan_test.cc @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -64,6 +65,21 @@ Result ReadIntTaskRows(const MetadataScanContext& context, }); } +class TestSnapshotMetadataTable : public MetadataTable { + public: + explicit TestSnapshotMetadataTable(std::shared_ptr table) + : MetadataTable(std::move(table)) {} + + MetadataTableKind kind() const noexcept override { return MetadataTableKind::kFiles; } + std::shared_ptr schema() const override { return IntSchema(); } + + protected: + Result> MakeScan( + MetadataScanContext context, std::shared_ptr io) const override { + return MakeSnapshotMetadataScan(std::move(context), std::move(io), ReadIntTaskRows); + } +}; + class SnapshotMetadataSupportTest : public MetadataTableTestBase { protected: MetadataScanContext Context() { @@ -71,13 +87,13 @@ class SnapshotMetadataSupportTest : public MetadataTableTestBase { return {.kind = MetadataTableKind::kFiles, .table_metadata = metadata_, .schema = schema, - .projected_schema = schema, - .io = io_}; + .projected_schema = schema}; } }; TEST_F(SnapshotMetadataSupportTest, TasksRetainOwnersAndCanBeReopened) { - ICEBERG_UNWRAP_OR_FAIL(auto scan, MakeSnapshotMetadataScan(Context(), ReadIntTaskRows)); + ICEBERG_UNWRAP_OR_FAIL(auto scan, + MakeSnapshotMetadataScan(Context(), io_, ReadIntTaskRows)); ICEBERG_UNWRAP_OR_FAIL(auto tasks, scan->PlanTasks()); ASSERT_EQ(tasks.size(), 1); scan.reset(); @@ -98,37 +114,71 @@ TEST_F(SnapshotMetadataSupportTest, TasksRetainOwnersAndCanBeReopened) { } } +TEST_F(SnapshotMetadataSupportTest, BuilderCapturesFileIOAcrossRefresh) { + TestSnapshotMetadataTable metadata_table(table_); + ICEBERG_UNWRAP_OR_FAIL(auto builder, metadata_table.NewScan()); + std::weak_ptr captured_io = io_; + + ICEBERG_UNWRAP_OR_FAIL( + auto refreshed, + Table::Make(table_->name(), std::make_shared(*metadata_), + "s3://bucket/refreshed.json", std::make_shared(), + catalog_)); + EXPECT_CALL(*catalog_, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table_->Refresh(), IsOk()); + EXPECT_NE(table_->io().get(), io_.get()); + io_.reset(); + EXPECT_FALSE(captured_io.expired()); + + ICEBERG_UNWRAP_OR_FAIL(auto scan, builder->Build()); + ICEBERG_UNWRAP_OR_FAIL(auto tasks, scan->PlanTasks()); + ASSERT_EQ(tasks.size(), 1); + builder.reset(); + scan.reset(); + EXPECT_FALSE(captured_io.expired()); + + ICEBERG_UNWRAP_OR_FAIL(auto reader, MetadataScanTaskReader::Make({.batch_size = 2})); + ICEBERG_UNWRAP_OR_FAIL(auto stream, reader->Open(*tasks.front())); + tasks.clear(); + reader.reset(); + EXPECT_FALSE(captured_io.expired()); + ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); + ASSERT_EQ(batches.size(), 2); + EXPECT_EQ(batches[0]->num_rows(), 2); + EXPECT_EQ(batches[1]->num_rows(), 1); + EXPECT_TRUE(captured_io.expired()); +} + TEST_F(SnapshotMetadataSupportTest, RejectsMissingInputs) { auto context = Context(); context.table_metadata.reset(); - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, io_, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); context = Context(); context.schema.reset(); - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, io_, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); context = Context(); context.projected_schema.reset(); - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, io_, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); context = Context(); - context.io.reset(); - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, nullptr, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); } TEST_F(SnapshotMetadataSupportTest, RejectsInvalidProjectionAndReader) { - EXPECT_THAT(MakeSnapshotMetadataScan(Context(), nullptr), + EXPECT_THAT(MakeSnapshotMetadataScan(Context(), io_, nullptr), IsError(ErrorKind::kInvalidArgument)); auto context = Context(); context.projected_schema = std::make_shared( std::vector{SchemaField::MakeRequired(99, "missing", int64())}); - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, io_, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); context = Context(); context.ref_name = "dev"; context.snapshot_id = 1; - EXPECT_THAT(MakeSnapshotMetadataScan(context, ReadIntTaskRows), + EXPECT_THAT(MakeSnapshotMetadataScan(context, io_, ReadIntTaskRows), IsError(ErrorKind::kInvalidArgument)); } From e5c816e6c3b40b03fa77b4bfa712e165fc50fce2 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 20 Nov 2026 11:49:15 +0800 Subject: [PATCH 7/7] test(inspect): avoid private scan classes in metadata tests Exercise static metadata reads through StaticMetadataTask and MetadataScanTaskReader. The concrete SnapshotsScan was made private in #1139, so calling its factory prevents PR merge builds from compiling. Preserve the static-read FileIO regression through the public task API. Co-authored-by: Codex --- src/iceberg/test/inspect/snapshots_table_test.cc | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/iceberg/test/inspect/snapshots_table_test.cc b/src/iceberg/test/inspect/snapshots_table_test.cc index 54320a3b6b..773ab167f1 100644 --- a/src/iceberg/test/inspect/snapshots_table_test.cc +++ b/src/iceberg/test/inspect/snapshots_table_test.cc @@ -32,6 +32,7 @@ #include #include "iceberg/constants.h" +#include "iceberg/inspect/metadata_scan_task.h" #include "iceberg/inspect/metadata_scan_task_reader.h" #include "iceberg/inspect/metadata_table.h" #include "iceberg/test/arrow_test_util.h" @@ -241,14 +242,12 @@ TEST_F(SnapshotsTableTest, ScanProjectedColumns) { EXPECT_EQ(snapshot_ids->Value(1), 2); } -TEST_F(SnapshotsTableTest, DirectScanDoesNotRequireFileIO) { - internal::MetadataScanContext context{ - .kind = MetadataTableKind::kSnapshots, - .table_metadata = table_->metadata(), - .schema = SnapshotsTable::CanonicalSchema(), - .projected_schema = SnapshotsTable::CanonicalSchema()}; - ICEBERG_UNWRAP_OR_FAIL(auto scan, SnapshotsScan::Make(std::move(context))); - ICEBERG_UNWRAP_OR_FAIL(auto stream, scan->ToArrowArrayStream()); +TEST_F(SnapshotsTableTest, StaticTaskDoesNotRequireFileIO) { + ICEBERG_UNWRAP_OR_FAIL(auto task, StaticMetadataTask::Make( + MetadataTableKind::kSnapshots, table_->metadata(), + SnapshotsTable::CanonicalSchema())); + ICEBERG_UNWRAP_OR_FAIL(auto reader, MetadataScanTaskReader::Make({})); + ICEBERG_UNWRAP_OR_FAIL(auto stream, reader->Open(*task)); ICEBERG_UNWRAP_OR_FAIL(auto batches, ReadAllBatches(std::move(stream))); ASSERT_EQ(batches.size(), 1); EXPECT_EQ(batches.front()->num_rows(), 2);