Skip to content

Commit 89ad9e9

Browse files
author
xiao.dong
committed
fix literal comparable
1 parent 786b5f4 commit 89ad9e9

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/iceberg/expression/literal.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,28 @@ std::strong_ordering CompareFloat(T lhs, T rhs) {
343343
return lhs_is_negative <=> rhs_is_negative;
344344
}
345345

346+
bool Literal::Comparable(TypeId type_id, TypeId other_type_id) {
347+
switch (type_id) {
348+
case TypeId::kInt:
349+
case TypeId::kDate:
350+
return other_type_id == TypeId::kInt || other_type_id == TypeId::kDate;
351+
case TypeId::kLong:
352+
case TypeId::kTimestamp:
353+
case TypeId::kTimestampTz:
354+
return other_type_id == TypeId::kLong || other_type_id == TypeId::kTimestamp ||
355+
other_type_id == TypeId::kTimestampTz;
356+
default:
357+
return type_id == other_type_id;
358+
}
359+
}
360+
346361
bool Literal::operator==(const Literal& other) const { return (*this <=> other) == 0; }
347362

348363
// Three-way comparison operator
349364
std::partial_ordering Literal::operator<=>(const Literal& other) const {
350-
// If types are different and value type not the same, comparison is unordered
351-
if (value_.index() != other.value_.index() &&
352-
type_->type_id() != other.type_->type_id()) {
365+
// If types are different, comparison is unordered
366+
// (Int & Date) (Timestamp & Long) were excluded from this check to allow comparison
367+
if (!Comparable(type_->type_id(), other.type_->type_id())) {
353368
return std::partial_ordering::unordered;
354369
}
355370

src/iceberg/expression/literal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class ICEBERG_EXPORT Literal : public util::Formattable {
128128
/// was not valid
129129
Result<Literal> CastTo(const std::shared_ptr<PrimitiveType>& target_type) const;
130130

131+
static bool Comparable(TypeId type_id, TypeId other_type_id);
132+
131133
bool operator==(const Literal& other) const;
132134

133135
/// \brief Compare two literals of the same primitive type.

src/iceberg/test/inclusive_metrics_evaluator_with_transform_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ TEST_F(InclusiveMetricsEvaluatorWithTransformTest, IntegerNotEqRewritten) {
371371
}
372372

373373
TEST_F(InclusiveMetricsEvaluatorWithTransformTest, CaseInsensitiveIntegerNotEqRewritten) {
374-
auto day_ts = ToBoundTransform(Expressions::Day("ts"));
374+
auto day_ts = ToBoundTransform(Expressions::Day("TS"));
375375
ExpectShouldRead(
376376
Expressions::Not(Expressions::Equal(day_ts, Literal::Long(kIntMinValue - 25))),
377377
kRowsMightMatch, nullptr, false);

0 commit comments

Comments
 (0)