Skip to content

Commit

Permalink
[CALCITE-6663] Support SPLIT_PART function for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuqi-lucas committed Nov 1, 2024
1 parent 43973fe commit 48434f4
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,8 @@ public static List<String> split(String s, String delimiter) {

/** SQL {@code SPLIT_PART(string, string, int)} function. */
public static String splitPart(String s, String delimiter, int n) {
if (s == null || delimiter == null || delimiter.isEmpty() || n == 0) {
return null;
if (s == null || s == "" || delimiter == null || delimiter.isEmpty() || n == 0) {
return "";
}

String[] parts = s.split(Pattern.quote(delimiter), -1);
Expand All @@ -929,7 +929,7 @@ public static String splitPart(String s, String delimiter, int n) {
}

if (n <= 0 || n > partCount) {
return null;
return "";
}

return parts[n - 1];
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1061,14 +1061,14 @@ private void checkCeil(int x, int y, int result) {

assertThat(SqlFunctions.splitPart("abc,,ghi", ",", 2), is(""));
assertThat(SqlFunctions.splitPart("", ",", 1), is(""));
assertThat(SqlFunctions.splitPart("abc", "", 1), is(nullValue()));
assertThat(SqlFunctions.splitPart("abc", "", 1), is(""));

assertThat(SqlFunctions.splitPart(null, ",", 1), is(nullValue()));
assertThat(SqlFunctions.splitPart("abc,def", null, 1), is(nullValue()));
assertThat(SqlFunctions.splitPart("abc,def", ",", 0), is(nullValue()));
assertThat(SqlFunctions.splitPart(null, ",", 1), is(""));
assertThat(SqlFunctions.splitPart("abc,def", null, 1), is(""));
assertThat(SqlFunctions.splitPart("abc,def", ",", 0), is(""));

assertThat(SqlFunctions.splitPart("abc,def", ",", 3), is(nullValue()));
assertThat(SqlFunctions.splitPart("abc,def", ",", -3), is(nullValue()));
assertThat(SqlFunctions.splitPart("abc,def", ",", 3), is(""));
assertThat(SqlFunctions.splitPart("abc,def", ",", -3), is(""));
}

@Test void testByteString() {
Expand Down
Loading

0 comments on commit 48434f4

Please sign in to comment.