martin-g commented on code in PR #25672:
URL: https://github.com/apache/datafusion/pull/25672#discussion_r4091787497


##########
datafusion/optimizer/src/push_down_limit.rs:
##########
@@ -171,8 +171,16 @@ fn rewrite_limit(mut limit: Limit) -> 
Result<Transformed<LogicalPlan>> {
                 }
             } else {
                 sort.fetch = new_fetch;
-                limit.input = Arc::new(LogicalPlan::Sort(sort));
-                Ok(Transformed::yes(LogicalPlan::Limit(limit)))
+                if skip > 0 {
+                    limit.input = Arc::new(LogicalPlan::Sort(sort));
+                    Ok(Transformed::yes(LogicalPlan::Limit(limit)))
+                } else {
+                    // With `skip = 0` the Sort's fetch already caps the 
output,
+                    // so drop the Limit now instead of on the next pass. The
+                    // Sort then replaces the Limit as the visited node and its
+                    // own visit is skipped, so apply the TopK pushdown here.
+                    Ok(Transformed::yes(push_topk_through_join(sort)?.data))

Review Comment:
   ```suggestion
                       push_topk_through_join(sort)
   ```
   ?
   `push_topk_through_join()` may return `Transformed::no()`



##########
datafusion/optimizer/src/push_down_limit.rs:
##########


Review Comment:
   Would that benefit of using `push_topk_through_join(sort)` too ?!



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