Skip to content

Commit f933198

Browse files
author
shuxu.li
committed
feat: static table metadata access support
1 parent 9b10557 commit f933198

File tree

9 files changed

+168
-161
lines changed

9 files changed

+168
-161
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ set(ICEBERG_SOURCES
3838
sort_field.cc
3939
sort_order.cc
4040
statistics_file.cc
41-
table.cc
41+
table_impl.cc
4242
table_metadata.cc
4343
transform.cc
4444
transform_function.cc

src/iceberg/table.h

Lines changed: 5 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@
1919

2020
#pragma once
2121

22-
#include <memory>
23-
#include <mutex>
2422
#include <string>
2523
#include <unordered_map>
2624
#include <vector>
2725

2826
#include "iceberg/iceberg_export.h"
29-
#include "iceberg/location_provider.h"
3027
#include "iceberg/result.h"
31-
#include "iceberg/table_scan.h"
32-
#include "iceberg/transaction.h"
3328
#include "iceberg/type_fwd.h"
3429

3530
namespace iceberg {
@@ -92,10 +87,11 @@ class ICEBERG_EXPORT Table {
9287
/// \return a vector of history entries
9388
virtual const std::vector<std::shared_ptr<HistoryEntry>>& history() const = 0;
9489

95-
/// \brief Create a new table scan for this table
96-
///
97-
/// Once a table scan is created, it can be refined to project columns and filter data.
98-
virtual Result<std::unique_ptr<TableScan>> NewScan() const = 0;
90+
// TODO(lishuxu): TableScan is not implemented yet, disable it for now.
91+
// /// \brief Create a new table scan for this table
92+
// ///
93+
// /// Once a table scan is created, it can be refined to project columns and filter
94+
// data. virtual Result<std::unique_ptr<TableScan>> NewScan() const = 0;
9995

10096
/// \brief Create a new append API to add files to this table and commit
10197
virtual Result<std::shared_ptr<AppendFiles>> NewAppend() = 0;
@@ -112,100 +108,4 @@ class ICEBERG_EXPORT Table {
112108
virtual Result<std::unique_ptr<LocationProvider>> location_provider() const = 0;
113109
};
114110

115-
/// \brief An abstract base implementation of the Iceberg Table interface.
116-
///
117-
/// BaseTable provides common functionality for Iceberg table implementations,
118-
/// including lazy initialization of schema, partition specs, sort orders,
119-
/// and snapshot metadata.
120-
///
121-
/// This class is not intended to be used directly by users, but serves as a foundation
122-
/// for concrete implementations such as StaticTable or CatalogTable.
123-
class ICEBERG_EXPORT BaseTable : public Table {
124-
public:
125-
~BaseTable() override = default;
126-
127-
BaseTable(std::string name, std::shared_ptr<TableMetadata> metadata);
128-
129-
const std::string& name() const override { return name_; }
130-
131-
const std::string& uuid() const override;
132-
133-
Result<std::shared_ptr<Schema>> schema() const override;
134-
135-
const std::unordered_map<int32_t, std::shared_ptr<Schema>>& schemas() const override;
136-
137-
Result<std::shared_ptr<PartitionSpec>> spec() const override;
138-
139-
const std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>>& specs()
140-
const override;
141-
142-
Result<std::shared_ptr<SortOrder>> sort_order() const override;
143-
144-
const std::unordered_map<int32_t, std::shared_ptr<SortOrder>>& sort_orders()
145-
const override;
146-
147-
const std::unordered_map<std::string, std::string>& properties() const override;
148-
149-
const std::string& location() const override;
150-
151-
Result<std::shared_ptr<Snapshot>> current_snapshot() const override;
152-
153-
Result<std::shared_ptr<Snapshot>> snapshot(int64_t snapshot_id) const override;
154-
155-
const std::vector<std::shared_ptr<Snapshot>>& snapshots() const override;
156-
157-
const std::vector<std::shared_ptr<HistoryEntry>>& history() const override;
158-
159-
private:
160-
void InitSchema() const;
161-
void InitPartitionSpec() const;
162-
void InitSortOrder() const;
163-
void InitSnapshot() const;
164-
165-
const std::string name_;
166-
167-
mutable std::shared_ptr<Schema> schema_;
168-
mutable std::unordered_map<int32_t, std::shared_ptr<Schema>> schemas_map_;
169-
170-
mutable std::shared_ptr<PartitionSpec> partition_spec_;
171-
mutable std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>> partition_spec_map_;
172-
173-
mutable std::shared_ptr<SortOrder> sort_order_;
174-
mutable std::unordered_map<int32_t, std::shared_ptr<SortOrder>> sort_orders_map_;
175-
176-
mutable std::shared_ptr<Snapshot> current_snapshot_;
177-
mutable std::unordered_map<int64_t, std::shared_ptr<Snapshot>> snapshots_map_;
178-
179-
mutable std::vector<std::shared_ptr<HistoryEntry>> history_;
180-
181-
std::shared_ptr<TableMetadata> metadata_;
182-
183-
// once_flags
184-
mutable std::once_flag init_schema_once_;
185-
mutable std::once_flag init_partition_spec_once_;
186-
mutable std::once_flag init_sort_order_once_;
187-
mutable std::once_flag init_snapshot_once_;
188-
};
189-
190-
/// \brief A read-only implementation of an Iceberg table.
191-
///
192-
/// StaticTable represents a snapshot of a table that does not support mutation.
193-
class ICEBERG_EXPORT StaticTable : public BaseTable {
194-
public:
195-
~StaticTable() override = default;
196-
197-
StaticTable(std::string name, std::shared_ptr<TableMetadata> metadata)
198-
: BaseTable(std::move(name), std::move(metadata)) {}
199-
200-
Status Refresh() override;
201-
202-
Result<std::unique_ptr<TableScan>> NewScan() const override;
203-
204-
Result<std::shared_ptr<AppendFiles>> NewAppend() override;
205-
206-
Result<std::unique_ptr<Transaction>> NewTransaction() override;
207-
208-
Result<std::unique_ptr<LocationProvider>> location_provider() const override;
209-
};
210-
211111
} // namespace iceberg

src/iceberg/table.cc renamed to src/iceberg/table_impl.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
* under the License.
1818
*/
1919

20-
#include "iceberg/table.h"
20+
#include "iceberg/table_impl.h"
2121

22-
#include <iostream>
23-
24-
#include "iceberg/exception.h"
2522
#include "iceberg/partition_spec.h"
26-
#include "iceberg/result.h"
2723
#include "iceberg/schema.h"
2824
#include "iceberg/snapshot.h"
2925
#include "iceberg/sort_order.h"
@@ -160,10 +156,6 @@ Status StaticTable::Refresh() {
160156
return NotSupported("Refresh is not supported for StaticTable");
161157
}
162158

163-
Result<std::unique_ptr<TableScan>> StaticTable::NewScan() const {
164-
return NotSupported("NewScan is not supported for StaticTable");
165-
}
166-
167159
Result<std::shared_ptr<AppendFiles>> StaticTable::NewAppend() {
168160
return NotSupported("NewAppend is not supported for StaticTable");
169161
}

src/iceberg/table_impl.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <string>
23+
#include <unordered_map>
24+
#include <vector>
25+
26+
#include "iceberg/iceberg_export.h"
27+
#include "iceberg/location_provider.h"
28+
#include "iceberg/result.h"
29+
#include "iceberg/table.h"
30+
#include "iceberg/transaction.h"
31+
#include "iceberg/type_fwd.h"
32+
33+
namespace iceberg {
34+
35+
/// \brief An abstract base implementation of the Iceberg Table interface.
36+
///
37+
/// BaseTable provides common functionality for Iceberg table implementations,
38+
/// including lazy initialization of schema, partition specs, sort orders,
39+
/// and snapshot metadata.
40+
///
41+
/// This class is not intended to be used directly by users, but serves as a foundation
42+
/// for concrete implementations such as StaticTable or CatalogTable.
43+
class ICEBERG_EXPORT BaseTable : public Table {
44+
public:
45+
~BaseTable() override = default;
46+
47+
BaseTable(std::string name, std::shared_ptr<TableMetadata> metadata);
48+
49+
const std::string& name() const override { return name_; }
50+
51+
const std::string& uuid() const override;
52+
53+
Result<std::shared_ptr<Schema>> schema() const override;
54+
55+
const std::unordered_map<int32_t, std::shared_ptr<Schema>>& schemas() const override;
56+
57+
Result<std::shared_ptr<PartitionSpec>> spec() const override;
58+
59+
const std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>>& specs()
60+
const override;
61+
62+
Result<std::shared_ptr<SortOrder>> sort_order() const override;
63+
64+
const std::unordered_map<int32_t, std::shared_ptr<SortOrder>>& sort_orders()
65+
const override;
66+
67+
const std::unordered_map<std::string, std::string>& properties() const override;
68+
69+
const std::string& location() const override;
70+
71+
Result<std::shared_ptr<Snapshot>> current_snapshot() const override;
72+
73+
Result<std::shared_ptr<Snapshot>> snapshot(int64_t snapshot_id) const override;
74+
75+
const std::vector<std::shared_ptr<Snapshot>>& snapshots() const override;
76+
77+
const std::vector<std::shared_ptr<HistoryEntry>>& history() const override;
78+
79+
private:
80+
void InitSchema() const;
81+
void InitPartitionSpec() const;
82+
void InitSortOrder() const;
83+
void InitSnapshot() const;
84+
85+
const std::string name_;
86+
87+
mutable std::shared_ptr<Schema> schema_;
88+
mutable std::unordered_map<int32_t, std::shared_ptr<Schema>> schemas_map_;
89+
90+
mutable std::shared_ptr<PartitionSpec> partition_spec_;
91+
mutable std::unordered_map<int32_t, std::shared_ptr<PartitionSpec>> partition_spec_map_;
92+
93+
mutable std::shared_ptr<SortOrder> sort_order_;
94+
mutable std::unordered_map<int32_t, std::shared_ptr<SortOrder>> sort_orders_map_;
95+
96+
mutable std::shared_ptr<Snapshot> current_snapshot_;
97+
mutable std::unordered_map<int64_t, std::shared_ptr<Snapshot>> snapshots_map_;
98+
99+
mutable std::vector<std::shared_ptr<HistoryEntry>> history_;
100+
101+
std::shared_ptr<TableMetadata> metadata_;
102+
103+
// once_flags
104+
mutable std::once_flag init_schema_once_;
105+
mutable std::once_flag init_partition_spec_once_;
106+
mutable std::once_flag init_sort_order_once_;
107+
mutable std::once_flag init_snapshot_once_;
108+
};
109+
110+
/// \brief A read-only implementation of an Iceberg table.
111+
///
112+
/// StaticTable represents a snapshot of a table that does not support mutation.
113+
class ICEBERG_EXPORT StaticTable : public BaseTable {
114+
public:
115+
~StaticTable() override = default;
116+
117+
StaticTable(std::string name, std::shared_ptr<TableMetadata> metadata)
118+
: BaseTable(std::move(name), std::move(metadata)) {}
119+
120+
Status Refresh() override;
121+
122+
Result<std::shared_ptr<AppendFiles>> NewAppend() override;
123+
124+
Result<std::unique_ptr<Transaction>> NewTransaction() override;
125+
126+
Result<std::unique_ptr<LocationProvider>> location_provider() const override;
127+
};
128+
129+
} // namespace iceberg

src/iceberg/table_scan.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/metadata_serde_test.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class MetadataSerdeTest : public ::testing::Test {
5050

5151
TEST_F(MetadataSerdeTest, DeserializeV1Valid) {
5252
std::unique_ptr<TableMetadata> metadata;
53-
ASSERT_NO_FATAL_FAILURE(
54-
test::ReadTableMetadata("TableMetadataV1Valid.json", &metadata));
53+
ASSERT_NO_FATAL_FAILURE(ReadTableMetadata("TableMetadataV1Valid.json", &metadata));
5554

5655
EXPECT_EQ(metadata->format_version, 1);
5756
EXPECT_EQ(metadata->table_uuid, "d20125c8-7284-442c-9aea-15fee620737c");
@@ -84,12 +83,13 @@ TEST_F(MetadataSerdeTest, DeserializeV1Valid) {
8483
auto partition_spec = metadata->PartitionSpec();
8584
ASSERT_TRUE(partition_spec.has_value());
8685
EXPECT_EQ(*(partition_spec.value().get()), *expected_spec);
86+
auto snapshot = metadata->Snapshot();
87+
ASSERT_FALSE(snapshot.has_value());
8788
}
8889

8990
TEST_F(MetadataSerdeTest, DeserializeV2Valid) {
9091
std::unique_ptr<TableMetadata> metadata;
91-
ASSERT_NO_FATAL_FAILURE(
92-
test::ReadTableMetadata("TableMetadataV2Valid.json", &metadata));
92+
ASSERT_NO_FATAL_FAILURE(ReadTableMetadata("TableMetadataV2Valid.json", &metadata));
9393

9494
EXPECT_EQ(metadata->format_version, 2);
9595
EXPECT_EQ(metadata->table_uuid, "9c12d441-03fe-4693-9a96-a0705ddf69c1");
@@ -136,7 +136,11 @@ TEST_F(MetadataSerdeTest, DeserializeV2Valid) {
136136
ASSERT_TRUE(sort_order.has_value());
137137
EXPECT_EQ(*(sort_order.value().get()), *expected_sort_order);
138138

139+
// Compare snapshot
139140
EXPECT_EQ(metadata->current_snapshot_id, 3055729675574597004);
141+
auto snapshot = metadata->Snapshot();
142+
ASSERT_TRUE(snapshot.has_value());
143+
EXPECT_EQ(snapshot.value()->snapshot_id, 3055729675574597004);
140144

141145
// Compare snapshots
142146
std::vector<Snapshot> expected_snapshots{

0 commit comments

Comments
 (0)