lliangyu-lin commented on code in PR #12969:
URL: https://github.com/apache/iceberg/pull/12969#discussion_r2101345405


##########
api/src/main/java/org/apache/iceberg/transforms/Truncate.java:
##########
@@ -513,5 +516,35 @@ public UnboundPredicate<BigDecimal> projectStrict(
       }
       return null;
     }
+
+    @Override
+    public Type getResultType(Type sourceType) {
+      if (sourceType.typeId() != Type.TypeID.DECIMAL) {
+        // This should ideally not happen if canTransform and bind are used 
correctly,
+        // but it's good practice to validate the input type.
+        throw new IllegalArgumentException(
+            "Truncate transform requires a DECIMAL type, but got: " + 
sourceType);
+      }
+
+      Types.DecimalType decimal = (Types.DecimalType) sourceType;
+      int precision = decimal.precision();
+      int scale = decimal.scale();
+
+      // Convert truncate width into BigDecimal: w * 10^(-scale)
+      BigDecimal unit = new BigDecimal(unscaledWidth, scale);
+
+      // Determine input bound
+      BigDecimal minInput =
+          BigDecimal.TEN
+              .pow(precision - scale)
+              .subtract(BigDecimal.ONE.movePointLeft(scale))
+              .negate();
+
+      // Truncate input bounds, find the min value that can appear in 
truncated output
+      BigDecimal minTrunc = minInput.divide(unit, 0, 
RoundingMode.FLOOR).multiply(unit);
+      int finalPrecision = Math.max(minTrunc.precision(), precision);
+
+      return Types.DecimalType.of(finalPrecision, scale);

Review Comment:
   Thank you for raising the overflow / underflow issue for other types. 
Definitely feel like it's a general problem for truncate on numeric values. 
What do you think about if we just capping to lowest / highest possible values 
when detect there is underflow / overflow? Ex: 
`Truncate.get(10).bind(Types.IntegerType.get()).apply(Integer.MIN_VALUE) = 
Integer.MIN_VALUE`



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to