raminqaf commented on code in PR #29021:
URL: https://github.com/apache/flink/pull/29021#discussion_r3860409828
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/binary/BinaryStringDataUtil.java:
##########
@@ -574,11 +574,34 @@ public static byte toByte(BinaryStringData str) throws
NumberFormatException {
}
public static double toDouble(BinaryStringData str) throws
NumberFormatException {
- return Double.parseDouble(str.toString());
+ return
Double.parseDouble(normalizeFloatingPointSpecialValue(str.toString()));
}
public static float toFloat(BinaryStringData str) throws
NumberFormatException {
- return Float.parseFloat(str.toString());
+ return
Float.parseFloat(normalizeFloatingPointSpecialValue(str.toString()));
+ }
+
+ /**
+ * Rewrites case-insensitive and abbreviated special values ({@code nan},
{@code inf}, {@code
+ * infinity}, with an optional sign on infinity) into the canonical tokens
{@link
+ * Double#parseDouble} and {@link Float#parseFloat} accept. Any other
input is returned
+ * unchanged.
+ */
+ private static String normalizeFloatingPointSpecialValue(String str) {
+ final String trimmed = str.trim();
+ if (trimmed.equalsIgnoreCase("nan")) {
+ return "NaN"; // NaN is unsigned, so a signed token is left to fail
+ }
+ String sign = "";
+ String magnitude = trimmed;
+ if (!trimmed.isEmpty() && (trimmed.charAt(0) == '+' ||
trimmed.charAt(0) == '-')) {
Review Comment:
Changed the implementation entirely. Now I try to parse the string. If it is
numeric we return. Otherwise, we try to see if it is a special character (NaN,
Inf, Infinity, etc.) and we return. If not we throw.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/binary/BinaryStringDataUtil.java:
##########
@@ -574,11 +574,34 @@ public static byte toByte(BinaryStringData str) throws
NumberFormatException {
}
public static double toDouble(BinaryStringData str) throws
NumberFormatException {
- return Double.parseDouble(str.toString());
+ return
Double.parseDouble(normalizeFloatingPointSpecialValue(str.toString()));
}
public static float toFloat(BinaryStringData str) throws
NumberFormatException {
- return Float.parseFloat(str.toString());
+ return
Float.parseFloat(normalizeFloatingPointSpecialValue(str.toString()));
+ }
+
+ /**
+ * Rewrites case-insensitive and abbreviated special values ({@code nan},
{@code inf}, {@code
+ * infinity}, with an optional sign on infinity) into the canonical tokens
{@link
+ * Double#parseDouble} and {@link Float#parseFloat} accept. Any other
input is returned
+ * unchanged.
+ */
Review Comment:
made it shorter
--
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]