iceberg-cpp
Loading...
Searching...
No Matches
literal.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
24
25#include <compare>
26#include <memory>
27#include <string>
28#include <variant>
29#include <vector>
30
32#include "iceberg/result.h"
33#include "iceberg/type.h"
36#include "iceberg/util/int128.h"
37#include "iceberg/util/uuid.h"
38
39namespace iceberg {
40
42class ICEBERG_EXPORT Literal : public util::Formattable {
43 public:
47 struct BelowMin {
48 bool operator==(const BelowMin&) const = default;
49 std::strong_ordering operator<=>(const BelowMin&) const = default;
50 };
51
55 struct AboveMax {
56 bool operator==(const AboveMax&) const = default;
57 std::strong_ordering operator<=>(const AboveMax&) const = default;
58 };
59 using Value = std::variant<std::monostate, // for null
60 bool, // for boolean
61 int32_t, // for int, date
62 int64_t, // for long, timestamp, timestamp_tz, time
63 float, // for float
64 double, // for double
65 std::string, // for string
66 std::vector<uint8_t>, // for binary, fixed
67 ::iceberg::Decimal, // for decimal
68 Uuid, // for uuid
70
72 static Literal Boolean(bool value);
73 static Literal Int(int32_t value);
74 static Literal Date(int32_t value);
75 static Literal Long(int64_t value);
76 static Literal Time(int64_t value);
77 static Literal Timestamp(int64_t value);
78 static Literal TimestampTz(int64_t value);
79 static Literal TimestampNs(int64_t value);
80 static Literal TimestampTzNs(int64_t value);
81 static Literal Float(float value);
82 static Literal Double(double value);
83 static Literal String(std::string value);
84 static Literal UUID(Uuid value);
85 static Literal Binary(std::vector<uint8_t> value);
86 static Literal Fixed(std::vector<uint8_t> value);
87
90 static Literal Decimal(int128_t value, int32_t precision, int32_t scale);
91
93 static Literal Null(std::shared_ptr<PrimitiveType> type) {
94 return {Value{std::monostate{}}, std::move(type)};
95 }
96
101 static Result<Literal> Deserialize(std::span<const uint8_t> data,
102 std::shared_ptr<PrimitiveType> type);
103
109
111 const std::shared_ptr<PrimitiveType>& type() const;
112
114 const Value& value() const { return value_; }
115
134 Result<Literal> CastTo(const std::shared_ptr<PrimitiveType>& target_type) const;
135
136 bool operator==(const Literal& other) const;
137
144 std::partial_ordering operator<=>(const Literal& other) const;
145
150 bool IsAboveMax() const;
151
156 bool IsBelowMin() const;
157
160 bool IsNull() const;
161
164 bool IsNaN() const;
165
166 std::string ToString() const override;
167
168 private:
169 Literal(Value value, std::shared_ptr<PrimitiveType> type);
170
171 friend class Conversions;
172 friend class LiteralCaster;
173
174 Value value_;
175 std::shared_ptr<PrimitiveType> type_;
176};
177
179struct ICEBERG_EXPORT LiteralValueHash {
180 std::size_t operator()(const Literal::Value& value) const noexcept;
181};
182
183struct ICEBERG_EXPORT LiteralHash {
184 std::size_t operator()(const Literal& value) const noexcept {
185 return LiteralValueHash{}(value.value());
186 }
187};
188
189template <TypeId type_id>
191 using ValueType = void;
192};
193
194#define DEFINE_LITERAL_TRAIT(TYPE_ID, VALUE_TYPE) \
195 template <> \
196 struct LiteralTraits<TypeId::TYPE_ID> { \
197 using ValueType = VALUE_TYPE; \
198 };
199
200DEFINE_LITERAL_TRAIT(kBoolean, bool)
201DEFINE_LITERAL_TRAIT(kInt, int32_t)
202DEFINE_LITERAL_TRAIT(kDate, int32_t)
203DEFINE_LITERAL_TRAIT(kLong, int64_t)
204DEFINE_LITERAL_TRAIT(kTime, int64_t)
205DEFINE_LITERAL_TRAIT(kTimestamp, int64_t)
206DEFINE_LITERAL_TRAIT(kTimestampTz, int64_t)
207DEFINE_LITERAL_TRAIT(kTimestampNs, int64_t)
208DEFINE_LITERAL_TRAIT(kTimestampTzNs, int64_t)
209DEFINE_LITERAL_TRAIT(kFloat, float)
210DEFINE_LITERAL_TRAIT(kDouble, double)
211DEFINE_LITERAL_TRAIT(kDecimal, Decimal)
212DEFINE_LITERAL_TRAIT(kString, std::string)
213DEFINE_LITERAL_TRAIT(kUuid, Uuid)
214DEFINE_LITERAL_TRAIT(kBinary, std::vector<uint8_t>)
215DEFINE_LITERAL_TRAIT(kFixed, std::vector<uint8_t>)
216
217#undef DEFINE_LITERAL_TRAIT
218
219} // namespace iceberg
Conversion utilities for primitive types.
Definition conversions.h:35
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Literal is a literal value that is associated with a primitive type.
Definition literal.h:42
std::string ToString() const override
Get a user-readable string representation.
static Literal Null(std::shared_ptr< PrimitiveType > type)
Create a literal representing a null value.
Definition literal.h:93
const Value & value() const
Get the literal value.
Definition literal.h:114
Result< Literal > CastTo(const std::shared_ptr< PrimitiveType > &target_type) const
Converts this literal to a literal of the given type.
Result< std::vector< uint8_t > > Serialize() const
Perform single-value serialization.
bool IsAboveMax() const
bool IsNaN() const
Check if this literal is NaN.
static Literal Decimal(int128_t value, int32_t precision, int32_t scale)
Create a decimal literal.
const std::shared_ptr< PrimitiveType > & type() const
Get the literal type.
static Result< Literal > Deserialize(std::span< const uint8_t > data, std::shared_ptr< PrimitiveType > type)
Restore a literal from single-value serialization.
bool IsBelowMin() const
bool IsNull() const
Check if this literal is null.
std::partial_ordering operator<=>(const Literal &other) const
Compare two literals of the same primitive type.
static Literal Boolean(bool value)
Factory methods for primitive types.
Definition uuid.h:36
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
128-bit fixed-point decimal numbers. Adapted from Apache Arrow with only Decimal128 support....
Define symbol visibility macros for core Iceberg APIs.
128-bit integer type
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
Definition literal.h:183
Definition literal.h:190
Hash function for Literal to facilitate use in unordered containers.
Definition literal.h:179
Sentinel value to indicate that the literal value is above the valid range of a specific primitive ty...
Definition literal.h:55
Sentinel value to indicate that the literal value is below the valid range of a specific primitive ty...
Definition literal.h:47
UUID (Universally Unique Identifier) representation.