Skip to content

Commit a74dfdf

Browse files
committed
fix: review comments and refactor transform test
1 parent 5e9bb1b commit a74dfdf

File tree

8 files changed

+478
-383
lines changed

8 files changed

+478
-383
lines changed

src/iceberg/expression/literal.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/iceberg/result.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum class ErrorKind {
4747
kNotFound,
4848
kNotImplemented,
4949
kNotSupported,
50+
kRescaleDataLoss,
5051
kUnknownError,
5152
};
5253

@@ -95,6 +96,7 @@ DEFINE_ERROR_FUNCTION(NotAllowed)
9596
DEFINE_ERROR_FUNCTION(NotFound)
9697
DEFINE_ERROR_FUNCTION(NotImplemented)
9798
DEFINE_ERROR_FUNCTION(NotSupported)
99+
DEFINE_ERROR_FUNCTION(RescaleDataLoss)
98100
DEFINE_ERROR_FUNCTION(UnknownError)
99101

100102
#undef DEFINE_ERROR_FUNCTION

src/iceberg/test/decimal_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ TEST(DecimalTest, Rescale) {
712712
ASSERT_EQ(Decimal(5), Decimal(500000).Rescale(6, 1).value());
713713
ASSERT_EQ(Decimal(500000), Decimal(5).Rescale(1, 6).value());
714714

715-
ASSERT_THAT(Decimal(5555555).Rescale(6, 1), IsError(ErrorKind::kInvalid));
715+
ASSERT_THAT(Decimal(5555555).Rescale(6, 1), IsError(ErrorKind::kRescaleDataLoss));
716716
}
717717

718718
} // namespace iceberg

0 commit comments

Comments
 (0)