iceberg-cpp
Loading...
Searching...
No Matches
transform.h
Go to the documentation of this file.
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#pragma once
21
23
24#include <cstdint>
25#include <memory>
26#include <string_view>
27#include <utility>
28#include <variant>
29
30#include "iceberg/expression/literal.h"
31#include "iceberg/iceberg_export.h"
32#include "iceberg/result.h"
33#include "iceberg/type_fwd.h"
35
36namespace iceberg {
37
39enum class TransformType {
46 kBucket,
50 kYear,
52 kMonth,
54 kDay,
56 kHour,
58 kVoid,
59};
60
62ICEBERG_EXPORT constexpr std::string_view TransformTypeToString(TransformType type) {
63 switch (type) {
64 case TransformType::kUnknown:
65 return "unknown";
66 case TransformType::kIdentity:
67 return "identity";
68 case TransformType::kBucket:
69 return "bucket";
70 case TransformType::kTruncate:
71 return "truncate";
72 case TransformType::kYear:
73 return "year";
74 case TransformType::kMonth:
75 return "month";
76 case TransformType::kDay:
77 return "day";
78 case TransformType::kHour:
79 return "hour";
80 case TransformType::kVoid:
81 return "void";
82 }
83 std::unreachable();
84}
85
90class ICEBERG_EXPORT Transform : public util::Formattable {
91 public:
96 static std::shared_ptr<Transform> Identity();
97
103 static std::shared_ptr<Transform> Bucket(int32_t num_buckets);
104
110 static std::shared_ptr<Transform> Truncate(int32_t width);
111
116 static std::shared_ptr<Transform> Year();
117
122 static std::shared_ptr<Transform> Month();
123
128 static std::shared_ptr<Transform> Day();
129
134 static std::shared_ptr<Transform> Hour();
135
140 static std::shared_ptr<Transform> Void();
141
143 TransformType transform_type() const;
144
151 Result<std::shared_ptr<TransformFunction>> Bind(
152 const std::shared_ptr<Type>& source_type) const;
153
157 bool CanTransform(const Type& source_type) const;
158
160 bool PreservesOrder() const;
161
171 bool SatisfiesOrderOf(const Transform& other) const;
172
182 Result<std::unique_ptr<UnboundPredicate>> Project(
183 std::string_view name, const std::shared_ptr<BoundPredicate>& predicate);
184
194 Result<std::unique_ptr<UnboundPredicate>> ProjectStrict(
195 std::string_view name, const std::shared_ptr<BoundPredicate>& predicate);
196
201 Result<std::string> ToHumanString(const Literal& value);
202
204 std::string ToString() const override;
205
208 std::string DedupName() const;
209
213 Result<std::string> GeneratePartitionName(std::string_view source_name) const;
214
216 friend bool operator==(const Transform& lhs, const Transform& rhs) {
217 return lhs.Equals(rhs);
218 }
219
220 private:
223 explicit Transform(TransformType transform_type);
224
228 Transform(TransformType transform_type, int32_t param);
229
231 [[nodiscard]] virtual bool Equals(const Transform& other) const;
232
233 TransformType transform_type_;
235 std::variant<std::monostate, int32_t> param_;
236};
247ICEBERG_EXPORT Result<std::shared_ptr<Transform>> TransformFromString(
248 std::string_view transform_str);
249
251class ICEBERG_EXPORT TransformFunction {
252 public:
253 virtual ~TransformFunction() = default;
254 TransformFunction(TransformType transform_type, std::shared_ptr<Type> source_type);
258 virtual Result<Literal> Transform(const Literal& literal) = 0;
260 TransformType transform_type() const;
262 const std::shared_ptr<Type>& source_type() const;
269 virtual std::shared_ptr<Type> ResultType() const = 0;
270
271 friend bool operator==(const TransformFunction& lhs, const TransformFunction& rhs) {
272 return lhs.Equals(rhs);
273 }
274
275 private:
277 [[nodiscard]] virtual bool Equals(const TransformFunction& other) const;
278
279 TransformType transform_type_;
280 std::shared_ptr<Type> source_type_;
281};
282
283} // namespace iceberg
Literal is a literal value that is associated with a primitive type.
Definition literal.h:39
A transform function used for partitioning.
Definition transform.h:251
virtual std::shared_ptr< Type > ResultType() const =0
Get the result type of transform function.
virtual Result< Literal > Transform(const Literal &literal)=0
Transform an input Literal to a new Literal.
Represents a transform used in partitioning or sorting in Iceberg.
Definition transform.h:90
friend bool operator==(const Transform &lhs, const Transform &rhs)
Equality comparison.
Definition transform.h:216
Interface for a data type for a field.
Definition type.h:44
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
ICEBERG_EXPORT constexpr std::string_view TransformTypeToString(TransformType type)
Get the relative transform name.
Definition transform.h:62
TransformType
Transform types used for partitioning.
Definition transform.h:39
@ kBucket
Hash of value, mod N
@ kTruncate
Value truncated to width W
@ kHour
Extract a timestamp hour, as hours from 1970-01-01 00:00:00.
@ kDay
Extract a date or timestamp day, as days from 1970-01-01.
@ kMonth
Extract a date or timestamp month, as months from 1970-01.
@ kVoid
Always produces null
@ kIdentity
Equal to source value, unmodified.
@ kYear
Extract a date or timestamp year, as years from 1970.