This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/master/pr-20566-fca82d3c0cac72e7a9913b8b46076ffac7520749
in repository https://gitbox.apache.org/repos/asf/hudi.git

commit 15277ae4562c1621289870e3a9d7316df108c88d
Author: Danny Chan <[email protected]>
AuthorDate: Thu Nov 12 03:48:37 2026 +0000

    fix(flink): preserve precision in decimal conversions (#20566)
    
    * fix(flink): preserve precision in decimal conversions
    
    * refactor(flink): select decimal converters by source type
    
    * fix(flink): trim strings before decimal conversion
---
 .../java/org/apache/hudi/util/TypeConverters.java  | 30 ++++----
 .../org/apache/hudi/util/TestTypeConverters.java   | 83 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 16 deletions(-)

diff --git 
a/hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/util/TypeConverters.java
 
b/hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/util/TypeConverters.java
index cef2df7c037f..eaebf74ee795 100644
--- 
a/hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/util/TypeConverters.java
+++ 
b/hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/util/TypeConverters.java
@@ -134,13 +134,23 @@ public class TypeConverters {
       }
 
       case DECIMAL: {
-        if (from == INTEGER || from == BIGINT || from == DOUBLE) {
+        if (from == INTEGER || from == BIGINT) {
           return new TypeConverter() {
             private static final long serialVersionUID = 1L;
 
             @Override
             public Object convert(Object val) {
-              return toDecimalData((Number) val, toType);
+              return toDecimalData(BigDecimal.valueOf(((Number) 
val).longValue()), toType);
+            }
+          };
+        }
+        if (from == DOUBLE) {
+          return new TypeConverter() {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public Object convert(Object val) {
+              return toDecimalData(BigDecimal.valueOf(((Number) 
val).doubleValue()), toType);
             }
           };
         }
@@ -150,7 +160,7 @@ public class TypeConverters {
 
             @Override
             public Object convert(Object val) {
-              return toDecimalData(Double.parseDouble(val.toString()), toType);
+              return 
toDecimalData(BigDecimal.valueOf(Double.parseDouble(val.toString())), toType);
             }
           };
         }
@@ -160,7 +170,7 @@ public class TypeConverters {
 
             @Override
             public Object convert(Object val) {
-              return toDecimalData(Double.parseDouble(val.toString()), toType);
+              return toDecimalData(new BigDecimal(val.toString().trim()), 
toType);
             }
           };
         }
@@ -370,18 +380,6 @@ public class TypeConverters {
     return !isComplex && fromType.equals(toType);
   }
 
-  /**
-   * Helper function to convert a Number object to Flink's DecimalData format.
-   *
-   * @param val         Number object to be converted
-   * @param decimalType DecimalType containing the output decimal's precision 
and scale specifications
-   * @return Converted decimal that is wrapped in Flink's DecimalData object.
-   */
-  private static DecimalData toDecimalData(Number val, LogicalType 
decimalType) {
-    BigDecimal valAsDecimal = BigDecimal.valueOf(val.doubleValue());
-    return toDecimalData(valAsDecimal, decimalType);
-  }
-
   /**
    * Helper function to convert a BigDecimal object to Flink's DecimalData 
format.
    *
diff --git 
a/hudi-client/hudi-flink-client/src/test/java/org/apache/hudi/util/TestTypeConverters.java
 
b/hudi-client/hudi-flink-client/src/test/java/org/apache/hudi/util/TestTypeConverters.java
new file mode 100644
index 000000000000..2b0bb70d691d
--- /dev/null
+++ 
b/hudi-client/hudi-flink-client/src/test/java/org/apache/hudi/util/TestTypeConverters.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hudi.util;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.math.BigDecimal;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class TestTypeConverters {
+
+  @ParameterizedTest
+  @ValueSource(longs = {0L, 9007199254740993L, -9007199254740993L, 
Long.MAX_VALUE, Long.MIN_VALUE})
+  void testLongToDecimalPreservesPrecision(long value) {
+    assertDecimal(DataTypes.BIGINT().getLogicalType(), value, 19, 0, 
Long.toString(value));
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"9007199254740993.01", "-9007199254740993.01", 
"12345678901234567890.12"})
+  void testStringToDecimalPreservesPrecision(String value) {
+    assertDecimal(DataTypes.STRING().getLogicalType(), 
StringData.fromString(value), 22, 2, value);
+  }
+
+  @Test
+  void testPaddedStringToDecimalPreservesPrecision() {
+    assertDecimal(DataTypes.STRING().getLogicalType(), StringData.fromString(" 
\t9007199254740993.01\r\n"),
+        22, 2, "9007199254740993.01");
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {" 1.5", "1.5 ", " \t1.5\r\n"})
+  void testStringToDecimalTrimsWhitespace(String value) {
+    assertDecimal(DataTypes.STRING().getLogicalType(), 
StringData.fromString(value), 3, 2, "1.50");
+  }
+
+  @Test
+  void testDecimalScaleAndNumericConversions() {
+    assertDecimal(DataTypes.INT().getLogicalType(), Integer.MAX_VALUE, 12, 2, 
"2147483647.00");
+    assertDecimal(DataTypes.BIGINT().getLogicalType(), 9007199254740993L, 18, 
2, "9007199254740993.00");
+    assertDecimal(DataTypes.FLOAT().getLogicalType(), 1.23f, 5, 2, "1.23");
+    assertDecimal(DataTypes.DOUBLE().getLogicalType(), 1.23d, 5, 2, "1.23");
+    assertDecimal(DataTypes.STRING().getLogicalType(), 
StringData.fromString("9007199254740993.015"),
+        18, 2, "9007199254740993.02");
+  }
+
+  @Test
+  void testDecimalOverflow() {
+    assertNull(TypeConverters.getInstance(DataTypes.BIGINT().getLogicalType(), 
DataTypes.DECIMAL(18, 0).getLogicalType())
+        .convert(Long.MAX_VALUE));
+    assertNull(TypeConverters.getInstance(DataTypes.STRING().getLogicalType(), 
DataTypes.DECIMAL(18, 2).getLogicalType())
+        .convert(StringData.fromString("12345678901234567890.12")));
+  }
+
+  private static void assertDecimal(LogicalType fromType, Object value, int 
precision, int scale, String expected) {
+    DecimalData result = (DecimalData) TypeConverters.getInstance(fromType, 
DataTypes.DECIMAL(precision, scale).getLogicalType())
+        .convert(value);
+    assertEquals(new BigDecimal(expected), result.toBigDecimal());
+  }
+}

Reply via email to