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

jermy pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-hugegraph-toolchain.git


The following commit(s) were added to refs/heads/master by this push:
     new f0cbdbcf Fix convert numbers to strings (#465)
f0cbdbcf is described below

commit f0cbdbcf5d2702f6784830a4f964c08660b8cda4
Author: Liu Xiao <[email protected]>
AuthorDate: Tue May 23 19:21:56 2023 +0800

    Fix convert numbers to strings (#465)
---
 .../hugegraph/structure/constant/DataType.java     |  4 +++
 .../apache/hugegraph/loader/util/DataTypeUtil.java |  6 +++-
 .../loader/test/functional/JDBCLoadTest.java       | 35 ++++++++++++++++++++
 .../resources/jdbc_number_to_string/schema.groovy  | 25 +++++++++++++++
 .../resources/jdbc_number_to_string/struct.json    | 37 ++++++++++++++++++++++
 5 files changed, 106 insertions(+), 1 deletion(-)

diff --git 
a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/DataType.java
 
b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/DataType.java
index 3bc1e3cc..fe68c77a 100644
--- 
a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/DataType.java
+++ 
b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/DataType.java
@@ -74,4 +74,8 @@ public enum DataType {
     public boolean isBoolean() {
         return this == BOOLEAN;
     }
+
+    public boolean isText() {
+        return this == DataType.TEXT;
+    }
 }
diff --git 
a/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DataTypeUtil.java
 
b/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DataTypeUtil.java
index 4e066d45..3052058d 100644
--- 
a/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DataTypeUtil.java
+++ 
b/hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DataTypeUtil.java
@@ -143,6 +143,10 @@ public final class DataTypeUtil {
             return parseDate(key, value, dateFormat, timeZone);
         } else if (dataType.isUUID()) {
             return parseUUID(key, value);
+        } else if (dataType.isText()) {
+            if (!(rawValue instanceof String)) {
+                value = rawValue.toString();
+            }
         }
         E.checkArgument(checkDataType(key, value, dataType),
                         "The value(key='%s') '%s'(%s) is not match with " +
@@ -312,7 +316,7 @@ public final class DataTypeUtil {
      */
     private static boolean checkDataType(String key, Object value,
                                          DataType dataType) {
-        if (value instanceof Number) {
+        if (value instanceof Number && dataType.isNumber()) {
             return parseNumber(key, value, dataType) != null;
         }
         return dataType.clazz().isInstance(value);
diff --git 
a/hugegraph-loader/src/test/java/org/apache/hugegraph/loader/test/functional/JDBCLoadTest.java
 
b/hugegraph-loader/src/test/java/org/apache/hugegraph/loader/test/functional/JDBCLoadTest.java
index 9a5c54e6..232596ee 100644
--- 
a/hugegraph-loader/src/test/java/org/apache/hugegraph/loader/test/functional/JDBCLoadTest.java
+++ 
b/hugegraph-loader/src/test/java/org/apache/hugegraph/loader/test/functional/JDBCLoadTest.java
@@ -146,6 +146,7 @@ public class JDBCLoadTest extends LoadTest {
                 "-s", configPath("jdbc_customized_schema/schema.groovy"),
                 "-g", GRAPH,
                 "-h", SERVER,
+                "-p", String.valueOf(PORT),
                 "--batch-insert-threads", "2",
                 "--test-mode", "true"
         };
@@ -173,6 +174,7 @@ public class JDBCLoadTest extends LoadTest {
                 "-s", configPath("jdbc_customized_schema/schema.groovy"),
                 "-g", GRAPH,
                 "-h", SERVER,
+                "-p", String.valueOf(PORT),
                 "--batch-insert-threads", "2",
                 "--test-mode", "true"
         };
@@ -196,6 +198,7 @@ public class JDBCLoadTest extends LoadTest {
                 "-s", configPath("jdbc_value_mapping/schema.groovy"),
                 "-g", GRAPH,
                 "-h", SERVER,
+                "-p", String.valueOf(PORT),
                 "--batch-insert-threads", "2",
                 "--test-mode", "true"
         };
@@ -208,4 +211,36 @@ public class JDBCLoadTest extends LoadTest {
         assertContains(vertices, "person", "name", "vadas",
                        "age", 27, "city", "Shanghai");
     }
+
+    @Test
+    public void testNumberToStringInJDBCSource() {
+        dbUtil.insert("INSERT INTO `person` VALUES " +
+                "(1,'marko',29,'Beijing')," +
+                "(2,'vadas',27,'HongKong')," +
+                "(3,'josh',32,'Beijing')," +
+                "(4,'peter',35,'Shanghai')," +
+                "(5,'li,nary',26,'Wu,han')," +
+                "(6,'tom',NULL,NULL);");
+
+        dbUtil.insert("INSERT INTO `software` VALUES " +
+                "(100,'lop','java',328.08)," +
+                "(200,'ripple','java',199.67);");
+
+        String[] args = new String[]{
+                "-f", configPath("jdbc_number_to_string/struct.json"),
+                "-s", configPath("jdbc_number_to_string/schema.groovy"),
+                "-g", GRAPH,
+                "-h", SERVER,
+                "-p", String.valueOf(PORT),
+                "--batch-insert-threads", "2",
+                "--test-mode", "true"
+        };
+        HugeGraphLoader.main(args);
+
+        List<Vertex> vertices = CLIENT.graph().listVertices();
+
+        Assert.assertEquals(8, vertices.size());
+        assertContains(vertices, "person", "age", "29");
+        assertContains(vertices, "software", "price", "199.67");
+    }
 }
diff --git 
a/hugegraph-loader/src/test/resources/jdbc_number_to_string/schema.groovy 
b/hugegraph-loader/src/test/resources/jdbc_number_to_string/schema.groovy
new file mode 100644
index 00000000..784bda54
--- /dev/null
+++ b/hugegraph-loader/src/test/resources/jdbc_number_to_string/schema.groovy
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+// Define schema
+schema.propertyKey("name").asText().ifNotExist().create();
+schema.propertyKey("age").asText().ifNotExist().create();
+schema.propertyKey("city").asText().ifNotExist().create();
+schema.propertyKey("lang").asText().ifNotExist().create();
+schema.propertyKey("price").asText().ifNotExist().create();
+
+schema.vertexLabel("person").useCustomizeNumberId().properties("name", "age", 
"city").nullableKeys("name", "age", "city").ifNotExist().create();
+schema.vertexLabel("software").useCustomizeNumberId().properties("name", 
"lang", "price").nullableKeys("name", "lang", "price").ifNotExist().create();
diff --git 
a/hugegraph-loader/src/test/resources/jdbc_number_to_string/struct.json 
b/hugegraph-loader/src/test/resources/jdbc_number_to_string/struct.json
new file mode 100644
index 00000000..f70861e2
--- /dev/null
+++ b/hugegraph-loader/src/test/resources/jdbc_number_to_string/struct.json
@@ -0,0 +1,37 @@
+{
+  "vertices": [
+    {
+      "label": "person",
+      "input": {
+        "type": "jdbc",
+        "vendor": "mysql",
+        "driver": "com.mysql.cj.jdbc.Driver",
+        "url": "jdbc:mysql://127.0.0.1:3306",
+        "database": "load_test",
+        "table": "person",
+        "username": "root",
+        "password": "root",
+        "batch_size": 2
+      },
+      "id": "id",
+      "null_values": ["NULL"],
+      "ignored": ["name","city"]
+    },
+    {
+      "label": "software",
+      "input": {
+        "type": "jdbc",
+        "vendor": "mysql",
+        "driver": "com.mysql.cj.jdbc.Driver",
+        "url": "jdbc:mysql://127.0.0.1:3306",
+        "database": "load_test",
+        "table": "software",
+        "username": "root",
+        "password": "root",
+        "batch_size": 2
+      },
+      "id": "id",
+      "ignored": ["name","lang"]
+    }
+  ]
+}

Reply via email to