wgtmac commented on code in PR #387:
URL: https://github.com/apache/iceberg-cpp/pull/387#discussion_r2589336690


##########
src/iceberg/util/projection_util_internal.h:
##########
@@ -40,248 +42,228 @@ namespace iceberg {
 
 class ProjectionUtil {
  private:
+  static Result<Literal> AdjustLiteral(const Literal& literal, int adjustment) 
{
+    switch (literal.type()->type_id()) {
+      case TypeId::kInt:
+        return Literal::Int(std::get<int32_t>(literal.value()) + adjustment);
+      case TypeId::kLong:
+        return Literal::Long(std::get<int64_t>(literal.value()) + adjustment);
+      case TypeId::kDate:
+        return Literal::Date(std::get<int32_t>(literal.value()) + adjustment);
+      case TypeId::kTimestamp:
+        return Literal::Timestamp(std::get<int64_t>(literal.value()) + 
adjustment);
+      case TypeId::kTimestampTz:
+        return Literal::TimestampTz(std::get<int64_t>(literal.value()) + 
adjustment);
+      case TypeId::kDecimal: {
+        const auto& decimal_type =
+            internal::checked_cast<const DecimalType&>(*literal.type());
+        Decimal adjusted = std::get<Decimal>(literal.value()) + 
Decimal(adjustment);
+        return Literal::Decimal(adjusted.value(), decimal_type.precision(),
+                                decimal_type.scale());
+      }
+      default:
+        return NotSupported("{} is not a valid literal type for value 
adjustment",
+                            literal.type()->ToString());
+    }
+  }
+
+  static Result<Literal> PlusOne(const Literal& literal) {
+    return AdjustLiteral(literal, /*adjustment=*/+1);
+  }
+
+  static Result<Literal> MinusOne(const Literal& literal) {
+    return AdjustLiteral(literal, /*adjustment=*/-1);
+  }
+
+  static Result<std::unique_ptr<UnboundPredicate>> MakePredicate(
+      Expression::Operation op, std::string_view name,
+      const std::shared_ptr<TransformFunction>& func, const Literal& literal) {
+    ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReference::Make(std::string(name)));
+    ICEBERG_ASSIGN_OR_RAISE(auto lit, func->Transform(literal));
+    return UnboundPredicateImpl<BoundReference>::Make(op, std::move(ref), 
std::move(lit));
+  }
+
   static Result<std::unique_ptr<UnboundPredicate>> TransformSet(
-      std::string_view name, const std::shared_ptr<BoundSetPredicate>& 
predicate,
+      std::string_view name, const std::shared_ptr<BoundSetPredicate>& pred,
       const std::shared_ptr<TransformFunction>& func) {
     std::vector<Literal> transformed;
-    transformed.reserve(predicate->literal_set().size());
-    for (const auto& lit : predicate->literal_set()) {
+    transformed.reserve(pred->literal_set().size());
+    for (const auto& lit : pred->literal_set()) {
       ICEBERG_ASSIGN_OR_RAISE(auto transformed_lit, func->Transform(lit));
       transformed.push_back(std::move(transformed_lit));
     }
     ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReference::Make(std::string(name)));
-    return UnboundPredicateImpl<BoundReference>::Make(predicate->op(), 
std::move(ref),
+    return UnboundPredicateImpl<BoundReference>::Make(pred->op(), 
std::move(ref),
                                                       std::move(transformed));
   }
 
-  // General transform for all literal predicates.  This is used as a fallback 
for special
-  // cases that are not handled by the other transform functions.
-  static Result<std::unique_ptr<UnboundPredicate>> GenericTransform(
-      std::unique_ptr<NamedReference> ref,
-      const std::shared_ptr<BoundLiteralPredicate>& predicate,
+  static Result<std::unique_ptr<UnboundPredicate>> TruncateByteArray(
+      std::string_view name, const std::shared_ptr<BoundLiteralPredicate>& 
pred,
       const std::shared_ptr<TransformFunction>& func) {
-    ICEBERG_ASSIGN_OR_RAISE(auto transformed, 
func->Transform(predicate->literal()));
-    switch (predicate->op()) {
+    switch (pred->op()) {
       case Expression::Operation::kLt:
-      case Expression::Operation::kLtEq: {
-        return UnboundPredicateImpl<BoundReference>::Make(
-            Expression::Operation::kLtEq, std::move(ref), 
std::move(transformed));
-      }
+      case Expression::Operation::kLtEq:
+        return MakePredicate(Expression::Operation::kLtEq, name, func, 
pred->literal());
       case Expression::Operation::kGt:
-      case Expression::Operation::kGtEq: {
-        return UnboundPredicateImpl<BoundReference>::Make(
-            Expression::Operation::kGtEq, std::move(ref), 
std::move(transformed));
-      }
-      case Expression::Operation::kEq: {
-        return UnboundPredicateImpl<BoundReference>::Make(
-            Expression::Operation::kEq, std::move(ref), 
std::move(transformed));
-      }
+      case Expression::Operation::kGtEq:
+        return MakePredicate(Expression::Operation::kGtEq, name, func, 
pred->literal());
+      case Expression::Operation::kEq:
+      case Expression::Operation::kStartsWith:
+        return MakePredicate(pred->op(), name, func, pred->literal());
       default:
         return nullptr;
     }
   }
 
-  static Result<std::unique_ptr<UnboundPredicate>> TruncateByteArray(
-      std::string_view name, const std::shared_ptr<BoundLiteralPredicate>& 
predicate,
+  static Result<std::unique_ptr<UnboundPredicate>> TruncateByteArrayStrict(
+      std::string_view name, const std::shared_ptr<BoundLiteralPredicate>& 
pred,
       const std::shared_ptr<TransformFunction>& func) {
-    ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReference::Make(std::string(name)));
-    switch (predicate->op()) {
-      case Expression::Operation::kStartsWith: {
-        ICEBERG_ASSIGN_OR_RAISE(auto transformed, 
func->Transform(predicate->literal()));
-        return UnboundPredicateImpl<BoundReference>::Make(
-            Expression::Operation::kStartsWith, std::move(ref), 
std::move(transformed));
-      }
+    switch (pred->op()) {
+      case Expression::Operation::kLt:
+      case Expression::Operation::kLtEq:
+        return MakePredicate(Expression::Operation::kLt, name, func, 
pred->literal());
+      case Expression::Operation::kGt:
+      case Expression::Operation::kGtEq:
+        return MakePredicate(Expression::Operation::kGt, name, func, 
pred->literal());
+      case Expression::Operation::kNotEq:
+        return MakePredicate(Expression::Operation::kNotEq, name, func, 
pred->literal());
       default:
-        return GenericTransform(std::move(ref), predicate, func);
+        return nullptr;
     }
   }
 
-  template <typename T>
-    requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t>
-  static Result<std::unique_ptr<UnboundPredicate>> TruncateInteger(
-      std::string_view name, const std::shared_ptr<BoundLiteralPredicate>& 
predicate,
+  // Apply to int32, int64, decimal

Review Comment:
   ```suggestion
     // Apply to int32, int64, decimal, and temporal types.
   ```



-- 
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]

Reply via email to