|
| 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 | +#include "iceberg/update/update_schema.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <optional> |
| 24 | +#include <ranges> |
| 25 | +#include <string> |
| 26 | +#include <string_view> |
| 27 | +#include <unordered_set> |
| 28 | +#include <utility> |
| 29 | + |
| 30 | +#include "iceberg/schema.h" |
| 31 | +#include "iceberg/table_metadata.h" |
| 32 | +#include "iceberg/transaction.h" |
| 33 | +#include "iceberg/type.h" |
| 34 | +#include "iceberg/util/error_collector.h" |
| 35 | +#include "iceberg/util/macros.h" |
| 36 | + |
| 37 | +namespace iceberg { |
| 38 | + |
| 39 | +Result<std::shared_ptr<UpdateSchema>> UpdateSchema::Make( |
| 40 | + std::shared_ptr<Transaction> transaction) { |
| 41 | + ICEBERG_PRECHECK(transaction != nullptr, |
| 42 | + "Cannot create UpdateSchema without transaction"); |
| 43 | + return std::shared_ptr<UpdateSchema>(new UpdateSchema(std::move(transaction))); |
| 44 | +} |
| 45 | + |
| 46 | +UpdateSchema::UpdateSchema(std::shared_ptr<Transaction> transaction) |
| 47 | + : PendingUpdate(std::move(transaction)) { |
| 48 | + const TableMetadata& base_metadata = transaction_->current(); |
| 49 | + |
| 50 | + // Get the current schema |
| 51 | + auto schema_result = base_metadata.Schema(); |
| 52 | + if (!schema_result.has_value()) { |
| 53 | + AddError(schema_result.error()); |
| 54 | + return; |
| 55 | + } |
| 56 | + schema_ = std::move(schema_result.value()); |
| 57 | + |
| 58 | + // Initialize last_column_id from base metadata |
| 59 | + last_column_id_ = base_metadata.last_column_id; |
| 60 | + |
| 61 | + // Initialize identifier field names from the current schema |
| 62 | + auto identifier_names_result = schema_->IdentifierFieldNames(); |
| 63 | + if (!identifier_names_result.has_value()) { |
| 64 | + AddError(identifier_names_result.error()); |
| 65 | + return; |
| 66 | + } |
| 67 | + identifier_field_names_ = identifier_names_result.value() | |
| 68 | + std::ranges::to<std::unordered_set<std::string>>(); |
| 69 | +} |
| 70 | + |
| 71 | +UpdateSchema::~UpdateSchema() = default; |
| 72 | + |
| 73 | +UpdateSchema& UpdateSchema::AllowIncompatibleChanges() { |
| 74 | + allow_incompatible_changes_ = true; |
| 75 | + return *this; |
| 76 | +} |
| 77 | + |
| 78 | +UpdateSchema& UpdateSchema::CaseSensitive(bool case_sensitive) { |
| 79 | + case_sensitive_ = case_sensitive; |
| 80 | + return *this; |
| 81 | +} |
| 82 | + |
| 83 | +UpdateSchema& UpdateSchema::AddColumn(std::string_view name, std::shared_ptr<Type> type, |
| 84 | + std::string_view doc) { |
| 85 | + // Check for "." in top-level name |
| 86 | + ICEBERG_BUILDER_CHECK(!name.contains('.'), |
| 87 | + "Cannot add column with ambiguous name: {}, use " |
| 88 | + "AddColumn(parent, name, type, doc)", |
| 89 | + name); |
| 90 | + return AddColumnInternal(std::nullopt, name, /*is_optional=*/true, std::move(type), |
| 91 | + doc); |
| 92 | +} |
| 93 | + |
| 94 | +UpdateSchema& UpdateSchema::AddColumn(std::optional<std::string_view> parent, |
| 95 | + std::string_view name, std::shared_ptr<Type> type, |
| 96 | + std::string_view doc) { |
| 97 | + return AddColumnInternal(std::move(parent), name, /*is_optional=*/true, std::move(type), |
| 98 | + doc); |
| 99 | +} |
| 100 | + |
| 101 | +UpdateSchema& UpdateSchema::AddRequiredColumn(std::string_view name, |
| 102 | + std::shared_ptr<Type> type, |
| 103 | + std::string_view doc) { |
| 104 | + // Check for "." in top-level name |
| 105 | + ICEBERG_BUILDER_CHECK(!name.contains('.'), |
| 106 | + "Cannot add column with ambiguous name: {}, use " |
| 107 | + "AddRequiredColumn(parent, name, type, doc)", |
| 108 | + name); |
| 109 | + return AddColumnInternal(std::nullopt, name, /*is_optional=*/false, std::move(type), |
| 110 | + doc); |
| 111 | +} |
| 112 | + |
| 113 | +UpdateSchema& UpdateSchema::AddRequiredColumn(std::optional<std::string_view> parent, |
| 114 | + std::string_view name, |
| 115 | + std::shared_ptr<Type> type, |
| 116 | + std::string_view doc) { |
| 117 | + return AddColumnInternal(std::move(parent), name, /*is_optional=*/false, |
| 118 | + std::move(type), doc); |
| 119 | +} |
| 120 | + |
| 121 | +UpdateSchema& UpdateSchema::UpdateColumn(std::string_view name, |
| 122 | + std::shared_ptr<PrimitiveType> new_type) { |
| 123 | + // TODO(Guotao Yu): Implement UpdateColumn |
| 124 | + AddError(NotImplemented("UpdateSchema::UpdateColumn not implemented")); |
| 125 | + return *this; |
| 126 | +} |
| 127 | + |
| 128 | +UpdateSchema& UpdateSchema::UpdateColumnDoc(std::string_view name, |
| 129 | + std::string_view new_doc) { |
| 130 | + // TODO(Guotao Yu): Implement UpdateColumnDoc |
| 131 | + AddError(NotImplemented("UpdateSchema::UpdateColumnDoc not implemented")); |
| 132 | + return *this; |
| 133 | +} |
| 134 | + |
| 135 | +UpdateSchema& UpdateSchema::AddColumnInternal(std::optional<std::string_view> parent, |
| 136 | + std::string_view name, bool is_optional, |
| 137 | + std::shared_ptr<Type> type, |
| 138 | + std::string_view doc) { |
| 139 | + // TODO(Guotao Yu): Implement AddColumnInternal logic |
| 140 | + // This is where the real work happens - finding parent, validating, etc. |
| 141 | + AddError(NotImplemented("UpdateSchema::AddColumnInternal not implemented")); |
| 142 | + return *this; |
| 143 | +} |
| 144 | + |
| 145 | +UpdateSchema& UpdateSchema::RenameColumn(std::string_view name, |
| 146 | + std::string_view new_name) { |
| 147 | + // TODO(Guotao Yu): Implement RenameColumn |
| 148 | + AddError(NotImplemented("UpdateSchema::RenameColumn not implemented")); |
| 149 | + return *this; |
| 150 | +} |
| 151 | + |
| 152 | +UpdateSchema& UpdateSchema::MakeColumnOptional(std::string_view name) { |
| 153 | + // TODO(Guotao Yu): Implement MakeColumnOptional |
| 154 | + AddError(NotImplemented("UpdateSchema::MakeColumnOptional not implemented")); |
| 155 | + return *this; |
| 156 | +} |
| 157 | + |
| 158 | +UpdateSchema& UpdateSchema::RequireColumn(std::string_view name) { |
| 159 | + // TODO(Guotao Yu): Implement RequireColumn |
| 160 | + AddError(NotImplemented("UpdateSchema::RequireColumn not implemented")); |
| 161 | + return *this; |
| 162 | +} |
| 163 | + |
| 164 | +UpdateSchema& UpdateSchema::DeleteColumn(std::string_view name) { |
| 165 | + // TODO(Guotao Yu): Implement DeleteColumn |
| 166 | + AddError(NotImplemented("UpdateSchema::DeleteColumn not implemented")); |
| 167 | + return *this; |
| 168 | +} |
| 169 | + |
| 170 | +UpdateSchema& UpdateSchema::MoveFirst(std::string_view name) { |
| 171 | + // TODO(Guotao Yu): Implement MoveFirst |
| 172 | + AddError(NotImplemented("UpdateSchema::MoveFirst not implemented")); |
| 173 | + return *this; |
| 174 | +} |
| 175 | + |
| 176 | +UpdateSchema& UpdateSchema::MoveBefore(std::string_view name, |
| 177 | + std::string_view before_name) { |
| 178 | + // TODO(Guotao Yu): Implement MoveBefore |
| 179 | + AddError(NotImplemented("UpdateSchema::MoveBefore not implemented")); |
| 180 | + return *this; |
| 181 | +} |
| 182 | + |
| 183 | +UpdateSchema& UpdateSchema::MoveAfter(std::string_view name, |
| 184 | + std::string_view after_name) { |
| 185 | + // TODO(Guotao Yu): Implement MoveAfter |
| 186 | + AddError(NotImplemented("UpdateSchema::MoveAfter not implemented")); |
| 187 | + return *this; |
| 188 | +} |
| 189 | + |
| 190 | +UpdateSchema& UpdateSchema::UnionByNameWith(std::shared_ptr<Schema> new_schema) { |
| 191 | + // TODO(Guotao Yu): Implement UnionByNameWith |
| 192 | + AddError(NotImplemented("UpdateSchema::UnionByNameWith not implemented")); |
| 193 | + return *this; |
| 194 | +} |
| 195 | + |
| 196 | +UpdateSchema& UpdateSchema::SetIdentifierFields( |
| 197 | + const std::span<std::string_view>& names) { |
| 198 | + identifier_field_names_ = names | std::ranges::to<std::unordered_set<std::string>>(); |
| 199 | + return *this; |
| 200 | +} |
| 201 | + |
| 202 | +Result<UpdateSchema::ApplyResult> UpdateSchema::Apply() { |
| 203 | + // TODO(Guotao Yu): Implement Apply |
| 204 | + return NotImplemented("UpdateSchema::Apply not implemented"); |
| 205 | +} |
| 206 | + |
| 207 | +} // namespace iceberg |
0 commit comments