You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by ga...@apache.org on 2026/06/15 04:55:35 UTC
(iceberg-cpp) branch main updated: feat(inspect): add base metadata table interface (#607)
This is an automated email from the ASF dual-hosted git repository.
wgtmac pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 7324142d feat(inspect): add base metadata table interface (#607)
7324142d is described below
commit 7324142d373d836ab068d94eb52fae7ff948dda1
Author: Huangshi Tian <Al...@users.noreply.github.com>
AuthorDate: Mon Jun 15 00:55:29 2026 -0400
feat(inspect): add base metadata table interface (#607)
This PR kicks off the implementation of table inspection support.
- Add `MetadataTable` class (without scan support for now).
- Add `SnapshotsTable` and `HistoryTable` as example impl.
---
src/iceberg/CMakeLists.txt | 4 ++
src/iceberg/inspect/CMakeLists.txt | 18 +++++
src/iceberg/inspect/history_table.cc | 62 +++++++++++++++++
src/iceberg/inspect/history_table.h | 44 ++++++++++++
src/iceberg/inspect/meson.build | 21 ++++++
src/iceberg/inspect/metadata_table.cc | 54 ++++++++++++++
src/iceberg/inspect/metadata_table.h | 62 +++++++++++++++++
src/iceberg/inspect/snapshots_table.cc | 68 ++++++++++++++++++
src/iceberg/inspect/snapshots_table.h | 44 ++++++++++++
src/iceberg/meson.build | 4 ++
src/iceberg/table.cc | 33 +++++++++
src/iceberg/table.h | 19 ++++-
src/iceberg/test/CMakeLists.txt | 1 +
src/iceberg/test/meson.build | 1 +
src/iceberg/test/metadata_table_test.cc | 120 ++++++++++++++++++++++++++++++++
src/iceberg/test/table_test.cc | 16 +++++
src/iceberg/type_fwd.h | 5 ++
17 files changed, 575 insertions(+), 1 deletion(-)
diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt
index 0fe418d4..ffa3f2f0 100644
--- a/src/iceberg/CMakeLists.txt
+++ b/src/iceberg/CMakeLists.txt
@@ -41,6 +41,9 @@ set(ICEBERG_SOURCES
file_io_registry.cc
file_reader.cc
file_writer.cc
+ inspect/history_table.cc
+ inspect/metadata_table.cc
+ inspect/snapshots_table.cc
inheritable_metadata.cc
json_serde.cc
location_provider.cc
@@ -238,6 +241,7 @@ add_subdirectory(puffin)
add_subdirectory(row)
add_subdirectory(update)
add_subdirectory(util)
+add_subdirectory(inspect)
add_subdirectory(metrics)
if(ICEBERG_BUILD_BUNDLE)
diff --git a/src/iceberg/inspect/CMakeLists.txt b/src/iceberg/inspect/CMakeLists.txt
new file mode 100644
index 00000000..2df84435
--- /dev/null
+++ b/src/iceberg/inspect/CMakeLists.txt
@@ -0,0 +1,18 @@
+# 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.
+
+iceberg_install_all_headers(iceberg/inspect)
diff --git a/src/iceberg/inspect/history_table.cc b/src/iceberg/inspect/history_table.cc
new file mode 100644
index 00000000..7fa84004
--- /dev/null
+++ b/src/iceberg/inspect/history_table.cc
@@ -0,0 +1,62 @@
+/*
+ * 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/history_table.h"
+
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/table.h"
+#include "iceberg/table_identifier.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+namespace {
+
+std::shared_ptr<Schema> MakeHistoryTableSchema() {
+ return std::make_shared<Schema>(std::vector<SchemaField>{
+ SchemaField::MakeRequired(1, "made_current_at", timestamp_tz()),
+ SchemaField::MakeRequired(2, "snapshot_id", int64()),
+ SchemaField::MakeOptional(3, "parent_id", int64()),
+ SchemaField::MakeRequired(4, "is_current_ancestor", boolean())});
+}
+
+TableIdentifier MakeHistoryTableName(const TableIdentifier& source_name) {
+ return TableIdentifier{.ns = source_name.ns, .name = source_name.name + ".history"};
+}
+
+} // namespace
+
+HistoryTable::HistoryTable(std::shared_ptr<Table> table)
+ : MetadataTable(table, MakeHistoryTableName(table->name()),
+ MakeHistoryTableSchema()) {}
+
+HistoryTable::~HistoryTable() = default;
+
+Result<std::unique_ptr<HistoryTable>> HistoryTable::Make(std::shared_ptr<Table> table) {
+ if (table == nullptr) [[unlikely]] {
+ return InvalidArgument("Table cannot be null");
+ }
+ return std::unique_ptr<HistoryTable>(new HistoryTable(std::move(table)));
+}
+
+} // namespace iceberg
diff --git a/src/iceberg/inspect/history_table.h b/src/iceberg/inspect/history_table.h
new file mode 100644
index 00000000..94a26368
--- /dev/null
+++ b/src/iceberg/inspect/history_table.h
@@ -0,0 +1,44 @@
+/*
+ * 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 <memory>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/inspect/metadata_table.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief History metadata table.
+class ICEBERG_EXPORT HistoryTable : public MetadataTable {
+ public:
+ static Result<std::unique_ptr<HistoryTable>> Make(std::shared_ptr<Table> table);
+
+ ~HistoryTable() override;
+
+ Kind kind() const noexcept override { return Kind::kHistory; }
+
+ private:
+ explicit HistoryTable(std::shared_ptr<Table> table);
+};
+
+} // namespace iceberg
diff --git a/src/iceberg/inspect/meson.build b/src/iceberg/inspect/meson.build
new file mode 100644
index 00000000..5c738008
--- /dev/null
+++ b/src/iceberg/inspect/meson.build
@@ -0,0 +1,21 @@
+# 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.
+
+install_headers(
+ ['history_table.h', 'metadata_table.h', 'snapshots_table.h'],
+ subdir: 'iceberg/inspect',
+)
diff --git a/src/iceberg/inspect/metadata_table.cc b/src/iceberg/inspect/metadata_table.cc
new file mode 100644
index 00000000..5e950400
--- /dev/null
+++ b/src/iceberg/inspect/metadata_table.cc
@@ -0,0 +1,54 @@
+/*
+ * 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.h"
+
+#include <memory>
+#include <utility>
+
+#include "iceberg/inspect/history_table.h"
+#include "iceberg/inspect/snapshots_table.h"
+
+namespace iceberg {
+
+MetadataTable::MetadataTable(std::shared_ptr<Table> source_table,
+ TableIdentifier identifier, std::shared_ptr<Schema> schema)
+ : identifier_(std::move(identifier)),
+ schema_(std::move(schema)),
+ source_table_(std::move(source_table)) {}
+
+MetadataTable::~MetadataTable() = default;
+
+Result<std::unique_ptr<MetadataTable>> MetadataTable::Make(std::shared_ptr<Table> table,
+ Kind kind) {
+ if (table == nullptr) [[unlikely]] {
+ return InvalidArgument("Table cannot be null");
+ }
+
+ switch (kind) {
+ case Kind::kSnapshots:
+ return SnapshotsTable::Make(table);
+ case Kind::kHistory:
+ return HistoryTable::Make(table);
+ }
+
+ return NotSupported("Unsupported metadata table type");
+}
+
+} // namespace iceberg
diff --git a/src/iceberg/inspect/metadata_table.h b/src/iceberg/inspect/metadata_table.h
new file mode 100644
index 00000000..7d0ac22d
--- /dev/null
+++ b/src/iceberg/inspect/metadata_table.h
@@ -0,0 +1,62 @@
+/*
+ * 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 <memory>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/table_identifier.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for Iceberg metadata tables.
+class ICEBERG_EXPORT MetadataTable {
+ public:
+ enum class Kind {
+ kSnapshots,
+ kHistory,
+ };
+
+ static Result<std::unique_ptr<MetadataTable>> Make(std::shared_ptr<Table> table,
+ Kind kind);
+
+ virtual ~MetadataTable();
+
+ virtual Kind kind() const noexcept = 0;
+
+ const TableIdentifier& name() const { return identifier_; }
+
+ const std::shared_ptr<Schema>& schema() const { return schema_; }
+
+ const std::shared_ptr<Table>& source_table() const { return source_table_; }
+
+ protected:
+ explicit MetadataTable(std::shared_ptr<Table> source_table, TableIdentifier identifier,
+ std::shared_ptr<Schema> schema);
+
+ private:
+ TableIdentifier identifier_;
+ std::shared_ptr<Schema> schema_;
+ std::shared_ptr<Table> source_table_;
+};
+
+} // namespace iceberg
diff --git a/src/iceberg/inspect/snapshots_table.cc b/src/iceberg/inspect/snapshots_table.cc
new file mode 100644
index 00000000..4b0c3ce9
--- /dev/null
+++ b/src/iceberg/inspect/snapshots_table.cc
@@ -0,0 +1,68 @@
+/*
+ * 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/snapshots_table.h"
+
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/table.h"
+#include "iceberg/table_identifier.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+namespace {
+
+std::shared_ptr<Schema> MakeSnapshotsTableSchema() {
+ return std::make_shared<Schema>(std::vector<SchemaField>{
+ SchemaField::MakeRequired(1, "committed_at", timestamp_tz()),
+ SchemaField::MakeRequired(2, "snapshot_id", int64()),
+ SchemaField::MakeOptional(3, "parent_id", int64()),
+ SchemaField::MakeOptional(4, "operation", string()),
+ SchemaField::MakeOptional(5, "manifest_list", string()),
+ SchemaField::MakeOptional(6, "summary",
+ std::make_shared<iceberg::MapType>(
+ SchemaField::MakeRequired(7, "key", string()),
+ SchemaField::MakeRequired(8, "value", string())))});
+}
+
+TableIdentifier MakeSnapshotsTableName(const TableIdentifier& source_name) {
+ return TableIdentifier{.ns = source_name.ns, .name = source_name.name + ".snapshots"};
+}
+
+} // namespace
+
+SnapshotsTable::SnapshotsTable(std::shared_ptr<Table> table)
+ : MetadataTable(table, MakeSnapshotsTableName(table->name()),
+ MakeSnapshotsTableSchema()) {}
+
+SnapshotsTable::~SnapshotsTable() = default;
+
+Result<std::unique_ptr<SnapshotsTable>> SnapshotsTable::Make(
+ std::shared_ptr<Table> table) {
+ if (table == nullptr) [[unlikely]] {
+ return InvalidArgument("Table cannot be null");
+ }
+ return std::unique_ptr<SnapshotsTable>(new SnapshotsTable(std::move(table)));
+}
+
+} // namespace iceberg
diff --git a/src/iceberg/inspect/snapshots_table.h b/src/iceberg/inspect/snapshots_table.h
new file mode 100644
index 00000000..50017796
--- /dev/null
+++ b/src/iceberg/inspect/snapshots_table.h
@@ -0,0 +1,44 @@
+/*
+ * 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 <memory>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/inspect/metadata_table.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Snapshots metadata table.
+class ICEBERG_EXPORT SnapshotsTable : public MetadataTable {
+ public:
+ static Result<std::unique_ptr<SnapshotsTable>> Make(std::shared_ptr<Table> table);
+
+ ~SnapshotsTable() override;
+
+ Kind kind() const noexcept override { return Kind::kSnapshots; }
+
+ private:
+ explicit SnapshotsTable(std::shared_ptr<Table> table);
+};
+
+} // namespace iceberg
diff --git a/src/iceberg/meson.build b/src/iceberg/meson.build
index b6792f99..75fae754 100644
--- a/src/iceberg/meson.build
+++ b/src/iceberg/meson.build
@@ -65,6 +65,9 @@ iceberg_sources = files(
'file_reader.cc',
'file_writer.cc',
'inheritable_metadata.cc',
+ 'inspect/history_table.cc',
+ 'inspect/metadata_table.cc',
+ 'inspect/snapshots_table.cc',
'json_serde.cc',
'location_provider.cc',
'manifest/manifest_adapter.cc',
@@ -296,6 +299,7 @@ subdir('puffin')
subdir('row')
subdir('update')
subdir('util')
+subdir('inspect')
if get_option('tests').enabled()
subdir('test')
diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc
index 1255871c..ba83de12 100644
--- a/src/iceberg/table.cc
+++ b/src/iceberg/table.cc
@@ -287,4 +287,37 @@ Result<std::shared_ptr<UpdateSchema>> StaticTable::NewUpdateSchema() {
return NotSupported("Cannot create an update schema for a static table");
}
+Result<std::shared_ptr<UpdateLocation>> StaticTable::NewUpdateLocation() {
+ return NotSupported("Cannot create an update location for a static table");
+}
+
+Result<std::shared_ptr<UpdatePartitionSpec>> StaticTable::NewUpdatePartitionSpec() {
+ return NotSupported("Cannot create an update partition spec for a static table");
+}
+
+Result<std::shared_ptr<UpdateSortOrder>> StaticTable::NewUpdateSortOrder() {
+ return NotSupported("Cannot create an update sort order for a static table");
+}
+
+Result<std::shared_ptr<ExpireSnapshots>> StaticTable::NewExpireSnapshots() {
+ return NotSupported("Cannot create an expire snapshots for a static table");
+}
+
+Result<std::shared_ptr<UpdateStatistics>> StaticTable::NewUpdateStatistics() {
+ return NotSupported("Cannot create an update statistics for a static table");
+}
+
+Result<std::shared_ptr<UpdatePartitionStatistics>>
+StaticTable::NewUpdatePartitionStatistics() {
+ return NotSupported("Cannot create an update partition statistics for a static table");
+}
+
+Result<std::shared_ptr<FastAppend>> StaticTable::NewFastAppend() {
+ return NotSupported("Cannot create a fast append for a static table");
+}
+
+Result<std::shared_ptr<SnapshotManager>> StaticTable::NewSnapshotManager() {
+ return NotSupported("Cannot create a snapshot manager for a static table");
+}
+
} // namespace iceberg
diff --git a/src/iceberg/table.h b/src/iceberg/table.h
index 8d8849f3..a5e10268 100644
--- a/src/iceberg/table.h
+++ b/src/iceberg/table.h
@@ -212,7 +212,7 @@ class ICEBERG_EXPORT StagedTable final : public Table {
/// \brief A read-only table.
-class ICEBERG_EXPORT StaticTable final : public Table {
+class ICEBERG_EXPORT StaticTable : public Table {
public:
static Result<std::shared_ptr<StaticTable>> Make(
TableIdentifier identifier, std::shared_ptr<TableMetadata> metadata,
@@ -228,6 +228,23 @@ class ICEBERG_EXPORT StaticTable final : public Table {
Result<std::shared_ptr<UpdateSchema>> NewUpdateSchema() override;
+ Result<std::shared_ptr<UpdateLocation>> NewUpdateLocation() override;
+
+ Result<std::shared_ptr<UpdatePartitionSpec>> NewUpdatePartitionSpec() override;
+
+ Result<std::shared_ptr<UpdateSortOrder>> NewUpdateSortOrder() override;
+
+ Result<std::shared_ptr<ExpireSnapshots>> NewExpireSnapshots() override;
+
+ Result<std::shared_ptr<UpdateStatistics>> NewUpdateStatistics() override;
+
+ Result<std::shared_ptr<UpdatePartitionStatistics>> NewUpdatePartitionStatistics()
+ override;
+
+ Result<std::shared_ptr<FastAppend>> NewFastAppend() override;
+
+ Result<std::shared_ptr<SnapshotManager>> NewSnapshotManager() override;
+
private:
using Table::Table;
};
diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt
index 0e8f0315..9dd3d238 100644
--- a/src/iceberg/test/CMakeLists.txt
+++ b/src/iceberg/test/CMakeLists.txt
@@ -89,6 +89,7 @@ add_iceberg_test(schema_test
add_iceberg_test(table_test
SOURCES
location_provider_test.cc
+ metadata_table_test.cc
metrics_config_test.cc
metrics_reporter_test.cc
metrics_test.cc
diff --git a/src/iceberg/test/meson.build b/src/iceberg/test/meson.build
index 03d9e1f6..c2093ed1 100644
--- a/src/iceberg/test/meson.build
+++ b/src/iceberg/test/meson.build
@@ -48,6 +48,7 @@ iceberg_tests = {
'table_test': {
'sources': files(
'location_provider_test.cc',
+ 'metadata_table_test.cc',
'metrics_config_test.cc',
'metrics_reporter_test.cc',
'metrics_test.cc',
diff --git a/src/iceberg/test/metadata_table_test.cc b/src/iceberg/test/metadata_table_test.cc
new file mode 100644
index 00000000..1e0a664c
--- /dev/null
+++ b/src/iceberg/test/metadata_table_test.cc
@@ -0,0 +1,120 @@
+/*
+ * 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.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/table.h"
+#include "iceberg/table_identifier.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/test/mock_catalog.h"
+#include "iceberg/test/mock_io.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+namespace {
+
+std::shared_ptr<Schema> MakeSnapshotsSchema() {
+ return std::make_shared<Schema>(std::vector<SchemaField>{
+ SchemaField::MakeRequired(1, "committed_at", timestamp_tz()),
+ SchemaField::MakeRequired(2, "snapshot_id", int64()),
+ SchemaField::MakeOptional(3, "parent_id", int64()),
+ SchemaField::MakeOptional(4, "operation", string()),
+ SchemaField::MakeOptional(5, "manifest_list", string()),
+ SchemaField::MakeOptional(
+ 6, "summary",
+ std::make_shared<MapType>(SchemaField::MakeRequired(7, "key", string()),
+ SchemaField::MakeRequired(8, "value", string())))});
+}
+
+std::shared_ptr<Schema> MakeHistorySchema() {
+ return std::make_shared<Schema>(std::vector<SchemaField>{
+ SchemaField::MakeRequired(1, "made_current_at", timestamp_tz()),
+ SchemaField::MakeRequired(2, "snapshot_id", int64()),
+ SchemaField::MakeOptional(3, "parent_id", int64()),
+ SchemaField::MakeRequired(4, "is_current_ancestor", boolean())});
+}
+
+} // namespace
+
+class MetadataTableTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ io_ = std::make_shared<MockFileIO>();
+ catalog_ = std::make_shared<MockCatalog>();
+
+ auto schema = std::make_shared<Schema>(
+ std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int64()),
+ SchemaField::MakeOptional(2, "name", string())},
+ 1);
+ metadata_ = std::make_shared<TableMetadata>(
+ TableMetadata{.format_version = 2, .schemas = {schema}, .current_schema_id = 1});
+
+ TableIdentifier source_ident{.ns = Namespace{.levels = {"db"}},
+ .name = "source_table"};
+ auto source_table_result =
+ Table::Make(source_ident, metadata_, "s3://bucket/meta.json", io_, catalog_);
+ EXPECT_THAT(source_table_result, IsOk());
+ source_table_ = *source_table_result;
+
+ auto snapshots_table_result =
+ MetadataTable::Make(source_table_, MetadataTable::Kind::kSnapshots);
+ EXPECT_THAT(snapshots_table_result, IsOk());
+ snapshots_table_ = std::move(*snapshots_table_result);
+ }
+
+ std::shared_ptr<MockFileIO> io_;
+ std::shared_ptr<MockCatalog> catalog_;
+ std::shared_ptr<TableMetadata> metadata_;
+ std::shared_ptr<Table> source_table_;
+ std::unique_ptr<MetadataTable> snapshots_table_;
+};
+
+TEST_F(MetadataTableTest, Constructor) {
+ EXPECT_EQ(snapshots_table_->kind(), MetadataTable::Kind::kSnapshots);
+ EXPECT_EQ(snapshots_table_->source_table(), source_table_);
+ EXPECT_EQ(snapshots_table_->name().name, "source_table.snapshots");
+ EXPECT_EQ(snapshots_table_->name().ns.levels, (std::vector<std::string>{"db"}));
+ EXPECT_NE(snapshots_table_->schema(), nullptr);
+}
+
+TEST_F(MetadataTableTest, SnapshotsSchemaMatchesIcebergSchema) {
+ EXPECT_TRUE(*snapshots_table_->schema() == *MakeSnapshotsSchema());
+}
+
+TEST_F(MetadataTableTest, HistorySchemaMatchesIcebergSchema) {
+ auto history_table_result =
+ MetadataTable::Make(source_table_, MetadataTable::Kind::kHistory);
+ ASSERT_THAT(history_table_result, IsOk());
+
+ EXPECT_TRUE(*(*history_table_result)->schema() == *MakeHistorySchema());
+}
+
+TEST_F(MetadataTableTest, FactoryRejectsNullSourceTable) {
+ auto result = MetadataTable::Make(nullptr, MetadataTable::Kind::kSnapshots);
+ EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
+ EXPECT_THAT(result, HasErrorMessage("Table cannot be null"));
+}
+
+} // namespace iceberg
diff --git a/src/iceberg/test/table_test.cc b/src/iceberg/test/table_test.cc
index 0ad8e8ce..9cfe9063 100644
--- a/src/iceberg/test/table_test.cc
+++ b/src/iceberg/test/table_test.cc
@@ -147,4 +147,20 @@ TYPED_TEST(TypedTableTest, NewTransaction) {
}
}
+TEST(StaticTableTest, NewMutatingOperationsAreNotSupported) {
+ auto io = std::make_shared<MockFileIO>();
+ auto schema = std::make_shared<Schema>(
+ std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int64())}, 1);
+ auto metadata = std::make_shared<TableMetadata>(
+ TableMetadata{.format_version = 2, .schemas = {schema}, .current_schema_id = 1});
+ TableIdentifier ident{.ns = Namespace{.levels = {"db"}}, .name = "test_table"};
+ ICEBERG_UNWRAP_OR_FAIL(auto table, StaticTable::Make(ident, std::move(metadata),
+ "s3://bucket/meta.json", io));
+
+ EXPECT_THAT(table->NewUpdateStatistics(), IsError(ErrorKind::kNotSupported));
+ EXPECT_THAT(table->NewUpdatePartitionStatistics(), IsError(ErrorKind::kNotSupported));
+ EXPECT_THAT(table->NewFastAppend(), IsError(ErrorKind::kNotSupported));
+ EXPECT_THAT(table->NewSnapshotManager(), IsError(ErrorKind::kNotSupported));
+}
+
} // namespace iceberg
diff --git a/src/iceberg/type_fwd.h b/src/iceberg/type_fwd.h
index 745c63ac..6b416003 100644
--- a/src/iceberg/type_fwd.h
+++ b/src/iceberg/type_fwd.h
@@ -240,6 +240,11 @@ class UpdateStatistics;
class DeleteLoader;
class PositionDeleteIndex;
+/// \brief Metadata tables.
+class HistoryTable;
+class MetadataTable;
+class SnapshotsTable;
+
/// ----------------------------------------------------------------------------
/// TODO: Forward declarations below are not added yet.
/// ----------------------------------------------------------------------------