Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ set(ICEBERG_SOURCES
manifest_reader_internal.cc
manifest_writer.cc
arrow_c_data_guard_internal.cc
util/conversions.cc
util/decimal.cc
util/gzip_internal.cc
util/murmurhash3_internal.cc
util/timepoint.cc
util/gzip_internal.cc
util/uuid.cc)

set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
Expand Down
30 changes: 26 additions & 4 deletions src/iceberg/expression/literal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <concepts>

#include "iceberg/exception.h"
#include "iceberg/util/conversions.h"
#include "iceberg/util/macros.h"

namespace iceberg {

Expand Down Expand Up @@ -149,13 +151,18 @@ Literal Literal::Binary(std::vector<uint8_t> value) {
return {Value{std::move(value)}, binary()};
}

Literal Literal::Fixed(std::vector<uint8_t> value) {
auto length = static_cast<int32_t>(value.size());
return {Value{std::move(value)}, fixed(length)};
}

Result<Literal> Literal::Deserialize(std::span<const uint8_t> data,
std::shared_ptr<PrimitiveType> type) {
return NotImplemented("Deserialization of Literal is not implemented yet");
return Conversions::FromBytes(std::move(type), data);
}

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

// Getters
Expand Down Expand Up @@ -189,7 +196,7 @@ bool Literal::operator==(const Literal& other) const { return (*this <=> other)
// Three-way comparison operator
std::partial_ordering Literal::operator<=>(const Literal& other) const {
// If types are different, comparison is unordered
if (type_->type_id() != other.type_->type_id()) {
if (*type_ != *other.type_) {
return std::partial_ordering::unordered;
}

Expand All @@ -216,6 +223,7 @@ std::partial_ordering Literal::operator<=>(const Literal& other) const {
}

case TypeId::kLong:
case TypeId::kTime:
case TypeId::kTimestamp:
case TypeId::kTimestampTz: {
auto this_val = std::get<int64_t>(value_);
Expand Down Expand Up @@ -249,6 +257,12 @@ std::partial_ordering Literal::operator<=>(const Literal& other) const {
return this_val <=> other_val;
}

case TypeId::kFixed: {
auto& this_val = std::get<std::vector<uint8_t>>(value_);
auto& other_val = std::get<std::vector<uint8_t>>(other.value_);
return this_val <=> other_val;
}

default:
// For unsupported types, return unordered
return std::partial_ordering::unordered;
Expand Down Expand Up @@ -294,9 +308,17 @@ std::string Literal::ToString() const {
}
return result;
}
case TypeId::kFixed: {
const auto& fixed_data = std::get<std::vector<uint8_t>>(value_);
std::string result;
result.reserve(fixed_data.size() * 2); // 2 chars per byte
for (const auto& byte : fixed_data) {
std::format_to(std::back_inserter(result), "{:02X}", byte);
}
return result;
}
case TypeId::kDecimal:
case TypeId::kUuid:
case TypeId::kFixed:
case TypeId::kDate:
case TypeId::kTime:
case TypeId::kTimestamp:
Expand Down
68 changes: 67 additions & 1 deletion src/iceberg/expression/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ICEBERG_EXPORT Literal : public util::Formattable {
static Literal Double(double value);
static Literal String(std::string value);
static Literal Binary(std::vector<uint8_t> value);
static Literal Fixed(std::vector<uint8_t> value);

/// \brief Create a literal representing a null value.
static Literal Null(std::shared_ptr<PrimitiveType> type) {
Expand Down Expand Up @@ -144,11 +145,76 @@ class ICEBERG_EXPORT Literal : public util::Formattable {
private:
Literal(Value value, std::shared_ptr<PrimitiveType> type);

friend class Conversions;
friend class LiteralCaster;

private:
Value value_;
std::shared_ptr<PrimitiveType> type_;
};

template <TypeId type_id>
struct LiteralTraits {
using ValueType = void;
};

template <>
struct LiteralTraits<TypeId::kBoolean> {
using ValueType = bool;
};

template <>
struct LiteralTraits<TypeId::kInt> {
using ValueType = int32_t;
};

template <>
struct LiteralTraits<TypeId::kDate> {
using ValueType = int32_t;
};

template <>
struct LiteralTraits<TypeId::kLong> {
using ValueType = int64_t;
};

template <>
struct LiteralTraits<TypeId::kTime> {
using ValueType = int64_t;
};

template <>
struct LiteralTraits<TypeId::kTimestamp> {
using ValueType = int64_t;
};

template <>
struct LiteralTraits<TypeId::kTimestampTz> {
using ValueType = int64_t;
};

template <>
struct LiteralTraits<TypeId::kFloat> {
using ValueType = float;
};

template <>
struct LiteralTraits<TypeId::kDouble> {
using ValueType = double;
};

template <>
struct LiteralTraits<TypeId::kString> {
using ValueType = std::string;
};

template <>
struct LiteralTraits<TypeId::kBinary> {
using ValueType = std::vector<uint8_t>;
};

template <>
struct LiteralTraits<TypeId::kFixed> {
using ValueType = std::vector<uint8_t>;
};

} // namespace iceberg
Loading
Loading