From 0c6f1bac1198b0c8a1aa630235b468a9a6cc033e Mon Sep 17 00:00:00 2001 From: qzhu Date: Fri, 1 Nov 2024 14:25:49 +0800 Subject: [PATCH] [CALCITE-6663] Support SPLIT_PART function for PostgreSQL --- .../org/apache/calcite/runtime/SqlFunctions.java | 6 +++--- .../org/apache/calcite/test/SqlFunctionsTest.java | 12 ++++++------ site/_docs/reference.md | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java index 1be115f261f..80af98fd23e 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -917,8 +917,8 @@ public static List 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); @@ -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]; diff --git a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java index c4e588fb238..253deb08dc9 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java @@ -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() { diff --git a/site/_docs/reference.md b/site/_docs/reference.md index 9924940c590..a2a4aba44e6 100644 --- a/site/_docs/reference.md +++ b/site/_docs/reference.md @@ -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