|
27 | 27 | #include "iceberg/exception.h" |
28 | 28 | #include "iceberg/util/checked_cast.h" |
29 | 29 | #include "iceberg/util/conversions.h" |
30 | | -#include "iceberg/util/decimal.h" |
31 | 30 | #include "iceberg/util/macros.h" |
32 | 31 |
|
33 | 32 | namespace iceberg { |
@@ -190,11 +189,14 @@ Result<Literal> LiteralCaster::CastFromString( |
190 | 189 | const auto& str_val = std::get<std::string>(literal.value_); |
191 | 190 |
|
192 | 191 | switch (target_type->type_id()) { |
| 192 | + case TypeId::kUuid: { |
| 193 | + ICEBERG_ASSIGN_OR_RAISE(auto uuid, Uuid::FromString(str_val)); |
| 194 | + return Literal::UUID(uuid); |
| 195 | + } |
193 | 196 | case TypeId::kDate: |
194 | 197 | case TypeId::kTime: |
195 | 198 | case TypeId::kTimestamp: |
196 | 199 | case TypeId::kTimestampTz: |
197 | | - case TypeId::kUuid: |
198 | 200 | return NotImplemented("Cast from String to {} is not implemented yet", |
199 | 201 | target_type->ToString()); |
200 | 202 | default: |
@@ -337,31 +339,39 @@ std::strong_ordering CompareFloat(T lhs, T rhs) { |
337 | 339 | return lhs_is_negative <=> rhs_is_negative; |
338 | 340 | } |
339 | 341 |
|
340 | | -std::strong_ordering CompareDecimal(Literal const& lhs, Literal const& rhs) { |
| 342 | +std::partial_ordering CompareDecimal(Literal const& lhs, Literal const& rhs) { |
341 | 343 | ICEBERG_DCHECK(std::holds_alternative<Decimal>(lhs.value()), |
342 | 344 | "LHS of decimal comparison must hold Decimal"); |
343 | 345 | ICEBERG_DCHECK(std::holds_alternative<Decimal>(rhs.value()), |
344 | 346 | "RHS of decimal comparison must hold decimal"); |
345 | | - const auto& lhs_type = std::dynamic_pointer_cast<DecimalType>(lhs.type()); |
346 | | - const auto& rhs_type = std::dynamic_pointer_cast<DecimalType>(rhs.type()); |
| 347 | + auto lhs_type = internal::checked_pointer_cast<DecimalType>(lhs.type()); |
| 348 | + auto rhs_type = internal::checked_pointer_cast<DecimalType>(rhs.type()); |
347 | 349 | auto lhs_decimal = std::get<Decimal>(lhs.value()); |
348 | 350 | auto rhs_decimal = std::get<Decimal>(rhs.value()); |
349 | 351 | if (lhs_type->scale() == rhs_type->scale()) { |
350 | 352 | return lhs_decimal <=> rhs_decimal; |
351 | 353 | } else if (lhs_type->scale() > rhs_type->scale()) { |
352 | 354 | // Rescale to larger scale |
353 | 355 | auto rhs_res = rhs_decimal.Rescale(rhs_type->scale(), lhs_type->scale()); |
354 | | - if (!rhs_res) { |
355 | | - // Rescale would cause data loss, so lhs is definitely less than rhs |
356 | | - return std::strong_ordering::less; |
| 356 | + if (!rhs_res.has_value()) { |
| 357 | + if (rhs_res.error().kind == ErrorKind::kRescaleDataLoss) { |
| 358 | + // Rescale would cause data loss, so lhs is definitely less than rhs |
| 359 | + return std::partial_ordering::less; |
| 360 | + } |
| 361 | + // Other errors should return unordered |
| 362 | + return std::partial_ordering::unordered; |
357 | 363 | } |
358 | 364 | return lhs_decimal <=> rhs_res.value(); |
359 | 365 | } else { |
360 | 366 | // Rescale to larger scale |
361 | 367 | auto lhs_res = lhs_decimal.Rescale(lhs_type->scale(), rhs_type->scale()); |
362 | | - if (!lhs_res) { |
363 | | - // Rescale would cause data loss, so lhs is definitely greater than rhs |
364 | | - return std::strong_ordering::greater; |
| 368 | + if (!lhs_res.has_value()) { |
| 369 | + if (lhs_res.error().kind == ErrorKind::kRescaleDataLoss) { |
| 370 | + // Rescale would cause data loss, so lhs is definitely greater than rhs |
| 371 | + return std::partial_ordering::greater; |
| 372 | + } |
| 373 | + // Other errors should return unordered |
| 374 | + return std::partial_ordering::unordered; |
365 | 375 | } |
366 | 376 | return lhs_res.value() <=> rhs_decimal; |
367 | 377 | } |
|
0 commit comments