iceberg-cpp
Loading...
Searching...
No Matches
table_requirement.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
28
29#include <optional>
30#include <string>
31
32#include "iceberg/iceberg_export.h"
33#include "iceberg/result.h"
34#include "iceberg/type_fwd.h"
36
37namespace iceberg {
38
44class ICEBERG_EXPORT TableRequirement {
45 public:
46 enum class Kind : uint8_t {
47 kAssertDoesNotExist,
48 kAssertUUID,
49 kAssertRefSnapshotID,
50 kAssertLastAssignedFieldId,
51 kAssertCurrentSchemaID,
52 kAssertLastAssignedPartitionId,
53 kAssertDefaultSpecID,
54 kAssertDefaultSortOrderID,
55 };
56
57 virtual ~TableRequirement() = default;
58
60 virtual Kind kind() const = 0;
61
66 virtual Status Validate(const TableMetadata* base) const = 0;
67
72 virtual bool Equals(const TableRequirement& other) const = 0;
73
77 virtual std::unique_ptr<TableRequirement> Clone() const = 0;
78
80 friend bool operator==(const TableRequirement& lhs, const TableRequirement& rhs) {
81 return lhs.Equals(rhs);
82 }
83};
84
85namespace table {
86
91class ICEBERG_EXPORT AssertDoesNotExist : public TableRequirement {
92 public:
93 AssertDoesNotExist() = default;
94
95 Kind kind() const override { return Kind::kAssertDoesNotExist; }
96
97 Status Validate(const TableMetadata* base) const override;
98
99 bool Equals(const TableRequirement& other) const override {
100 return other.kind() == Kind::kAssertDoesNotExist;
101 }
102
103 std::unique_ptr<TableRequirement> Clone() const override {
104 return std::make_unique<AssertDoesNotExist>();
105 }
106};
107
112class ICEBERG_EXPORT AssertUUID : public TableRequirement {
113 public:
114 explicit AssertUUID(std::string uuid) : uuid_(std::move(uuid)) {}
115
116 const std::string& uuid() const { return uuid_; }
117
118 Kind kind() const override { return Kind::kAssertUUID; }
119
120 Status Validate(const TableMetadata* base) const override;
121
122 bool Equals(const TableRequirement& other) const override {
123 if (other.kind() != Kind::kAssertUUID) {
124 return false;
125 }
126 const auto& other_uuid = internal::checked_cast<const AssertUUID&>(other);
127 return uuid_ == other_uuid.uuid_;
128 }
129
130 std::unique_ptr<TableRequirement> Clone() const override {
131 return std::make_unique<AssertUUID>(uuid_);
132 }
133
134 private:
135 std::string uuid_;
136};
137
143class ICEBERG_EXPORT AssertRefSnapshotID : public TableRequirement {
144 public:
145 AssertRefSnapshotID(std::string ref_name, std::optional<int64_t> snapshot_id)
146 : ref_name_(std::move(ref_name)), snapshot_id_(snapshot_id) {}
147
148 const std::string& ref_name() const { return ref_name_; }
149
150 const std::optional<int64_t>& snapshot_id() const { return snapshot_id_; }
151
152 Kind kind() const override { return Kind::kAssertRefSnapshotID; }
153
154 Status Validate(const TableMetadata* base) const override;
155
156 bool Equals(const TableRequirement& other) const override {
157 if (other.kind() != Kind::kAssertRefSnapshotID) {
158 return false;
159 }
160 const auto& other_ref = internal::checked_cast<const AssertRefSnapshotID&>(other);
161 return ref_name_ == other_ref.ref_name_ && snapshot_id_ == other_ref.snapshot_id_;
162 }
163
164 std::unique_ptr<TableRequirement> Clone() const override {
165 return std::make_unique<AssertRefSnapshotID>(ref_name_, snapshot_id_);
166 }
167
168 private:
169 std::string ref_name_;
170 std::optional<int64_t> snapshot_id_;
171};
172
177class ICEBERG_EXPORT AssertLastAssignedFieldId : public TableRequirement {
178 public:
179 explicit AssertLastAssignedFieldId(int32_t last_assigned_field_id)
180 : last_assigned_field_id_(last_assigned_field_id) {}
181
182 int32_t last_assigned_field_id() const { return last_assigned_field_id_; }
183
184 Kind kind() const override { return Kind::kAssertLastAssignedFieldId; }
185
186 Status Validate(const TableMetadata* base) const override;
187
188 bool Equals(const TableRequirement& other) const override {
189 if (other.kind() != Kind::kAssertLastAssignedFieldId) {
190 return false;
191 }
192 const auto& other_field =
193 internal::checked_cast<const AssertLastAssignedFieldId&>(other);
194 return last_assigned_field_id_ == other_field.last_assigned_field_id_;
195 }
196
197 std::unique_ptr<TableRequirement> Clone() const override {
198 return std::make_unique<AssertLastAssignedFieldId>(last_assigned_field_id_);
199 }
200
201 private:
202 int32_t last_assigned_field_id_;
203};
204
209class ICEBERG_EXPORT AssertCurrentSchemaID : public TableRequirement {
210 public:
211 explicit AssertCurrentSchemaID(int32_t schema_id) : schema_id_(schema_id) {}
212
213 int32_t schema_id() const { return schema_id_; }
214
215 Kind kind() const override { return Kind::kAssertCurrentSchemaID; }
216
217 Status Validate(const TableMetadata* base) const override;
218
219 bool Equals(const TableRequirement& other) const override {
220 if (other.kind() != Kind::kAssertCurrentSchemaID) {
221 return false;
222 }
223 const auto& other_schema =
224 internal::checked_cast<const AssertCurrentSchemaID&>(other);
225 return schema_id_ == other_schema.schema_id_;
226 }
227
228 std::unique_ptr<TableRequirement> Clone() const override {
229 return std::make_unique<AssertCurrentSchemaID>(schema_id_);
230 }
231
232 private:
233 int32_t schema_id_;
234};
235
240class ICEBERG_EXPORT AssertLastAssignedPartitionId : public TableRequirement {
241 public:
242 explicit AssertLastAssignedPartitionId(int32_t last_assigned_partition_id)
243 : last_assigned_partition_id_(last_assigned_partition_id) {}
244
245 int32_t last_assigned_partition_id() const { return last_assigned_partition_id_; }
246
247 Kind kind() const override { return Kind::kAssertLastAssignedPartitionId; }
248
249 Status Validate(const TableMetadata* base) const override;
250
251 bool Equals(const TableRequirement& other) const override {
252 if (other.kind() != Kind::kAssertLastAssignedPartitionId) {
253 return false;
254 }
255 const auto& other_partition =
256 internal::checked_cast<const AssertLastAssignedPartitionId&>(other);
257 return last_assigned_partition_id_ == other_partition.last_assigned_partition_id_;
258 }
259
260 std::unique_ptr<TableRequirement> Clone() const override {
261 return std::make_unique<AssertLastAssignedPartitionId>(last_assigned_partition_id_);
262 }
263
264 private:
265 int32_t last_assigned_partition_id_;
266};
267
272class ICEBERG_EXPORT AssertDefaultSpecID : public TableRequirement {
273 public:
274 explicit AssertDefaultSpecID(int32_t spec_id) : spec_id_(spec_id) {}
275
276 int32_t spec_id() const { return spec_id_; }
277
278 Kind kind() const override { return Kind::kAssertDefaultSpecID; }
279
280 Status Validate(const TableMetadata* base) const override;
281
282 bool Equals(const TableRequirement& other) const override {
283 if (other.kind() != Kind::kAssertDefaultSpecID) {
284 return false;
285 }
286 const auto& other_spec = internal::checked_cast<const AssertDefaultSpecID&>(other);
287 return spec_id_ == other_spec.spec_id_;
288 }
289
290 std::unique_ptr<TableRequirement> Clone() const override {
291 return std::make_unique<AssertDefaultSpecID>(spec_id_);
292 }
293
294 private:
295 int32_t spec_id_;
296};
297
302class ICEBERG_EXPORT AssertDefaultSortOrderID : public TableRequirement {
303 public:
304 explicit AssertDefaultSortOrderID(int32_t sort_order_id)
305 : sort_order_id_(sort_order_id) {}
306
307 int32_t sort_order_id() const { return sort_order_id_; }
308
309 Kind kind() const override { return Kind::kAssertDefaultSortOrderID; }
310
311 Status Validate(const TableMetadata* base) const override;
312
313 bool Equals(const TableRequirement& other) const override {
314 if (other.kind() != Kind::kAssertDefaultSortOrderID) {
315 return false;
316 }
317 const auto& other_sort =
318 internal::checked_cast<const AssertDefaultSortOrderID&>(other);
319 return sort_order_id_ == other_sort.sort_order_id_;
320 }
321
322 std::unique_ptr<TableRequirement> Clone() const override {
323 return std::make_unique<AssertDefaultSortOrderID>(sort_order_id_);
324 }
325
326 private:
327 int32_t sort_order_id_;
328};
329
330} // namespace table
331
332} // namespace iceberg
Checked cast functions for dynamic_cast and static_cast. Adapted from Apache Arrow https://github....
Base class for update requirement operations.
Definition table_requirement.h:44
virtual std::unique_ptr< TableRequirement > Clone() const =0
Create a deep copy of this requirement.
virtual bool Equals(const TableRequirement &other) const =0
Check equality with another TableRequirement.
friend bool operator==(const TableRequirement &lhs, const TableRequirement &rhs)
Compare two TableRequirement instances for equality.
Definition table_requirement.h:80
virtual Status Validate(const TableMetadata *base) const =0
Validate this requirement against table metadata.
virtual Kind kind() const =0
Return the kind of requirement.
Requirement that the current schema ID matches.
Definition table_requirement.h:209
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:219
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:228
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:215
Requirement that the default sort order ID matches.
Definition table_requirement.h:302
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:322
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:309
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:313
Requirement that the default partition spec ID matches.
Definition table_requirement.h:272
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:278
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:282
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:290
Requirement that the table does not exist.
Definition table_requirement.h:91
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:95
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:103
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:99
Requirement that the last assigned field ID matches.
Definition table_requirement.h:177
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:197
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:184
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:188
Requirement that the last assigned partition ID matches.
Definition table_requirement.h:240
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:247
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:251
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:260
Requirement that a reference (branch or tag) points to a specific snapshot.
Definition table_requirement.h:143
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:164
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:156
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:152
Requirement that the table UUID matches the expected value.
Definition table_requirement.h:112
bool Equals(const TableRequirement &other) const override
Check equality with another TableRequirement.
Definition table_requirement.h:122
Kind kind() const override
Return the kind of requirement.
Definition table_requirement.h:118
std::unique_ptr< TableRequirement > Clone() const override
Create a deep copy of this requirement.
Definition table_requirement.h:130
ICEBERG_EXPORT const std::shared_ptr< UuidType > & uuid()
Return a UuidType instance.
Represents the metadata for an Iceberg table.
Definition table_metadata.h:72