|
21 | 21 |
|
22 | 22 | #include <concepts> |
23 | 23 | #include <cstdint> |
| 24 | +#include <string> |
24 | 25 |
|
25 | 26 | #include "iceberg/exception.h" |
| 27 | +#include "iceberg/type_fwd.h" |
26 | 28 |
|
27 | 29 | namespace iceberg { |
28 | 30 |
|
@@ -294,6 +296,15 @@ Literal Literal::Fixed(std::vector<uint8_t> value) { |
294 | 296 | return {Value{std::move(value)}, fixed(size)}; |
295 | 297 | } |
296 | 298 |
|
| 299 | +Literal Literal::Decimal(const iceberg::Decimal& value, int32_t precision, |
| 300 | + int32_t scale) { |
| 301 | + return {Value{value.value()}, decimal(precision, scale)}; |
| 302 | +} |
| 303 | + |
| 304 | +Literal Literal::Decimal(int128_t value, int32_t precision, int32_t scale) { |
| 305 | + return {Value{value}, decimal(precision, scale)}; |
| 306 | +} |
| 307 | + |
297 | 308 | Result<Literal> Literal::Deserialize(std::span<const uint8_t> data, |
298 | 309 | std::shared_ptr<PrimitiveType> type) { |
299 | 310 | return NotImplemented("Deserialization of Literal is not implemented yet"); |
@@ -403,6 +414,8 @@ std::partial_ordering Literal::operator<=>(const Literal& other) const { |
403 | 414 | } |
404 | 415 |
|
405 | 416 | std::string Literal::ToString() const { |
| 417 | + auto invalid_ = [this]() { return std::format(" = {}", type_->ToString()); }; |
| 418 | + |
406 | 419 | if (std::holds_alternative<BelowMin>(value_)) { |
407 | 420 | return "belowMin"; |
408 | 421 | } |
@@ -452,9 +465,20 @@ std::string Literal::ToString() const { |
452 | 465 | case TypeId::kDate: { |
453 | 466 | return std::to_string(std::get<int32_t>(value_)); |
454 | 467 | } |
455 | | - case TypeId::kDecimal: |
| 468 | + case TypeId::kDecimal: { |
| 469 | + const auto unscaled_value = std::get<int128_t>(value_); |
| 470 | + auto decimal_type = std::static_pointer_cast<DecimalType>(type_); |
| 471 | + int32_t scale = decimal_type->scale(); |
| 472 | + |
| 473 | + iceberg::Decimal decimal_val(unscaled_value); |
| 474 | + Result<std::string> str_res = decimal_val.ToString(scale); |
| 475 | + if (str_res.has_value()) { |
| 476 | + return str_res.value(); |
| 477 | + } |
| 478 | + return |
| 479 | + } |
456 | 480 | case TypeId::kUuid: { |
457 | | - throw NotImplemented("kDecimal and kUuid are not implemented yet"); |
| 481 | + return {"kDecimal and kUuid are not implemented yet"}; |
458 | 482 | } |
459 | 483 | default: { |
460 | 484 | throw IcebergError("Unknown type: " + type_->ToString()); |
|
0 commit comments