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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/InferWindowGroupLimitSuite.scala:
##########
@@ -351,6 +352,199 @@ class InferWindowGroupLimitSuite extends PlanTest {
     }
   }
 
+  test("Eliminate the Window/Filter once WindowGroupLimit already truncates to 
one row") {
+    for (cond <- Seq($"rn" === 1, $"rn" <= 1, $"rn" < 2,
+        Literal(1) === $"rn", Literal(1) >= $"rn", Literal(2) > $"rn");
+      order <- Seq(c.desc, c.asc)) {
+      val originalQuery =
+        testRelation
+          .select(a, b, c,
+            windowExpr(RowNumber(),
+              windowSpec(a :: Nil, order :: Nil, windowFrame)).as("rn"))
+          .where(cond)
+
+      val correctAnswer =
+        testRelation
+          .windowGroupLimit(a :: Nil, order :: Nil, RowNumber(), 1)
+          .select(a, b, c, Literal(1).as("rn"))
+
+      comparePlans(
+        Optimize.execute(originalQuery.analyze),
+        WithoutOptimize.execute(correctAnswer.analyze))
+    }
+  }
+
+  test("Eliminate the Window/Filter with a multi-column partition/order spec") 
{
+    val originalQuery =
+      testRelation
+        .select(a, b, c,
+          windowExpr(RowNumber(),
+            windowSpec(Seq(a, b), Seq(c.desc, b.asc), windowFrame)).as("rn"))
+        .where($"rn" === 1)
+
+    val correctAnswer =
+      testRelation
+        .windowGroupLimit(Seq(a, b), Seq(c.desc, b.asc), RowNumber(), 1)
+        .select(a, b, c, Literal(1).as("rn"))
+
+    comparePlans(
+      Optimize.execute(originalQuery.analyze),
+      WithoutOptimize.execute(correctAnswer.analyze))
+  }
+
+  test("Eliminate the Window/Filter also fires with an empty partitionSpec 
when the " +
+      "top-k sort fallback is disabled") {
+    // With the default config, limit == 1 and an empty partitionSpec take the 
Limit+Sort
+    // fallback path instead (see "Insert limit node for top-k computation" 
above) and never
+    // reach this rule's WindowGroupLimit-based elimination. Disabling the 
fallback here forces
+    // the WindowGroupLimit path so the elimination branch is exercised with 
no PARTITION BY.
+    withSQLConf(SQLConf.TOP_K_SORT_FALLBACK_THRESHOLD.key -> "0") {
+      val originalQuery =
+        testRelation
+          .select(a, b, c,
+            windowExpr(RowNumber(),
+              windowSpec(Nil, c.desc :: Nil, windowFrame)).as("rn"))
+          .where($"rn" === 1)
+
+      val correctAnswer =
+        testRelation
+          .windowGroupLimit(Nil, c.desc :: Nil, RowNumber(), 1)
+          .select(a, b, c, Literal(1).as("rn"))
+
+      comparePlans(
+        Optimize.execute(originalQuery.analyze),
+        WithoutOptimize.execute(correctAnswer.analyze))
+    }
+  }
+
+  test("Eliminate the Window/Filter keeps rn's exprId for downstream 
references") {
+    val originalQuery =
+      testRelation
+        .select(a, b, c,
+          windowExpr(RowNumber(),
+            windowSpec(a :: Nil, c.desc :: Nil, windowFrame)).as("rn"))
+        .where($"rn" === 1)
+        .select(a, b, c, $"rn")
+
+    val result = Optimize.execute(originalQuery.analyze)
+    assert(result.output.map(_.exprId) == 
originalQuery.analyze.output.map(_.exprId))
+    assert(!result.exists(_.isInstanceOf[Window]) && 
!result.exists(_.isInstanceOf[Filter]))
+  }
+
+  test("Eliminate the Window/Filter preserves rn's qualifier and metadata") {
+    val metadata = new MetadataBuilder().putString("tag", "pii").build()
+    val rnAlias = Alias(
+      windowExpr(RowNumber(), windowSpec(a :: Nil, c.desc :: Nil, 
windowFrame)), "rn")(
+      qualifier = Seq("t"), explicitMetadata = Some(metadata))
+
+    val originalQuery =
+      testRelation
+        .select(a, b, c, rnAlias)
+        .where($"rn" === 1)
+        .analyze
+
+    val rnAttr = Optimize.execute(originalQuery).output.last

Review Comment:
   Fixed -- the test now asserts `!optimized.exists(_.isInstanceOf[Window]) && 
!optimized.exists(_.isInstanceOf[Filter])` before checking the qualifier and 
metadata, exactly as suggested.



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