rubenada commented on code in PR #3237:
URL: https://github.com/apache/calcite/pull/3237#discussion_r1223065806


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -4102,6 +4102,43 @@ public static List reverse(List list) {
     return list;
   }
 
+  /** SQL {@code ARRAY_TO_STRING(array, delimiter)} function. */
+  public static String arrayToString(List list, String delimiter) {
+    return arrayToString(list, delimiter, null);
+  }
+
+  /** SQL {@code ARRAY_TO_STRING(array, delimiter, nullText)} function. */
+  public static String arrayToString(List list, String delimiter, @Nullable 
String nullText) {
+    StringBuilder sb = new StringBuilder();
+    for (Object item : list) {
+      String str;
+      if (item == null) {
+        str = null;
+      } else if (item instanceof String) {
+        str = (String) item;
+      } else if (item instanceof ByteString) {
+        str = item.toString();
+      } else {
+        throw new IllegalStateException(
+            "arrayToString supports only String or ByteString, but got "
+                + item.getClass().getName());
+      }
+      if (str == null) {
+        if (nullText == null) {
+          continue;
+        } else {
+          str = nullText;
+        }
+      }
+
+      if (sb.length() > 0) {

Review Comment:
   Sorry @zoudan , one last detail (rather an edge case): what is the expected 
behavior if the item (`str` in this point) is the empty string? Will it be 
appended like any other value with the corresponding delimiter before the 
precious value? 
   For example:
   ```
   array_to_string(array['', 'b'], '-') => '-b'    ???
   array_to_string(array['', ''], '-') => '-'    ???
   ```
   If that is the case, I have the impression that this condition is wrong and 
we would need to find another mechanism, e.g.
   ```
   boolean first = true;
   for (Object item : list) {
     ...
     if (!first) {
       sb.append(delimiter);
     }
     sb.append(str);
     first = false;
   } 
   ```
   wdyt?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to