You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "chucheng92 (via GitHub)" <gi...@apache.org> on 2023/08/03 12:35:13 UTC

[GitHub] [calcite] chucheng92 commented on a diff in pull request #3320: [CALCITE-5830] Add ARRAY_INSERT function(enabled in Spark library)

chucheng92 commented on code in PR #3320:
URL: https://github.com/apache/calcite/pull/3320#discussion_r1283139677


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -4107,6 +4113,97 @@ public static List arrayExcept(List list1, List list2) {
     return new ArrayList<>(result);
   }
 
+  /** Support the ARRAY_INSERT(array, pos, val) function. */
+  public static @Nullable List arrayInsert(List baselist, Object pos, Object val) {
+    if (baselist == null || pos == null) {
+      return null;
+    }
+    int posInt = (int) pos;
+    if (posInt == 0) {
+      throw new IllegalArgumentException("The index 0 is invalid. "
+          + "An index shall be either < 0 or > 0 (the first element has index 1)");
+    }
+
+    boolean usePositivePos = posInt > 0;
+
+    if (usePositivePos) {
+      int newArrayLength = Math.max(baselist.size() + 1, posInt);

Review Comment:
   @NobiGo if position > arrayLength, it will insert null. pls see:
   ```
   spark-sql (default)> SELECT array_insert(array(1, 2, 3, 4), -4, 5);
   [5,1,2,3,4]
   Time taken: 0.062 seconds, Fetched 1 row(s)
   spark-sql (default)> SELECT array_insert(array(1, 2, 3, 4), -10, 5);
   [5,null,null,null,null,null,null,1,2,3,4]
   
   ```



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org