ldudas-marx commented on code in PR #16262:
URL: https://github.com/apache/iceberg/pull/16262#discussion_r3241903345


##########
api/src/main/java/org/apache/iceberg/transforms/Cast.java:
##########
@@ -0,0 +1,1010 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.transforms;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Objects;
+import org.apache.iceberg.expressions.BoundLiteralPredicate;
+import org.apache.iceberg.expressions.BoundPredicate;
+import org.apache.iceberg.expressions.BoundSetPredicate;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.expressions.UnboundPredicate;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.DateTimeUtil;
+import org.apache.iceberg.util.SerializableFunction;
+
+/**
+ * A transform that casts values from one type to another.
+ *
+ * <p>This transform performs explicit type conversions, similar to SQL CAST 
operations. It supports
+ * casting between compatible types with appropriate handling of overflow, 
precision loss, and null
+ * values.
+ *
+ * @param <S> the source type
+ * @param <T> the target type
+ */
+class Cast<S, T> implements Transform<S, T> {
+  private static final long MICROS_PER_DAY = 86_400_000_000L;
+  private static final long NANOS_PER_MICRO = 1_000L;
+  private static final long NANOS_PER_DAY = 86_400_000_000_000L;
+
+  private final Type targetType;
+
+  private Cast(Type targetType) {
+    Preconditions.checkNotNull(targetType, "Target type cannot be null");
+    this.targetType = targetType;
+  }
+
+  @SuppressWarnings("unchecked")
+  static <S, T> Cast<S, T> get(Type targetType) {
+    return new Cast<>(targetType);
+  }
+
+  @Override
+  public SerializableFunction<S, T> bind(Type sourceType) {
+    return new CastFunction<>(sourceType, targetType);
+  }
+
+  @Override
+  public boolean canTransform(Type sourceType) {
+    return isValidCast(sourceType, targetType);
+  }
+
+  @Override
+  public Type getResultType(Type sourceType) {
+    Preconditions.checkArgument(
+        canTransform(sourceType), "Cannot cast from %s to %s", sourceType, 
targetType);
+    return targetType;
+  }
+
+  @Override
+  public boolean preservesOrder() {
+    // Cast order preservation depends on source and target types.
+    return false;
+  }
+
+  @Override
+  public boolean preservesOrder(Type sourceType) {
+    Type.TypeID source = sourceType.typeId();
+    Type.TypeID target = targetType.typeId();
+
+    if (source == target) {
+      return true;
+    }
+
+    if (isNumeric(source) && isNumeric(target)) {
+      return true;
+    }
+
+    if (isDateTimeOrderPreserving(source, target)) {
+      return true;
+    }
+
+    return false;
+  }
+
+  private static boolean isNumeric(Type.TypeID type) {
+    switch (type) {
+      case INTEGER:
+      case LONG:
+      case FLOAT:
+      case DOUBLE:
+      case DECIMAL:
+        return true;
+      default:
+        return false;
+    }
+  }
+
+  private static boolean isDateTimeOrderPreserving(Type.TypeID source, 
Type.TypeID target) {
+    switch (source) {
+      case INTEGER:
+        return target == Type.TypeID.DATE;
+
+      case LONG:
+        return target == Type.TypeID.TIME
+            || target == Type.TypeID.TIMESTAMP
+            || target == Type.TypeID.TIMESTAMP_NANO
+            || target == Type.TypeID.DATE;
+
+      case DATE:
+        return target == Type.TypeID.TIMESTAMP
+            || target == Type.TypeID.TIMESTAMP_NANO
+            || target == Type.TypeID.STRING;
+
+      case TIME:
+        return target == Type.TypeID.LONG || target == Type.TypeID.STRING;
+
+      case TIMESTAMP:
+        return target == Type.TypeID.DATE
+            || target == Type.TypeID.TIMESTAMP_NANO
+            || target == Type.TypeID.LONG;
+
+      case TIMESTAMP_NANO:
+        return target == Type.TypeID.DATE
+            || target == Type.TypeID.TIMESTAMP
+            || target == Type.TypeID.LONG;
+
+      case STRING:
+        return target == Type.TypeID.DATE;
+
+      default:
+        return false;
+    }
+  }
+
+  @Override
+  public UnboundPredicate<T> project(String name, BoundPredicate<S> predicate) 
{

Review Comment:
   If Cast is not supported in partitioning this is dead code. I would just 
throw UnsupportedOperationException here.



##########
api/src/main/java/org/apache/iceberg/transforms/Identity.java:
##########
@@ -146,16 +149,31 @@ public UnboundPredicate<T> project(String name, 
BoundPredicate<T> predicate) {
 
   @Override
   public UnboundPredicate<T> projectStrict(String name, BoundPredicate<T> 
predicate) {
+    // Reconstruct the unbound term with the partition column name
+    UnboundTerm<T> term = unboundTerm(name, predicate.term());
+
     if (predicate.isUnaryPredicate()) {
-      return Expressions.predicate(predicate.op(), name);
+      return Expressions.predicate(predicate.op(), term);
     } else if (predicate.isLiteralPredicate()) {
-      return Expressions.predicate(predicate.op(), name, 
predicate.asLiteralPredicate().literal());
+      return Expressions.predicate(predicate.op(), term, 
predicate.asLiteralPredicate().literal());
     } else if (predicate.isSetPredicate()) {
-      return Expressions.predicate(predicate.op(), name, 
predicate.asSetPredicate().literalSet());
+      return Expressions.predicate(predicate.op(), term, 
predicate.asSetPredicate().literalSet());
     }
     return null;
   }
 
+  @SuppressWarnings("unchecked")
+  private <S> UnboundTerm<S> unboundTerm(String name, BoundTerm<S> bound) {

Review Comment:
   ExpressionUtil.unbind(term) already implements this functionality.



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