navinvishy commented on code in PR #38947:
URL: https://github.com/apache/spark/pull/38947#discussion_r1042620296
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -1399,6 +1399,119 @@ case class ArrayContains(left: Expression, right:
Expression)
copy(left = newLeft, right = newRight)
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage =
+ "_FUNC_(array, value) - Returns an array containing value as well as all
elements from array. The new element is positioned at the beginning of the
array.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3), 4);
+ [4, 1, 2, 3]
+ """,
+ group = "array_funcs",
+ since = "3.4.0")
+case class ArrayPrepend(left: Expression, right: Expression)
+ extends BinaryExpression
+ with ImplicitCastInputTypes
+ with NullIntolerant
+ with QueryErrorsBase {
+
+ override def nullSafeEval(arr: Any, value: Any): Any = {
+ val numberOfElements = arr.asInstanceOf[ArrayData].numElements()
+ if (numberOfElements + 1 > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
+ throw
QueryExecutionErrors.concatArraysWithElementsExceedLimitError(numberOfElements)
+ }
+ val newArray = new Array[Any](numberOfElements + 1)
+ newArray(0) = value
+ var pos = 1
+ arr
+ .asInstanceOf[ArrayData]
+ .foreach(
+ right.dataType,
+ (i, v) => {
+ newArray(pos) = v
+ pos += 1
+ })
+ new GenericArrayData(newArray)
+ }
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ nullSafeCodeGen(
+ ctx,
+ ev,
+ (arr, value) => {
+ val newArraySize = ctx.freshName("newArraySize")
+ val newArray = ctx.freshName("newArray")
+ val i = ctx.freshName("i")
+ val pos = ctx.freshName("pos")
+ val allocation = CodeGenerator.createArrayData(
+ newArray,
+ right.dataType,
+ newArraySize,
+ s" $prettyName failed.")
+ val assignment =
+ CodeGenerator.createArrayAssignment(newArray, right.dataType, arr,
pos, i, false)
+ val newElemAssignment =
+ CodeGenerator.setArrayElement(newArray, right.dataType, pos, value)
+ s"""
+ |int $pos = 0;
+ |int $newArraySize = $arr.numElements() + 1;
+ |$allocation
+ |$newElemAssignment
+ |$pos = $pos + 1;
+ |for (int $i = 0; $i < $arr.numElements(); $i ++) {
+ | $assignment
+ | $pos = $pos + 1;
+ |}
+ |${ev.value} = $newArray;
+ |""".stripMargin
+ })
+ }
+
+ override def prettyName: String = "array_prepend"
+ override protected def withNewChildrenInternal(
+ newLeft: Expression,
+ newRight: Expression):
ArrayPrepend =
+ copy(left = newLeft, right = newRight)
+ override def dataType: DataType = left.dataType
+ override def checkInputDataTypes(): TypeCheckResult = {
Review Comment:
I did what `ArrayContains` does. Maybe we should consolidate this, since it
makes sense for many of these to do the same thing? eg. `ArrayContains`,
`ArrayRemove`, `ArrayPrepend`, `ArrayAppend` etc.
--
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]