@@ -337,31 +337,39 @@ std::strong_ordering CompareFloat(T lhs, T rhs) {
337337 return lhs_is_negative <=> rhs_is_negative;
338338}
339339
340- std::strong_ordering CompareDecimal (Literal const & lhs, Literal const & rhs) {
340+ std::partial_ordering CompareDecimal (Literal const & lhs, Literal const & rhs) {
341341 ICEBERG_DCHECK (std::holds_alternative<Decimal>(lhs.value ()),
342342 " LHS of decimal comparison must hold Decimal" );
343343 ICEBERG_DCHECK (std::holds_alternative<Decimal>(rhs.value ()),
344344 " 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 ());
345+ auto lhs_type = internal::checked_pointer_cast <DecimalType>(lhs.type ());
346+ auto rhs_type = internal::checked_pointer_cast <DecimalType>(rhs.type ());
347347 auto lhs_decimal = std::get<Decimal>(lhs.value ());
348348 auto rhs_decimal = std::get<Decimal>(rhs.value ());
349349 if (lhs_type->scale () == rhs_type->scale ()) {
350350 return lhs_decimal <=> rhs_decimal;
351351 } else if (lhs_type->scale () > rhs_type->scale ()) {
352352 // Rescale to larger scale
353353 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;
354+ if (!rhs_res.has_value ()) {
355+ if (rhs_res.error ().kind == ErrorKind::kRescaleDataLoss ) {
356+ // Rescale would cause data loss, so lhs is definitely less than rhs
357+ return std::partial_ordering::less;
358+ }
359+ // Other errors should return unordered
360+ return std::partial_ordering::unordered;
357361 }
358362 return lhs_decimal <=> rhs_res.value ();
359363 } else {
360364 // Rescale to larger scale
361365 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;
366+ if (!lhs_res.has_value ()) {
367+ if (lhs_res.error ().kind == ErrorKind::kRescaleDataLoss ) {
368+ // Rescale would cause data loss, so lhs is definitely greater than rhs
369+ return std::partial_ordering::greater;
370+ }
371+ // Other errors should return unordered
372+ return std::partial_ordering::unordered;
365373 }
366374 return lhs_res.value () <=> rhs_decimal;
367375 }
0 commit comments