Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ public static Object castFromStringInternal(String s, DataType type, boolean isC
} else {
// Compatible canal-cdc
Float f = Float.valueOf(s);
if (f.toString().length() != s.length()) {
throw new NumberFormatException(
s + " cannot be cast to float due to precision loss");
} else {
// Validate precision by comparing the rounded value
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float f = Float.parseFloat(s);

if ((double) f == d || Math.abs(f - d) < Math.abs(d * 1e-6)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use "d * 1e-6"?
what about Double#compare?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The value 1e-6 (which is 0.000001) is typically a reasonable threshold for floating-point comparisons, it's a balance between precision and practical usability.
  2. This approach helps avoid rejecting values that are within the acceptable precision for FLOAT types in databases like MySQL, while still accounting for the limitations of floating-point precision.

return f;
} else {
// Significant precision loss detected
throw new NumberFormatException(
s + " cannot be cast to FLOAT due to precision loss");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception message need improve. It needs to cover the comparative failure situation.

}
}
case DOUBLE:
Expand Down