Skip to content

Commit

Permalink
[Scalar Function][Array] Add ArrayJoinString Scalar function (apache#…
Browse files Browse the repository at this point in the history
…14537)

* [Scalar Function][Array] Add ArrayJoinString function

* Update func name and allow null replacement

* Remove unnecessary changes

* Fix tests
  • Loading branch information
nikhilsu authored Nov 26, 2024
1 parent b34e805 commit 1975268
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,22 @@ public static double[] generateDoubleArray(double start, double end, double inc)
}
return arr;
}

@ScalarFunction
public static String arrayToString(String[] values, String delimiter) {
return String.join(delimiter, values);
}

@ScalarFunction
public static String arrayToString(String[] values, String delimiter, String nullString) {
if (values == null || values.length == 0) {
return NullValuePlaceHolder.STRING;
}

return String.join(
delimiter,
Arrays.stream(values)
.map(s -> s == null || s.equals(NullValuePlaceHolder.STRING) ? nullString : s)
.toArray(String[]::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Object[][] arrayFunctionsDataProvider() {
row.putValue("intArray", new int[]{3, 2, 10, 6, 1, 12});
row.putValue("integerArray", new Integer[]{3, 2, 10, 6, 1, 12});
row.putValue("stringArray", new String[]{"3", "2", "10", "6", "1", "12"});
row.putValue("stringArrayWithNulls", new String[]{"3", "2", "10", "6", "1", "12", "", null});

inputs.add(new Object[]{
"array_reverse_int(intArray)", Collections.singletonList("intArray"), row, new int[]{12, 1, 6, 10, 2, 3}
Expand Down Expand Up @@ -180,6 +181,14 @@ public Object[][] arrayFunctionsDataProvider() {
"array_concat_string(stringArray, stringArray)", Lists.newArrayList("stringArray", "stringArray"), row,
new String[]{"3", "2", "10", "6", "1", "12", "3", "2", "10", "6", "1", "12"}
});
inputs.add(new Object[]{
"array_to_string(stringArray, '::')", Collections.singletonList("stringArray"), row,
"3::2::10::6::1::12"
});
inputs.add(new Object[]{
"array_to_string(stringArrayWithNulls, '::', '*')", Collections.singletonList("stringArrayWithNulls"), row,
"3::2::10::6::1::12::*::*"
});
return inputs.toArray(new Object[0][]);
}
}

0 comments on commit 1975268

Please sign in to comment.