iceberg-cpp
Loading...
Searching...
No Matches
table_metadata.h
Go to the documentation of this file.
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
24
25#include <memory>
26#include <string>
27#include <string_view>
28#include <unordered_map>
29#include <vector>
30
32#include "iceberg/iceberg_export.h"
33#include "iceberg/table_properties.h"
34#include "iceberg/type_fwd.h"
36#include "iceberg/util/lazy.h"
37#include "iceberg/util/timepoint.h"
38
39namespace iceberg {
40
42struct ICEBERG_EXPORT SnapshotLogEntry {
44 TimePointMs timestamp_ms;
46 int64_t snapshot_id;
47
48 friend bool operator==(const SnapshotLogEntry& lhs, const SnapshotLogEntry& rhs) {
49 return lhs.timestamp_ms == rhs.timestamp_ms && lhs.snapshot_id == rhs.snapshot_id;
50 }
51};
52
54struct ICEBERG_EXPORT MetadataLogEntry {
56 TimePointMs timestamp_ms;
58 std::string metadata_file;
59
60 friend bool operator==(const MetadataLogEntry& lhs, const MetadataLogEntry& rhs) {
61 return lhs.timestamp_ms == rhs.timestamp_ms && lhs.metadata_file == rhs.metadata_file;
62 }
63};
64
73struct ICEBERG_EXPORT TableMetadata {
74 static constexpr int8_t kDefaultTableFormatVersion = 2;
75 static constexpr int8_t kSupportedTableFormatVersion = 3;
76 static constexpr int8_t kMinFormatVersionRowLineage = 3;
77 static constexpr int8_t kMinFormatVersionDefaultValues = 3;
78 static constexpr int64_t kInitialSequenceNumber = 0;
79 static constexpr int64_t kInitialRowId = 0;
80
81 static inline const std::unordered_map<TypeId, int8_t> kMinFormatVersions = {
82 {TypeId::kTimestampNs, 3}, {TypeId::kTimestampTzNs, 3}, {TypeId::kUnknown, 3},
83 {TypeId::kVariant, 3}, {TypeId::kGeometry, 3}, {TypeId::kGeography, 3},
84 };
85
89 std::string table_uuid;
91 std::string location;
95 TimePointMs last_updated_ms;
99 std::vector<std::shared_ptr<iceberg::Schema>> schemas;
103 std::vector<std::shared_ptr<iceberg::PartitionSpec>> partition_specs;
113 std::vector<std::shared_ptr<iceberg::Snapshot>> snapshots;
116 std::vector<SnapshotLogEntry> snapshot_log;
119 std::vector<MetadataLogEntry> metadata_log;
121 std::vector<std::shared_ptr<iceberg::SortOrder>> sort_orders;
125 std::unordered_map<std::string, std::shared_ptr<SnapshotRef>> refs;
127 std::vector<std::shared_ptr<struct StatisticsFile>> statistics;
129 std::vector<std::shared_ptr<struct PartitionStatisticsFile>> partition_statistics;
131 int64_t next_row_id;
133 std::vector<EncryptedKey> encryption_keys;
134
135 static Result<std::unique_ptr<TableMetadata>> Make(
136 const iceberg::Schema& schema, const iceberg::PartitionSpec& spec,
137 const iceberg::SortOrder& sort_order, const std::string& location,
138 const std::unordered_map<std::string, std::string>& properties,
139 int format_version = kDefaultTableFormatVersion);
140
143 Result<std::shared_ptr<iceberg::Schema>> Schema() const;
146 Result<std::shared_ptr<iceberg::Schema>> SchemaById(int32_t schema_id) const;
149 Result<std::shared_ptr<iceberg::PartitionSpec>> PartitionSpec() const;
152 Result<std::shared_ptr<iceberg::PartitionSpec>> PartitionSpecById(
153 int32_t spec_id) const;
156 Result<std::shared_ptr<iceberg::SortOrder>> SortOrder() const;
159 Result<std::shared_ptr<iceberg::SortOrder>> SortOrderById(int32_t sort_order_id) const;
162 Result<std::shared_ptr<iceberg::Snapshot>> Snapshot() const;
165 Result<std::shared_ptr<iceberg::Snapshot>> SnapshotById(int64_t snapshot_id) const;
167 int64_t NextSequenceNumber() const;
168
169 ICEBERG_EXPORT friend bool operator==(const TableMetadata& lhs,
170 const TableMetadata& rhs);
171};
172
173// Cache for table metadata mappings to facilitate fast lookups.
174class ICEBERG_EXPORT TableMetadataCache {
175 public:
176 explicit TableMetadataCache(const TableMetadata* metadata) : metadata_(metadata) {}
177
178 template <typename T>
179 using ByIdMap = std::unordered_map<int32_t, std::shared_ptr<T>>;
180 using SchemasMap = ByIdMap<Schema>;
181 using PartitionSpecsMap = ByIdMap<PartitionSpec>;
182 using SortOrdersMap = ByIdMap<SortOrder>;
183 using SnapshotsMap = std::unordered_map<int64_t, std::shared_ptr<Snapshot>>;
184 using SchemasMapRef = std::reference_wrapper<const SchemasMap>;
185 using PartitionSpecsMapRef = std::reference_wrapper<const PartitionSpecsMap>;
186 using SortOrdersMapRef = std::reference_wrapper<const SortOrdersMap>;
187 using SnapshotsMapRef = std::reference_wrapper<const SnapshotsMap>;
188
189 Result<SchemasMapRef> GetSchemasById() const;
190 Result<PartitionSpecsMapRef> GetPartitionSpecsById() const;
191 Result<SortOrdersMapRef> GetSortOrdersById() const;
192 Result<SnapshotsMapRef> GetSnapshotsById() const;
193
194 private:
195 static Result<SchemasMap> InitSchemasMap(const TableMetadata* metadata);
196 static Result<PartitionSpecsMap> InitPartitionSpecsMap(const TableMetadata* metadata);
197 static Result<SortOrdersMap> InitSortOrdersMap(const TableMetadata* metadata);
198 static Result<SnapshotsMap> InitSnapshotMap(const TableMetadata* metadata);
199
200 const TableMetadata* metadata_;
201 Lazy<InitSchemasMap> schemas_map_;
202 Lazy<InitPartitionSpecsMap> partition_specs_map_;
203 Lazy<InitSortOrdersMap> sort_orders_map_;
204 Lazy<InitSnapshotMap> snapshot_map_;
205};
206
208ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
209
211ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
212
226class ICEBERG_EXPORT TableMetadataBuilder : public ErrorCollector {
227 public:
232 static std::unique_ptr<TableMetadataBuilder> BuildFromEmpty(
233 int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
234
240 static std::unique_ptr<TableMetadataBuilder> BuildFrom(const TableMetadata* base);
241
246 TableMetadataBuilder& ApplyChangesForCreate(const TableMetadata& base);
247
252 TableMetadataBuilder& SetMetadataLocation(std::string_view metadata_location);
253
258 TableMetadataBuilder& SetPreviousMetadataLocation(
259 std::string_view previous_metadata_location);
260
265 TableMetadataBuilder& AssignUUID();
266
271 TableMetadataBuilder& AssignUUID(std::string_view uuid);
272
277 TableMetadataBuilder& UpgradeFormatVersion(int8_t new_format_version);
278
284 TableMetadataBuilder& SetCurrentSchema(const std::shared_ptr<Schema>& schema,
285 int32_t new_last_column_id);
286
291 TableMetadataBuilder& SetCurrentSchema(int32_t schema_id);
292
297 TableMetadataBuilder& AddSchema(const std::shared_ptr<Schema>& schema);
298
303 TableMetadataBuilder& SetDefaultPartitionSpec(std::shared_ptr<PartitionSpec> spec);
304
309 TableMetadataBuilder& SetDefaultPartitionSpec(int32_t spec_id);
310
315 TableMetadataBuilder& AddPartitionSpec(std::shared_ptr<PartitionSpec> spec);
316
321 TableMetadataBuilder& RemovePartitionSpecs(const std::vector<int32_t>& spec_ids);
322
327 TableMetadataBuilder& RemoveSchemas(const std::unordered_set<int32_t>& schema_ids);
328
333 TableMetadataBuilder& SetDefaultSortOrder(std::shared_ptr<SortOrder> order);
334
339 TableMetadataBuilder& SetDefaultSortOrder(int32_t order_id);
340
345 TableMetadataBuilder& AddSortOrder(std::shared_ptr<SortOrder> order);
346
351 TableMetadataBuilder& AddSnapshot(std::shared_ptr<Snapshot> snapshot);
352
358 TableMetadataBuilder& SetBranchSnapshot(int64_t snapshot_id, const std::string& branch);
359
365 TableMetadataBuilder& SetBranchSnapshot(std::shared_ptr<Snapshot> snapshot,
366 const std::string& branch);
367
373 TableMetadataBuilder& SetRef(const std::string& name, std::shared_ptr<SnapshotRef> ref);
374
379 TableMetadataBuilder& RemoveRef(const std::string& name);
380
385 TableMetadataBuilder& RemoveSnapshots(
386 const std::vector<std::shared_ptr<Snapshot>>& snapshots_to_remove);
387
392 TableMetadataBuilder& RemoveSnapshots(const std::vector<int64_t>& snapshot_ids);
393
401 TableMetadataBuilder& SuppressHistoricalSnapshots();
402
407 TableMetadataBuilder& SetStatistics(std::shared_ptr<StatisticsFile> statistics_file);
408
413 TableMetadataBuilder& RemoveStatistics(int64_t snapshot_id);
414
419 TableMetadataBuilder& SetPartitionStatistics(
420 const std::shared_ptr<PartitionStatisticsFile>& partition_statistics_file);
421
426 TableMetadataBuilder& RemovePartitionStatistics(int64_t snapshot_id);
427
432 TableMetadataBuilder& SetProperties(
433 const std::unordered_map<std::string, std::string>& updated);
434
439 TableMetadataBuilder& RemoveProperties(const std::unordered_set<std::string>& removed);
440
445 TableMetadataBuilder& SetLocation(std::string_view location);
446
451 TableMetadataBuilder& AddEncryptionKey(EncryptedKey key);
452
457 TableMetadataBuilder& RemoveEncryptionKey(std::string_view key_id);
458
462 Result<std::unique_ptr<TableMetadata>> Build();
463
465 const std::vector<std::unique_ptr<TableUpdate>>& changes() const;
466
468 const TableMetadata* base() const;
469
471 const TableMetadata& current() const;
472
475
476 // Delete copy operations (use BuildFrom to create a new builder)
478 TableMetadataBuilder& operator=(const TableMetadataBuilder&) = delete;
479
480 // Enable move operations
482 TableMetadataBuilder& operator=(TableMetadataBuilder&&) noexcept;
483
484 private:
486 explicit TableMetadataBuilder(int8_t format_version);
487
489 explicit TableMetadataBuilder(const TableMetadata* base);
490
492 struct Impl;
493 std::unique_ptr<Impl> impl_;
494};
495
497enum class ICEBERG_EXPORT MetadataFileCodecType {
498 kNone,
499 kGzip,
500};
501
503struct ICEBERG_EXPORT TableMetadataUtil {
504 struct ICEBERG_EXPORT Codec {
509 static Result<MetadataFileCodecType> FromString(std::string_view name);
510
515 static Result<MetadataFileCodecType> FromFileName(std::string_view file_name);
516
520 static Result<std::string> NameToFileExtension(std::string_view codec);
521
525 static std::string TypeToFileExtension(MetadataFileCodecType codec);
526
527 static constexpr std::string_view kTableMetadataFileSuffix = ".metadata.json";
528 static constexpr std::string_view kCompGzipTableMetadataFileSuffix =
529 ".metadata.json.gz";
530 static constexpr std::string_view kGzipTableMetadataFileSuffix = ".gz.metadata.json";
531 static constexpr std::string_view kGzipTableMetadataFileExtension = ".gz";
532 static constexpr std::string_view kCodecTypeGzip = "GZIP";
533 static constexpr std::string_view kCodecTypeNone = "NONE";
534 };
535
542 static Result<std::unique_ptr<TableMetadata>> Read(
543 class FileIO& io, const std::string& location,
544 std::optional<size_t> length = std::nullopt);
545
556 static Result<std::string> Write(FileIO& io, const TableMetadata* base,
557 const std::string& base_metadata_location,
558 TableMetadata& metadata);
559
569 static void DeleteRemovedMetadataFiles(FileIO& io, const TableMetadata* base,
570 const TableMetadata& metadata);
571
577 static Status Write(FileIO& io, const std::string& location,
578 const TableMetadata& metadata);
579
580 private:
590 static int32_t ParseVersionFromLocation(std::string_view metadata_location);
591
597 static Result<std::string> NewTableMetadataFilePath(const TableMetadata& metadata,
598 int32_t version);
599};
600
601} // namespace iceberg
602
603namespace std {
604template <>
605struct hash<iceberg::MetadataLogEntry> {
606 size_t operator()(const iceberg::MetadataLogEntry& m) const noexcept {
607 return std::hash<std::string>{}(m.metadata_file);
608 }
609};
610
611} // namespace std
Base class for collecting errors in the builder pattern.
Definition error_collector.h:93
Pluggable module for reading, writing, and deleting files.
Definition file_io.h:125
A partition spec for a Table.
Definition partition_spec.h:47
A schema for a Table.
Definition schema.h:51
A sort order for a table.
Definition sort_order.h:40
Definition table_metadata.cc:550
Builder class for constructing TableMetadata objects.
Definition table_metadata.h:226
~TableMetadataBuilder() override
Destructor.
Definition table_metadata.h:174
Table properties for Iceberg tables.
Definition table_properties.h:37
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
STL namespace.
Represents an encrypted table key entry.
Definition encrypted_key.h:34
Represents a metadata log entry.
Definition table_metadata.h:54
std::string metadata_file
Metadata file location.
Definition table_metadata.h:58
TimePointMs timestamp_ms
The timestamp in milliseconds of the change.
Definition table_metadata.h:56
Represents a snapshot log entry.
Definition table_metadata.h:42
TimePointMs timestamp_ms
The timestamp in milliseconds of the change.
Definition table_metadata.h:44
int64_t snapshot_id
ID of the snapshot.
Definition table_metadata.h:46
A snapshot of the data in a table at a point in time.
Definition snapshot.h:389
Definition table_metadata.h:504
Utility class for table metadata.
Definition table_metadata.h:503
Represents the metadata for an Iceberg table.
Definition table_metadata.h:73
int64_t current_snapshot_id
ID of the current table snapshot.
Definition table_metadata.h:111
TimePointMs last_updated_ms
Timestamp in milliseconds from the unix epoch when the table was last updated.
Definition table_metadata.h:95
int32_t last_partition_id
The highest assigned partition field ID across all partition specs for the table.
Definition table_metadata.h:107
std::vector< std::shared_ptr< iceberg::PartitionSpec > > partition_specs
A list of partition specs.
Definition table_metadata.h:103
std::vector< MetadataLogEntry > metadata_log
Definition table_metadata.h:119
int32_t last_column_id
The highest assigned column ID for the table.
Definition table_metadata.h:97
int32_t default_spec_id
ID of the current partition spec that writers should use by default.
Definition table_metadata.h:105
std::unordered_map< std::string, std::shared_ptr< SnapshotRef > > refs
A map of snapshot references.
Definition table_metadata.h:125
TableProperties properties
A string to string map of table properties.
Definition table_metadata.h:109
std::vector< std::shared_ptr< iceberg::Snapshot > > snapshots
A list of valid snapshots.
Definition table_metadata.h:113
std::vector< std::shared_ptr< iceberg::SortOrder > > sort_orders
A list of sort orders.
Definition table_metadata.h:121
int32_t default_sort_order_id
Default sort order id of the table.
Definition table_metadata.h:123
int64_t last_sequence_number
The table's highest assigned sequence number.
Definition table_metadata.h:93
std::vector< std::shared_ptr< struct PartitionStatisticsFile > > partition_statistics
A list of partition statistics.
Definition table_metadata.h:129
int64_t next_row_id
A long higher than all assigned row IDs.
Definition table_metadata.h:131
std::string location
The table's base location.
Definition table_metadata.h:91
std::vector< SnapshotLogEntry > snapshot_log
Definition table_metadata.h:116
std::vector< std::shared_ptr< iceberg::Schema > > schemas
A list of schemas.
Definition table_metadata.h:99
std::vector< std::shared_ptr< struct StatisticsFile > > statistics
A list of table statistics.
Definition table_metadata.h:127
int8_t format_version
An integer version number for the format.
Definition table_metadata.h:87
int32_t current_schema_id
ID of the table's current schema.
Definition table_metadata.h:101
std::string table_uuid
A UUID that identifies the table.
Definition table_metadata.h:89
std::vector< EncryptedKey > encryption_keys
A list of encrypted table key entries.
Definition table_metadata.h:133