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

terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a4d9975849 Fix numeric type conversion in YamlRowStatisticsSwapper 
(#39320)
9a4d9975849 is described below

commit 9a4d99758493212a662d241352b570786b6476bc
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Aug 3 17:06:28 2026 +0800

    Fix numeric type conversion in YamlRowStatisticsSwapper (#39320)
    
    * Fix numeric type conversion in YamlRowStatisticsSwapper
    
    * Fix numeric type conversion in YamlRowStatisticsSwapper
    
    * Fix numeric type conversion in YamlRowStatisticsSwapper
---
 .../data/swapper/YamlRowStatisticsSwapper.java     | 15 ++--
 .../data/swapper/YamlRowStatisticsSwapperTest.java | 84 ++++++++++++----------
 2 files changed, 56 insertions(+), 43 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapper.java
index 8c0d2eed456..649548c4e92 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapper.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapper.java
@@ -53,10 +53,9 @@ public final class YamlRowStatisticsSwapper implements 
YamlConfigurationSwapper<
     }
     
     private Object convertDataType(final Object data, final int dataType) {
-        if (Types.DECIMAL == dataType || Types.BIGINT == dataType) {
+        if (Types.DECIMAL == dataType || Types.NUMERIC == dataType) {
             return null == data ? null : data.toString();
         }
-        // TODO use general type convertor
         return data;
     }
     
@@ -76,16 +75,18 @@ public final class YamlRowStatisticsSwapper implements 
YamlConfigurationSwapper<
         if (null == data) {
             return null;
         }
-        if (Types.DECIMAL == dataType) {
+        if ((Types.DECIMAL == dataType || Types.NUMERIC == dataType) && !(data 
instanceof BigDecimal)) {
             return new BigDecimal(data.toString());
         }
         if (Types.BIGINT == dataType) {
-            return Long.valueOf(data.toString());
+            return data instanceof Long ? data : Long.valueOf(data.toString());
         }
-        if (Types.REAL == dataType || Types.FLOAT == dataType) {
-            return Float.parseFloat(data.toString());
+        if (Types.REAL == dataType) {
+            return data instanceof Float ? data : 
Float.valueOf(data.toString());
+        }
+        if ((Types.FLOAT == dataType || Types.DOUBLE == dataType) && !(data 
instanceof Double)) {
+            return Double.valueOf(data.toString());
         }
-        // TODO use general type convertor
         return data;
     }
 }
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapperTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapperTest.java
index a2f2e696c03..b3b8c199023 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapperTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlRowStatisticsSwapperTest.java
@@ -21,12 +21,14 @@ import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
 import org.apache.shardingsphere.infra.metadata.statistics.RowStatistics;
 import org.apache.shardingsphere.infra.yaml.data.pojo.YamlRowStatistics;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 import java.math.BigDecimal;
 import java.sql.Types;
-import java.util.Arrays;
 import java.util.Collections;
-import java.util.List;
+import java.util.stream.Stream;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
@@ -38,51 +40,61 @@ class YamlRowStatisticsSwapperTest {
     @Test
     void assertSwapToYamlConfigurationWithNullRows() {
         YamlRowStatisticsSwapper swapper = new 
YamlRowStatisticsSwapper(Collections.emptyList());
-        YamlRowStatistics actual = swapper.swapToYamlConfiguration(new 
RowStatistics("uk", null));
+        YamlRowStatistics actual = swapper.swapToYamlConfiguration(new 
RowStatistics("foo_unique_key", null));
         assertThat(actual.getRows(), is(empty()));
-        assertThat(actual.getUniqueKey(), is("uk"));
+        assertThat(actual.getUniqueKey(), is("foo_unique_key"));
     }
     
-    @Test
-    void assertConvertSpecialTypesWhenSwappingToYaml() {
-        List<ShardingSphereColumn> columns = Arrays.asList(
-                new ShardingSphereColumn("decimal_col", Types.DECIMAL, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("bigint_col", Types.BIGINT, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("decimal_null", Types.DECIMAL, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("varchar_col", Types.VARCHAR, false, 
false, false, true, false, true));
-        List<Object> rows = Arrays.asList(null, 5L, new BigDecimal("7.5"), 
"raw");
-        YamlRowStatisticsSwapper swapper = new 
YamlRowStatisticsSwapper(columns);
-        assertThat(swapper.swapToYamlConfiguration(new RowStatistics("uk", 
rows)).getRows(), contains(null, is("5"), is("7.5"), is("raw")));
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("getSwapToYamlConfigurationArguments")
+    void assertSwapToYamlConfigurationWithDataType(final String name, final 
int dataType, final Object input, final Object expected) {
+        ShardingSphereColumn column = new ShardingSphereColumn("foo_col", 
dataType, false, false, false, true, false, true);
+        YamlRowStatistics actual = new 
YamlRowStatisticsSwapper(Collections.singletonList(column))
+                .swapToYamlConfiguration(new RowStatistics("foo_unique_key", 
Collections.singletonList(input)));
+        assertThat(actual.getRows(), contains(expected));
     }
     
-    @Test
-    void assertSwapToObjectWithNullRows() {
-        assertThat(new 
YamlRowStatisticsSwapper(Collections.emptyList()).swapToObject(new 
YamlRowStatistics()).getRows(), is(empty()));
+    private static Stream<Arguments> getSwapToYamlConfigurationArguments() {
+        return Stream.of(
+                Arguments.of("decimal", Types.DECIMAL, new BigDecimal("7.5"), 
"7.5"),
+                Arguments.of("numeric", Types.NUMERIC, new BigDecimal("8.25"), 
"8.25"),
+                Arguments.of("null decimal", Types.DECIMAL, null, null),
+                Arguments.of("bigint", Types.BIGINT, 5L, 5L),
+                Arguments.of("varchar", Types.VARCHAR, "foo_value", 
"foo_value"));
     }
     
     @Test
-    void assertSwapToObjectWithEmptyRows() {
-        List<ShardingSphereColumn> columns = Collections.singletonList(new 
ShardingSphereColumn("col", Types.VARCHAR, false, false, false, true, false, 
true));
+    void assertSwapToObjectWithNullRows() {
         YamlRowStatistics yamlConfig = new YamlRowStatistics();
-        yamlConfig.setRows(Collections.emptyList());
-        assertThat(new 
YamlRowStatisticsSwapper(columns).swapToObject(yamlConfig).getRows(), 
is(empty()));
+        yamlConfig.setUniqueKey("foo_unique_key");
+        yamlConfig.setRows(null);
+        RowStatistics actual = new 
YamlRowStatisticsSwapper(Collections.emptyList()).swapToObject(yamlConfig);
+        assertThat(actual.getRows(), is(empty()));
+        assertThat(actual.getUniqueKey(), is("foo_unique_key"));
     }
     
-    @Test
-    void assertConvertDataTypesWhenSwappingToObject() {
-        List<ShardingSphereColumn> columns = Arrays.asList(
-                new ShardingSphereColumn("decimal_col", Types.DECIMAL, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("bigint_col", Types.BIGINT, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("real_col", Types.REAL, false, false, 
false, true, false, true),
-                new ShardingSphereColumn("float_col", Types.FLOAT, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("varchar_col", Types.VARCHAR, false, 
false, false, true, false, true),
-                new ShardingSphereColumn("decimal_null", Types.DECIMAL, false, 
false, false, true, false, true));
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("getSwapToObjectArguments")
+    void assertSwapToObjectWithDataType(final String name, final int dataType, 
final Object input, final Object expected) {
+        ShardingSphereColumn column = new ShardingSphereColumn("foo_col", 
dataType, false, false, false, true, false, true);
         YamlRowStatistics yamlConfig = new YamlRowStatistics();
-        yamlConfig.setUniqueKey("uk");
-        yamlConfig.setRows(Arrays.asList("1.5", "2", "3.3", "4.4", "text", 
null));
-        YamlRowStatisticsSwapper swapper = new 
YamlRowStatisticsSwapper(columns);
-        RowStatistics actual = swapper.swapToObject(yamlConfig);
-        assertThat(actual.getUniqueKey(), is("uk"));
-        assertThat(actual.getRows(), contains(new BigDecimal("1.5"), 2L, 
Float.parseFloat("3.3"), Float.parseFloat("4.4"), "text", null));
+        yamlConfig.setRows(Collections.singletonList(input));
+        RowStatistics actual = new 
YamlRowStatisticsSwapper(Collections.singletonList(column)).swapToObject(yamlConfig);
+        assertThat(actual.getRows(), contains(expected));
+    }
+    
+    private static Stream<Arguments> getSwapToObjectArguments() {
+        return Stream.of(
+                Arguments.of("decimal string", Types.DECIMAL, "1.5", new 
BigDecimal("1.5")),
+                Arguments.of("numeric string", Types.NUMERIC, "2.5", new 
BigDecimal("2.5")),
+                Arguments.of("numeric BigDecimal", Types.NUMERIC, new 
BigDecimal("3.5"), new BigDecimal("3.5")),
+                Arguments.of("bigint integer", Types.BIGINT, 3, 3L),
+                Arguments.of("bigint long", Types.BIGINT, 4L, 4L),
+                Arguments.of("real double", Types.REAL, 5.5D, 5.5F),
+                Arguments.of("real float", Types.REAL, 6.5F, 6.5F),
+                Arguments.of("float double", Types.FLOAT, 7.5D, 7.5D),
+                Arguments.of("double string", Types.DOUBLE, "8.5", 8.5D),
+                Arguments.of("varchar", Types.VARCHAR, "foo_text", "foo_text"),
+                Arguments.of("null decimal", Types.DECIMAL, null, null));
     }
 }

Reply via email to