adriangb commented on code in PR #25672:
URL: https://github.com/apache/datafusion/pull/25672#discussion_r4097872615


##########
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:
   It is intentional. At this point the `Limit` is already removed, so the plan 
changed even when `push_topk_through_join` returns `Transformed::no`. Passing 
that `no` through would report an unchanged plan. I made this explicit in the 
code (684da9c205) and added `limit_push_down_sort_reports_transformed`, which 
fails with the suggested change.



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


Review Comment:
   Yes, good catch. That branch has the same skipped visit, so the TopK 
pushdown waited one more pass there too. In 684da9c205 both `skip == 0` 
branches share one path that calls `push_topk_through_join`, and 
`topk_pushed_through_limit_over_sort_with_{same,smaller}_fetch_in_one_pass` 
cover it with `max_passes = 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