yuxiqian commented on code in PR #3786:
URL: https://github.com/apache/flink-cdc/pull/3786#discussion_r1879161416
##########
flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/functions/SystemFunctionUtils.java:
##########
@@ -606,71 +606,176 @@ public static String castToString(Object object) {
return object.toString();
}
- public static Byte castToByte(Object object) {
+ public static Boolean castToBoolean(Object object) {
if (object == null) {
return null;
+ } else if (object instanceof Boolean) {
+ return (Boolean) object;
+ } else if (object instanceof Byte) {
+ return !object.equals((byte) 0);
+ } else if (object instanceof Short) {
+ return !object.equals((short) 0);
+ } else if (object instanceof Integer) {
+ return !object.equals(0);
+ } else if (object instanceof Long) {
+ return !object.equals(0L);
+ } else if (object instanceof Float) {
+ return !object.equals(0f);
+ } else if (object instanceof Double) {
+ return !object.equals(0d);
+ } else if (object instanceof BigDecimal) {
+ return ((BigDecimal) object).compareTo(BigDecimal.ZERO) != 0;
}
- return Byte.valueOf(castObjectIntoString(object));
+ return Boolean.valueOf(castToString(object));
}
- public static Boolean castToBoolean(Object object) {
+ public static Byte castToByte(Object object) {
if (object == null) {
return null;
}
- if (object instanceof Byte
- || object instanceof Short
- || object instanceof Integer
- || object instanceof Long
- || object instanceof Float
- || object instanceof Double
- || object instanceof BigDecimal) {
- return !object.equals(0);
+ if (object instanceof Boolean) {
+ return (byte) ((Boolean) object ? 1 : 0);
+ }
+ if (object instanceof BigDecimal) {
+ return ((BigDecimal) object).byteValue();
+ }
+ if (object instanceof Double) {
+ return ((Double) object).byteValue();
+ }
+ if (object instanceof Float) {
+ return ((Float) object).byteValue();
+ }
+ String stringRep = castToString(object);
+ try {
+ return Byte.valueOf(stringRep);
+ } catch (NumberFormatException e) {
+ return Double.valueOf(stringRep).byteValue();
}
Review Comment:
`Integer.valueOf` throws a `NumberFormatException` not only when an
non-numeric value passed into it. It actually rejects any **non-integral**
values like `3.14`.
--
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]