|
21 | 21 |
|
22 | 22 | #include <format> |
23 | 23 |
|
| 24 | +#include "iceberg/transform_function.h" |
| 25 | +#include "iceberg/type.h" |
| 26 | + |
24 | 27 | namespace iceberg { |
25 | 28 |
|
26 | | -namespace { |
27 | | -/// \brief Get the relative transform name |
28 | | -constexpr std::string_view ToString(TransformType type) { |
29 | | - switch (type) { |
30 | | - case TransformType::kUnknown: |
31 | | - return "unknown"; |
| 29 | +std::shared_ptr<Transform> Transform::Identity() { |
| 30 | + static auto instance = std::make_shared<Transform>(TransformType::kIdentity); |
| 31 | + return instance; |
| 32 | +} |
| 33 | + |
| 34 | +Transform::Transform(TransformType transform_type) : transform_type_(transform_type) {} |
| 35 | + |
| 36 | +Transform::Transform(TransformType transform_type, int32_t param) |
| 37 | + : transform_type_(transform_type), param_(param) {} |
| 38 | + |
| 39 | +TransformType Transform::transform_type() const { return transform_type_; } |
| 40 | + |
| 41 | +expected<std::unique_ptr<TransformFunction>, Error> Transform::Bind( |
| 42 | + const std::shared_ptr<Type>& source_type) const { |
| 43 | + auto type_str = TransformTypeToString(transform_type_); |
| 44 | + |
| 45 | + switch (transform_type_) { |
32 | 46 | case TransformType::kIdentity: |
33 | | - return "identity"; |
34 | | - case TransformType::kBucket: |
35 | | - return "bucket"; |
36 | | - case TransformType::kTruncate: |
37 | | - return "truncate"; |
| 47 | + return std::make_unique<IdentityTransform>(source_type); |
| 48 | + |
| 49 | + case TransformType::kBucket: { |
| 50 | + if (auto param = std::get_if<int32_t>(¶m_)) { |
| 51 | + return std::make_unique<BucketTransform>(source_type, *param); |
| 52 | + } |
| 53 | + return unexpected<Error>({ |
| 54 | + .kind = ErrorKind::kInvalidArgument, |
| 55 | + .message = std::format( |
| 56 | + "Bucket requires int32 param, none found in transform '{}'", type_str), |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + case TransformType::kTruncate: { |
| 61 | + if (auto param = std::get_if<int32_t>(¶m_)) { |
| 62 | + return std::make_unique<TruncateTransform>(source_type, *param); |
| 63 | + } |
| 64 | + return unexpected<Error>({ |
| 65 | + .kind = ErrorKind::kInvalidArgument, |
| 66 | + .message = std::format( |
| 67 | + "Truncate requires int32 param, none found in transform '{}'", type_str), |
| 68 | + }); |
| 69 | + } |
| 70 | + |
38 | 71 | case TransformType::kYear: |
39 | | - return "year"; |
| 72 | + return std::make_unique<YearTransform>(source_type); |
40 | 73 | case TransformType::kMonth: |
41 | | - return "month"; |
| 74 | + return std::make_unique<MonthTransform>(source_type); |
42 | 75 | case TransformType::kDay: |
43 | | - return "day"; |
| 76 | + return std::make_unique<DayTransform>(source_type); |
44 | 77 | case TransformType::kHour: |
45 | | - return "hour"; |
| 78 | + return std::make_unique<HourTransform>(source_type); |
46 | 79 | case TransformType::kVoid: |
47 | | - return "void"; |
| 80 | + return std::make_unique<VoidTransform>(source_type); |
| 81 | + |
48 | 82 | default: |
49 | | - return "invalid"; |
| 83 | + return unexpected<Error>({ |
| 84 | + .kind = ErrorKind::kNotSupported, |
| 85 | + .message = std::format("Unsupported transform type: '{}'", type_str), |
| 86 | + }); |
50 | 87 | } |
51 | 88 | } |
52 | | -} // namespace |
53 | | - |
54 | | -TransformFunction::TransformFunction(TransformType type) : transform_type_(type) {} |
55 | | - |
56 | | -TransformType TransformFunction::transform_type() const { return transform_type_; } |
57 | 89 |
|
58 | | -std::string TransformFunction::ToString() const { |
59 | | - return std::format("{}", iceberg::ToString(transform_type_)); |
| 90 | +bool TransformFunction::Equals(const TransformFunction& other) const { |
| 91 | + return transform_type_ == other.transform_type_ && *source_type_ == *other.source_type_; |
60 | 92 | } |
61 | 93 |
|
62 | | -bool TransformFunction::Equals(const TransformFunction& other) const { |
63 | | - return transform_type_ == other.transform_type_; |
| 94 | +std::string Transform::ToString() const { |
| 95 | + switch (transform_type_) { |
| 96 | + case TransformType::kIdentity: |
| 97 | + case TransformType::kYear: |
| 98 | + case TransformType::kMonth: |
| 99 | + case TransformType::kDay: |
| 100 | + case TransformType::kHour: |
| 101 | + case TransformType::kVoid: |
| 102 | + case TransformType::kUnknown: |
| 103 | + return std::format("{}", TransformTypeToString(transform_type_)); |
| 104 | + case TransformType::kBucket: |
| 105 | + case TransformType::kTruncate: |
| 106 | + return std::format("{}[{}]", TransformTypeToString(transform_type_), |
| 107 | + std::get<int32_t>(param_)); |
| 108 | + } |
64 | 109 | } |
65 | 110 |
|
66 | | -IdentityTransformFunction::IdentityTransformFunction() |
67 | | - : TransformFunction(TransformType::kIdentity) {} |
| 111 | +TransformFunction::TransformFunction(TransformType transform_type, |
| 112 | + std::shared_ptr<Type> source_type) |
| 113 | + : transform_type_(transform_type), source_type_(std::move(source_type)) {} |
| 114 | + |
| 115 | +TransformType TransformFunction::transform_type() const { return transform_type_; } |
| 116 | + |
| 117 | +std::shared_ptr<Type> const& TransformFunction::source_type() const { |
| 118 | + return source_type_; |
| 119 | +} |
68 | 120 |
|
69 | | -expected<ArrowArray, Error> IdentityTransformFunction::Transform( |
70 | | - const ArrowArray& input) { |
71 | | - return unexpected<Error>({.kind = ErrorKind::kNotSupported, |
72 | | - .message = "IdentityTransformFunction::Transform"}); |
| 121 | +bool Transform::Equals(const Transform& other) const { |
| 122 | + return transform_type_ == other.transform_type_ && param_ == other.param_; |
73 | 123 | } |
74 | 124 |
|
75 | | -expected<std::unique_ptr<TransformFunction>, Error> TransformFunctionFromString( |
76 | | - std::string_view str) { |
| 125 | +expected<std::unique_ptr<Transform>, Error> TransformFromString(std::string_view str) { |
77 | 126 | if (str == "identity") { |
78 | | - return std::make_unique<IdentityTransformFunction>(); |
| 127 | + return std::make_unique<Transform>(TransformType::kIdentity); |
79 | 128 | } |
80 | | - return unexpected<Error>( |
81 | | - {.kind = ErrorKind::kInvalidArgument, |
82 | | - .message = "Invalid TransformFunction string: " + std::string(str)}); |
| 129 | + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, |
| 130 | + .message = std::format("Invalid Transform string: {}", str)}); |
83 | 131 | } |
84 | 132 |
|
85 | 133 | } // namespace iceberg |
0 commit comments