Github user ueshin commented on a diff in the pull request:
https://github.com/apache/spark/pull/21040#discussion_r182701643
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
---
@@ -287,3 +287,101 @@ case class ArrayContains(left: Expression, right:
Expression)
override def prettyName: String = "array_contains"
}
+
+
+/**
+ * Slices an array according to the requested start index and length
+ */
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(a1, a2) - Subsets array x starting from index start (or
starting from the end if start is negative) with the specified length.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(array(1, 2, 3, 4), 2, 2);
+ [2,3]
+ > SELECT _FUNC_(array(1, 2, 3, 4), -2, 2);
+ [3,4]
+ """, since = "2.4.0")
+// scalastyle:on line.size.limit
+case class Slice(x: Expression, start: Expression, length: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes {
+
+ override def dataType: DataType = x.dataType
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType,
IntegerType, IntegerType)
+
+ override def nullable: Boolean = children.exists(_.nullable)
+
+ override def foldable: Boolean = children.forall(_.foldable)
+
+ override def children: Seq[Expression] = Seq(x, start, length)
+
+ override def nullSafeEval(xVal: Any, startVal: Any, lengthVal: Any): Any
= {
+ val startInt = startVal.asInstanceOf[Int]
+ val lengthInt = lengthVal.asInstanceOf[Int]
+ val arr = xVal.asInstanceOf[ArrayData]
+ val startIndex = if (startInt == 0) {
+ throw new RuntimeException(
+ s"Unexpected value for start in function $prettyName: SQL array
indices start at 1.")
+ } else if (startInt < 0) {
+ startInt + arr.numElements()
+ } else {
+ startInt - 1
+ }
+ if (lengthInt < 0) {
+ throw new RuntimeException(s"Unexpected value for length in function
$prettyName: " +
+ s"length must be greater than or equal to 0.")
--- End diff --
nit: unnecessary `s`.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]