iceberg-cpp
Loading...
Searching...
No Matches
table_update.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 <optional>
27#include <string>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32#include "iceberg/iceberg_export.h"
33#include "iceberg/snapshot.h"
34#include "iceberg/type_fwd.h"
35
36namespace iceberg {
37
42class ICEBERG_EXPORT TableUpdate {
43 public:
44 enum class Kind : uint8_t {
45 kAssignUUID,
46 kUpgradeFormatVersion,
47 kAddSchema,
48 kSetCurrentSchema,
49 kAddPartitionSpec,
50 kSetDefaultPartitionSpec,
51 kRemovePartitionSpecs,
52 kRemoveSchemas,
53 kAddSortOrder,
54 kSetDefaultSortOrder,
55 kAddSnapshot,
56 kRemoveSnapshots,
57 kRemoveSnapshotRef,
58 kSetSnapshotRef,
59 kSetProperties,
60 kRemoveProperties,
61 kSetLocation,
62 kSetStatistics,
63 kRemoveStatistics,
64 kSetPartitionStatistics,
65 kRemovePartitionStatistics,
66 };
67
68 virtual ~TableUpdate();
69
71 virtual Kind kind() const = 0;
72
80 virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
81
89 virtual void GenerateRequirements(TableUpdateContext& context) const = 0;
90
95 virtual bool Equals(const TableUpdate& other) const = 0;
96
100 virtual std::unique_ptr<TableUpdate> Clone() const = 0;
101
103 friend bool operator==(const TableUpdate& lhs, const TableUpdate& rhs) {
104 return lhs.Equals(rhs);
105 }
106};
107
108namespace table {
109
111class ICEBERG_EXPORT AssignUUID : public TableUpdate {
112 public:
113 explicit AssignUUID(std::string uuid) : uuid_(std::move(uuid)) {}
114
115 const std::string& uuid() const { return uuid_; }
116
117 void ApplyTo(TableMetadataBuilder& builder) const override;
118
119 void GenerateRequirements(TableUpdateContext& context) const override;
120
121 Kind kind() const override { return Kind::kAssignUUID; }
122
123 bool Equals(const TableUpdate& other) const override;
124
125 std::unique_ptr<TableUpdate> Clone() const override;
126
127 private:
128 std::string uuid_;
129};
130
132class ICEBERG_EXPORT UpgradeFormatVersion : public TableUpdate {
133 public:
134 explicit UpgradeFormatVersion(int8_t format_version)
135 : format_version_(format_version) {}
136
137 int8_t format_version() const { return format_version_; }
138
139 void ApplyTo(TableMetadataBuilder& builder) const override;
140
141 void GenerateRequirements(TableUpdateContext& context) const override;
142
143 Kind kind() const override { return Kind::kUpgradeFormatVersion; }
144
145 bool Equals(const TableUpdate& other) const override;
146
147 std::unique_ptr<TableUpdate> Clone() const override;
148
149 private:
150 int8_t format_version_;
151};
152
154class ICEBERG_EXPORT AddSchema : public TableUpdate {
155 public:
156 explicit AddSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
157 : schema_(std::move(schema)), last_column_id_(last_column_id) {}
158
159 const std::shared_ptr<Schema>& schema() const { return schema_; }
160
161 int32_t last_column_id() const { return last_column_id_; }
162
163 void ApplyTo(TableMetadataBuilder& builder) const override;
164
165 void GenerateRequirements(TableUpdateContext& context) const override;
166
167 Kind kind() const override { return Kind::kAddSchema; }
168
169 bool Equals(const TableUpdate& other) const override;
170
171 std::unique_ptr<TableUpdate> Clone() const override;
172
173 private:
174 std::shared_ptr<Schema> schema_;
175 int32_t last_column_id_;
176};
177
179class ICEBERG_EXPORT SetCurrentSchema : public TableUpdate {
180 public:
181 explicit SetCurrentSchema(int32_t schema_id) : schema_id_(schema_id) {}
182
183 int32_t schema_id() const { return schema_id_; }
184
185 void ApplyTo(TableMetadataBuilder& builder) const override;
186
187 void GenerateRequirements(TableUpdateContext& context) const override;
188
189 Kind kind() const override { return Kind::kSetCurrentSchema; }
190
191 bool Equals(const TableUpdate& other) const override;
192
193 std::unique_ptr<TableUpdate> Clone() const override;
194
195 private:
196 int32_t schema_id_;
197};
198
200class ICEBERG_EXPORT AddPartitionSpec : public TableUpdate {
201 public:
202 explicit AddPartitionSpec(std::shared_ptr<PartitionSpec> spec)
203 : spec_(std::move(spec)) {}
204
205 const std::shared_ptr<PartitionSpec>& spec() const { return spec_; }
206
207 void ApplyTo(TableMetadataBuilder& builder) const override;
208
209 void GenerateRequirements(TableUpdateContext& context) const override;
210
211 Kind kind() const override { return Kind::kAddPartitionSpec; }
212
213 bool Equals(const TableUpdate& other) const override;
214
215 std::unique_ptr<TableUpdate> Clone() const override;
216
217 private:
218 std::shared_ptr<PartitionSpec> spec_;
219};
220
222class ICEBERG_EXPORT SetDefaultPartitionSpec : public TableUpdate {
223 public:
224 explicit SetDefaultPartitionSpec(int32_t spec_id) : spec_id_(spec_id) {}
225
226 int32_t spec_id() const { return spec_id_; }
227
228 void ApplyTo(TableMetadataBuilder& builder) const override;
229
230 void GenerateRequirements(TableUpdateContext& context) const override;
231
232 Kind kind() const override { return Kind::kSetDefaultPartitionSpec; }
233
234 bool Equals(const TableUpdate& other) const override;
235
236 std::unique_ptr<TableUpdate> Clone() const override;
237
238 private:
239 int32_t spec_id_;
240};
241
243class ICEBERG_EXPORT RemovePartitionSpecs : public TableUpdate {
244 public:
245 explicit RemovePartitionSpecs(std::vector<int32_t> spec_ids)
246 : spec_ids_(std::move(spec_ids)) {}
247
248 const std::vector<int32_t>& spec_ids() const { return spec_ids_; }
249
250 void ApplyTo(TableMetadataBuilder& builder) const override;
251
252 void GenerateRequirements(TableUpdateContext& context) const override;
253
254 Kind kind() const override { return Kind::kRemovePartitionSpecs; }
255
256 bool Equals(const TableUpdate& other) const override;
257
258 std::unique_ptr<TableUpdate> Clone() const override;
259
260 private:
261 std::vector<int32_t> spec_ids_;
262};
263
265class ICEBERG_EXPORT RemoveSchemas : public TableUpdate {
266 public:
267 explicit RemoveSchemas(std::unordered_set<int32_t> schema_ids)
268 : schema_ids_(std::move(schema_ids)) {}
269
270 const std::unordered_set<int32_t>& schema_ids() const { return schema_ids_; }
271
272 void ApplyTo(TableMetadataBuilder& builder) const override;
273
274 void GenerateRequirements(TableUpdateContext& context) const override;
275
276 Kind kind() const override { return Kind::kRemoveSchemas; }
277
278 bool Equals(const TableUpdate& other) const override;
279
280 std::unique_ptr<TableUpdate> Clone() const override;
281
282 private:
283 std::unordered_set<int32_t> schema_ids_;
284};
285
287class ICEBERG_EXPORT AddSortOrder : public TableUpdate {
288 public:
289 explicit AddSortOrder(std::shared_ptr<SortOrder> sort_order)
290 : sort_order_(std::move(sort_order)) {}
291
292 const std::shared_ptr<SortOrder>& sort_order() const { return sort_order_; }
293
294 void ApplyTo(TableMetadataBuilder& builder) const override;
295
296 void GenerateRequirements(TableUpdateContext& context) const override;
297
298 Kind kind() const override { return Kind::kAddSortOrder; }
299
300 bool Equals(const TableUpdate& other) const override;
301
302 std::unique_ptr<TableUpdate> Clone() const override;
303
304 private:
305 std::shared_ptr<SortOrder> sort_order_;
306};
307
309class ICEBERG_EXPORT SetDefaultSortOrder : public TableUpdate {
310 public:
311 explicit SetDefaultSortOrder(int32_t sort_order_id) : sort_order_id_(sort_order_id) {}
312
313 int32_t sort_order_id() const { return sort_order_id_; }
314
315 void ApplyTo(TableMetadataBuilder& builder) const override;
316
317 void GenerateRequirements(TableUpdateContext& context) const override;
318
319 Kind kind() const override { return Kind::kSetDefaultSortOrder; }
320
321 bool Equals(const TableUpdate& other) const override;
322
323 std::unique_ptr<TableUpdate> Clone() const override;
324
325 private:
326 int32_t sort_order_id_;
327};
328
330class ICEBERG_EXPORT AddSnapshot : public TableUpdate {
331 public:
332 explicit AddSnapshot(std::shared_ptr<Snapshot> snapshot)
333 : snapshot_(std::move(snapshot)) {}
334
335 const std::shared_ptr<Snapshot>& snapshot() const { return snapshot_; }
336
337 void ApplyTo(TableMetadataBuilder& builder) const override;
338
339 void GenerateRequirements(TableUpdateContext& context) const override;
340
341 Kind kind() const override { return Kind::kAddSnapshot; }
342
343 bool Equals(const TableUpdate& other) const override;
344
345 std::unique_ptr<TableUpdate> Clone() const override;
346
347 private:
348 std::shared_ptr<Snapshot> snapshot_;
349};
350
352class ICEBERG_EXPORT RemoveSnapshots : public TableUpdate {
353 public:
354 explicit RemoveSnapshots(std::vector<int64_t> snapshot_ids)
355 : snapshot_ids_(std::move(snapshot_ids)) {}
356
357 const std::vector<int64_t>& snapshot_ids() const { return snapshot_ids_; }
358
359 void ApplyTo(TableMetadataBuilder& builder) const override;
360
361 void GenerateRequirements(TableUpdateContext& context) const override;
362
363 Kind kind() const override { return Kind::kRemoveSnapshots; }
364
365 bool Equals(const TableUpdate& other) const override;
366
367 std::unique_ptr<TableUpdate> Clone() const override;
368
369 private:
370 std::vector<int64_t> snapshot_ids_;
371};
372
374class ICEBERG_EXPORT RemoveSnapshotRef : public TableUpdate {
375 public:
376 explicit RemoveSnapshotRef(std::string ref_name) : ref_name_(std::move(ref_name)) {}
377
378 const std::string& ref_name() const { return ref_name_; }
379
380 void ApplyTo(TableMetadataBuilder& builder) const override;
381
382 void GenerateRequirements(TableUpdateContext& context) const override;
383
384 Kind kind() const override { return Kind::kRemoveSnapshotRef; }
385
386 bool Equals(const TableUpdate& other) const override;
387
388 std::unique_ptr<TableUpdate> Clone() const override;
389
390 private:
391 std::string ref_name_;
392};
393
395class ICEBERG_EXPORT SetSnapshotRef : public TableUpdate {
396 public:
397 SetSnapshotRef(std::string ref_name, int64_t snapshot_id, SnapshotRefType type,
398 std::optional<int32_t> min_snapshots_to_keep = std::nullopt,
399 std::optional<int64_t> max_snapshot_age_ms = std::nullopt,
400 std::optional<int64_t> max_ref_age_ms = std::nullopt)
401 : ref_name_(std::move(ref_name)),
402 snapshot_id_(snapshot_id),
403 type_(type),
404 min_snapshots_to_keep_(min_snapshots_to_keep),
405 max_snapshot_age_ms_(max_snapshot_age_ms),
406 max_ref_age_ms_(max_ref_age_ms) {}
407
408 SetSnapshotRef(std::string ref_name, const SnapshotRef& ref)
409 : ref_name_(std::move(ref_name)), snapshot_id_(ref.snapshot_id), type_(ref.type()) {
410 if (type_ == SnapshotRefType::kBranch) {
411 const auto& branch = std::get<SnapshotRef::Branch>(ref.retention);
412 min_snapshots_to_keep_ = branch.min_snapshots_to_keep;
413 max_snapshot_age_ms_ = branch.max_snapshot_age_ms;
414 max_ref_age_ms_ = branch.max_ref_age_ms;
415 } else {
416 const auto& tag = std::get<SnapshotRef::Tag>(ref.retention);
417 max_ref_age_ms_ = tag.max_ref_age_ms;
418 }
419 }
420
421 const std::string& ref_name() const { return ref_name_; }
422 int64_t snapshot_id() const { return snapshot_id_; }
423 SnapshotRefType type() const { return type_; }
424 const std::optional<int32_t>& min_snapshots_to_keep() const {
425 return min_snapshots_to_keep_;
426 }
427 const std::optional<int64_t>& max_snapshot_age_ms() const {
428 return max_snapshot_age_ms_;
429 }
430 const std::optional<int64_t>& max_ref_age_ms() const { return max_ref_age_ms_; }
431
432 void ApplyTo(TableMetadataBuilder& builder) const override;
433
434 void GenerateRequirements(TableUpdateContext& context) const override;
435
436 Kind kind() const override { return Kind::kSetSnapshotRef; }
437
438 bool Equals(const TableUpdate& other) const override;
439
440 std::unique_ptr<TableUpdate> Clone() const override;
441
442 private:
443 std::string ref_name_;
444 int64_t snapshot_id_;
445 SnapshotRefType type_;
446 std::optional<int32_t> min_snapshots_to_keep_;
447 std::optional<int64_t> max_snapshot_age_ms_;
448 std::optional<int64_t> max_ref_age_ms_;
449};
450
452class ICEBERG_EXPORT SetProperties : public TableUpdate {
453 public:
454 explicit SetProperties(std::unordered_map<std::string, std::string> updated)
455 : updated_(std::move(updated)) {}
456
457 const std::unordered_map<std::string, std::string>& updated() const { return updated_; }
458
459 void ApplyTo(TableMetadataBuilder& builder) const override;
460
461 void GenerateRequirements(TableUpdateContext& context) const override;
462
463 Kind kind() const override { return Kind::kSetProperties; }
464
465 bool Equals(const TableUpdate& other) const override;
466
467 std::unique_ptr<TableUpdate> Clone() const override;
468
469 private:
470 std::unordered_map<std::string, std::string> updated_;
471};
472
474class ICEBERG_EXPORT RemoveProperties : public TableUpdate {
475 public:
476 explicit RemoveProperties(std::unordered_set<std::string> removed)
477 : removed_(std::move(removed)) {}
478
479 const std::unordered_set<std::string>& removed() const { return removed_; }
480
481 void ApplyTo(TableMetadataBuilder& builder) const override;
482
483 void GenerateRequirements(TableUpdateContext& context) const override;
484
485 Kind kind() const override { return Kind::kRemoveProperties; }
486
487 bool Equals(const TableUpdate& other) const override;
488
489 std::unique_ptr<TableUpdate> Clone() const override;
490
491 private:
492 std::unordered_set<std::string> removed_;
493};
494
496class ICEBERG_EXPORT SetLocation : public TableUpdate {
497 public:
498 explicit SetLocation(std::string location) : location_(std::move(location)) {}
499
500 const std::string& location() const { return location_; }
501
502 void ApplyTo(TableMetadataBuilder& builder) const override;
503
504 void GenerateRequirements(TableUpdateContext& context) const override;
505
506 Kind kind() const override { return Kind::kSetLocation; }
507
508 bool Equals(const TableUpdate& other) const override;
509
510 std::unique_ptr<TableUpdate> Clone() const override;
511
512 private:
513 std::string location_;
514};
515
517class ICEBERG_EXPORT SetStatistics : public TableUpdate {
518 public:
519 explicit SetStatistics(std::shared_ptr<StatisticsFile> statistics_file)
520 : statistics_file_(std::move(statistics_file)) {}
521
522 int64_t snapshot_id() const;
523
524 const std::shared_ptr<StatisticsFile>& statistics_file() const {
525 return statistics_file_;
526 }
527
528 void ApplyTo(TableMetadataBuilder& builder) const override;
529
530 void GenerateRequirements(TableUpdateContext& context) const override;
531
532 Kind kind() const override { return Kind::kSetStatistics; }
533
534 bool Equals(const TableUpdate& other) const override;
535
536 std::unique_ptr<TableUpdate> Clone() const override;
537
538 private:
539 std::shared_ptr<StatisticsFile> statistics_file_;
540};
541
543class ICEBERG_EXPORT RemoveStatistics : public TableUpdate {
544 public:
545 explicit RemoveStatistics(int64_t snapshot_id) : snapshot_id_(snapshot_id) {}
546
547 int64_t snapshot_id() const { return snapshot_id_; }
548
549 void ApplyTo(TableMetadataBuilder& builder) const override;
550
551 void GenerateRequirements(TableUpdateContext& context) const override;
552
553 Kind kind() const override { return Kind::kRemoveStatistics; }
554
555 bool Equals(const TableUpdate& other) const override;
556
557 std::unique_ptr<TableUpdate> Clone() const override;
558
559 private:
560 int64_t snapshot_id_;
561};
562
564class ICEBERG_EXPORT SetPartitionStatistics : public TableUpdate {
565 public:
566 explicit SetPartitionStatistics(
567 std::shared_ptr<PartitionStatisticsFile> partition_statistics_file)
568 : partition_statistics_file_(std::move(partition_statistics_file)) {}
569
570 int64_t snapshot_id() const;
571
572 const std::shared_ptr<PartitionStatisticsFile>& partition_statistics_file() const {
573 return partition_statistics_file_;
574 }
575
576 void ApplyTo(TableMetadataBuilder& builder) const override;
577
578 void GenerateRequirements(TableUpdateContext& context) const override;
579
580 Kind kind() const override { return Kind::kSetPartitionStatistics; }
581
582 bool Equals(const TableUpdate& other) const override;
583
584 std::unique_ptr<TableUpdate> Clone() const override;
585
586 private:
587 std::shared_ptr<PartitionStatisticsFile> partition_statistics_file_;
588};
589
591class ICEBERG_EXPORT RemovePartitionStatistics : public TableUpdate {
592 public:
593 explicit RemovePartitionStatistics(int64_t snapshot_id) : snapshot_id_(snapshot_id) {}
594
595 int64_t snapshot_id() const { return snapshot_id_; }
596
597 void ApplyTo(TableMetadataBuilder& builder) const override;
598
599 void GenerateRequirements(TableUpdateContext& context) const override;
600
601 Kind kind() const override { return Kind::kRemovePartitionStatistics; }
602
603 bool Equals(const TableUpdate& other) const override;
604
605 std::unique_ptr<TableUpdate> Clone() const override;
606
607 private:
608 int64_t snapshot_id_;
609};
610
611} // namespace table
612
613} // namespace iceberg
Builder class for constructing TableMetadata objects.
Definition table_metadata.h:220
Context for generating table requirements.
Definition table_requirements.h:45
Base class for metadata update operations.
Definition table_update.h:42
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:103
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 a new partition spec to the table.
Definition table_update.h:200
Kind kind() const override
Return the kind of this update.
Definition table_update.h:211
Represents adding a new schema to the table.
Definition table_update.h:154
Kind kind() const override
Return the kind of this update.
Definition table_update.h:167
Represents adding a snapshot to the table.
Definition table_update.h:330
Kind kind() const override
Return the kind of this update.
Definition table_update.h:341
Represents adding a new sort order to the table.
Definition table_update.h:287
Kind kind() const override
Return the kind of this update.
Definition table_update.h:298
Represents an assignment of a UUID to the table.
Definition table_update.h:111
Kind kind() const override
Return the kind of this update.
Definition table_update.h:121
Represents removing partition specs from the table.
Definition table_update.h:243
Kind kind() const override
Return the kind of this update.
Definition table_update.h:254
Represents removing partition statistics for a snapshot.
Definition table_update.h:591
Kind kind() const override
Return the kind of this update.
Definition table_update.h:601
Represents removing table properties.
Definition table_update.h:474
Kind kind() const override
Return the kind of this update.
Definition table_update.h:485
Represents removing schemas from the table.
Definition table_update.h:265
Kind kind() const override
Return the kind of this update.
Definition table_update.h:276
Represents removing a snapshot reference.
Definition table_update.h:374
Kind kind() const override
Return the kind of this update.
Definition table_update.h:384
Represents removing snapshots from the table.
Definition table_update.h:352
Kind kind() const override
Return the kind of this update.
Definition table_update.h:363
Represents removing statistics for a snapshot.
Definition table_update.h:543
Kind kind() const override
Return the kind of this update.
Definition table_update.h:553
Represents setting the current schema.
Definition table_update.h:179
Kind kind() const override
Return the kind of this update.
Definition table_update.h:189
Represents setting the default partition spec.
Definition table_update.h:222
Kind kind() const override
Return the kind of this update.
Definition table_update.h:232
Represents setting the default sort order.
Definition table_update.h:309
Kind kind() const override
Return the kind of this update.
Definition table_update.h:319
Represents setting the table location.
Definition table_update.h:496
Kind kind() const override
Return the kind of this update.
Definition table_update.h:506
Represents setting partition statistics for a snapshot.
Definition table_update.h:564
Kind kind() const override
Return the kind of this update.
Definition table_update.h:580
Represents setting table properties.
Definition table_update.h:452
Kind kind() const override
Return the kind of this update.
Definition table_update.h:463
Represents setting a snapshot reference.
Definition table_update.h:395
Kind kind() const override
Return the kind of this update.
Definition table_update.h:436
Represents setting statistics for a snapshot.
Definition table_update.h:517
Kind kind() const override
Return the kind of this update.
Definition table_update.h:532
Represents an upgrade of the table format version.
Definition table_update.h:132
Kind kind() const override
Return the kind of this update.
Definition table_update.h:143
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
A reference to a snapshot, either a branch or a tag.
Definition snapshot.h:68