From 5032c2015cc996ee0265626451cac4708d62996e Mon Sep 17 00:00:00 2001 From: sohurdc Date: Mon, 30 Dec 2024 12:59:28 +0800 Subject: [PATCH] =?UTF-8?q?[Hotfix][Transform][Sql]=20function=20concat=5F?= =?UTF-8?q?ws=20handle=20array=20type=EF=BC=8C=20avoid=20write=20[Ljava.la?= =?UTF-8?q?ng.String.=20(#8369)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sql/zeta/functions/StringFunction.java | 21 +++++- .../sql/zeta/ConcatWsFunctionTest.java | 67 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/ConcatWsFunctionTest.java diff --git a/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java b/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java index 2bf71e3b606..85467a95b22 100644 --- a/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java +++ b/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java @@ -23,12 +23,16 @@ import org.apache.groovy.parser.antlr4.util.StringUtils; +import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; import java.time.temporal.Temporal; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class StringFunction { private static final byte[] SOUNDEX_INDEX = @@ -105,7 +109,22 @@ public static String concatWs(List args) { } f = true; } - builder.append(arg); + if (arg.getClass().isArray()) { + int len = Array.getLength(arg); + List ll = new ArrayList<>(); + for (int j = 0; j < len; j++) { + Object o = Array.get(arg, j); + ll.add(o); + } + String s = + ll.stream() + .filter(Objects::nonNull) + .map(Object::toString) + .collect(Collectors.joining(separator != null ? separator : "")); + builder.append(s); + } else { + builder.append(arg); + } } return builder.toString(); } diff --git a/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/ConcatWsFunctionTest.java b/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/ConcatWsFunctionTest.java new file mode 100644 index 00000000000..c0f290255fb --- /dev/null +++ b/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/sql/zeta/ConcatWsFunctionTest.java @@ -0,0 +1,67 @@ +/* + * 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; + +import org.apache.seatunnel.transform.sql.zeta.functions.StringFunction; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ConcatWsFunctionTest { + + @Test + public void testConcatWs() { + Assertions.assertEquals("", StringFunction.concatWs(genArgs(";", new String[] {}))); + Assertions.assertEquals("", StringFunction.concatWs(genArgs(null, new String[] {}))); + Assertions.assertEquals( + "a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"}))); + Assertions.assertEquals( + "a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", null, "b"}))); + Assertions.assertEquals( + "ab", + StringFunction.concatWs(genArgs("", new String[] {null, "a", null, "b", null}))); + Assertions.assertEquals( + "ab", StringFunction.concatWs(genArgs(null, new String[] {"a", "b", null}))); + Assertions.assertEquals( + "a;b;c", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"}, "c"))); + Assertions.assertEquals( + "a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"}, null))); + Assertions.assertEquals( + "a;b;1;2", + StringFunction.concatWs( + genArgs(";", new String[] {"a", "b"}, new String[] {"1", "2"}))); + } + + public List genArgs(String separator, String[] arr) { + List list = new ArrayList<>(); + list.add(separator); + list.add(arr); + return list; + } + + public List genArgs(String separator, Object... arr) { + List list = new ArrayList<>(); + list.add(separator); + Collections.addAll(list, arr); + return list; + } +}