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
35#include "iceberg/result.h"
36#include "iceberg/type_fwd.h"
38
39namespace iceberg {
40
45class ICEBERG_EXPORT UpdateSchema : public PendingUpdate {
46 public:
48 std::shared_ptr<TransactionContext> ctx);
49
50 ~UpdateSchema() override;
51
67
86 UpdateSchema& AddColumn(std::string_view name, std::shared_ptr<Type> type,
87 std::string_view doc = "",
88 std::optional<Literal> default_value = std::nullopt);
89
113 UpdateSchema& AddColumn(std::optional<std::string_view> parent, std::string_view name,
114 std::shared_ptr<Type> type, std::string_view doc = "",
115 std::optional<Literal> default_value = std::nullopt);
116
136 UpdateSchema& AddRequiredColumn(std::string_view name, std::shared_ptr<Type> type,
137 std::string_view doc = "",
138 std::optional<Literal> default_value = std::nullopt);
139
164 UpdateSchema& AddRequiredColumn(std::optional<std::string_view> parent,
165 std::string_view name, std::shared_ptr<Type> type,
166 std::string_view doc = "",
167 std::optional<Literal> default_value = std::nullopt);
168
184 UpdateSchema& RenameColumn(std::string_view name, std::string_view new_name);
185
201 UpdateSchema& UpdateColumn(std::string_view name,
202 std::shared_ptr<PrimitiveType> new_type);
203
214 UpdateSchema& UpdateColumnDoc(std::string_view name, std::string_view new_doc);
215
227 UpdateSchema& UpdateColumnDefault(std::string_view name,
228 std::optional<Literal> new_default);
229
234 UpdateSchema& MakeColumnOptional(std::string_view name);
235
243 UpdateSchema& RequireColumn(std::string_view name);
244
254 UpdateSchema& DeleteColumn(std::string_view name);
255
264 UpdateSchema& MoveFirst(std::string_view name);
265
279 UpdateSchema& MoveBefore(std::string_view name, std::string_view before_name);
280
294 UpdateSchema& MoveAfter(std::string_view name, std::string_view after_name);
295
318 UpdateSchema& UnionByNameWith(std::shared_ptr<Schema> new_schema);
319
327 UpdateSchema& SetIdentifierFields(const std::span<std::string_view>& names);
328
335 UpdateSchema& CaseSensitive(bool case_sensitive);
336
338 struct Move {
339 enum class MoveType { kFirst, kBefore, kAfter };
340
341 int32_t field_id;
342 int32_t reference_field_id; // Only used for kBefore and kAfter
343 MoveType type;
344
345 static Move First(int32_t field_id);
346
347 static Move Before(int32_t field_id, int32_t reference_field_id);
348
349 static Move After(int32_t field_id, int32_t reference_field_id);
350 };
351
352 Kind kind() const final { return Kind::kUpdateSchema; }
353
359 bool IsRetryable() const override { return false; }
360
361 struct ApplyResult {
362 std::shared_ptr<Schema> schema;
363 int32_t new_last_column_id;
364 std::unordered_map<std::string, std::string> updated_props;
365 };
366
373
374 private:
375 explicit UpdateSchema(std::shared_ptr<TransactionContext> ctx);
376
386 UpdateSchema& AddColumnInternal(std::optional<std::string_view> parent,
387 std::string_view name, bool is_optional,
388 std::shared_ptr<Type> type, std::string_view doc,
389 std::optional<Literal> default_value);
390
396 UpdateSchema& UpdateColumnRequirementInternal(std::string_view name, bool is_optional);
397
399 int32_t AssignNewColumnId();
400
403 std::string_view name) const;
404
414 std::string_view name) const;
415
423 std::string CaseSensitivityAwareName(std::string_view name) const;
424
426 Result<int32_t> FindFieldIdForMove(std::string_view name) const;
427
429 UpdateSchema& MoveInternal(std::string_view name, const Move& move);
430
431 // Internal state
432 std::shared_ptr<Schema> schema_;
433 int32_t last_column_id_;
434 bool allow_incompatible_changes_{false};
435 bool case_sensitive_{true};
436 std::vector<std::string> identifier_field_names_;
437
438 // Tracking changes
439 // field ID -> parent field ID
440 std::unordered_map<int32_t, int32_t> id_to_parent_;
441 // field IDs to delete
442 std::unordered_set<int32_t> deletes_;
443 // field ID -> updated field
444 std::unordered_map<int32_t, std::shared_ptr<SchemaField>> updates_;
445 // parent ID -> added child IDs
446 std::unordered_map<int32_t, std::vector<int32_t>> parent_to_added_ids_;
447 // full name -> field ID for added fields
448 std::unordered_map<std::string, int32_t> added_name_to_id_;
449 // parent ID -> move operations
450 std::unordered_map<int32_t, std::vector<Move>> moves_;
451};
452
453} // namespace iceberg
Base class for all kinds of table metadata updates.
Definition pending_update.h:41
API for schema evolution.
Definition update_schema.h:45
UpdateSchema & AddColumn(std::optional< std::string_view > parent, std::string_view name, std::shared_ptr< Type > type, std::string_view doc="", std::optional< Literal > default_value=std::nullopt)
Add a new optional column to a nested struct with documentation.
UpdateSchema & RequireColumn(std::string_view name)
Update a column to be required.
UpdateSchema & DeleteColumn(std::string_view name)
Delete a column in the schema.
UpdateSchema & SetIdentifierFields(const std::span< std::string_view > &names)
Set the identifier fields given a set of field names.
Kind kind() const final
Return the kind of this pending update.
Definition update_schema.h:352
UpdateSchema & MoveFirst(std::string_view name)
Move a column from its current position to the start of the schema or its parent struct.
UpdateSchema & MakeColumnOptional(std::string_view name)
Update a column to be optional.
UpdateSchema & CaseSensitive(bool case_sensitive)
Determines if the case of schema needs to be considered when comparing column names.
UpdateSchema & AllowIncompatibleChanges()
Allow incompatible changes to the schema.
Result< ApplyResult > Apply()
Apply the pending changes to the original schema and return the result.
bool IsRetryable() const override
Schema updates are not retryable.
Definition update_schema.h:359
UpdateSchema & UpdateColumn(std::string_view name, std::shared_ptr< PrimitiveType > new_type)
Update a column in the schema to a new primitive type.
UpdateSchema & MoveAfter(std::string_view name, std::string_view after_name)
Move a column from its current position to directly after a reference column.
UpdateSchema & AddColumn(std::string_view name, std::shared_ptr< Type > type, std::string_view doc="", std::optional< Literal > default_value=std::nullopt)
Add a new optional top-level column with documentation.
UpdateSchema & AddRequiredColumn(std::string_view name, std::shared_ptr< Type > type, std::string_view doc="", std::optional< Literal > default_value=std::nullopt)
Add a new required top-level column with documentation.
UpdateSchema & AddRequiredColumn(std::optional< std::string_view > parent, std::string_view name, std::shared_ptr< Type > type, std::string_view doc="", std::optional< Literal > default_value=std::nullopt)
Add a new required column to a nested struct with documentation.
UpdateSchema & UnionByNameWith(std::shared_ptr< Schema > new_schema)
Applies all field additions and updates from the provided new schema to the existing schema to create...
UpdateSchema & MoveBefore(std::string_view name, std::string_view before_name)
Move a column from its current position to directly before a reference column.
UpdateSchema & RenameColumn(std::string_view name, std::string_view new_name)
Rename a column in the schema.
UpdateSchema & UpdateColumnDefault(std::string_view name, std::optional< Literal > new_default)
Update the write-default value for a column (v3+).
UpdateSchema & UpdateColumnDoc(std::string_view name, std::string_view new_doc)
Update the documentation string for a column.
Define symbol visibility macros for core Iceberg APIs.
Define typed literal values used by expressions.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
@ kFirst
Nulls are sorted first.
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
Definition update_schema.h:361
Represents a column move operation within a struct (internal use only).
Definition update_schema.h:338