diff --git a/src/iceberg/expression/literal.cc b/src/iceberg/expression/literal.cc index a1222ff69..790b59a2d 100644 --- a/src/iceberg/expression/literal.cc +++ b/src/iceberg/expression/literal.cc @@ -348,7 +348,7 @@ bool Literal::operator==(const Literal& other) const { return (*this <=> other) // Three-way comparison operator std::partial_ordering Literal::operator<=>(const Literal& other) const { // If types are different, comparison is unordered - if (*type_ != *other.type_) { + if (type_->type_id() != other.type_->type_id()) { return std::partial_ordering::unordered; } diff --git a/src/iceberg/expression/literal.h b/src/iceberg/expression/literal.h index 13ffafe68..664b5a966 100644 --- a/src/iceberg/expression/literal.h +++ b/src/iceberg/expression/literal.h @@ -25,6 +25,7 @@ #include #include +#include "iceberg/iceberg_export.h" #include "iceberg/result.h" #include "iceberg/type.h" #include "iceberg/util/decimal.h" @@ -129,8 +130,12 @@ class ICEBERG_EXPORT Literal : public util::Formattable { bool operator==(const Literal& other) const; - /// \brief Compare two PrimitiveLiterals. Both literals must have the same type - /// and should not be AboveMax or BelowMin. + /// \brief Compare two literals of the same primitive type. + /// \param other The other literal to compare with. + /// \return The comparison result as std::partial_ordering. If either side is AboveMax, + /// BelowMin or Null, the result is unordered. + /// Note: This comparison cannot be used for sorting literals if any literal is + /// AboveMax, BelowMin or Null. std::partial_ordering operator<=>(const Literal& other) const; /// Check if this literal represents a value above the maximum allowed value diff --git a/src/iceberg/test/literal_test.cc b/src/iceberg/test/literal_test.cc index 084af12c5..23703c20d 100644 --- a/src/iceberg/test/literal_test.cc +++ b/src/iceberg/test/literal_test.cc @@ -611,6 +611,10 @@ INSTANTIATE_TEST_SUITE_P( .small_literal = Literal::Decimal(123456, 6, 2), .large_literal = Literal::Decimal(234567, 6, 2), .equal_literal = Literal::Decimal(123456, 6, 2)}, + ComparisonLiteralTestParam{.test_name = "DecimalDifferentScales", + .small_literal = Literal::Decimal(123456, 6, 2), + .large_literal = Literal::Decimal(1234567, 7, 3), + .equal_literal = Literal::Decimal(1234560, 7, 3)}, ComparisonLiteralTestParam{.test_name = "String", .small_literal = Literal::String("apple"), .large_literal = Literal::String("banana"), @@ -620,11 +624,21 @@ INSTANTIATE_TEST_SUITE_P( .small_literal = Literal::Binary(std::vector{0x01, 0x02}), .large_literal = Literal::Binary(std::vector{0x01, 0x03}), .equal_literal = Literal::Binary(std::vector{0x01, 0x02})}, + ComparisonLiteralTestParam{ + .test_name = "BinaryDifferentLengths", + .small_literal = Literal::Binary(std::vector{0x01, 0x02}), + .large_literal = Literal::Binary(std::vector{0x01, 0x02, 0x03}), + .equal_literal = Literal::Binary(std::vector{0x01, 0x02})}, ComparisonLiteralTestParam{ .test_name = "Fixed", .small_literal = Literal::Fixed(std::vector{0x01, 0x02}), .large_literal = Literal::Fixed(std::vector{0x01, 0x03}), .equal_literal = Literal::Fixed(std::vector{0x01, 0x02})}, + ComparisonLiteralTestParam{ + .test_name = "FixedDifferentLengths", + .small_literal = Literal::Fixed(std::vector{0x01, 0x02}), + .large_literal = Literal::Fixed(std::vector{0x01, 0x02, 0x03}), + .equal_literal = Literal::Fixed(std::vector{0x01, 0x02})}, ComparisonLiteralTestParam{.test_name = "Date", .small_literal = Literal::Date(100), .large_literal = Literal::Date(200),