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
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) {
65 return "unknown";
67 return "identity";
69 return "bucket";
71 return "truncate";
73 return "year";
75 return "month";
77 return "day";
79 return "hour";
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
144
152 const std::shared_ptr<Type>& source_type) const;
153
155 std::shared_ptr<Type> ResultType(const std::shared_ptr<Type>& source_type) const;
156
160 bool CanTransform(const Type& source_type) const;
161
165 Status Validate(const std::shared_ptr<Type>& source_type) const;
166
168 bool PreservesOrder() const;
169
179 bool SatisfiesOrderOf(const Transform& other) const;
180
191 std::string_view name, const std::shared_ptr<BoundPredicate>& predicate);
192
203 std::string_view name, const std::shared_ptr<BoundPredicate>& predicate);
204
210
212 std::string ToString() const override;
213
216 std::string DedupName() const;
217
221 Result<std::string> GeneratePartitionName(std::string_view source_name) const;
222
224 friend bool operator==(const Transform& lhs, const Transform& rhs) {
225 return lhs.Equals(rhs);
226 }
227
228 private:
231 explicit Transform(TransformType transform_type);
232
236 Transform(TransformType transform_type, int32_t param);
237
239 [[nodiscard]] virtual bool Equals(const Transform& other) const;
240
241 TransformType transform_type_;
243 std::variant<std::monostate, int32_t> param_;
244};
256 std::string_view transform_str);
257
259class ICEBERG_EXPORT TransformFunction {
260 public:
261 virtual ~TransformFunction() = default;
262 TransformFunction(TransformType transform_type, std::shared_ptr<Type> source_type);
266 virtual Result<Literal> Transform(const Literal& literal) = 0;
270 const std::shared_ptr<Type>& source_type() const;
277 virtual std::shared_ptr<Type> ResultType() const = 0;
278
279 friend bool operator==(const TransformFunction& lhs, const TransformFunction& rhs) {
280 return lhs.Equals(rhs);
281 }
282
283 protected:
285 [[nodiscard]] virtual bool Equals(const TransformFunction& other) const;
286
287 private:
288 TransformType transform_type_;
289 std::shared_ptr<Type> source_type_;
290};
291
292} // namespace iceberg
Literal is a literal value that is associated with a primitive type.
Definition literal.h:42
A transform function used for partitioning.
Definition transform.h:259
const std::shared_ptr< Type > & source_type() const
Get the source type of transform function.
virtual bool Equals(const TransformFunction &other) const
Compare the common properties of two transform functions for equality.
virtual std::shared_ptr< Type > ResultType() const =0
Get the result type of transform function.
TransformType transform_type() const
Get the transform type.
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
Result< std::unique_ptr< UnboundPredicate > > ProjectStrict(std::string_view name, const std::shared_ptr< BoundPredicate > &predicate)
Transforms a BoundPredicate to a strict predicate on the partition values produced by the transform.
Result< std::string > GeneratePartitionName(std::string_view source_name) const
Generates a partition name for the transform.
Result< std::shared_ptr< TransformFunction > > Bind(const std::shared_ptr< Type > &source_type) const
Binds this transform to a source type, returning a typed TransformFunction.
static std::shared_ptr< Transform > Month()
Creates a shared singleton instance of the Month transform.
bool CanTransform(const Type &source_type) const
Checks whether this function can be applied to the given Type.
std::string DedupName() const
Return the unique transform name to check if similar transforms for the same source field are added m...
friend bool operator==(const Transform &lhs, const Transform &rhs)
Equality comparison.
Definition transform.h:224
static std::shared_ptr< Transform > Bucket(int32_t num_buckets)
Creates a shared instance of the Bucket transform.
static std::shared_ptr< Transform > Hour()
Creates a shared singleton instance of the Hour transform.
std::shared_ptr< Type > ResultType(const std::shared_ptr< Type > &source_type) const
Return the type produced by this transform for a source type.
static std::shared_ptr< Transform > Year()
Creates a shared singleton instance of the Year transform.
static std::shared_ptr< Transform > Day()
Creates a shared singleton instance of the Day transform.
bool SatisfiesOrderOf(const Transform &other) const
Whether ordering by this transform's result satisfies the ordering of another transform's result.
Status Validate(const std::shared_ptr< Type > &source_type) const
Validates whether this transform can bind to the given source type.
Result< std::unique_ptr< UnboundPredicate > > Project(std::string_view name, const std::shared_ptr< BoundPredicate > &predicate)
Transforms a BoundPredicate to an inclusive predicate on the partition values produced by the transfo...
static std::shared_ptr< Transform > Identity()
Returns a shared singleton instance of the Identity transform.
static std::shared_ptr< Transform > Truncate(int32_t width)
Creates a shared instance of the Truncate transform.
static std::shared_ptr< Transform > Void()
Creates a shared singleton instance of the Void transform.
std::string ToString() const override
Returns a string representation of this transform (e.g., "bucket[16]").
bool PreservesOrder() const
Whether the transform preserves the order of values (is monotonic).
TransformType transform_type() const
Returns the transform type.
Result< std::string > ToHumanString(const Literal &value)
Returns a human-readable string representation of a transformed value.
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
Define symbol visibility macros for core Iceberg APIs.
Define typed literal values used by expressions.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
ICEBERG_EXPORT Result< std::shared_ptr< Transform > > TransformFromString(std::string_view transform_str)
Converts a string representation of a transform into a Transform instance.
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.
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.