This is an automated email from the ASF dual-hosted git repository.

gangwu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new e393cb4  feat: fix literal comparison operator (#275)
e393cb4 is described below

commit e393cb4003b89d9e51f963acf1be9a8a8b06be02
Author: Junwang Zhao <[email protected]>
AuthorDate: Mon Oct 27 13:17:12 2025 +0800

    feat: fix literal comparison operator (#275)
    
    - Enable comparison for Fixed type with different length and Decimal type 
with different scale.
    - Add some test cases to verify that.
---
 src/iceberg/expression/literal.cc |  2 +-
 src/iceberg/expression/literal.h  |  9 +++++++--
 src/iceberg/test/literal_test.cc  | 14 ++++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/iceberg/expression/literal.cc 
b/src/iceberg/expression/literal.cc
index a1222ff..790b59a 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 13ffafe..664b5a9 100644
--- a/src/iceberg/expression/literal.h
+++ b/src/iceberg/expression/literal.h
@@ -25,6 +25,7 @@
 #include <variant>
 #include <vector>
 
+#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 084af12..23703c2 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<uint8_t>{0x01, 0x02}),
             .large_literal = Literal::Binary(std::vector<uint8_t>{0x01, 0x03}),
             .equal_literal = Literal::Binary(std::vector<uint8_t>{0x01, 
0x02})},
+        ComparisonLiteralTestParam{
+            .test_name = "BinaryDifferentLengths",
+            .small_literal = Literal::Binary(std::vector<uint8_t>{0x01, 0x02}),
+            .large_literal = Literal::Binary(std::vector<uint8_t>{0x01, 0x02, 
0x03}),
+            .equal_literal = Literal::Binary(std::vector<uint8_t>{0x01, 
0x02})},
         ComparisonLiteralTestParam{
             .test_name = "Fixed",
             .small_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x02}),
             .large_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x03}),
             .equal_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x02})},
+        ComparisonLiteralTestParam{
+            .test_name = "FixedDifferentLengths",
+            .small_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x02}),
+            .large_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x02, 
0x03}),
+            .equal_literal = Literal::Fixed(std::vector<uint8_t>{0x01, 0x02})},
         ComparisonLiteralTestParam{.test_name = "Date",
                                    .small_literal = Literal::Date(100),
                                    .large_literal = Literal::Date(200),

Reply via email to