snuyanzin commented on a change in pull request #17800:
URL: https://github.com/apache/flink/pull/17800#discussion_r750217355



##########
File path: 
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/binary/BinaryStringDataUtil.java
##########
@@ -525,56 +545,102 @@ public static Integer toInt(BinaryStringData str) {
         while (i < sizeInBytes) {
             byte currentByte = tmpBytes[i];
             if (currentByte < '0' || currentByte > '9') {
-                return null;
+                throw numberFormatExceptionFor(str, "INT", "Invalid character 
found");
             }
             i++;
         }
 
         if (!negative) {
             result = -result;
             if (result < 0) {
-                return null;
+                throw numberFormatExceptionFor(str, "INT", "Overflow");
             }
         }
         return result;
     }
 
-    public static Short toShort(BinaryStringData str) {
-        Integer intValue = toInt(str);
-        if (intValue != null) {
-            short result = intValue.shortValue();
-            if (result == intValue) {
-                return result;
-            }
+    public static short toShort(BinaryStringData str) throws TableException {
+        int intValue = toInt(str);
+        short result = (short) intValue;
+        if (result == intValue) {
+            return result;
         }
-        return null;
+        throw numberFormatExceptionFor(str, "SHORT", "Overflow");
     }
 
-    public static Byte toByte(BinaryStringData str) {
-        Integer intValue = toInt(str);
-        if (intValue != null) {
-            byte result = intValue.byteValue();
-            if (result == intValue) {
-                return result;
-            }
+    public static byte toByte(BinaryStringData str) throws TableException {
+        int intValue = toInt(str);
+        byte result = (byte) intValue;
+        if (result == intValue) {
+            return result;
         }
-        return null;
+        throw numberFormatExceptionFor(str, "BYTE", "Overflow");
     }
 
-    public static Double toDouble(BinaryStringData str) {
+    public static double toDouble(BinaryStringData str) throws TableException {
         try {
-            return Double.valueOf(str.toString());
+            return Double.parseDouble(str.toString());
         } catch (NumberFormatException e) {
-            return null;
+            throw new TableException("Cannot parse STRING to DOUBLE", e);
         }
     }
 
-    public static Float toFloat(BinaryStringData str) {
+    public static float toFloat(BinaryStringData str) throws TableException {
         try {
-            return Float.valueOf(str.toString());
+            return Float.parseFloat(str.toString());
         } catch (NumberFormatException e) {
-            return null;
+            throw new TableException("Cannot parse STRING to FLOAT", e);
+        }
+    }
+
+    private static TableException numberFormatExceptionFor(
+            StringData input, String targetType, @Nullable String reason) {
+        if (reason == null) {
+            return new TableException(
+                    "Cannot parse STRING to " + targetType,
+                    new NumberFormatException("For input string: \"" + input + 
"\""));
+        }
+
+        return new TableException(
+                "Cannot parse STRING to " + targetType,
+                new NumberFormatException("For input string: \"" + input + 
"\". " + reason));
+    }
+
+    public static int toDate(BinaryStringData str) throws TableException {
+        Integer date = DateTimeUtils.dateStringToUnixDate(str.toString());
+        if (date == null) {
+            throw new TableException("Cannot parse STRING to DATE");
         }
+
+        return date;
+    }
+
+    public static int toTime(BinaryStringData str) throws TableException {
+        Integer date = DateTimeUtils.timeStringToUnixDate(str.toString());
+        if (date == null) {
+            throw new TableException("Cannot parse STRING to TIME");

Review comment:
       Probably it makes sense to specify what string cannot be parsed like it 
is done for `toBoolean`.




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