HyukjinKwon commented on a change in pull request #25728: 
[SPARK-29020][WIP][SQL] Improving array_sort behaviour
URL: https://github.com/apache/spark/pull/25728#discussion_r324487219
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala
 ##########
 @@ -285,6 +285,72 @@ case class ArrayTransform(
   override def prettyName: String = "transform"
 }
 
+
+case class ArraySorting(
+    argument: Expression,
+    function: Expression)
+  extends ArrayBasedSimpleHigherOrderFunction with CodegenFallback {
+
+
+  @transient lazy val elementType: DataType =
+    argument.dataType.asInstanceOf[ArrayType].elementType
+  override def dataType: ArrayType = ArrayType(elementType, function.nullable)
+
+  override def bind(f: (Expression, Seq[(DataType, Boolean)]) => 
LambdaFunction): ArraySorting = {
+    val ArrayType(elementType, containsNull) = argument.dataType
+    function match {
+      case LambdaFunction(_, arguments, _) if arguments.size == 2 =>
+        copy(function =
+          f(function, (elementType, containsNull) :: (elementType, 
containsNull) :: Nil))
+      case _ =>
+        copy(function = f(function, (elementType, containsNull) :: Nil))
+    }
+  }
+
+  @transient lazy val (firstParam, secondParam) = {
+    val LambdaFunction(_, (firstParam: NamedLambdaVariable) +: tail, _) = 
function
+    val secondParam = tail.head.asInstanceOf[NamedLambdaVariable]
+    (firstParam, secondParam)
+  }
+
+
+  override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any = {
+    val arr = argumentValue.asInstanceOf[ArrayData]
+    val f = functionForEval
+
+    @transient lazy val customComparator: Comparator[Any] = {
+      (o1: Any, o2: Any) => {
+        if (o1 == null && o2 == null) {
+          0
+        } else if (o1 == null) {
+          1
+        } else if (o2 == null) {
+          -1
+        } else {
+          firstParam.value.set(o2)
+          secondParam.value.set(o1)
+          f.eval(inputRow).asInstanceOf[Int]
+        }
+      }
+    }
+
+   sortEval(arr, customComparator)
+  }
+
+  def sortEval(array: Any, comparator: Comparator[Any]): Any = {
+    val data = array.asInstanceOf[ArrayData].toArray[AnyRef](elementType)
+    if (elementType!= NullType) {
+      java.util.Arrays.sort(data, comparator)
+    }
+    new GenericArrayData(data.asInstanceOf[Array[Any]])
+  }
+
+  override def prettyName: String = "array_new_sort"
 
 Review comment:
   Why is it a new name instead of just `arrow_sort`?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to