raminqaf commented on code in PR #28758:
URL: https://github.com/apache/flink/pull/28758#discussion_r3656077829


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/VariantToPrimitiveCastRule.java:
##########
@@ -166,38 +164,35 @@ protected String generateCodeBlockInternal(
     }
 
     /**
-     * Converts a numeric variant to the numeric {@code target} via the 
matching {@link Number}
-     * accessor, mirroring regular numeric cast semantics. A non-numeric 
variant raises {@link
-     * ClassCastException}, failing {@code CAST} and yielding {@code null} for 
{@code TRY_CAST}.
+     * Converts a numeric variant to the numeric {@code target}. Integer and 
decimal targets are
+     * range-checked so that an out-of-range value fails {@code CAST} and 
yields {@code null} for
+     * {@code TRY_CAST}; {@code FLOAT} and {@code DOUBLE} keep lenient IEEE 
conversion (overflow
+     * becomes infinity). A non-numeric variant raises {@link 
ClassCastException}, which fails
+     * {@code CAST} and yields {@code null} for {@code TRY_CAST}.
      */
     private static String numericExpression(String inputTerm, LogicalType 
target) {
         final String number = cast(className(Number.class), 
methodCall(inputTerm, "get"));
-        if (!target.is(LogicalTypeRoot.DECIMAL)) {
-            return methodCall(number, numberAccessor(target));
-        }
-        final DecimalType decimalType = (DecimalType) target;
-        return staticCall(
-                DecimalData.class,
-                "fromBigDecimal",
-                constructorCall(BigDecimal.class, methodCall(number, 
"toString")),
-                decimalType.getPrecision(),
-                decimalType.getScale());
-    }
-
-    private static String numberAccessor(LogicalType target) {
         switch (target.getTypeRoot()) {
             case TINYINT:
-                return "byteValue";
+                return staticCall(VariantCastUtils.class, "toByteExact", 
number);
             case SMALLINT:
-                return "shortValue";
+                return staticCall(VariantCastUtils.class, "toShortExact", 
number);
             case INTEGER:
-                return "intValue";
+                return staticCall(VariantCastUtils.class, "toIntExact", 
number);
             case BIGINT:
-                return "longValue";
+                return staticCall(VariantCastUtils.class, "toLongExact", 
number);
             case FLOAT:
-                return "floatValue";
+                return methodCall(number, "floatValue");

Review Comment:
   Introduced `toFloatExact` and `toDoubleExact`. In case there is a precision 
loss the CAST will fail and TRY_CAST will return null.



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

Reply via email to