|
| 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/transform/transform_factory.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include <nanoarrow/nanoarrow.hpp> |
| 25 | + |
| 26 | +#include "iceberg/transform/transform_function.h" |
| 27 | +#include "iceberg/transform/transform_spec.h" |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +int32_t GetInt32FromParamArray(ArrowArray const& param_array) { |
| 34 | + ArrowArrayView view; |
| 35 | + ArrowArrayViewInitFromType(&view, NANOARROW_TYPE_INT32); |
| 36 | + NANOARROW_THROW_NOT_OK(ArrowArrayViewSetArray(&view, ¶m_array, nullptr)); |
| 37 | + const auto value = view.buffer_views[1].data.as_int32[0]; |
| 38 | + ArrowArrayViewReset(&view); |
| 39 | + return value; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +expected<std::unique_ptr<TransformFunction>, Error> TransformFactory::create( |
| 45 | + const TransformSpec& spec) { |
| 46 | + switch (spec.transform_type) { |
| 47 | + case TransformType::kIdentity: |
| 48 | + return std::make_unique<IdentityTransform>(spec.source_type); |
| 49 | + case TransformType::kBucket: { |
| 50 | + if (!spec.params_opt.has_value()) { |
| 51 | + return unexpected<Error>( |
| 52 | + {.kind = ErrorKind::kInvalidArgument, |
| 53 | + .message = "Bucket transform requires 1 parameter (number of buckets), but " |
| 54 | + "none were provided."}); |
| 55 | + } |
| 56 | + if (spec.params_opt->length != 1) { |
| 57 | + return unexpected<Error>( |
| 58 | + {.kind = ErrorKind::kInvalidArgument, |
| 59 | + .message = std::format("Bucket transform expects exactly 1 parameter " |
| 60 | + "(number of buckets), but got {}.", |
| 61 | + spec.params_opt->length)}); |
| 62 | + } |
| 63 | + auto num_buckets = GetInt32FromParamArray(spec.params_opt.value()); |
| 64 | + return std::make_unique<BucketTransform>(spec.source_type, num_buckets); |
| 65 | + } |
| 66 | + case TransformType::kTruncate: { |
| 67 | + if (!spec.params_opt.has_value()) { |
| 68 | + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, |
| 69 | + .message = "Truncate transform requires 1 parameter " |
| 70 | + "(width), but none were provided."}); |
| 71 | + } |
| 72 | + if (spec.params_opt->length != 1) { |
| 73 | + return unexpected<Error>( |
| 74 | + {.kind = ErrorKind::kInvalidArgument, |
| 75 | + .message = std::format( |
| 76 | + "Truncate transform expects exactly 1 parameter (width), but got {}.", |
| 77 | + spec.params_opt->length)}); |
| 78 | + } |
| 79 | + auto width = GetInt32FromParamArray(spec.params_opt.value()); |
| 80 | + return std::make_unique<TruncateTransform>(spec.source_type, width); |
| 81 | + } |
| 82 | + case TransformType::kYear: |
| 83 | + return std::make_unique<YearTransform>(spec.source_type); |
| 84 | + case TransformType::kMonth: |
| 85 | + return std::make_unique<MonthTransform>(spec.source_type); |
| 86 | + case TransformType::kDay: |
| 87 | + return std::make_unique<DayTransform>(spec.source_type); |
| 88 | + case TransformType::kHour: |
| 89 | + return std::make_unique<HourTransform>(spec.source_type); |
| 90 | + case TransformType::kVoid: |
| 91 | + return std::make_unique<VoidTransform>(spec.source_type); |
| 92 | + default: |
| 93 | + return unexpected<Error>( |
| 94 | + {.kind = ErrorKind::kInvalidArgument, |
| 95 | + .message = std::format("Unsupported or invalid transform type: {}", |
| 96 | + ToString(spec.transform_type))}); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace iceberg |
0 commit comments