Skip to content

Commit 4cd69a1

Browse files
committed
feat: implement literal expressions with binary serialization support
1 parent 1cea1e9 commit 4cd69a1

File tree

10 files changed

+602
-66
lines changed

10 files changed

+602
-66
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ set(ICEBERG_SOURCES
4949
manifest_reader_internal.cc
5050
manifest_writer.cc
5151
arrow_c_data_guard_internal.cc
52+
util/conversions.cc
5253
util/decimal.cc
54+
util/gzip_internal.cc
5355
util/murmurhash3_internal.cc
54-
util/timepoint.cc
55-
util/gzip_internal.cc)
56+
util/timepoint.cc)
5657

5758
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
5859
set(ICEBERG_SHARED_BUILD_INTERFACE_LIBS)

src/iceberg/expression/literal.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <concepts>
2424

2525
#include "iceberg/exception.h"
26+
#include "iceberg/util/conversions.h"
2627

2728
namespace iceberg {
2829

@@ -151,11 +152,11 @@ Literal Literal::Binary(std::vector<uint8_t> value) {
151152

152153
Result<Literal> Literal::Deserialize(std::span<const uint8_t> data,
153154
std::shared_ptr<PrimitiveType> type) {
154-
return NotImplemented("Deserialization of Literal is not implemented yet");
155+
return Conversions::FromBytes(type, data);
155156
}
156157

157158
Result<std::vector<uint8_t>> Literal::Serialize() const {
158-
return NotImplemented("Serialization of Literal is not implemented yet");
159+
return Conversions::ToBytes(*this);
159160
}
160161

161162
// Getters

src/iceberg/expression/literal.h

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,76 @@ class ICEBERG_EXPORT Literal {
143143
private:
144144
Literal(Value value, std::shared_ptr<PrimitiveType> type);
145145

146+
friend class Conversions;
146147
friend class LiteralCaster;
147148

148-
private:
149149
Value value_;
150150
std::shared_ptr<PrimitiveType> type_;
151151
};
152152

153+
template <TypeId type_id>
154+
struct LiteralTraits {
155+
using ValueType = void;
156+
};
157+
158+
template <>
159+
struct LiteralTraits<TypeId::kBoolean> {
160+
using ValueType = bool;
161+
};
162+
163+
template <>
164+
struct LiteralTraits<TypeId::kInt> {
165+
using ValueType = int32_t;
166+
};
167+
168+
template <>
169+
struct LiteralTraits<TypeId::kDate> {
170+
using ValueType = int32_t;
171+
};
172+
173+
template <>
174+
struct LiteralTraits<TypeId::kLong> {
175+
using ValueType = int64_t;
176+
};
177+
178+
template <>
179+
struct LiteralTraits<TypeId::kTime> {
180+
using ValueType = int64_t;
181+
};
182+
183+
template <>
184+
struct LiteralTraits<TypeId::kTimestamp> {
185+
using ValueType = int64_t;
186+
};
187+
188+
template <>
189+
struct LiteralTraits<TypeId::kTimestampTz> {
190+
using ValueType = int64_t;
191+
};
192+
193+
template <>
194+
struct LiteralTraits<TypeId::kFloat> {
195+
using ValueType = float;
196+
};
197+
198+
template <>
199+
struct LiteralTraits<TypeId::kDouble> {
200+
using ValueType = double;
201+
};
202+
203+
template <>
204+
struct LiteralTraits<TypeId::kString> {
205+
using ValueType = std::string;
206+
};
207+
208+
template <>
209+
struct LiteralTraits<TypeId::kBinary> {
210+
using ValueType = std::vector<uint8_t>;
211+
};
212+
213+
template <>
214+
struct LiteralTraits<TypeId::kFixed> {
215+
using ValueType = std::vector<uint8_t>;
216+
};
217+
153218
} // namespace iceberg

src/iceberg/type.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <format>
2323
#include <iterator>
2424
#include <memory>
25+
#include <utility>
2526

2627
#include "iceberg/exception.h"
2728
#include "iceberg/util/formatter.h" // IWYU pragma: keep
@@ -386,4 +387,45 @@ std::shared_ptr<StructType> struct_(std::vector<SchemaField> fields) {
386387
return std::make_shared<StructType>(std::move(fields));
387388
}
388389

390+
std::string_view ToString(TypeId id) {
391+
switch (id) {
392+
case TypeId::kStruct:
393+
return "struct";
394+
case TypeId::kList:
395+
return "list";
396+
case TypeId::kMap:
397+
return "map";
398+
case TypeId::kBoolean:
399+
return "boolean";
400+
case TypeId::kInt:
401+
return "int";
402+
case TypeId::kLong:
403+
return "long";
404+
case TypeId::kFloat:
405+
return "float";
406+
case TypeId::kDouble:
407+
return "double";
408+
case TypeId::kDecimal:
409+
return "decimal";
410+
case TypeId::kDate:
411+
return "date";
412+
case TypeId::kTime:
413+
return "time";
414+
case TypeId::kTimestamp:
415+
return "timestamp";
416+
case TypeId::kTimestampTz:
417+
return "timestamptz";
418+
case TypeId::kString:
419+
return "string";
420+
case TypeId::kUuid:
421+
return "uuid";
422+
case TypeId::kFixed:
423+
return "fixed";
424+
case TypeId::kBinary:
425+
return "binary";
426+
}
427+
428+
std::unreachable();
429+
}
430+
389431
} // namespace iceberg

src/iceberg/type.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,13 @@ ICEBERG_EXPORT std::shared_ptr<MapType> map(SchemaField key, SchemaField value);
531531

532532
/// @}
533533

534+
/// \brief Get the lowercase string representation of a TypeId.
535+
///
536+
/// This returns the same lowercase string as used by Type::ToString() methods.
537+
/// For example: TypeId::kBoolean -> "boolean", TypeId::kInt -> "int", etc.
538+
///
539+
/// \param id The TypeId to convert to string
540+
/// \return A string_view containing the lowercase type name
541+
ICEBERG_EXPORT std::string_view ToString(TypeId id);
542+
534543
} // namespace iceberg

0 commit comments

Comments
 (0)