|
| 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/struct_like.h" |
| 21 | + |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include "iceberg/result.h" |
| 25 | +#include "iceberg/util/checked_cast.h" |
| 26 | +#include "iceberg/util/formatter_internal.h" |
| 27 | +#include "iceberg/util/macros.h" |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +StructLikeAccessor::StructLikeAccessor(std::shared_ptr<Type> type, |
| 32 | + std::span<const size_t> position_path) |
| 33 | + : type_(std::move(type)) { |
| 34 | + if (position_path.size() == 1) { |
| 35 | + accessor_ = [pos = |
| 36 | + position_path[0]](const StructLike& struct_like) -> Result<Scalar> { |
| 37 | + return struct_like.GetField(pos); |
| 38 | + }; |
| 39 | + } else if (position_path.size() == 2) { |
| 40 | + accessor_ = [pos0 = position_path[0], pos1 = position_path[1]]( |
| 41 | + const StructLike& struct_like) -> Result<Scalar> { |
| 42 | + ICEBERG_ASSIGN_OR_RAISE(auto first_level_field, struct_like.GetField(pos0)); |
| 43 | + if (!std::holds_alternative<std::shared_ptr<StructLike>>(first_level_field)) { |
| 44 | + return InvalidSchema("Encountered non-struct in the position path [{},{}]", pos0, |
| 45 | + pos1); |
| 46 | + } |
| 47 | + return std::get<std::shared_ptr<StructLike>>(first_level_field)->GetField(pos1); |
| 48 | + }; |
| 49 | + } else if (!position_path.empty()) { |
| 50 | + accessor_ = [position_path](const StructLike& struct_like) -> Result<Scalar> { |
| 51 | + std::vector<std::shared_ptr<StructLike>> backups; |
| 52 | + const StructLike* current_struct_like = &struct_like; |
| 53 | + for (size_t i = 0; i < position_path.size() - 1; ++i) { |
| 54 | + ICEBERG_ASSIGN_OR_RAISE(auto field, |
| 55 | + current_struct_like->GetField(position_path[i])); |
| 56 | + if (!std::holds_alternative<std::shared_ptr<StructLike>>(field)) { |
| 57 | + return InvalidSchema("Encountered non-struct in the position path [{}]", |
| 58 | + position_path); |
| 59 | + } |
| 60 | + backups.push_back(std::get<std::shared_ptr<StructLike>>(field)); |
| 61 | + current_struct_like = backups.back().get(); |
| 62 | + } |
| 63 | + return current_struct_like->GetField(position_path.back()); |
| 64 | + }; |
| 65 | + } else { |
| 66 | + accessor_ = [](const StructLike&) -> Result<Scalar> { |
| 67 | + return Invalid("Cannot read StructLike with empty position path"); |
| 68 | + }; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +Result<Literal> StructLikeAccessor::GetLiteral(const StructLike& struct_like) const { |
| 73 | + if (!type_->is_primitive()) { |
| 74 | + return NotSupported("Cannot get literal value for non-primitive type {}", |
| 75 | + type_->ToString()); |
| 76 | + } |
| 77 | + |
| 78 | + ICEBERG_ASSIGN_OR_RAISE(auto scalar, Get(struct_like)); |
| 79 | + |
| 80 | + if (std::holds_alternative<std::monostate>(scalar)) { |
| 81 | + return Literal::Null(internal::checked_pointer_cast<PrimitiveType>(type_)); |
| 82 | + } |
| 83 | + |
| 84 | + switch (type_->type_id()) { |
| 85 | + case TypeId::kBoolean: |
| 86 | + return Literal::Boolean(std::get<bool>(scalar)); |
| 87 | + case TypeId::kInt: |
| 88 | + return Literal::Int(std::get<int32_t>(scalar)); |
| 89 | + case TypeId::kLong: |
| 90 | + return Literal::Long(std::get<int64_t>(scalar)); |
| 91 | + case TypeId::kFloat: |
| 92 | + return Literal::Float(std::get<float>(scalar)); |
| 93 | + case TypeId::kDouble: |
| 94 | + return Literal::Double(std::get<double>(scalar)); |
| 95 | + case TypeId::kString: |
| 96 | + return Literal::String(std::string(std::get<std::string_view>(scalar))); |
| 97 | + case TypeId::kBinary: { |
| 98 | + auto binary_data = std::get<std::string_view>(scalar); |
| 99 | + return Literal::Binary( |
| 100 | + std::vector<uint8_t>(binary_data.cbegin(), binary_data.cend())); |
| 101 | + } |
| 102 | + case TypeId::kDecimal: { |
| 103 | + const auto& decimal_type = internal::checked_cast<const DecimalType&>(*type_); |
| 104 | + return Literal::Decimal(std::get<Decimal>(scalar).value(), decimal_type.precision(), |
| 105 | + decimal_type.scale()); |
| 106 | + } |
| 107 | + case TypeId::kDate: |
| 108 | + return Literal::Date(std::get<int32_t>(scalar)); |
| 109 | + case TypeId::kTime: |
| 110 | + return Literal::Time(std::get<int64_t>(scalar)); |
| 111 | + case TypeId::kTimestamp: |
| 112 | + return Literal::Timestamp(std::get<int64_t>(scalar)); |
| 113 | + case TypeId::kTimestampTz: |
| 114 | + return Literal::TimestampTz(std::get<int64_t>(scalar)); |
| 115 | + case TypeId::kFixed: { |
| 116 | + const auto& fixed_data = std::get<std::string_view>(scalar); |
| 117 | + return Literal::Fixed(std::vector<uint8_t>(fixed_data.cbegin(), fixed_data.cend())); |
| 118 | + } |
| 119 | + case TypeId::kUuid: |
| 120 | + // TODO(gangwu): Implement UUID type |
| 121 | + default: |
| 122 | + return NotSupported("Cannot convert scalar to literal of type {}", |
| 123 | + type_->ToString()); |
| 124 | + } |
| 125 | + |
| 126 | + std::unreachable(); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace iceberg |
0 commit comments