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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InferWindowGroupLimit.scala:
##########
@@ -112,20 +152,33 @@ object InferWindowGroupLimit extends Rule[LogicalPlan] 
with PredicateHelper {
           }
           // Pick a rank-like function with the smallest limit
           selectedLimits.minBy(_._1) match {
-            case (limit, rankLikeFunction) if limit <= 
conf.windowGroupLimitThreshold &&
+            case (limit, rankLikeFunction, alias) if limit <= 
conf.windowGroupLimitThreshold &&
               child.maxRows.forall(_ > limit) =>
               if (limit > 0) {
-                val newFilterChild = if 
(rankLikeFunction.isInstanceOf[RowNumber] &&
+                if (rankLikeFunction.isInstanceOf[RowNumber] &&
                   partitionSpec.isEmpty && limit < 
conf.topKSortFallbackThreshold) {
                   // Top n (Limit + Sort) have better performance than 
WindowGroupLimit if the
                   // window function is RowNumber and Window partitionSpec is 
empty.
-                  Limit(Literal(limit), window)
+                  filter.withNewChildren(Seq(Limit(Literal(limit), window)))
                 } else {
                   val windowGroupLimit =
                     WindowGroupLimit(partitionSpec, orderSpec, 
rankLikeFunction, limit, child)
-                  window.withNewChildren(Seq(windowGroupLimit))
+                  if (limit == 1 && windowExpressions.length == 1 && 
window.hint.isEmpty &&
+                    rankLikeFunction.isInstanceOf[RowNumber] &&
+                    isTop1(condition, alias.toAttribute)) {

Review Comment:
   Fixed -- added `partitionSpec.forall(e => 
UnsafeRowUtils.isBinaryStable(e.dataType))` to the elimination guard, the same 
check `WindowExec` uses to decide its own grouping behavior.



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