You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/08/27 10:22:33 UTC

[GitHub] [incubator-seatunnel] laglangyue opened a new pull request, #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

laglangyue opened a new pull request, #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545

   <!--
   
   Thank you for contributing to SeaTunnel! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [GITHUB issue](https://github.com/apache/incubator-seatunnel/issues).
   
     - Name the pull request in the form "[Feature] [component] Title of the pull request", where *Feature* can be replaced by `Hotfix`, `Bug`, etc.
   
     - Minor fixes should be named following this pattern: `[hotfix] [docs] Fix typo in README.md doc`.
   
   -->
   
   ## Purpose of this pull request
   when I test Map translation for Spark-Engine,I find the method named `fieldToString ` has some bug
   It's my fault. 
   (1) int[] -> Object[] will error
   (2) for row Object will StackOverflow
   I fix it and add some unit
   
   ## Check list
   
   * [ ] Code changed are covered with tests, or it does not need tests for reason:
   * [ ] If any new Jar binary package adding in your PR, please add License Notice according
     [New License Guide](https://github.com/apache/incubator-seatunnel/blob/dev/docs/en/contribution/new-license.md)
   * [ ] If necessary, please update the documentation to describe the new feature. https://github.com/apache/incubator-seatunnel/tree/dev/docs
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] Hisoka-X commented on a diff in pull request #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

Posted by GitBox <gi...@apache.org>.
Hisoka-X commented on code in PR #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545#discussion_r957395022


##########
seatunnel-connectors-v2/connector-console/src/test/java/org/apache/seatunnel/connectors/seatunnel/console/sink/ConsoleSinkWriterIT.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.console.sink;
+
+import org.apache.seatunnel.api.table.type.ArrayType;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.MapType;
+import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.utils.ReflectionUtils;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Optional;
+
+public class ConsoleSinkWriterIT {
+
+    private ConsoleSinkWriter consoleSinkWriter;
+
+    @BeforeEach
+    void setUp() {
+        String[] fieldNames = {};
+        SeaTunnelDataType<?>[] fieldTypes = {};
+        SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+        consoleSinkWriter = new ConsoleSinkWriter(seaTunnelRowType);
+    }
+
+    private Object fieldToStringTest(SeaTunnelDataType<?> dataType, Object value) {
+        Optional<Method> fieldToString = ReflectionUtils.getDeclaredMethod(ConsoleSinkWriter.class, "fieldToString", SeaTunnelDataType.class, Object.class);
+        Method method = fieldToString.orElseThrow(() -> new RuntimeException("method fieldToString not found"));
+        try {
+            return method.invoke(consoleSinkWriter, dataType, value);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    void arrayIntTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            Integer[] integerArr = {1};
+            Object integerArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, integerArr);
+            Assertions.assertEquals(integerArrString, "[1]");
+            int[] intArr = {1, 2};
+            Object intArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, intArr);
+            Assertions.assertEquals(intArrString, "[1, 2]");
+        });
+    }
+
+    @Test
+    void stringTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String str = RandomStringUtils.randomAlphanumeric(10);
+            Object obj = fieldToStringTest(BasicType.STRING_TYPE, str);
+            Assertions.assertTrue(obj instanceof String);
+            Assertions.assertEquals(10, ((String) obj).length());
+        });
+    }
+
+    @Test
+    void hashMapTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            HashMap<Object, Object> map = new HashMap<>();
+            map.put("key", "value");
+            MapType<String, String> mapType = new MapType<>(BasicType.STRING_TYPE, BasicType.STRING_TYPE);
+            Object mapString = fieldToStringTest(mapType, map);
+            Assertions.assertNotNull(mapString);
+        });
+    }
+
+    @Test
+    void rowTypeTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String[] fieldNames = {"c_byte", "c_array", "bytes"};
+            SeaTunnelDataType<?>[] fieldTypes = {BasicType.BYTE_TYPE, ArrayType.BYTE_ARRAY_TYPE, PrimitiveByteArrayType.INSTANCE};
+            SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+            byte[] bytes = RandomUtils.nextBytes(10);
+            Object[] rowData = {(byte) 1, bytes, bytes};
+            Object rowString = fieldToStringTest(seaTunnelRowType, rowData);
+            Assertions.assertNotNull(rowString);

Review Comment:
   Please use equals to make sure your `fieldToString` convert are right.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] Hisoka-X merged pull request #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

Posted by GitBox <gi...@apache.org>.
Hisoka-X merged PR #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] EricJoy2048 commented on pull request #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

Posted by GitBox <gi...@apache.org>.
EricJoy2048 commented on PR #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545#issuecomment-1229809545

   Please retry the CI


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] laglangyue commented on a diff in pull request #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

Posted by GitBox <gi...@apache.org>.
laglangyue commented on code in PR #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545#discussion_r957421805


##########
seatunnel-connectors-v2/connector-console/src/test/java/org/apache/seatunnel/connectors/seatunnel/console/sink/ConsoleSinkWriterIT.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.console.sink;
+
+import org.apache.seatunnel.api.table.type.ArrayType;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.MapType;
+import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.utils.ReflectionUtils;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Optional;
+
+public class ConsoleSinkWriterIT {
+
+    private ConsoleSinkWriter consoleSinkWriter;
+
+    @BeforeEach
+    void setUp() {
+        String[] fieldNames = {};
+        SeaTunnelDataType<?>[] fieldTypes = {};
+        SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+        consoleSinkWriter = new ConsoleSinkWriter(seaTunnelRowType);
+    }
+
+    private Object fieldToStringTest(SeaTunnelDataType<?> dataType, Object value) {
+        Optional<Method> fieldToString = ReflectionUtils.getDeclaredMethod(ConsoleSinkWriter.class, "fieldToString", SeaTunnelDataType.class, Object.class);
+        Method method = fieldToString.orElseThrow(() -> new RuntimeException("method fieldToString not found"));
+        try {
+            return method.invoke(consoleSinkWriter, dataType, value);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    void arrayIntTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            Integer[] integerArr = {1};
+            Object integerArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, integerArr);
+            Assertions.assertEquals(integerArrString, "[1]");
+            int[] intArr = {1, 2};
+            Object intArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, intArr);
+            Assertions.assertEquals(intArrString, "[1, 2]");
+        });
+    }
+
+    @Test
+    void stringTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String str = RandomStringUtils.randomAlphanumeric(10);
+            Object obj = fieldToStringTest(BasicType.STRING_TYPE, str);
+            Assertions.assertTrue(obj instanceof String);
+            Assertions.assertEquals(10, ((String) obj).length());
+        });
+    }
+
+    @Test
+    void hashMapTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            HashMap<Object, Object> map = new HashMap<>();
+            map.put("key", "value");
+            MapType<String, String> mapType = new MapType<>(BasicType.STRING_TYPE, BasicType.STRING_TYPE);
+            Object mapString = fieldToStringTest(mapType, map);
+            Assertions.assertNotNull(mapString);
+        });
+    }
+
+    @Test
+    void rowTypeTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String[] fieldNames = {"c_byte", "c_array", "bytes"};
+            SeaTunnelDataType<?>[] fieldTypes = {BasicType.BYTE_TYPE, ArrayType.BYTE_ARRAY_TYPE, PrimitiveByteArrayType.INSTANCE};
+            SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+            byte[] bytes = RandomUtils.nextBytes(10);
+            Object[] rowData = {(byte) 1, bytes, bytes};
+            Object rowString = fieldToStringTest(seaTunnelRowType, rowData);
+            Assertions.assertNotNull(rowString);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org