Skip to content

Commit

Permalink
[Hotfix][Transform][Sql] function concat_ws handle array type, avoid …
Browse files Browse the repository at this point in the history
…write [Ljava.lang.String. (apache#8369)
  • Loading branch information
sohurdc authored Dec 30, 2024
1 parent b8e1177 commit 5032c20
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -105,7 +109,22 @@ public static String concatWs(List<Object> args) {
}
f = true;
}
builder.append(arg);
if (arg.getClass().isArray()) {
int len = Array.getLength(arg);
List<Object> 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> genArgs(String separator, String[] arr) {
List<Object> list = new ArrayList<>();
list.add(separator);
list.add(arr);
return list;
}

public List<Object> genArgs(String separator, Object... arr) {
List<Object> list = new ArrayList<>();
list.add(separator);
Collections.addAll(list, arr);
return list;
}
}

0 comments on commit 5032c20

Please sign in to comment.