28#include <unordered_map>
29#include <unordered_set>
33#include "iceberg/iceberg_export.h"
34#include "iceberg/snapshot.h"
45 enum class Kind : uint8_t {
47 kUpgradeFormatVersion,
51 kSetDefaultPartitionSpec,
52 kRemovePartitionSpecs,
65 kSetPartitionStatistics,
66 kRemovePartitionStatistics,
74 virtual Kind
kind()
const = 0;
103 virtual std::unique_ptr<TableUpdate>
Clone()
const = 0;
107 return lhs.Equals(rhs);
118 const std::string&
uuid()
const {
return uuid_; }
124 Kind
kind()
const override {
return Kind::kAssignUUID; }
126 bool Equals(
const TableUpdate& other)
const override;
128 std::unique_ptr<TableUpdate> Clone()
const override;
138 : format_version_(format_version) {}
140 int8_t format_version()
const {
return format_version_; }
146 Kind
kind()
const override {
return Kind::kUpgradeFormatVersion; }
148 bool Equals(
const TableUpdate& other)
const override;
150 std::unique_ptr<TableUpdate> Clone()
const override;
153 int8_t format_version_;
159 explicit AddSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
160 : schema_(std::move(schema)), last_column_id_(last_column_id) {}
162 const std::shared_ptr<Schema>& schema()
const {
return schema_; }
164 int32_t last_column_id()
const {
return last_column_id_; }
170 Kind
kind()
const override {
return Kind::kAddSchema; }
172 bool Equals(
const TableUpdate& other)
const override;
174 std::unique_ptr<TableUpdate> Clone()
const override;
177 std::shared_ptr<Schema> schema_;
178 int32_t last_column_id_;
186 int32_t schema_id()
const {
return schema_id_; }
192 Kind
kind()
const override {
return Kind::kSetCurrentSchema; }
194 bool Equals(
const TableUpdate& other)
const override;
196 std::unique_ptr<TableUpdate> Clone()
const override;
206 : spec_(std::move(spec)) {}
208 const std::shared_ptr<PartitionSpec>& spec()
const {
return spec_; }
214 Kind
kind()
const override {
return Kind::kAddPartitionSpec; }
216 bool Equals(
const TableUpdate& other)
const override;
218 std::unique_ptr<TableUpdate> Clone()
const override;
221 std::shared_ptr<PartitionSpec> spec_;
229 int32_t spec_id()
const {
return spec_id_; }
235 Kind
kind()
const override {
return Kind::kSetDefaultPartitionSpec; }
237 bool Equals(
const TableUpdate& other)
const override;
239 std::unique_ptr<TableUpdate> Clone()
const override;
249 : spec_ids_(std::move(spec_ids)) {}
251 const std::vector<int32_t>& spec_ids()
const {
return spec_ids_; }
257 Kind
kind()
const override {
return Kind::kRemovePartitionSpecs; }
259 bool Equals(
const TableUpdate& other)
const override;
261 std::unique_ptr<TableUpdate> Clone()
const override;
264 std::vector<int32_t> spec_ids_;
270 explicit RemoveSchemas(std::unordered_set<int32_t> schema_ids)
271 : schema_ids_(std::move(schema_ids)) {}
273 const std::unordered_set<int32_t>& schema_ids()
const {
return schema_ids_; }
279 Kind
kind()
const override {
return Kind::kRemoveSchemas; }
281 bool Equals(
const TableUpdate& other)
const override;
283 std::unique_ptr<TableUpdate> Clone()
const override;
286 std::unordered_set<int32_t> schema_ids_;
292 explicit AddSortOrder(std::shared_ptr<SortOrder> sort_order)
293 : sort_order_(std::move(sort_order)) {}
295 const std::shared_ptr<SortOrder>& sort_order()
const {
return sort_order_; }
301 Kind
kind()
const override {
return Kind::kAddSortOrder; }
303 bool Equals(
const TableUpdate& other)
const override;
305 std::unique_ptr<TableUpdate> Clone()
const override;
308 std::shared_ptr<SortOrder> sort_order_;
316 int32_t sort_order_id()
const {
return sort_order_id_; }
322 Kind
kind()
const override {
return Kind::kSetDefaultSortOrder; }
324 bool Equals(
const TableUpdate& other)
const override;
326 std::unique_ptr<TableUpdate> Clone()
const override;
329 int32_t sort_order_id_;
335 explicit AddSnapshot(std::shared_ptr<Snapshot> snapshot)
336 : snapshot_(std::move(snapshot)) {}
338 const std::shared_ptr<Snapshot>& snapshot()
const {
return snapshot_; }
344 Kind
kind()
const override {
return Kind::kAddSnapshot; }
346 bool Equals(
const TableUpdate& other)
const override;
348 std::unique_ptr<TableUpdate> Clone()
const override;
351 std::shared_ptr<Snapshot> snapshot_;
358 : snapshot_ids_(std::move(snapshot_ids)) {}
360 const std::vector<int64_t>& snapshot_ids()
const {
return snapshot_ids_; }
366 Kind
kind()
const override {
return Kind::kRemoveSnapshots; }
368 bool Equals(
const TableUpdate& other)
const override;
370 std::unique_ptr<TableUpdate> Clone()
const override;
373 std::vector<int64_t> snapshot_ids_;
379 explicit RemoveSnapshotRef(std::string ref_name) : ref_name_(std::move(ref_name)) {}
381 const std::string& ref_name()
const {
return ref_name_; }
387 Kind
kind()
const override {
return Kind::kRemoveSnapshotRef; }
389 bool Equals(
const TableUpdate& other)
const override;
391 std::unique_ptr<TableUpdate> Clone()
const override;
394 std::string ref_name_;
400 SetSnapshotRef(std::string ref_name, int64_t snapshot_id, SnapshotRefType type,
401 std::optional<int32_t> min_snapshots_to_keep = std::nullopt,
402 std::optional<int64_t> max_snapshot_age_ms = std::nullopt,
403 std::optional<int64_t> max_ref_age_ms = std::nullopt)
404 : ref_name_(std::move(ref_name)),
405 snapshot_id_(snapshot_id),
407 min_snapshots_to_keep_(min_snapshots_to_keep),
408 max_snapshot_age_ms_(max_snapshot_age_ms),
409 max_ref_age_ms_(max_ref_age_ms) {}
412 : ref_name_(std::move(ref_name)), snapshot_id_(ref.snapshot_id), type_(ref.type()) {
413 if (type_ == SnapshotRefType::kBranch) {
414 const auto& branch = std::get<SnapshotRef::Branch>(ref.retention);
415 min_snapshots_to_keep_ = branch.min_snapshots_to_keep;
416 max_snapshot_age_ms_ = branch.max_snapshot_age_ms;
417 max_ref_age_ms_ = branch.max_ref_age_ms;
419 const auto& tag = std::get<SnapshotRef::Tag>(ref.retention);
420 max_ref_age_ms_ = tag.max_ref_age_ms;
424 const std::string& ref_name()
const {
return ref_name_; }
425 int64_t snapshot_id()
const {
return snapshot_id_; }
426 SnapshotRefType type()
const {
return type_; }
427 const std::optional<int32_t>& min_snapshots_to_keep()
const {
428 return min_snapshots_to_keep_;
430 const std::optional<int64_t>& max_snapshot_age_ms()
const {
431 return max_snapshot_age_ms_;
433 const std::optional<int64_t>& max_ref_age_ms()
const {
return max_ref_age_ms_; }
439 Kind
kind()
const override {
return Kind::kSetSnapshotRef; }
441 bool Equals(
const TableUpdate& other)
const override;
443 std::unique_ptr<TableUpdate> Clone()
const override;
446 std::string ref_name_;
447 int64_t snapshot_id_;
448 SnapshotRefType type_;
449 std::optional<int32_t> min_snapshots_to_keep_;
450 std::optional<int64_t> max_snapshot_age_ms_;
451 std::optional<int64_t> max_ref_age_ms_;
457 explicit SetProperties(std::unordered_map<std::string, std::string> updated)
458 : updated_(std::move(updated)) {}
460 const std::unordered_map<std::string, std::string>& updated()
const {
return updated_; }
466 Kind
kind()
const override {
return Kind::kSetProperties; }
468 bool Equals(
const TableUpdate& other)
const override;
470 std::unique_ptr<TableUpdate> Clone()
const override;
473 std::unordered_map<std::string, std::string> updated_;
480 : removed_(std::move(removed)) {}
482 const std::unordered_set<std::string>& removed()
const {
return removed_; }
488 Kind
kind()
const override {
return Kind::kRemoveProperties; }
490 bool Equals(
const TableUpdate& other)
const override;
492 std::unique_ptr<TableUpdate> Clone()
const override;
495 std::unordered_set<std::string> removed_;
501 explicit SetLocation(std::string location) : location_(std::move(location)) {}
503 const std::string& location()
const {
return location_; }
509 Kind
kind()
const override {
return Kind::kSetLocation; }
511 bool Equals(
const TableUpdate& other)
const override;
513 std::unique_ptr<TableUpdate> Clone()
const override;
516 std::string location_;
522 explicit SetStatistics(std::shared_ptr<StatisticsFile> statistics_file)
523 : statistics_file_(std::move(statistics_file)) {}
525 int64_t snapshot_id()
const;
527 const std::shared_ptr<StatisticsFile>& statistics_file()
const {
528 return statistics_file_;
535 Kind
kind()
const override {
return Kind::kSetStatistics; }
537 bool Equals(
const TableUpdate& other)
const override;
539 std::unique_ptr<TableUpdate> Clone()
const override;
542 std::shared_ptr<StatisticsFile> statistics_file_;
548 explicit RemoveStatistics(int64_t snapshot_id) : snapshot_id_(snapshot_id) {}
550 int64_t snapshot_id()
const {
return snapshot_id_; }
556 Kind
kind()
const override {
return Kind::kRemoveStatistics; }
558 bool Equals(
const TableUpdate& other)
const override;
560 std::unique_ptr<TableUpdate> Clone()
const override;
563 int64_t snapshot_id_;
570 std::shared_ptr<PartitionStatisticsFile> partition_statistics_file)
571 : partition_statistics_file_(std::move(partition_statistics_file)) {}
573 int64_t snapshot_id()
const;
575 const std::shared_ptr<PartitionStatisticsFile>& partition_statistics_file()
const {
576 return partition_statistics_file_;
583 Kind
kind()
const override {
return Kind::kSetPartitionStatistics; }
585 bool Equals(
const TableUpdate& other)
const override;
587 std::unique_ptr<TableUpdate> Clone()
const override;
590 std::shared_ptr<PartitionStatisticsFile> partition_statistics_file_;
598 int64_t snapshot_id()
const {
return snapshot_id_; }
604 Kind
kind()
const override {
return Kind::kRemovePartitionStatistics; }
606 bool Equals(
const TableUpdate& other)
const override;
608 std::unique_ptr<TableUpdate> Clone()
const override;
611 int64_t snapshot_id_;
625 Kind
kind()
const override {
return Kind::kAddEncryptionKey; }
627 bool Equals(
const TableUpdate& other)
const override;
629 std::unique_ptr<TableUpdate> Clone()
const override;
640 const std::string& key_id()
const {
return key_id_; }
646 Kind
kind()
const override {
return Kind::kRemoveEncryptionKey; }
648 bool Equals(
const TableUpdate& other)
const override;
650 std::unique_ptr<TableUpdate> Clone()
const override;
Context for generating table requirements.
Definition table_requirements.h:45
Base class for metadata update operations.
Definition table_update.h:43
virtual Kind kind() const =0
Return the kind of this update.
friend bool operator==(const TableUpdate &lhs, const TableUpdate &rhs)
Compare two TableUpdate instances for equality.
Definition table_update.h:106
virtual void GenerateRequirements(TableUpdateContext &context) const =0
Generate update requirements for this metadata update.
virtual void ApplyTo(TableMetadataBuilder &builder) const =0
Apply this update to a TableMetadataBuilder.
virtual bool Equals(const TableUpdate &other) const =0
Check equality with another TableUpdate.
virtual std::unique_ptr< TableUpdate > Clone() const =0
Create a deep copy of this update.
Represents adding an encryption key to the table.
Definition table_update.h:615
Kind kind() const override
Return the kind of this update.
Definition table_update.h:625
Represents adding a new partition spec to the table.
Definition table_update.h:203
Kind kind() const override
Return the kind of this update.
Definition table_update.h:214
Represents adding a new schema to the table.
Definition table_update.h:157
Kind kind() const override
Return the kind of this update.
Definition table_update.h:170
Represents adding a snapshot to the table.
Definition table_update.h:333
Kind kind() const override
Return the kind of this update.
Definition table_update.h:344
Represents adding a new sort order to the table.
Definition table_update.h:290
Kind kind() const override
Return the kind of this update.
Definition table_update.h:301
Represents an assignment of a UUID to the table.
Definition table_update.h:114
Kind kind() const override
Return the kind of this update.
Definition table_update.h:124
Represents removing an encryption key from the table.
Definition table_update.h:636
Kind kind() const override
Return the kind of this update.
Definition table_update.h:646
Represents removing partition specs from the table.
Definition table_update.h:246
Kind kind() const override
Return the kind of this update.
Definition table_update.h:257
Represents removing partition statistics for a snapshot.
Definition table_update.h:594
Kind kind() const override
Return the kind of this update.
Definition table_update.h:604
Represents removing table properties.
Definition table_update.h:477
Kind kind() const override
Return the kind of this update.
Definition table_update.h:488
Represents removing schemas from the table.
Definition table_update.h:268
Kind kind() const override
Return the kind of this update.
Definition table_update.h:279
Represents removing a snapshot reference.
Definition table_update.h:377
Kind kind() const override
Return the kind of this update.
Definition table_update.h:387
Represents removing snapshots from the table.
Definition table_update.h:355
Kind kind() const override
Return the kind of this update.
Definition table_update.h:366
Represents removing statistics for a snapshot.
Definition table_update.h:546
Kind kind() const override
Return the kind of this update.
Definition table_update.h:556
Represents setting the current schema.
Definition table_update.h:182
Kind kind() const override
Return the kind of this update.
Definition table_update.h:192
Represents setting the default partition spec.
Definition table_update.h:225
Kind kind() const override
Return the kind of this update.
Definition table_update.h:235
Represents setting the default sort order.
Definition table_update.h:312
Kind kind() const override
Return the kind of this update.
Definition table_update.h:322
Represents setting the table location.
Definition table_update.h:499
Kind kind() const override
Return the kind of this update.
Definition table_update.h:509
Represents setting partition statistics for a snapshot.
Definition table_update.h:567
Kind kind() const override
Return the kind of this update.
Definition table_update.h:583
Represents setting table properties.
Definition table_update.h:455
Kind kind() const override
Return the kind of this update.
Definition table_update.h:466
Represents setting a snapshot reference.
Definition table_update.h:398
Kind kind() const override
Return the kind of this update.
Definition table_update.h:439
Represents setting statistics for a snapshot.
Definition table_update.h:520
Kind kind() const override
Return the kind of this update.
Definition table_update.h:535
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
Represents an encrypted table key entry.
Definition encrypted_key.h:34
A reference to a snapshot, either a branch or a tag.
Definition snapshot.h:68