ueshin 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_r345375638
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala
 ##########
 @@ -285,6 +286,113 @@ case class ArrayTransform(
   override def prettyName: String = "transform"
 }
 
+/**
+ * Sorts elements in an array using a comparator function.
+ */
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = """_FUNC_(expr, func) - Sorts the input array in ascending order. 
The elements of the
+    input array must be orderable. Null elements will be placed at the end of 
the returned
+    array. Since 3.0.0 also sorts and returns the array based on the given
+    comparator function. The comparator will take two nullable arguments
+    representing two nullable elements of the array.
+    It returns -1, 0, or 1 as the first nullable element is less than, equal 
to, or greater
+    than the second nullable element. If the comparator function returns other
+    values (including NULL), the query will fail and raise an error.
+    """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_(array(5, 6, 1), (left, right) -> If(And(IsNull(left), 
IsNull(right)), 0, If(IsNull(left), 1, If(IsNull(right), -1, If(left < right, 
-1, If(left > right, 1, 0))))));
+       [1,5,6]
+      > SELECT _FUNC_(array('bc', 'ab', 'dc'), (left, right) -> 
If(And(IsNull(left), IsNull(right)), 0, If(IsNull(left), 1, If(IsNull(right), 
-1, If(left < right, -1, If(left > right, 1, 0))))));
+       ["dc","bc","ab"]
+      > SELECT _FUNC_(array('b', 'd', null, 'c', 'a'));
+       ["a","b","c","d",null]
 
 Review comment:
   How about something like:
   
   ```diff
        Examples:
   -      > SELECT _FUNC_(array(5, 6, 1), (left, right) -> If(And(IsNull(left), 
IsNull(right)), 0, If(IsNull(left), 1, If(IsNull(right), -1, If(left < right, 
-1, If(left > right, 1, 0))))));
   -       [1,5,6]
   -      > SELECT _FUNC_(array('bc', 'ab', 'dc'), (left, right) -> 
If(And(IsNull(left), IsNull(right)), 0, If(IsNull(left), 1, If(IsNull(right), 
-1, If(left < right, -1, If(left > right, 1, 0))))));
   -       ["dc","bc","ab"]
          > SELECT _FUNC_(array('b', 'd', null, 'c', 'a'));
           ["a","b","c","d",null]
   +      > SELECT _FUNC_(array(5, 6, 1), (left, right) -> case when left < 
right then -1 when left > right then 1 else 0 end);
   +       [1,5,6]
   +      > SELECT _FUNC_(array('bc', 'ab', null, 'dc'), (left, right) -> case 
when left is null and right is null then 0 when left is null then -1 when right 
is null then 1 when left < right then 1 when left > right then -1 else 0 end);
   +       [null,"dc","bc","ab"]
   ```

----------------------------------------------------------------
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