Skip to content

Commit 5032c20

Browse files
authored
[Hotfix][Transform][Sql] function concat_ws handle array type, avoid write [Ljava.lang.String. (#8369)
1 parent b8e1177 commit 5032c20

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/StringFunction.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323

2424
import org.apache.groovy.parser.antlr4.util.StringUtils;
2525

26+
import java.lang.reflect.Array;
2627
import java.nio.charset.StandardCharsets;
2728
import java.time.temporal.Temporal;
29+
import java.util.ArrayList;
2830
import java.util.Arrays;
2931
import java.util.List;
32+
import java.util.Objects;
3033
import java.util.regex.Matcher;
3134
import java.util.regex.Pattern;
35+
import java.util.stream.Collectors;
3236

3337
public class StringFunction {
3438
private static final byte[] SOUNDEX_INDEX =
@@ -105,7 +109,22 @@ public static String concatWs(List<Object> args) {
105109
}
106110
f = true;
107111
}
108-
builder.append(arg);
112+
if (arg.getClass().isArray()) {
113+
int len = Array.getLength(arg);
114+
List<Object> ll = new ArrayList<>();
115+
for (int j = 0; j < len; j++) {
116+
Object o = Array.get(arg, j);
117+
ll.add(o);
118+
}
119+
String s =
120+
ll.stream()
121+
.filter(Objects::nonNull)
122+
.map(Object::toString)
123+
.collect(Collectors.joining(separator != null ? separator : ""));
124+
builder.append(s);
125+
} else {
126+
builder.append(arg);
127+
}
109128
}
110129
return builder.toString();
111130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.transform.sql.zeta;
19+
20+
import org.apache.seatunnel.transform.sql.zeta.functions.StringFunction;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.ArrayList;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class ConcatWsFunctionTest {
30+
31+
@Test
32+
public void testConcatWs() {
33+
Assertions.assertEquals("", StringFunction.concatWs(genArgs(";", new String[] {})));
34+
Assertions.assertEquals("", StringFunction.concatWs(genArgs(null, new String[] {})));
35+
Assertions.assertEquals(
36+
"a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"})));
37+
Assertions.assertEquals(
38+
"a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", null, "b"})));
39+
Assertions.assertEquals(
40+
"ab",
41+
StringFunction.concatWs(genArgs("", new String[] {null, "a", null, "b", null})));
42+
Assertions.assertEquals(
43+
"ab", StringFunction.concatWs(genArgs(null, new String[] {"a", "b", null})));
44+
Assertions.assertEquals(
45+
"a;b;c", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"}, "c")));
46+
Assertions.assertEquals(
47+
"a;b", StringFunction.concatWs(genArgs(";", new String[] {"a", "b"}, null)));
48+
Assertions.assertEquals(
49+
"a;b;1;2",
50+
StringFunction.concatWs(
51+
genArgs(";", new String[] {"a", "b"}, new String[] {"1", "2"})));
52+
}
53+
54+
public List<Object> genArgs(String separator, String[] arr) {
55+
List<Object> list = new ArrayList<>();
56+
list.add(separator);
57+
list.add(arr);
58+
return list;
59+
}
60+
61+
public List<Object> genArgs(String separator, Object... arr) {
62+
List<Object> list = new ArrayList<>();
63+
list.add(separator);
64+
Collections.addAll(list, arr);
65+
return list;
66+
}
67+
}

0 commit comments

Comments
 (0)