28#include <unordered_map>
32#include "iceberg/iceberg_export.h"
33#include "iceberg/table_properties.h"
37#include "iceberg/util/timepoint.h"
49 return lhs.timestamp_ms == rhs.timestamp_ms && lhs.snapshot_id == rhs.snapshot_id;
61 return lhs.timestamp_ms == rhs.timestamp_ms && lhs.metadata_file == rhs.metadata_file;
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;
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},
99 std::vector<std::shared_ptr<iceberg::Schema>>
schemas;
113 std::vector<std::shared_ptr<iceberg::Snapshot>>
snapshots;
125 std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>
refs;
127 std::vector<std::shared_ptr<struct StatisticsFile>>
statistics;
135 static Result<std::unique_ptr<TableMetadata>> Make(
138 const std::unordered_map<std::string, std::string>& properties,
139 int format_version = kDefaultTableFormatVersion);
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;
169 ICEBERG_EXPORT
friend bool operator==(
const TableMetadata& lhs,
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>;
189 Result<SchemasMapRef> GetSchemasById()
const;
190 Result<PartitionSpecsMapRef> GetPartitionSpecsById()
const;
191 Result<SortOrdersMapRef> GetSortOrdersById()
const;
192 Result<SnapshotsMapRef> GetSnapshotsById()
const;
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);
201 Lazy<InitSchemasMap> schemas_map_;
202 Lazy<InitPartitionSpecsMap> partition_specs_map_;
203 Lazy<InitSortOrdersMap> sort_orders_map_;
204 Lazy<InitSnapshotMap> snapshot_map_;
232 static std::unique_ptr<TableMetadataBuilder> BuildFromEmpty(
233 int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
240 static std::unique_ptr<TableMetadataBuilder> BuildFrom(
const TableMetadata* base);
259 std::string_view previous_metadata_location);
285 int32_t new_last_column_id);
366 const std::string& branch);
386 const std::vector<std::shared_ptr<Snapshot>>& snapshots_to_remove);
420 const std::shared_ptr<PartitionStatisticsFile>& partition_statistics_file);
433 const std::unordered_map<std::string, std::string>& updated);
462 Result<std::unique_ptr<TableMetadata>> Build();
465 const std::vector<std::unique_ptr<TableUpdate>>& changes()
const;
497enum class ICEBERG_EXPORT MetadataFileCodecType {
509 static Result<MetadataFileCodecType> FromString(std::string_view name);
515 static Result<MetadataFileCodecType> FromFileName(std::string_view file_name);
520 static Result<std::string> NameToFileExtension(std::string_view codec);
525 static std::string TypeToFileExtension(MetadataFileCodecType codec);
527 static constexpr std::string_view kTableMetadataFileSuffix =
".metadata.json";
528 static constexpr std::string_view kCompGzipTableMetadataFileSuffix =
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";
542 static Result<std::unique_ptr<TableMetadata>> Read(
543 class FileIO& io,
const std::string& location,
544 std::optional<size_t> length = std::nullopt);
557 const std::string& base_metadata_location,
577 static Status Write(
FileIO& io,
const std::string& location,
590 static int32_t ParseVersionFromLocation(std::string_view metadata_location);
597 static Result<std::string> NewTableMetadataFilePath(
const TableMetadata& metadata,
605struct hash<iceberg::MetadataLogEntry> {
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
Table properties for Iceberg tables.
Definition table_properties.h:37
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
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