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 0c6f1ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 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.equals("") || 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
1 change: 1 addition & 0 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,7 @@ In the following:
| s | SOUNDEX(string) | Returns the phonetic representation of *string*; return original *string* if *string* is encoded with multi-byte encoding such as UTF-8
| m s | SPACE(integer) | Returns a string of *integer* spaces; returns an empty string if *integer* is less than 1
| b | SPLIT(string [, delimiter ]) | Returns the string array of *string* split at *delimiter* (if omitted, default is comma). If the *string* is empty it returns an empty array, otherwise, if the *delimiter* is empty, it returns an array containing the original *string*.
| p | SPLIT_PART(string, delimiter, position) | Returns the *position* number field in *string* using *delimiter*; returns empty string if *position* is less than 1 or greater than the number of fields, and the position can be negative to count from the end.
| f s | STARTSWITH(string1, string2) | Returns whether *string2* is a prefix of *string1*
| b p | STARTS_WITH(string1, string2) | Equivalent to `STARTSWITH(string1, string2)`
| m | STRCMP(string, string) | Returns 0 if both of the strings are same and returns -1 when the first argument is smaller than the second and 1 when the second one is smaller than the first one
Expand Down

0 comments on commit 0c6f1ba

Please sign in to comment.