wangyum commented on code in PR #40115:
URL: https://github.com/apache/spark/pull/40115#discussion_r1115112991


##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala:
##########
@@ -3592,6 +3592,34 @@ class DataFrameSuite extends QueryTest
     val df = Seq("0.5944910").toDF("a")
     checkAnswer(df.selectExpr("cast(a as decimal(7,7)) div 100"), Row(0))
   }
+
+  test("SPARK-42525: collapse two adjacent windows with the same 
partition/order in subquery") {
+    val df1 = spark.range(10).map(_ => (Random.nextInt(10), 
Random.nextInt(10)))
+      .selectExpr("_1 as a", "_2 as b")
+    df1.cache()
+    df1.createOrReplaceTempView("t1")
+    val df2 = sql(
+      """
+        |select a, b, c, row_number() over (partition by a order by b) as d 
from
+        |( select a, b, rank() over (partition by a order by b) as c from t1) 
t2
+        |""".stripMargin
+    )
+    val df3 = sql(
+      """
+        |select a, b,
+        |rank() over (partition by a order by b) as c,
+        |row_number() over (partition by a order by b) as d
+        |from t1
+        |""".stripMargin
+    )
+    val captured = new ByteArrayOutputStream()
+    Console.withOut(captured) {
+      df2.explain()
+    }
+    checkAnswer(df2, df3)
+    val output = captured.toString
+    assert(output.split("\\+- Window").size == 2)

Review Comment:
   Move this test to `DataFrameWindowFunctionsSuite`
   ```scala
   withTempView("t1") {
     Seq((1, 1), (2, 2)).toDF("a", "b").createOrReplaceTempView("t1")
     val df = sql(
       """
         |SELECT a, b, rk, row_number() OVER (PARTITION BY a ORDER BY b) AS rn
         |FROM   (SELECT a, b, rank() OVER (PARTITION BY a ORDER BY b) AS rk
         |        FROM t1) t2
         |""".stripMargin)
   
     val windows = df.queryExecution.optimizedPlan.collect { case w: 
LogicalWindow => w }
     assert(windows.size === 1)
   }
   ```



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

Reply via email to