aykut-bozkurt commented on code in PR #12969: URL: https://github.com/apache/iceberg/pull/12969#discussion_r2099467565
########## 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: Right, I get wrong query results with truncation on int types. Similar issue https://github.com/apache/iceberg/issues/13105 -- 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