nandorKollar commented on code in PR #12969:
URL: https://github.com/apache/iceberg/pull/12969#discussion_r2101928619


##########
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:
   Capping it might work, though for positive values, overflows are avoidable I 
think, when `v - (v % W)` is used instead of `v - (((v % W) + W) % W)` for 
positive values. Nevertheless, this sounds like a breaking change for me, 
probably better to discuss it with community first?



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