iceberg-cpp
Loading...
Searching...
No Matches
update_schema.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 <span>
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <unordered_set>
32
33#include "iceberg/iceberg_export.h"
34#include "iceberg/result.h"
35#include "iceberg/type_fwd.h"
37
38namespace iceberg {
39
48class ICEBERG_EXPORT UpdateSchema : public PendingUpdate {
49 public:
50 static Result<std::shared_ptr<UpdateSchema>> Make(
51 std::shared_ptr<TransactionContext> ctx);
52
53 ~UpdateSchema() override;
54
69 UpdateSchema& AllowIncompatibleChanges();
70
88 UpdateSchema& AddColumn(std::string_view name, std::shared_ptr<Type> type,
89 std::string_view doc = "");
90
113 UpdateSchema& AddColumn(std::optional<std::string_view> parent, std::string_view name,
114 std::shared_ptr<Type> type, std::string_view doc = "");
115
135 UpdateSchema& AddRequiredColumn(std::string_view name, std::shared_ptr<Type> type,
136 std::string_view doc = "");
137
162 UpdateSchema& AddRequiredColumn(std::optional<std::string_view> parent,
163 std::string_view name, std::shared_ptr<Type> type,
164 std::string_view doc = "");
165
181 UpdateSchema& RenameColumn(std::string_view name, std::string_view new_name);
182
198 UpdateSchema& UpdateColumn(std::string_view name,
199 std::shared_ptr<PrimitiveType> new_type);
200
211 UpdateSchema& UpdateColumnDoc(std::string_view name, std::string_view new_doc);
212
217 UpdateSchema& MakeColumnOptional(std::string_view name);
218
226 UpdateSchema& RequireColumn(std::string_view name);
227
237 UpdateSchema& DeleteColumn(std::string_view name);
238
247 UpdateSchema& MoveFirst(std::string_view name);
248
262 UpdateSchema& MoveBefore(std::string_view name, std::string_view before_name);
263
277 UpdateSchema& MoveAfter(std::string_view name, std::string_view after_name);
278
301 UpdateSchema& UnionByNameWith(std::shared_ptr<Schema> new_schema);
302
310 UpdateSchema& SetIdentifierFields(const std::span<std::string_view>& names);
311
318 UpdateSchema& CaseSensitive(bool case_sensitive);
319
321 struct Move {
322 enum class MoveType { kFirst, kBefore, kAfter };
323
324 int32_t field_id;
325 int32_t reference_field_id; // Only used for kBefore and kAfter
326 MoveType type;
327
328 static Move First(int32_t field_id);
329
330 static Move Before(int32_t field_id, int32_t reference_field_id);
331
332 static Move After(int32_t field_id, int32_t reference_field_id);
333 };
334
335 Kind kind() const final { return Kind::kUpdateSchema; }
336
342 bool IsRetryable() const override { return false; }
343
344 struct ApplyResult {
345 std::shared_ptr<Schema> schema;
346 int32_t new_last_column_id;
347 std::unordered_map<std::string, std::string> updated_props;
348 };
349
355 Result<ApplyResult> Apply();
356
357 private:
358 explicit UpdateSchema(std::shared_ptr<TransactionContext> ctx);
359
368 UpdateSchema& AddColumnInternal(std::optional<std::string_view> parent,
369 std::string_view name, bool is_optional,
370 std::shared_ptr<Type> type, std::string_view doc);
371
377 UpdateSchema& UpdateColumnRequirementInternal(std::string_view name, bool is_optional);
378
380 int32_t AssignNewColumnId();
381
383 Result<std::optional<std::reference_wrapper<const SchemaField>>> FindField(
384 std::string_view name) const;
385
394 Result<std::optional<std::reference_wrapper<const SchemaField>>> FindFieldForUpdate(
395 std::string_view name) const;
396
404 std::string CaseSensitivityAwareName(std::string_view name) const;
405
407 Result<int32_t> FindFieldIdForMove(std::string_view name) const;
408
410 UpdateSchema& MoveInternal(std::string_view name, const Move& move);
411
412 // Internal state
413 std::shared_ptr<Schema> schema_;
414 int32_t last_column_id_;
415 bool allow_incompatible_changes_{false};
416 bool case_sensitive_{true};
417 std::vector<std::string> identifier_field_names_;
418
419 // Tracking changes
420 // field ID -> parent field ID
421 std::unordered_map<int32_t, int32_t> id_to_parent_;
422 // field IDs to delete
423 std::unordered_set<int32_t> deletes_;
424 // field ID -> updated field
425 std::unordered_map<int32_t, std::shared_ptr<SchemaField>> updates_;
426 // parent ID -> added child IDs
427 std::unordered_map<int32_t, std::vector<int32_t>> parent_to_added_ids_;
428 // full name -> field ID for added fields
429 std::unordered_map<std::string, int32_t> added_name_to_id_;
430 // parent ID -> move operations
431 std::unordered_map<int32_t, std::vector<Move>> moves_;
432};
433
434} // namespace iceberg
Base class for all kinds of table metadata updates.
Definition pending_update.h:41
API for schema evolution.
Definition update_schema.h:48
Kind kind() const final
Return the kind of this pending update.
Definition update_schema.h:335
bool IsRetryable() const override
Schema updates are not retryable.
Definition update_schema.h:342
@ kFirst
Nulls are sorted first.
Definition update_schema.h:344
Represents a column move operation within a struct (internal use only).
Definition update_schema.h:321