viirya commented on code in PR #54134:
URL: https://github.com/apache/spark/pull/54134#discussion_r2824754440
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala:
##########
@@ -1134,6 +1134,277 @@ class DataFrameAggregateSuite extends QueryTest
}
}
+ test("max_by and min_by with k") {
+ // Basic: string values, integer ordering
+ checkAnswer(
+ sql(
+ """
+ |SELECT max_by(x, y, 2), min_by(x, y, 2)
+ |FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y)
+ """.stripMargin),
+ Row(Seq("b", "c"), Seq("a", "c")) :: Nil
+ )
+
+ // DataFrame API
+ checkAnswer(
+ spark.sql("SELECT * FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS
tab(x, y)")
+ .agg(max_by(col("x"), col("y"), 2), min_by(col("x"), col("y"), 2)),
+ Row(Seq("b", "c"), Seq("a", "c")) :: Nil
+ )
+
+ // k larger than available rows
+ checkAnswer(
+ sql(
+ """
+ |SELECT max_by(x, y, 5), min_by(x, y, 5)
+ |FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y)
+ """.stripMargin),
+ Row(Seq("b", "c", "a"), Seq("a", "c", "b")) :: Nil
+ )
+
+ // k = 1
+ checkAnswer(
+ sql(
+ """
+ |SELECT max_by(x, y, 1), min_by(x, y, 1)
+ |FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y)
+ """.stripMargin),
+ Row(Seq("b"), Seq("a")) :: Nil
+ )
+
+ // NULL orderings are skipped
+ checkAnswer(
+ sql(
+ """
+ |SELECT max_by(x, y, 2), min_by(x, y, 2)
+ |FROM VALUES (('a', 10)), (('b', null)), (('c', 20)) AS tab(x, y)
+ """.stripMargin),
+ Row(Seq("c", "a"), Seq("a", "c")) :: Nil
+ )
+
+ // All NULL orderings yields empty array
+ checkAnswer(
+ sql(
+ """
+ |SELECT max_by(x, y, 2), min_by(x, y, 2)
+ |FROM VALUES (('a', null)), (('b', null)) AS tab(x, y)
+ """.stripMargin),
+ Row(Seq(), Seq()) :: Nil
+ )
Review Comment:
For the current two parameter MaxBy and MinBy, seems they also skip those
inputs with order expressions of null value. But one difference is that if all
inputs are null in order expression, the aggregated output of them are null.
However, the three parameter MaxBy and MinBy will produce empty array.
Does it match the behavior of other systems?
--
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]