From 386c9830981af74fdd69e75536657c8fca46a148 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Sat, 7 Dec 2024 18:26:10 +0800 Subject: [PATCH] [common] Fix FLOAT precision checking. (#4656) --- .../main/java/org/apache/paimon/utils/TypeUtils.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/paimon-common/src/main/java/org/apache/paimon/utils/TypeUtils.java b/paimon-common/src/main/java/org/apache/paimon/utils/TypeUtils.java index 8d8d84953da7..50096ab63621 100644 --- a/paimon-common/src/main/java/org/apache/paimon/utils/TypeUtils.java +++ b/paimon-common/src/main/java/org/apache/paimon/utils/TypeUtils.java @@ -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 + if ((double) f == d || Math.abs(f - d) < Math.abs(d * 1e-6)) { return f; + } else { + // Significant precision loss detected + throw new NumberFormatException( + s + " cannot be cast to FLOAT due to precision loss"); } } case DOUBLE: