Skip to content

Commit c9fe0ac

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

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

src/iceberg/table_impl.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "iceberg/table_impl.h"
2121

22+
#include "iceberg/exception.h"
2223
#include "iceberg/partition_spec.h"
2324
#include "iceberg/schema.h"
2425
#include "iceberg/snapshot.h"
@@ -30,23 +31,16 @@ namespace iceberg {
3031
BaseTable::BaseTable(std::string name, std::shared_ptr<TableMetadata> metadata)
3132
: name_(std::move(name)), metadata_(std::move(metadata)) {
3233
if (!metadata_) {
33-
throw std::invalid_argument("Table metadata cannot be null");
34+
throw IcebergError("Table metadata cannot be null");
3435
}
3536
}
3637

3738
void BaseTable::InitSchema() const {
3839
for (const auto& schema : metadata_->schemas) {
3940
if (schema->schema_id()) {
4041
schemas_map_.emplace(schema->schema_id().value(), schema);
41-
if (schema->schema_id().value() == metadata_->current_schema_id) {
42-
schema_ = schema;
43-
}
4442
}
4543
}
46-
// compatible with V1 table schema
47-
if (!schema_ && metadata_->schemas.size() == 1UL) {
48-
schema_ = metadata_->schemas.front();
49-
}
5044
}
5145

5246
void BaseTable::InitPartitionSpec() const {
@@ -79,13 +73,7 @@ void BaseTable::InitSnapshot() const {
7973

8074
const std::string& BaseTable::uuid() const { return metadata_->table_uuid; }
8175

82-
Result<std::shared_ptr<Schema>> BaseTable::schema() const {
83-
std::call_once(init_schema_once_, [this]() { InitSchema(); });
84-
if (!schema_) {
85-
return NotFound("Current schema is not defined for this table");
86-
}
87-
return schema_;
88-
}
76+
Result<std::shared_ptr<Schema>> BaseTable::schema() const { return metadata_->Schema(); }
8977

9078
const std::unordered_map<int32_t, std::shared_ptr<Schema>>& BaseTable::schemas() const {
9179
std::call_once(init_schema_once_, [this]() { InitSchema(); });

src/iceberg/table_impl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class ICEBERG_EXPORT BaseTable : public Table {
8484

8585
const std::string name_;
8686

87-
mutable std::shared_ptr<Schema> schema_;
8887
mutable std::unordered_map<int32_t, std::shared_ptr<Schema>> schemas_map_;
8988

9089
mutable std::shared_ptr<PartitionSpec> partition_spec_;

src/iceberg/table_metadata.cc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,24 @@ std::string ToString(const MetadataLogEntry& entry) {
4747
}
4848

4949
Result<std::shared_ptr<Schema>> TableMetadata::Schema() const {
50-
auto iter = std::ranges::find_if(schemas, [this](const auto& schema) {
51-
return schema->schema_id() == current_schema_id;
50+
std::call_once(init_schema_once, [this]() {
51+
auto iter = std::ranges::find_if(schemas, [this](const auto& schema) {
52+
return schema->schema_id() == current_schema_id;
53+
});
54+
if (iter != schemas.end()) {
55+
schema = *iter;
56+
}
57+
58+
// compatible with V1 table schema
59+
if (!schema && schemas.size() == 1UL) {
60+
schema = schemas.front();
61+
}
5262
});
53-
if (iter == schemas.end()) {
54-
return NotFound("Current schema is not found");
63+
64+
if (!schema) {
65+
return NotFound("Current schema is not defined for this table");
5566
}
56-
return *iter;
67+
return schema;
5768
}
5869

5970
Result<std::shared_ptr<PartitionSpec>> TableMetadata::PartitionSpec() const {

src/iceberg/table_metadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ struct ICEBERG_EXPORT TableMetadata {
9292
TimePointMs last_updated_ms;
9393
/// The highest assigned column ID for the table
9494
int32_t last_column_id;
95+
/// The current schema for the table, or null if not set
96+
mutable std::shared_ptr<Schema> schema;
9597
/// A list of schemas
9698
std::vector<std::shared_ptr<Schema>> schemas;
9799
/// ID of the table's current schema
@@ -127,6 +129,8 @@ struct ICEBERG_EXPORT TableMetadata {
127129
/// A `long` higher than all assigned row IDs
128130
int64_t next_row_id;
129131

132+
mutable std::once_flag init_schema_once;
133+
130134
/// \brief Get the current schema, return NotFoundError if not found
131135
Result<std::shared_ptr<Schema>> Schema() const;
132136
/// \brief Get the current partition spec, return NotFoundError if not found

0 commit comments

Comments
 (0)