|
| 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/partition_spec.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include "iceberg/exception.h" |
| 25 | +#include "iceberg/type.h" |
| 26 | +#include "iceberg/util/formatter.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +PartitionSpec::PartitionSpec(int32_t spec_id, std::vector<PartitionField> fields) |
| 31 | + : spec_id_(spec_id), fields_(std::move(fields)) { |
| 32 | + size_t index = 0; |
| 33 | + for (const auto& field : fields_) { |
| 34 | + auto [it, inserted] = field_id_to_index_.try_emplace(field.field_id(), index); |
| 35 | + ICEBERG_CHECK_FMT(inserted, |
| 36 | + "PartitionSpec: duplicate field ID {} (field indices {} and {})", |
| 37 | + field.field_id(), it->second, index); |
| 38 | + ++index; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +int32_t PartitionSpec::spec_id() const { return spec_id_; } |
| 43 | + |
| 44 | +std::span<const PartitionField> PartitionSpec::fields() const { return fields_; } |
| 45 | + |
| 46 | +std::optional<std::reference_wrapper<const PartitionField>> PartitionSpec::GetFieldById( |
| 47 | + int32_t field_id) const { |
| 48 | + if (auto it = field_id_to_index_.find(field_id); it != field_id_to_index_.end()) { |
| 49 | + return fields_[it->second]; |
| 50 | + } |
| 51 | + return std::nullopt; |
| 52 | +} |
| 53 | + |
| 54 | +std::optional<std::reference_wrapper<const PartitionField>> |
| 55 | +PartitionSpec::GetFieldByIndex(int32_t index) const { |
| 56 | + if (index >= 0 && index < static_cast<int>(fields_.size())) { |
| 57 | + return fields_[index]; |
| 58 | + } |
| 59 | + return std::nullopt; |
| 60 | +} |
| 61 | + |
| 62 | +std::optional<std::reference_wrapper<const PartitionField>> PartitionSpec::GetFieldByName( |
| 63 | + std::string_view name) const { |
| 64 | + auto findField = [&name](auto& field) { return field.name() == name; }; |
| 65 | + if (auto it = std::ranges::find_if(fields_, findField); it != fields_.end()) { |
| 66 | + return *it; |
| 67 | + } |
| 68 | + return std::nullopt; |
| 69 | +} |
| 70 | + |
| 71 | +std::string PartitionSpec::ToString() const { |
| 72 | + std::string repr = "partition_spec(<"; |
| 73 | + for (const auto& field : fields_) { |
| 74 | + std::format_to(std::back_inserter(repr), " {}\n", field); |
| 75 | + } |
| 76 | + repr += ">"; |
| 77 | + return repr; |
| 78 | +} |
| 79 | + |
| 80 | +bool PartitionSpec::Equals(const PartitionSpec& other) const { |
| 81 | + return spec_id_ == other.spec_id_ && fields_ == other.fields_; |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace iceberg |
0 commit comments