vinodkc commented on code in PR #58017:
URL: https://github.com/apache/spark/pull/58017#discussion_r3818804671
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -2226,6 +2226,87 @@ case class Slice(x: Expression, start: Expression,
length: Expression)
copy(x = newFirst, start = newSecond, length = newThird)
}
+/**
+ * Removes the last `n` elements from the given array, per the ANSI SQL
`TRIM_ARRAY` function.
+ */
+@ExpressionDescription(
+ usage = """
+ _FUNC_(array, n) - Returns the given array with the last `n` elements
removed. Raises an error
+ if `n` is negative or greater than the number of elements in the
array.""",
+ arguments = """
+ Arguments:
+ * array - the array to trim.
+ * n - the number of elements to remove from the end of the array. Must
be between 0 and the
+ number of elements in the array (inclusive).
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4, 5), 2);
+ [1,2,3]
+ > SELECT _FUNC_(array('a', 'b', 'c'), 0);
+ ["a","b","c"]
+ > SELECT _FUNC_(array(1, 2, 3), 3);
+ []
+ """,
+ group = "array_funcs",
+ since = "4.4.0")
+case class TrimArray(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+ override def nullIntolerant: Boolean = true
+
+ override def prettyName: String = "trim_array"
+
+ override def dataType: DataType = left.dataType
+
+ private def resultArrayElementNullable =
dataType.asInstanceOf[ArrayType].containsNull
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType, IntegerType)
+
+ @transient private lazy val elementType: DataType =
+ left.dataType.asInstanceOf[ArrayType].elementType
+
+ override def nullSafeEval(arrayVal: Any, nVal: Any): Any = {
+ val arr = arrayVal.asInstanceOf[ArrayData]
+ val n = nVal.asInstanceOf[Int]
+ val numElements = arr.numElements()
+ if (n < 0 || n > numElements) {
+ throw
QueryExecutionErrors.invalidElementCountForTrimArrayError(prettyName,
numElements, n)
+ }
+ val data = arr.toSeq[AnyRef](elementType)
+ new GenericArrayData(data.slice(0, numElements - n))
Review Comment:
Done — replaced toSeq + slice with a direct Array[Any] build that copies the
retained prefix once.
Thanks.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]