JoshRosen commented on a change in pull request #24557: [SPARK-27653][SQL] Add 
max_by() SQL aggregate function
URL: https://github.com/apache/spark/pull/24557#discussion_r282141704
 
 

 ##########
 File path: 
sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala
 ##########
 @@ -782,4 +782,47 @@ class DataFrameAggregateSuite extends QueryTest with 
SharedSQLContext {
     val countAndDistinct = df.select(count("*"), countDistinct("*"))
     checkAnswer(countAndDistinct, Row(100000, 100))
   }
+
+  test("max_by") {
+    val yearOfMaxEarnings =
+      sql("SELECT course, max_by(year, earnings) FROM courseSales GROUP BY 
course")
+    checkAnswer(yearOfMaxEarnings, Row("dotNET", 2013) :: Row("Java", 2013) :: 
Nil)
+
+    checkAnswer(
+      sql("SELECT max_by(x, y) FROM VALUES (('a', 10)), (('b', 50)), (('c', 
20)) AS tab(x, y)"),
+      Row("b") :: Nil
+    )
+
+    checkAnswer(
+      sql("SELECT max_by(x, y) FROM VALUES (('a', 10)), (('b', null)), (('c', 
20)) AS tab(x, y)"),
+      Row("c") :: Nil
+    )
+
+    checkAnswer(
+      sql("SELECT max_by(x, y) FROM VALUES (('a', null)), (('b', null)), 
(('c', 20)) AS tab(x, y)"),
+      Row("c") :: Nil
+    )
+
+    checkAnswer(
+      sql("SELECT max_by(x, y) FROM VALUES (('a', 10)), (('b', 50)), (('c', 
null)) AS tab(x, y)"),
+      Row("b") :: Nil
+    )
+
+    checkAnswer(
+      sql("SELECT max_by(x, y) FROM VALUES (('a', null)), (('b', null)) AS 
tab(x, y)"),
 
 Review comment:
   This returns `null` because all values of the ordering column are `null`? 
That seems to match Presto behavior:
   
   ```
   SELECT max_by(x, y) FROM (
     VALUES
       ('a', null),
       ('b', null)
   ) AS tab (x, y)
   ```
   
   also returns `null` in Presto :thumbsup:

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to