| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) Facebook, Inc. and its affiliates.  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +#include "velox/connectors/lakehouse/iceberg/PartitionSpec.h"  | 
 | 18 | + | 
 | 19 | +namespace facebook::velox::connector::lakehouse::iceberg {  | 
 | 20 | + | 
 | 21 | +namespace {  | 
 | 22 | +std::string transformTypeToString(TransformType type) {  | 
 | 23 | +  switch (type) {  | 
 | 24 | +    case TransformType::kIdentity:  | 
 | 25 | +      return "identity";  | 
 | 26 | +    case TransformType::kHour:  | 
 | 27 | +      return "hour";  | 
 | 28 | +    case TransformType::kDay:  | 
 | 29 | +      return "day";  | 
 | 30 | +    case TransformType::kMonth:  | 
 | 31 | +      return "month";  | 
 | 32 | +    case TransformType::kYear:  | 
 | 33 | +      return "year";  | 
 | 34 | +    case TransformType::kBucket:  | 
 | 35 | +      return "bucket";  | 
 | 36 | +    case TransformType::kTruncate:  | 
 | 37 | +      return "truncate";  | 
 | 38 | +  }  | 
 | 39 | +  VELOX_UNREACHABLE("Unknown TransformType");  | 
 | 40 | +}  | 
 | 41 | + | 
 | 42 | +TransformType transformTypeFromString(const std::string& str) {  | 
 | 43 | +  if (str == "identity") {  | 
 | 44 | +    return TransformType::kIdentity;  | 
 | 45 | +  } else if (str == "hour") {  | 
 | 46 | +    return TransformType::kHour;  | 
 | 47 | +  } else if (str == "day") {  | 
 | 48 | +    return TransformType::kDay;  | 
 | 49 | +  } else if (str == "month") {  | 
 | 50 | +    return TransformType::kMonth;  | 
 | 51 | +  } else if (str == "year") {  | 
 | 52 | +    return TransformType::kYear;  | 
 | 53 | +  } else if (str == "bucket") {  | 
 | 54 | +    return TransformType::kBucket;  | 
 | 55 | +  } else if (str == "truncate") {  | 
 | 56 | +    return TransformType::kTruncate;  | 
 | 57 | +  } else {  | 
 | 58 | +    VELOX_USER_FAIL("Unknown TransformType: {}", str);  | 
 | 59 | +  }  | 
 | 60 | +}  | 
 | 61 | +} // anonymous namespace  | 
 | 62 | + | 
 | 63 | +folly::dynamic IcebergPartitionSpec::Field::serialize() const {  | 
 | 64 | +  folly::dynamic obj = folly::dynamic::object;  | 
 | 65 | +  obj["name"] = "Field";  | 
 | 66 | +  obj["fieldName"] = name;  | 
 | 67 | +  obj["transformType"] = transformTypeToString(transformType);  | 
 | 68 | +  if (parameter.has_value()) {  | 
 | 69 | +    obj["parameter"] = parameter.value();  | 
 | 70 | +  } else {  | 
 | 71 | +    obj["parameter"] = nullptr;  | 
 | 72 | +  }  | 
 | 73 | +  return obj;  | 
 | 74 | +}  | 
 | 75 | + | 
 | 76 | +std::shared_ptr<const ISerializable> IcebergPartitionSpec::Field::create(  | 
 | 77 | +    const folly::dynamic& obj,  | 
 | 78 | +    void* context) {  | 
 | 79 | +  VELOX_CHECK(obj.isObject(), "Field::create expects object");  | 
 | 80 | + | 
 | 81 | +  const auto* fieldNamePtr = obj.get_ptr("fieldName");  | 
 | 82 | +  VELOX_CHECK(fieldNamePtr, "Field::create: missing 'fieldName'");  | 
 | 83 | +  VELOX_CHECK(  | 
 | 84 | +      fieldNamePtr->isString(), "Field::create: 'fieldName' must be string");  | 
 | 85 | +  auto fieldName = fieldNamePtr->asString();  | 
 | 86 | + | 
 | 87 | +  const auto* transformTypePtr = obj.get_ptr("transformType");  | 
 | 88 | +  VELOX_CHECK(transformTypePtr, "Field::create: missing 'transformType'");  | 
 | 89 | +  VELOX_CHECK(  | 
 | 90 | +      transformTypePtr->isString(),  | 
 | 91 | +      "Field::create: 'transformType' must be string");  | 
 | 92 | +  auto transformType = transformTypeFromString(transformTypePtr->asString());  | 
 | 93 | + | 
 | 94 | +  std::optional<int32_t> parameter = std::nullopt;  | 
 | 95 | +  const auto* parameterPtr = obj.get_ptr("parameter");  | 
 | 96 | +  if (parameterPtr && !parameterPtr->isNull()) {  | 
 | 97 | +    VELOX_CHECK(  | 
 | 98 | +        parameterPtr->isInt(),  | 
 | 99 | +        "Field::create: 'parameter' must be integer if present");  | 
 | 100 | +    parameter = static_cast<int32_t>(parameterPtr->asInt());  | 
 | 101 | +  }  | 
 | 102 | + | 
 | 103 | +  return std::make_shared<const Field>(fieldName, transformType, parameter);  | 
 | 104 | +}  | 
 | 105 | + | 
 | 106 | +void IcebergPartitionSpec::Field::registerSerDe() {  | 
 | 107 | +  auto& registry = DeserializationWithContextRegistryForSharedPtr();  | 
 | 108 | +  registry.Register("Field", Field::create);  | 
 | 109 | +}  | 
 | 110 | + | 
 | 111 | +folly::dynamic IcebergPartitionSpec::serialize() const {  | 
 | 112 | +  folly::dynamic obj = folly::dynamic::object;  | 
 | 113 | +  obj["name"] = "IcebergPartitionSpec";  | 
 | 114 | +  obj["specId"] = specId;  | 
 | 115 | + | 
 | 116 | +  folly::dynamic fieldsArray = folly::dynamic::array;  | 
 | 117 | +  fieldsArray.reserve(fields.size());  | 
 | 118 | +  for (const auto& field : fields) {  | 
 | 119 | +    fieldsArray.push_back(field.serialize());  | 
 | 120 | +  }  | 
 | 121 | +  obj["fields"] = std::move(fieldsArray);  | 
 | 122 | +  return obj;  | 
 | 123 | +}  | 
 | 124 | + | 
 | 125 | +std::shared_ptr<const ISerializable> IcebergPartitionSpec::create(  | 
 | 126 | +    const folly::dynamic& obj,  | 
 | 127 | +    void* context) {  | 
 | 128 | +  VELOX_CHECK(obj.isObject(), "IcebergPartitionSpec::create expects object");  | 
 | 129 | + | 
 | 130 | +  const auto* specIdPtr = obj.get_ptr("specId");  | 
 | 131 | +  VELOX_CHECK(specIdPtr, "IcebergPartitionSpec::create: missing 'specId'");  | 
 | 132 | +  VELOX_CHECK(  | 
 | 133 | +      specIdPtr->isInt(),  | 
 | 134 | +      "IcebergPartitionSpec::create: 'specId' must be integer");  | 
 | 135 | +  auto specId = static_cast<int32_t>(specIdPtr->asInt());  | 
 | 136 | + | 
 | 137 | +  const auto* fieldsPtr = obj.get_ptr("fields");  | 
 | 138 | +  VELOX_CHECK(fieldsPtr, "IcebergPartitionSpec::create: missing 'fields'");  | 
 | 139 | +  VELOX_CHECK(  | 
 | 140 | +      fieldsPtr->isArray(),  | 
 | 141 | +      "IcebergPartitionSpec::create: 'fields' must be array");  | 
 | 142 | + | 
 | 143 | +  std::vector<Field> deserializedFields;  | 
 | 144 | +  deserializedFields.reserve(fieldsPtr->size());  | 
 | 145 | +  for (const auto& fieldObj : *fieldsPtr) {  | 
 | 146 | +    auto fieldPtr = Field::create(fieldObj, context);  | 
 | 147 | +    auto field = std::static_pointer_cast<const Field>(fieldPtr);  | 
 | 148 | +    deserializedFields.push_back(*field);  | 
 | 149 | +  }  | 
 | 150 | + | 
 | 151 | +  return std::make_shared<const IcebergPartitionSpec>(  | 
 | 152 | +      specId, std::move(deserializedFields));  | 
 | 153 | +}  | 
 | 154 | + | 
 | 155 | +void IcebergPartitionSpec::registerSerDe() {  | 
 | 156 | +  Field::registerSerDe();  | 
 | 157 | +  auto& registry = DeserializationWithContextRegistryForSharedPtr();  | 
 | 158 | +  registry.Register("IcebergPartitionSpec", IcebergPartitionSpec::create);  | 
 | 159 | +}  | 
 | 160 | + | 
 | 161 | +} // namespace facebook::velox::connector::lakehouse::iceberg  | 
0 commit comments