chl-wxp commented on code in PR #10145: URL: https://github.com/apache/seatunnel/pull/10145#discussion_r2621401527
########## seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/ArrayFunction.java: ########## @@ -20,7 +20,7 @@ import org.apache.seatunnel.api.table.type.BasicType; import org.apache.seatunnel.api.table.type.SeaTunnelDataType; import org.apache.seatunnel.api.table.type.SeaTunnelRowType; -import org.apache.seatunnel.common.exception.CommonErrorCode; +import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; Review Comment: CommonErrorCodeDeprecated This class has been marked as obsolete and is deprecated. ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/SQLHashFunctionsTest.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.transform.sql; + +import org.apache.seatunnel.shade.com.google.common.hash.Hashing; + +import org.apache.seatunnel.api.configuration.ReadonlyConfig; +import org.apache.seatunnel.api.table.catalog.CatalogTable; +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.List; + +/** Tests for hash functions like MURMUR64 */ +public class SQLHashFunctionsTest { + + private SeaTunnelRow runSql(String query, SeaTunnelRowType rowType, Object... values) { + CatalogTable table = CatalogTableUtil.getCatalogTable("test", rowType); + ReadonlyConfig config = ReadonlyConfig.fromMap(Collections.singletonMap("query", query)); + SQLTransform transform = new SQLTransform(config, table); + List<SeaTunnelRow> out = transform.transformRow(new SeaTunnelRow(values)); + Assertions.assertNotNull(out); + Assertions.assertFalse(out.isEmpty()); + return out.get(0); + } + + private static Long murmur64Direct(String input) { + if (input == null) { + return null; + } + return Hashing.murmur3_128().hashString(input, StandardCharsets.UTF_8).asLong(); + } + + @Test + public void testMurmur64WithNormalString() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = + runSql("select MURMUR64(text) as hash from dual", rowType, "hello world"); + + Assertions.assertEquals(murmur64Direct("hello world"), outRow.getField(0)); Review Comment: Add an assertion to determine the return type ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/functions/ArrayFunctionTest.java: ########## @@ -72,4 +81,140 @@ void testNestedArrayEvaluateWithSQLEngine() { Assertions.assertEquals(3, ((Number) inner2[0]).intValue()); Assertions.assertEquals(4, ((Number) inner2[1]).intValue()); } + + // ------------------ arrayMax / arrayMin ------------------ + + @Test + void testArrayMaxAndMinWithIntegers() { + Object[] values = new Object[] {1, 3, 2}; + Object max = ArrayFunction.arrayMax(java.util.Collections.singletonList((Object) values)); + Object min = ArrayFunction.arrayMin(java.util.Collections.singletonList((Object) values)); + + Assertions.assertEquals(3, max); + Assertions.assertEquals(1, min); + } + + @Test + void testArrayMaxAndMinWithStrings() { + Object[] values = new Object[] {"a", "c", "b"}; + Object max = ArrayFunction.arrayMax(java.util.Collections.singletonList((Object) values)); + Object min = ArrayFunction.arrayMin(java.util.Collections.singletonList((Object) values)); + + Assertions.assertEquals("c", max); + Assertions.assertEquals("a", min); + } + + @Test + void testArrayMaxAndMinWithEmptyOrNullArray() { + Object[] empty = new Object[] {}; + Assertions.assertNull( + ArrayFunction.arrayMax(java.util.Collections.singletonList((Object) empty))); + Assertions.assertNull( + ArrayFunction.arrayMin(java.util.Collections.singletonList((Object) empty))); + Review Comment: remove ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/SQLHashFunctionsTest.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.transform.sql; + +import org.apache.seatunnel.shade.com.google.common.hash.Hashing; + +import org.apache.seatunnel.api.configuration.ReadonlyConfig; +import org.apache.seatunnel.api.table.catalog.CatalogTable; +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.List; + +/** Tests for hash functions like MURMUR64 */ +public class SQLHashFunctionsTest { + + private SeaTunnelRow runSql(String query, SeaTunnelRowType rowType, Object... values) { + CatalogTable table = CatalogTableUtil.getCatalogTable("test", rowType); + ReadonlyConfig config = ReadonlyConfig.fromMap(Collections.singletonMap("query", query)); + SQLTransform transform = new SQLTransform(config, table); + List<SeaTunnelRow> out = transform.transformRow(new SeaTunnelRow(values)); + Assertions.assertNotNull(out); + Assertions.assertFalse(out.isEmpty()); + return out.get(0); + } + + private static Long murmur64Direct(String input) { + if (input == null) { + return null; + } + return Hashing.murmur3_128().hashString(input, StandardCharsets.UTF_8).asLong(); + } + + @Test + public void testMurmur64WithNormalString() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = + runSql("select MURMUR64(text) as hash from dual", rowType, "hello world"); + + Assertions.assertEquals(murmur64Direct("hello world"), outRow.getField(0)); + } + + @Test + public void testMurmur64WithEmptyString() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = runSql("select MURMUR64(text) as hash from dual", rowType, ""); + + Assertions.assertEquals(murmur64Direct(""), outRow.getField(0)); Review Comment: Add an assertion to determine the return type ########## seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/ArrayFunction.java: ########## @@ -53,8 +53,8 @@ public static Object arrayMax(List<Object> args) { .orElse(null); } throw new TransformException( - CommonErrorCode.UNSUPPORTED_DATA_TYPE, - String.format("Unsupported function max() arguments: %s", args)); + CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE, Review Comment: CommonErrorCodeDeprecated This class has been marked as obsolete and is deprecated. ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/functions/udf/DesEncryptTest.java: ########## @@ -0,0 +1,62 @@ +/* + * 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.transform.sql.zeta.functions.udf; + +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +public class DesEncryptTest { + + @Test + public void testFunctionNameAndResultType() { + DesEncrypt udf = new DesEncrypt(); + Assertions.assertEquals("DES_ENCRYPT", udf.functionName()); + + List<SeaTunnelDataType<?>> argTypes = + Arrays.asList(BasicType.STRING_TYPE, BasicType.STRING_TYPE); + Assertions.assertEquals(BasicType.STRING_TYPE, udf.resultType(argTypes)); + } + + @Test + public void testEvaluateEncryptsWithValidArguments() { + DesEncrypt udf = new DesEncrypt(); + String password = "password123"; + String plain = "hello-udf"; + + List<Object> args = Arrays.asList(password, plain); + Object result = udf.evaluate(args); + Assertions.assertTrue(result instanceof String); + + String decrypted = DESUtil.decrypt(password, (String) result); + Assertions.assertEquals(plain, decrypted); + } + + @Test + public void testEvaluateReturnsNullWhenPasswordOrDataIsNull() { + DesEncrypt udf = new DesEncrypt(); + Review Comment: Remove blank line breaks ########## seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java: ########## @@ -479,7 +480,7 @@ private static int makeRegexpFlags(String stringFlags, boolean ignoreGlobalFlag) CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION, Review Comment: use CommonErrorCode ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/SQLHashFunctionsTest.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.transform.sql; + +import org.apache.seatunnel.shade.com.google.common.hash.Hashing; + +import org.apache.seatunnel.api.configuration.ReadonlyConfig; +import org.apache.seatunnel.api.table.catalog.CatalogTable; +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.List; + +/** Tests for hash functions like MURMUR64 */ +public class SQLHashFunctionsTest { + + private SeaTunnelRow runSql(String query, SeaTunnelRowType rowType, Object... values) { + CatalogTable table = CatalogTableUtil.getCatalogTable("test", rowType); + ReadonlyConfig config = ReadonlyConfig.fromMap(Collections.singletonMap("query", query)); + SQLTransform transform = new SQLTransform(config, table); + List<SeaTunnelRow> out = transform.transformRow(new SeaTunnelRow(values)); + Assertions.assertNotNull(out); + Assertions.assertFalse(out.isEmpty()); + return out.get(0); + } + + private static Long murmur64Direct(String input) { + if (input == null) { + return null; + } + return Hashing.murmur3_128().hashString(input, StandardCharsets.UTF_8).asLong(); + } + + @Test + public void testMurmur64WithNormalString() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = + runSql("select MURMUR64(text) as hash from dual", rowType, "hello world"); + + Assertions.assertEquals(murmur64Direct("hello world"), outRow.getField(0)); + } + + @Test + public void testMurmur64WithEmptyString() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = runSql("select MURMUR64(text) as hash from dual", rowType, ""); + + Assertions.assertEquals(murmur64Direct(""), outRow.getField(0)); + } + + @Test + public void testMurmur64WithNull() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + SeaTunnelRow outRow = + runSql("select MURMUR64(text) as hash from dual", rowType, (Object) null); + + Assertions.assertNull(outRow.getField(0)); + } + + @Test + public void testMurmur64Consistency() { + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"text"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE}); + + // Same input should always produce same hash + SeaTunnelRow outRow1 = + runSql("select MURMUR64(text) as hash from dual", rowType, "test123"); + SeaTunnelRow outRow2 = + runSql("select MURMUR64(text) as hash from dual", rowType, "test123"); + + Assertions.assertEquals(outRow1.getField(0), outRow2.getField(0)); Review Comment: Add an assertion to determine the return type ########## seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/functions/udf/DESUtilTest.java: ########## @@ -0,0 +1,55 @@ +/* + * 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.transform.sql.zeta.functions.udf; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DESUtilTest { + + @Test + public void testEncryptDecryptRoundTrip() { + String password = "password123"; + String data = "hello-world"; + + String encrypted = DESUtil.encrypt(password, data); + Assertions.assertNotNull(encrypted); + Assertions.assertNotEquals(data, encrypted); + + String decrypted = DESUtil.decrypt(password, encrypted); + Assertions.assertEquals(data, decrypted); + } + + @Test + public void testEncryptAndDecryptNullData() { + String password = "password123"; + Review Comment: Remove blank line breaks -- 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]
