Copilot commented on code in PR #9299:
URL: https://github.com/apache/seatunnel/pull/9299#discussion_r2106458936


##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/SystemFunction.java:
##########
@@ -105,10 +116,26 @@ public static Object castAs(List<Object> args) {
                 return v1.toString();
             case "INT":
             case "INTEGER":
-                return Integer.parseInt(v1.toString());
+                if (v1 instanceof String) {
+                    return Integer.parseInt(v1.toString());
+                } else if (v1 instanceof Number) {
+                    return ((Number) v1).intValue();

Review Comment:
   [nitpick] Consider adding comments to clarify the conversion logic in this 
section for 'INT' and 'LONG' types to improve code readability and facilitate 
future maintenance.
   ```suggestion
               case "INTEGER":
                   // Handle conversion to INT/INTEGER type
                   // If the input is a String, attempt to parse it as an 
Integer
                   if (v1 instanceof String) {
                       return Integer.parseInt(v1.toString());
                   // If the input is a Number, extract its integer value
                   } else if (v1 instanceof Number) {
                       return ((Number) v1).intValue();
                   // Throw an exception for unsupported types
   ```



##########
seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/SQLTransformTest.java:
##########
@@ -441,4 +441,217 @@ public void tesBooleanField() {
         Assertions.assertEquals(true, result.get(0).getField(1));
         Assertions.assertEquals(false, result.get(0).getField(2));
     }
+
+    @Test
+    public void testCoalesceStringAndIntType() {
+        String tableName = "test";
+        String[] fields = new String[] {"id", "stringField", "intField", 
"doubleField"};
+        CatalogTable table =
+                CatalogTableUtil.getCatalogTable(
+                        tableName,
+                        new SeaTunnelRowType(
+                                fields,
+                                new SeaTunnelDataType[] {
+                                    BasicType.INT_TYPE,
+                                    BasicType.STRING_TYPE,
+                                    BasicType.INT_TYPE,
+                                    BasicType.DOUBLE_TYPE
+                                }));
+
+        // Test that the first argument of COALESCE is a string type, followed 
by an integer type
+        ReadonlyConfig config =
+                ReadonlyConfig.fromMap(
+                        Collections.singletonMap(
+                                "query",
+                                "select id, COALESCE(stringField, intField) as 
result from dual"));
+        SQLTransform sqlTransform = new SQLTransform(config, table);
+        TableSchema tableSchema = sqlTransform.transformTableSchema();
+
+        // Verify that the result field type is STRING
+        Assertions.assertEquals("result", tableSchema.getFieldNames()[1]);
+        Assertions.assertEquals(
+                BasicType.STRING_TYPE, 
tableSchema.getColumns().get(1).getDataType());
+
+        // The first field is not null, the value of the first field should be 
returned directly
+        List<SeaTunnelRow> result =
+                sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
"test", 123, 123.45}));
+        Assertions.assertEquals("test", result.get(0).getField(1));
+
+        // If the first field is null, the value of the second field converted 
to a string should
+        // be returned.
+        result = sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
null, 123, 123.45}));
+        Assertions.assertEquals("123", result.get(0).getField(1));
+        // Make sure the return value is a string and not an integer.
+        Assertions.assertTrue(
+                result.get(0).getField(1) instanceof String,
+                "The result should be a string type, but is actually "
+                        + result.get(0).getField(1).getClass().getName());
+    }
+
+    @Test
+    public void testCoalesceIntAndDoubleType() {
+        String tableName = "test";
+        String[] fields = new String[] {"id", "stringField", "intField", 
"doubleField"};
+        CatalogTable table =
+                CatalogTableUtil.getCatalogTable(
+                        tableName,
+                        new SeaTunnelRowType(
+                                fields,
+                                new SeaTunnelDataType[] {
+                                    BasicType.INT_TYPE,
+                                    BasicType.STRING_TYPE,
+                                    BasicType.INT_TYPE,
+                                    BasicType.DOUBLE_TYPE
+                                }));
+
+        // Test that the first argument of COALESCE is an integer type, 
followed by a floating point
+        // type
+        ReadonlyConfig config =
+                ReadonlyConfig.fromMap(
+                        Collections.singletonMap(
+                                "query",
+                                "select id, COALESCE(intField, doubleField) as 
result from dual"));
+        SQLTransform sqlTransform = new SQLTransform(config, table);
+        TableSchema tableSchema = sqlTransform.transformTableSchema();
+
+        // Verify that the result field type is INT
+        Assertions.assertEquals("result", tableSchema.getFieldNames()[1]);
+        Assertions.assertEquals(BasicType.INT_TYPE, 
tableSchema.getColumns().get(1).getDataType());
+
+        // The first field is not null, the value of the first field should be 
returned directly
+        List<SeaTunnelRow> result =
+                sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
"test", 123, 123.45}));
+        Assertions.assertEquals(123, result.get(0).getField(1));
+        Assertions.assertTrue(
+                result.get(0).getField(1) instanceof Integer,
+                "The result should be an integer type, but is actually "
+                        + result.get(0).getField(1).getClass().getName());
+
+        // If the first field is null, the value of the second field converted 
to an integer should
+        // be returned.
+        result =
+                sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
"test", null, 456.78}));
+        Assertions.assertEquals(456, result.get(0).getField(1));
+        Assertions.assertTrue(
+                result.get(0).getField(1) instanceof Integer,
+                "The result should be an integer type, but is actually "
+                        + result.get(0).getField(1).getClass().getName());
+    }
+
+    @Test
+    public void testCoalesceWithNullFirstArgument() {
+        String tableName = "test";
+        String[] fields = new String[] {"id", "stringField", "intField", 
"doubleField"};
+        CatalogTable table =
+                CatalogTableUtil.getCatalogTable(
+                        tableName,
+                        new SeaTunnelRowType(
+                                fields,
+                                new SeaTunnelDataType[] {
+                                    BasicType.INT_TYPE,
+                                    BasicType.STRING_TYPE,
+                                    BasicType.INT_TYPE,
+                                    BasicType.DOUBLE_TYPE
+                                }));
+
+        // Test COALESCE with null as first argument
+        ReadonlyConfig config =
+                ReadonlyConfig.fromMap(
+                        Collections.singletonMap(
+                                "query",
+                                "select id, COALESCE(null, stringField, 
intField) as result from dual"));
+        SQLTransform sqlTransform = new SQLTransform(config, table);
+        TableSchema tableSchema = sqlTransform.transformTableSchema();
+
+        // Verify that the result field type is STRING (since stringField is 
the first non-null
+        // parameter)
+        Assertions.assertEquals("result", tableSchema.getFieldNames()[1]);
+        Assertions.assertEquals(
+                BasicType.STRING_TYPE, 
tableSchema.getColumns().get(1).getDataType());
+
+        // Test with both stringField and intField having values
+        List<SeaTunnelRow> result =
+                sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
"test", 123, 123.45}));
+        Assertions.assertEquals("test", result.get(0).getField(1));
+        Assertions.assertTrue(
+                result.get(0).getField(1) instanceof String,
+                "The result should be a string type, but is actually "
+                        + result.get(0).getField(1).getClass().getName());
+
+        // Test with stringField being null, should return intField as string
+        result = sqlTransform.transformRow(new SeaTunnelRow(new Object[] {1, 
null, 123, 123.45}));
+        Assertions.assertEquals("123", result.get(0).getField(1));
+        Assertions.assertTrue(
+                result.get(0).getField(1) instanceof String,
+                "The result should be a string type, but is actually "
+                        + result.get(0).getField(1).getClass().getName());
+    }
+
+    @Test
+    public void testCoalesceWithNoParameters() {
+        String tableName = "test";
+        String[] fields = new String[] {"id", "name"};
+        CatalogTable table =
+                CatalogTableUtil.getCatalogTable(
+                        tableName,
+                        new SeaTunnelRowType(
+                                fields,
+                                new SeaTunnelDataType[] {
+                                    BasicType.INT_TYPE, BasicType.STRING_TYPE
+                                }));
+
+        // Testing the COALESCE function without parameters
+        ReadonlyConfig config =
+                ReadonlyConfig.fromMap(
+                        Collections.singletonMap(
+                                "query", "select id, COALESCE() as result from 
dual"));
+
+        // An exception should be thrown because COALESCE requires at least 
one parameter
+        TransformException exception =
+                Assertions.assertThrows(
+                        TransformException.class,
+                        () -> {
+                            SQLTransform sqlTransform = new 
SQLTransform(config, table);
+                            sqlTransform.transformTableSchema();
+                        });
+
+        Assertions.assertTrue(
+                exception
+                        .getMessage()
+                        .contains("COALESCE function requires at least one 
parameter"),
+                "COALESCE function requires at least one parameter");
+    }
+
+    @Test
+    public void testCoalesceWithNonExistentColumn() {
+        String tableName = "test";
+        String[] fields = new String[] {"id", "name"};
+        CatalogTable table =
+                CatalogTableUtil.getCatalogTable(
+                        tableName,
+                        new SeaTunnelRowType(
+                                fields,
+                                new SeaTunnelDataType[] {
+                                    BasicType.INT_TYPE, BasicType.STRING_TYPE
+                                }));
+
+        // Testing COALESCE functions that use non-existent fields
+        ReadonlyConfig config =
+                ReadonlyConfig.fromMap(
+                        Collections.singletonMap(
+                                "query",
+                                "select id, COALESCE(nonexistent_column, name) 
as result from dual"));
+
+        // An exception should be thrown because nonexistent_column does not 
exist
+        Exception exception =
+                Assertions.assertThrows(
+                        Exception.class,

Review Comment:
   Consider using a more specific exception type (e.g., TransformException) 
instead of the generic Exception when testing non-existent columns so that the 
test precisely captures the expected failure condition.
   ```suggestion
           TransformException exception =
                   Assertions.assertThrows(
                           TransformException.class,
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to