|
| 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/properties_update.h" |
| 21 | + |
| 22 | +#include <cstdint> |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +#include "iceberg/catalog.h" |
| 26 | +#include "iceberg/file_format.h" |
| 27 | +#include "iceberg/metrics_config.h" |
| 28 | +#include "iceberg/result.h" |
| 29 | +#include "iceberg/table.h" |
| 30 | +#include "iceberg/table_identifier.h" |
| 31 | +#include "iceberg/table_metadata.h" |
| 32 | +#include "iceberg/table_properties.h" |
| 33 | +#include "iceberg/table_requirements.h" |
| 34 | +#include "iceberg/table_update.h" |
| 35 | +#include "iceberg/util/macros.h" |
| 36 | + |
| 37 | +namespace iceberg { |
| 38 | + |
| 39 | +PropertiesUpdate::PropertiesUpdate(TableIdentifier identifier, |
| 40 | + std::shared_ptr<Catalog> catalog, |
| 41 | + std::shared_ptr<TableMetadata> metadata) |
| 42 | + : identifier_(std::move(identifier)), |
| 43 | + catalog_(std::move(catalog)), |
| 44 | + metadata_(std::move(metadata)) {} |
| 45 | + |
| 46 | +PropertiesUpdate& PropertiesUpdate::Set(std::string key, std::string value) { |
| 47 | + if (std::ranges::find(removals_, key) != std::ranges::end(removals_)) { |
| 48 | + return *this; |
| 49 | + } |
| 50 | + |
| 51 | + if (!TableProperties::reserved_properties().contains(key) || |
| 52 | + key == TableProperties::kFormatVersion.key()) { |
| 53 | + updates_.emplace(std::move(key), std::move(value)); |
| 54 | + } |
| 55 | + |
| 56 | + return *this; |
| 57 | +} |
| 58 | + |
| 59 | +PropertiesUpdate& PropertiesUpdate::Remove(std::string key) { |
| 60 | + if (updates_.contains(key)) { |
| 61 | + return *this; |
| 62 | + } |
| 63 | + if (std::ranges::find(removals_, key) == removals_.end()) { |
| 64 | + removals_.push_back(std::move(key)); |
| 65 | + } |
| 66 | + return *this; |
| 67 | +} |
| 68 | + |
| 69 | +PropertiesUpdate& PropertiesUpdate::DefaultFormat(FileFormatType format) { |
| 70 | + updates_.emplace(TableProperties::kDefaultFileFormat.key(), ToString(format)); |
| 71 | + return *this; |
| 72 | +} |
| 73 | + |
| 74 | +Result<void> PropertiesUpdate::Apply() { |
| 75 | + if (!metadata_) { |
| 76 | + return InvalidArgument("Cannot commit an empty table"); |
| 77 | + } |
| 78 | + |
| 79 | + auto iter = updates_.find(TableProperties::kFormatVersion.key()); |
| 80 | + if (iter != updates_.end()) { |
| 81 | + try { |
| 82 | + int parsed_version = std::stoi(iter->second); |
| 83 | + if (parsed_version > TableMetadata::kSupportedTableFormatVersion) { |
| 84 | + return InvalidArgument( |
| 85 | + "Cannot upgrade table to unsupported format version: v{} (supported: v{})", |
| 86 | + parsed_version, TableMetadata::kSupportedTableFormatVersion); |
| 87 | + } |
| 88 | + format_version_ = static_cast<int8_t>(parsed_version); |
| 89 | + } catch (const std::invalid_argument& e) { |
| 90 | + return InvalidArgument("Invalid format version '{}': not a valid integer", |
| 91 | + iter->second); |
| 92 | + } catch (const std::out_of_range& e) { |
| 93 | + return InvalidArgument("Format version '{}' is out of range", iter->second); |
| 94 | + } |
| 95 | + |
| 96 | + updates_.erase(iter); |
| 97 | + } |
| 98 | + |
| 99 | + if (metadata_->Schema().has_value()) { |
| 100 | + ICEBERG_RETURN_UNEXPECTED( |
| 101 | + MetricsConfig::VerifyReferencedColumns(updates_, *metadata_->Schema().value())); |
| 102 | + } |
| 103 | + return {}; |
| 104 | +} |
| 105 | + |
| 106 | +Status PropertiesUpdate::Commit() { |
| 107 | + ICEBERG_RETURN_UNEXPECTED(Apply()); |
| 108 | + |
| 109 | + std::vector<std::unique_ptr<TableUpdate>> updates; |
| 110 | + if (!updates_.empty()) { |
| 111 | + updates.emplace_back(std::make_unique<table::SetProperties>(std::move(updates_))); |
| 112 | + } |
| 113 | + if (!removals_.empty()) { |
| 114 | + updates.emplace_back(std::make_unique<table::RemoveProperties>(std::move(removals_))); |
| 115 | + } |
| 116 | + if (format_version_.has_value()) { |
| 117 | + updates.emplace_back( |
| 118 | + std::make_unique<table::UpgradeFormatVersion>(format_version_.value())); |
| 119 | + }; |
| 120 | + |
| 121 | + if (!updates.empty()) { |
| 122 | + ICEBERG_ASSIGN_OR_RAISE(auto requirements, |
| 123 | + TableRequirements::ForUpdateTable(*metadata_, updates)); |
| 124 | + ICEBERG_RETURN_UNEXPECTED( |
| 125 | + catalog_->UpdateTable(identifier_, requirements, std::move(updates))); |
| 126 | + } |
| 127 | + return {}; |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace iceberg |
0 commit comments