Skip to content

Commit 1e337c4

Browse files
committed
fmt
1 parent 03c8e8f commit 1e337c4

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/iceberg/expression/literal.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ Result<std::vector<uint8_t>> LiteralSerializer::ToBytes(const Literal& literal)
465465
case TypeId::kBinary: {
466466
// Binary value (without length)
467467
const auto& binary_data = std::get<std::vector<uint8_t>>(value);
468-
result = binary_data;
468+
result.insert(result.end(), binary_data.begin(), binary_data.end());
469469
return result;
470470
}
471471

@@ -478,7 +478,11 @@ Result<std::vector<uint8_t>> LiteralSerializer::ToBytes(const Literal& literal)
478478
} else if (std::holds_alternative<std::vector<uint8_t>>(value)) {
479479
result = std::get<std::vector<uint8_t>>(value);
480480
} else {
481-
return InvalidArgument("Invalid value type for Fixed literal");
481+
std::string actual_type = std::visit(
482+
[](auto&& arg) -> std::string { return typeid(arg).name(); }, value);
483+
484+
return InvalidArgument("Invalid value type for Fixed literal, got type: {}",
485+
actual_type);
482486
}
483487
return result;
484488
}

src/iceberg/util/endian.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,27 @@ concept LittleEndianWritable =
3434
std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
3535
std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, uint8_t>;
3636

37+
/// \brief Convert a value to little-endian format.
38+
template <LittleEndianWritable T>
39+
T ToLittleEndian(T value) {
40+
if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
41+
return std::byteswap(value);
42+
}
43+
return value;
44+
}
45+
46+
/// \brief Convert a value from little-endian format.
47+
template <LittleEndianWritable T>
48+
T FromLittleEndian(T value) {
49+
return ToLittleEndian(value);
50+
}
51+
3752
/// \brief Write a value in little-endian format to the buffer.
3853
template <LittleEndianWritable T>
3954
void WriteLittleEndian(std::vector<uint8_t>& buffer, T value) {
40-
if constexpr (std::endian::native == std::endian::little) {
41-
const auto* bytes = reinterpret_cast<const uint8_t*>(&value);
42-
buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
43-
} else if constexpr (sizeof(T) > 1) {
44-
T le_value = std::byteswap(value);
45-
const auto* bytes = reinterpret_cast<const uint8_t*>(&le_value);
46-
buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
47-
} else {
48-
// For single byte types, no byteswap needed
49-
buffer.push_back(static_cast<uint8_t>(value));
50-
}
55+
T le_value = ToLittleEndian(value);
56+
const auto* bytes = reinterpret_cast<const uint8_t*>(&le_value);
57+
buffer.insert(buffer.end(), bytes, bytes + sizeof(T));
5158
}
5259

5360
/// \brief Read a value in little-endian format from the data.
@@ -60,11 +67,7 @@ Result<T> ReadLittleEndian(std::span<const uint8_t> data) {
6067

6168
T value;
6269
std::memcpy(&value, data.data(), sizeof(T));
63-
64-
if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
65-
value = std::byteswap(value);
66-
}
67-
return value;
70+
return FromLittleEndian(value);
6871
}
6972

7073
/// \brief Write a 16-byte value in big-endian format (for UUID and Decimal).

0 commit comments

Comments
 (0)