zhjwpku commented on code in PR #238:
URL: https://github.com/apache/iceberg-cpp/pull/238#discussion_r2436582271
##########
src/iceberg/expression/literal.cc:
##########
@@ -193,12 +205,44 @@ std::strong_ordering CompareFloat(T lhs, T rhs) {
return lhs_is_negative <=> rhs_is_negative;
}
+std::strong_ordering CompareDecimal(Literal const& lhs, Literal const& rhs) {
+ ICEBERG_DCHECK(std::holds_alternative<Decimal>(lhs.value()),
+ "LHS of decimal comparison must hold Decimal");
+ ICEBERG_DCHECK(std::holds_alternative<Decimal>(rhs.value()),
+ "RHS of decimal comparison must hold decimal");
+ const auto& lhs_type = std::dynamic_pointer_cast<DecimalType>(lhs.type());
+ const auto& rhs_type = std::dynamic_pointer_cast<DecimalType>(rhs.type());
+ ICEBERG_DCHECK(lhs_type != nullptr, "LHS type must be DecimalType");
+ ICEBERG_DCHECK(rhs_type != nullptr, "RHS type must be DecimalType");
+ auto lhs_decimal = std::get<Decimal>(lhs.value());
+ auto rhs_decimal = std::get<Decimal>(rhs.value());
+ if (lhs_type->scale() == rhs_type->scale()) {
+ return lhs_decimal <=> rhs_decimal;
+ } else if (lhs_type->scale() > rhs_type->scale()) {
+ // Rescale to larger scale
+ auto rhs_res = rhs_decimal.Rescale(rhs_type->scale(), lhs_type->scale());
+ if (!rhs_res) {
+ // Rescale would cause data loss, so lhs is definitely less than rhs
+ return std::strong_ordering::less;
+ }
+ return lhs_decimal <=> rhs_res.value();
+ } else {
+ // Rescale to larger scale
+ auto lhs_res = lhs_decimal.Rescale(lhs_type->scale(), rhs_type->scale());
+ if (!lhs_res) {
+ // Rescale would cause data loss, so lhs is definitely greater than rhs
+ return std::strong_ordering::greater;
+ }
+ return lhs_res.value() <=> rhs_decimal;
+ }
+}
+
bool Literal::operator==(const Literal& other) const { return (*this <=>
other) == 0; }
// 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()) {
Review Comment:
I plan to look into this later in a separate PR ;)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]