cloud-fan commented on code in PR #38799:
URL: https://github.com/apache/spark/pull/38799#discussion_r1100255241
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowFunctionsSuite.scala:
##########
@@ -1265,4 +1265,168 @@ class DataFrameWindowFunctionsSuite extends QueryTest
)
)
}
+
+ test("SPARK-37099: Insert window group limit node for top-k computation") {
+
+ val nullStr: String = null
+ val df = Seq(
+ ("a", 0, "c", 1.0),
+ ("a", 1, "x", 2.0),
+ ("a", 2, "y", 3.0),
+ ("a", 3, "z", -1.0),
+ ("a", 4, "", 2.0),
+ ("a", 4, "", 2.0),
+ ("b", 1, "h", Double.NaN),
+ ("b", 1, "n", Double.PositiveInfinity),
+ ("c", 1, "z", -2.0),
+ ("c", 1, "a", -4.0),
+ ("c", 2, nullStr, 5.0)).toDF("key", "value", "order", "value2")
+
+ val window = Window.partitionBy($"key").orderBy($"order".asc_nulls_first)
+ val window2 = Window.partitionBy($"key").orderBy($"order".desc_nulls_first)
+
+ Seq(-1, 100).foreach { threshold =>
+ withSQLConf(SQLConf.WINDOW_GROUP_LIMIT_THRESHOLD.key ->
threshold.toString) {
+ Seq($"rn" === 0, $"rn" < 1, $"rn" <= 0).foreach { condition =>
+ checkAnswer(df.withColumn("rn",
row_number().over(window)).where(condition),
+ Seq.empty[Row]
+ )
+ }
+
+ Seq($"rn" === 1, $"rn" < 2, $"rn" <= 1).foreach { condition =>
+ checkAnswer(df.withColumn("rn",
row_number().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 1),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn",
rank().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 1),
+ Row("a", 4, "", 2.0, 1),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn",
dense_rank().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 1),
+ Row("a", 4, "", 2.0, 1),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+ }
+
+ Seq($"rn" < 3, $"rn" <= 2).foreach { condition =>
+ checkAnswer(df.withColumn("rn",
row_number().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 1),
+ Row("a", 4, "", 2.0, 2),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("b", 1, "n", Double.PositiveInfinity, 2),
+ Row("c", 1, "a", -4.0, 2),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn",
rank().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 1),
+ Row("a", 4, "", 2.0, 1),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("b", 1, "n", Double.PositiveInfinity, 2),
+ Row("c", 1, "a", -4.0, 2),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn",
dense_rank().over(window)).where(condition),
+ Seq(
+ Row("a", 0, "c", 1.0, 2),
+ Row("a", 4, "", 2.0, 1),
+ Row("a", 4, "", 2.0, 1),
+ Row("b", 1, "h", Double.NaN, 1),
+ Row("b", 1, "n", Double.PositiveInfinity, 2),
+ Row("c", 1, "a", -4.0, 2),
+ Row("c", 2, null, 5.0, 1)
+ )
+ )
+ }
+
+ val condition = $"rn" === 2 && $"value2" > 0.5
+ checkAnswer(df.withColumn("rn",
row_number().over(window)).where(condition),
+ Seq(
+ Row("a", 4, "", 2.0, 2),
+ Row("b", 1, "n", Double.PositiveInfinity, 2)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn", rank().over(window)).where(condition),
+ Seq(
+ Row("b", 1, "n", Double.PositiveInfinity, 2)
+ )
+ )
+
+ checkAnswer(df.withColumn("rn",
dense_rank().over(window)).where(condition),
+ Seq(
+ Row("a", 0, "c", 1.0, 2),
+ Row("b", 1, "n", Double.PositiveInfinity, 2)
+ )
+ )
+
+ val multipleRowNumbers = df
+ .withColumn("rn", row_number().over(window))
+ .withColumn("rn2", row_number().over(window))
+ .where('rn < 2 && 'rn2 < 3)
+ checkAnswer(multipleRowNumbers,
+ Seq(
+ Row("a", 4, "", 2.0, 1, 1),
+ Row("b", 1, "h", Double.NaN, 1, 1),
+ Row("c", 2, null, 5.0, 1, 1)
+ )
+ )
+
+ val multipleRanks = df
+ .withColumn("rn", rank().over(window))
+ .withColumn("rn2", rank().over(window))
+ .where('rn < 2 && 'rn2 < 3)
+ checkAnswer(multipleRanks,
+ Seq(
+ Row("a", 4, "", 2.0, 1, 1),
+ Row("a", 4, "", 2.0, 1, 1),
+ Row("b", 1, "h", Double.NaN, 1, 1),
+ Row("c", 2, null, 5.0, 1, 1)
+ )
+ )
+
+ val multipleDenseRanks = df
+ .withColumn("rn", dense_rank().over(window))
+ .withColumn("rn2", dense_rank().over(window))
+ .where('rn < 2 && 'rn2 < 3)
+ checkAnswer(multipleDenseRanks,
+ Seq(
+ Row("a", 4, "", 2.0, 1, 1),
+ Row("a", 4, "", 2.0, 1, 1),
+ Row("b", 1, "h", Double.NaN, 1, 1),
+ Row("c", 2, null, 5.0, 1, 1)
+ )
+ )
+
+ val multipleWindowsOne = df
+ .withColumn("rn2", row_number().over(window2))
+ .withColumn("rn", row_number().over(window))
Review Comment:
How does this work? If there are two window specs, then the query has two
Window node?
--
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]