|
| 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/row/partition_values.h" |
| 21 | + |
| 22 | +namespace iceberg { |
| 23 | + |
| 24 | +PartitionValues& PartitionValues::operator=(const PartitionValues& other) { |
| 25 | + if (this != &other) { |
| 26 | + values_ = other.values_; |
| 27 | + } |
| 28 | + return *this; |
| 29 | +} |
| 30 | + |
| 31 | +bool PartitionValues::operator==(const PartitionValues& other) const { |
| 32 | + return values_ == other.values_; |
| 33 | +} |
| 34 | + |
| 35 | +Result<Scalar> PartitionValues::GetField(size_t pos) const { |
| 36 | + if (pos >= values_.size()) { |
| 37 | + return InvalidArgument( |
| 38 | + "Position {} is out of bounds for PartitionValues with {} fields", pos, |
| 39 | + values_.size()); |
| 40 | + } |
| 41 | + |
| 42 | + const auto& literal = values_[pos]; |
| 43 | + |
| 44 | + // Handle null values |
| 45 | + if (literal.IsNull()) { |
| 46 | + return Scalar{std::monostate{}}; |
| 47 | + } |
| 48 | + |
| 49 | + // Convert Literal to Scalar based on type |
| 50 | + switch (literal.type()->type_id()) { |
| 51 | + case TypeId::kBoolean: |
| 52 | + return Scalar{std::get<bool>(literal.value())}; |
| 53 | + case TypeId::kInt: |
| 54 | + case TypeId::kDate: |
| 55 | + return Scalar{std::get<int32_t>(literal.value())}; |
| 56 | + case TypeId::kLong: |
| 57 | + case TypeId::kTime: |
| 58 | + case TypeId::kTimestamp: |
| 59 | + case TypeId::kTimestampTz: |
| 60 | + return Scalar{std::get<int64_t>(literal.value())}; |
| 61 | + case TypeId::kFloat: |
| 62 | + return Scalar{std::get<float>(literal.value())}; |
| 63 | + case TypeId::kDouble: |
| 64 | + return Scalar{std::get<double>(literal.value())}; |
| 65 | + case TypeId::kString: { |
| 66 | + const auto& str = std::get<std::string>(literal.value()); |
| 67 | + return Scalar{std::string_view(str)}; |
| 68 | + } |
| 69 | + case TypeId::kBinary: |
| 70 | + case TypeId::kFixed: { |
| 71 | + const auto& bytes = std::get<std::vector<uint8_t>>(literal.value()); |
| 72 | + return Scalar{ |
| 73 | + std::string_view(reinterpret_cast<const char*>(bytes.data()), bytes.size())}; |
| 74 | + } |
| 75 | + case TypeId::kDecimal: |
| 76 | + return Scalar{std::get<Decimal>(literal.value())}; |
| 77 | + default: |
| 78 | + return NotSupported("Cannot convert literal of type {} to Scalar", |
| 79 | + literal.type()->ToString()); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +Result<std::reference_wrapper<const Literal>> PartitionValues::ValueAt(size_t pos) const { |
| 84 | + if (pos >= values_.size()) { |
| 85 | + return InvalidArgument("Cannot get partition value at {} from {} fields", pos, |
| 86 | + values_.size()); |
| 87 | + } |
| 88 | + return std::cref(values_[pos]); |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace iceberg |
0 commit comments