NobiGo commented on code in PR #3320:
URL: https://github.com/apache/calcite/pull/3320#discussion_r1282887653
##########
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) {
Review Comment:
The second parameter is the Object type. Why not Integer type?
##########
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);
+
+ if (newArrayLength > MAX_ARRAY_LENGTH) {
+ throw new IndexOutOfBoundsException(
+ String.format(Locale.ROOT, "The new array length %s exceeds the
allowed limit.",
+ newArrayLength));
+ }
+
+ Object[] newArray = new Object[newArrayLength];
+
+ int posIndex = posInt - 1;
+ for (int i = 0; i < baselist.size(); i++) {
+ if (i >= posIndex) {
+ newArray[i + 1] = baselist.get(i);
+ } else {
+ newArray[i] = baselist.get(i);
+ }
+ }
+
+ newArray[posIndex] = val;
+
+ return Arrays.asList(newArray);
+ } else {
+ int posIndex = posInt;
+
+ boolean newPosExtendsArrayLeft = -posIndex > baselist.size();
Review Comment:
How about we use 'posIndex+baselist.size()' >0 or <0? `-posIndex` maybe
having a number overflow exception.
##########
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:
When we use `posInt` as the new Array length, According to the code logic,
We can't insert this value to Array.
So I want to know the Spark's behavior about this. For example:
`SELECT array_insert(array(1, 2, 3, 4), 10, 5)` and `SELECT
array_insert(array(1, 2, 3, 4), -10, 5)`
##########
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. */
Review Comment:
Keep the format:
`Support the ARRAY_INSERT function.`
##########
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);
+
+ if (newArrayLength > MAX_ARRAY_LENGTH) {
+ throw new IndexOutOfBoundsException(
+ String.format(Locale.ROOT, "The new array length %s exceeds the
allowed limit.",
+ newArrayLength));
+ }
+
+ Object[] newArray = new Object[newArrayLength];
+
+ int posIndex = posInt - 1;
Review Comment:
Can we use `list.add(pos,element)` ?
--
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]