iceberg-cpp
Loading...
Searching...
No Matches
literal.h
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
22#include <compare>
23#include <memory>
24#include <string>
25#include <variant>
26#include <vector>
27
28#include "iceberg/iceberg_export.h"
29#include "iceberg/result.h"
30#include "iceberg/type.h"
33#include "iceberg/util/int128.h"
34#include "iceberg/util/uuid.h"
35
36namespace iceberg {
37
39class ICEBERG_EXPORT Literal : public util::Formattable {
40 public:
44 struct BelowMin {
45 bool operator==(const BelowMin&) const = default;
46 std::strong_ordering operator<=>(const BelowMin&) const = default;
47 };
48
52 struct AboveMax {
53 bool operator==(const AboveMax&) const = default;
54 std::strong_ordering operator<=>(const AboveMax&) const = default;
55 };
56 using Value = std::variant<std::monostate, // for null
57 bool, // for boolean
58 int32_t, // for int, date
59 int64_t, // for long, timestamp, timestamp_tz, time
60 float, // for float
61 double, // for double
62 std::string, // for string
63 std::vector<uint8_t>, // for binary, fixed
64 ::iceberg::Decimal, // for decimal
65 Uuid, // for uuid
67
69 static Literal Boolean(bool value);
70 static Literal Int(int32_t value);
71 static Literal Date(int32_t value);
72 static Literal Long(int64_t value);
73 static Literal Time(int64_t value);
74 static Literal Timestamp(int64_t value);
75 static Literal TimestampTz(int64_t value);
76 static Literal Float(float value);
77 static Literal Double(double value);
78 static Literal String(std::string value);
79 static Literal UUID(Uuid value);
80 static Literal Binary(std::vector<uint8_t> value);
81 static Literal Fixed(std::vector<uint8_t> value);
82
85 static Literal Decimal(int128_t value, int32_t precision, int32_t scale);
86
88 static Literal Null(std::shared_ptr<PrimitiveType> type) {
89 return {Value{std::monostate{}}, std::move(type)};
90 }
91
96 static Result<Literal> Deserialize(std::span<const uint8_t> data,
97 std::shared_ptr<PrimitiveType> type);
98
103 Result<std::vector<uint8_t>> Serialize() const;
104
106 const std::shared_ptr<PrimitiveType>& type() const;
107
109 const Value& value() const { return value_; }
110
129 Result<Literal> CastTo(const std::shared_ptr<PrimitiveType>& target_type) const;
130
131 bool operator==(const Literal& other) const;
132
139 std::partial_ordering operator<=>(const Literal& other) const;
140
145 bool IsAboveMax() const;
146
151 bool IsBelowMin() const;
152
155 bool IsNull() const;
156
159 bool IsNaN() const;
160
161 std::string ToString() const override;
162
163 private:
164 Literal(Value value, std::shared_ptr<PrimitiveType> type);
165
166 friend class Conversions;
167 friend class LiteralCaster;
168
169 Value value_;
170 std::shared_ptr<PrimitiveType> type_;
171};
172
174struct ICEBERG_EXPORT LiteralValueHash {
175 std::size_t operator()(const Literal::Value& value) const noexcept;
176};
177
178struct ICEBERG_EXPORT LiteralHash {
179 std::size_t operator()(const Literal& value) const noexcept {
180 return LiteralValueHash{}(value.value());
181 }
182};
183
184template <TypeId type_id>
186 using ValueType = void;
187};
188
189#define DEFINE_LITERAL_TRAIT(TYPE_ID, VALUE_TYPE) \
190 template <> \
191 struct LiteralTraits<TypeId::TYPE_ID> { \
192 using ValueType = VALUE_TYPE; \
193 };
194
195DEFINE_LITERAL_TRAIT(kBoolean, bool)
196DEFINE_LITERAL_TRAIT(kInt, int32_t)
197DEFINE_LITERAL_TRAIT(kDate, int32_t)
198DEFINE_LITERAL_TRAIT(kLong, int64_t)
199DEFINE_LITERAL_TRAIT(kTime, int64_t)
200DEFINE_LITERAL_TRAIT(kTimestamp, int64_t)
201DEFINE_LITERAL_TRAIT(kTimestampTz, int64_t)
202DEFINE_LITERAL_TRAIT(kFloat, float)
203DEFINE_LITERAL_TRAIT(kDouble, double)
204DEFINE_LITERAL_TRAIT(kDecimal, Decimal)
205DEFINE_LITERAL_TRAIT(kString, std::string)
206DEFINE_LITERAL_TRAIT(kUuid, Uuid)
207DEFINE_LITERAL_TRAIT(kBinary, std::vector<uint8_t>)
208DEFINE_LITERAL_TRAIT(kFixed, std::vector<uint8_t>)
209
210#undef DEFINE_LITERAL_TRAIT
211
212} // 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
LiteralCaster handles type casting operations for Literal. This is an internal implementation class.
Definition literal.cc:41
Literal is a literal value that is associated with a primitive type.
Definition literal.h:39
static Literal Null(std::shared_ptr< PrimitiveType > type)
Create a literal representing a null value.
Definition literal.h:88
const Value & value() const
Get the literal value.
Definition literal.h:109
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....
128-bit integer type
Definition literal.h:178
Definition literal.h:185
Hash function for Literal to facilitate use in unordered containers.
Definition literal.h:174
Sentinel value to indicate that the literal value is above the valid range of a specific primitive ty...
Definition literal.h:52
Sentinel value to indicate that the literal value is below the valid range of a specific primitive ty...
Definition literal.h:44
UUID (Universally Unique Identifier) representation.