snuyanzin commented on code in PR #3779: URL: https://github.com/apache/calcite/pull/3779#discussion_r1595997376
########## core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java: ########## @@ -1822,6 +1823,35 @@ public static RelDataType extractLastNFields(RelDataTypeFactory typeFactory, type.getFieldList().subList(fieldsCnt - numToKeep, fieldsCnt)); } + /** + * Returns whether the decimal value can be represented without information loss + * using the specified type. + * For example, 1111.11 + * - cannot be represented exactly using DECIMAL(3, 1) since it overflows. + * - cannot be represented exactly using DECIMAL(6, 3) since it overflows. + * - cannot be represented exactly using DECIMAL(6, 1) since it requires rounding. + * - can be represented exactly using DECIMAL(6, 2) + * + * @param value A decimal value + * @param toType A DECIMAL type. + * @return whether the value is valid for the type + */ + public static boolean canBeRepresentedExactly(@Nullable BigDecimal value, RelDataType toType) { + assert toType.getSqlTypeName() == SqlTypeName.DECIMAL; + if (value == null) { + return true; + } + value = value.stripTrailingZeros(); + if (value.scale() < 0) { Review Comment: it would be great also to have a test with negative scale -- 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: commits-unsubscr...@calcite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org