Skip to content

Commit fbc9d10

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

File tree

9 files changed

+488
-385
lines changed

9 files changed

+488
-385
lines changed

src/iceberg/expression/literal.cc

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "iceberg/exception.h"
2828
#include "iceberg/util/checked_cast.h"
2929
#include "iceberg/util/conversions.h"
30-
#include "iceberg/util/decimal.h"
3130
#include "iceberg/util/macros.h"
3231

3332
namespace iceberg {
@@ -190,11 +189,14 @@ Result<Literal> LiteralCaster::CastFromString(
190189
const auto& str_val = std::get<std::string>(literal.value_);
191190

192191
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+
}
193196
case TypeId::kDate:
194197
case TypeId::kTime:
195198
case TypeId::kTimestamp:
196199
case TypeId::kTimestampTz:
197-
case TypeId::kUuid:
198200
return NotImplemented("Cast from String to {} is not implemented yet",
199201
target_type->ToString());
200202
default:
@@ -337,31 +339,39 @@ std::strong_ordering CompareFloat(T lhs, T rhs) {
337339
return lhs_is_negative <=> rhs_is_negative;
338340
}
339341

340-
std::strong_ordering CompareDecimal(Literal const& lhs, Literal const& rhs) {
342+
std::partial_ordering CompareDecimal(Literal const& lhs, Literal const& rhs) {
341343
ICEBERG_DCHECK(std::holds_alternative<Decimal>(lhs.value()),
342344
"LHS of decimal comparison must hold Decimal");
343345
ICEBERG_DCHECK(std::holds_alternative<Decimal>(rhs.value()),
344346
"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());
347349
auto lhs_decimal = std::get<Decimal>(lhs.value());
348350
auto rhs_decimal = std::get<Decimal>(rhs.value());
349351
if (lhs_type->scale() == rhs_type->scale()) {
350352
return lhs_decimal <=> rhs_decimal;
351353
} else if (lhs_type->scale() > rhs_type->scale()) {
352354
// Rescale to larger scale
353355
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;
357363
}
358364
return lhs_decimal <=> rhs_res.value();
359365
} else {
360366
// Rescale to larger scale
361367
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;
365375
}
366376
return lhs_res.value() <=> rhs_decimal;
367377
}

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

src/iceberg/test/literal_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ INSTANTIATE_TEST_SUITE_P(
719719
.target_type = fixed(4),
720720
.expected_literal = Literal::Fixed(std::vector<uint8_t>{
721721
0x01, 0x02, 0x03, 0x04})},
722+
// String cast tests
723+
CastLiteralTestParam{
724+
.test_name = "StringToUuid",
725+
.source_literal = Literal::String("123e4567-e89b-12d3-a456-426614174000"),
726+
.target_type = uuid(),
727+
.expected_literal = Literal::UUID(
728+
Uuid::FromString("123e4567-e89b-12d3-a456-426614174000").value())},
722729
// Same type cast test
723730
CastLiteralTestParam{.test_name = "IntToInt",
724731
.source_literal = Literal::Int(42),

0 commit comments

Comments
 (0)