alamb commented on code in PR #25408:
URL: https://github.com/apache/datafusion/pull/25408#discussion_r4233556506


##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -536,6 +536,22 @@ fn pushdown_requirement_to_children(
             )
             .then(|| Ok(vec![Some(parent_required)]))
             .transpose()
+    } else if plan.is::<GlobalLimitExec>() || plan.is::<LocalLimitExec>() {
+        // Sorting below a limit changes which rows it keeps. Only refine a
+        // SortExec directly below it, which changes how ties are broken.
+        // Otherwise, sort the retained rows and let the input stop early.
+        let refines_sort_below = plan.children()[0].is::<SortExec>()
+            && plan.output_ordering().is_some_and(|ordering| {
+                plan.equivalence_properties().requirements_compatible(
+                    parent_required.first().clone(),
+                    ordering.clone().into(),
+                )
+            });
+        if refines_sort_below {
+            Ok(Some(vec![Some(parent_required)]))
+        } else {
+            Ok(None)
+        }
     } else if plan.fetch().is_some()

Review Comment:
   - filed https://github.com/apache/datafusion/issues/26162



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