cool9850311 commented on code in PR #27532:
URL: https://github.com/apache/datafusion/pull/27532#discussion_r4675914099


##########
datafusion/physical-optimizer/src/limit_pushdown_past_window.rs:
##########
@@ -190,21 +234,105 @@ fn get_limit(node: &Arc<dyn ExecutionPlan>, ctx: &mut 
TraverseState) -> bool {
     // In distributed execution, GlobalLimitExec becomes LocalLimitExec
     // per partition. Handle it the same way (LocalLimitExec has no skip).
     if let Some(limit) = RowBound::of_limit_node(node) {
-        ctx.reset_limit(limit.input_rows());
+        ctx.reset_limit(limit.input_rows(), None);
         return true;
     }
-    if let Some(limit) = node.downcast_ref::<SortPreservingMergeExec>() {
-        ctx.reset_limit(limit.fetch());
+    if let Some(merge) = node.downcast_ref::<SortPreservingMergeExec>() {
+        ctx.reset_limit(merge.fetch(), Some(merge_ordering(merge)));
         return true;
     }
     false
 }
 
+/// Whether every window partition `node` reads is whole within one of its
+/// output partitions, so that a row's own earlier rows share its partition.
+fn keeps_partitions_whole(node: &Arc<dyn ExecutionPlan>, rank_needed: usize) 
-> bool {
+    let Partitioning::Hash(hash_exprs, _) = node.output_partitioning() else {
+        return false;
+    };
+    node.downcast_ref::<SortExec>().is_some_and(|sort| {
+        hash_exprs.len() <= rank_needed
+            && hash_exprs
+                .iter()
+                .zip(sort.expr().iter())
+                .all(|(hashed, sorted)| hashed.eq(&sorted.expr))
+    })
+}
+
+/// The expressions of `merge`'s ordering that order its rows: one that is
+/// constant in its input orders nothing.
+fn merge_ordering(merge: &SortPreservingMergeExec) -> Vec<Arc<dyn 
PhysicalExpr>> {
+    let input = merge.input().equivalence_properties();
+    merge
+        .expr()
+        .iter()
+        .filter(|sort_expr| input.is_expr_constant(&sort_expr.expr).is_none())
+        .map(|sort_expr| Arc::clone(&sort_expr.expr))
+        .collect()
+}
+
+/// Whether the ordering in hand sorts `node`'s rows into the groups its 
windows
+/// read: every `PARTITION BY` expression has to be one of the ordering's own. 
A
+/// window with no `PARTITION BY` reads one group that no ordering separates.
+fn separates_partitions(node: &Arc<dyn ExecutionPlan>, ctx: &TraverseState) -> 
bool {
+    let (Some(exprs), Some(window)) = (
+        ctx.merge_exprs.as_ref(),
+        node.downcast_ref::<BoundedWindowAggExec>(),
+    ) else {
+        return false;
+    };
+    !window.window_expr().is_empty()
+        && window.window_expr().iter().all(|window_expr| {
+            !window_expr.partition_by().is_empty()
+                && window_expr
+                    .partition_by()
+                    .iter()
+                    .all(|key| exprs.iter().any(|expr| expr.eq(key)))
+        })
+}
+
+/// The narrowest `PARTITION BY` of `node`'s window expressions, or 
`usize::MAX`
+/// for anything else -- [`separates_partitions`] blocks those on its own.
+fn partition_by_width(node: &Arc<dyn ExecutionPlan>) -> usize {
+    let Some(window) = node.downcast_ref::<BoundedWindowAggExec>() else {
+        return usize::MAX;
+    };
+    window
+        .window_expr()
+        .iter()
+        .map(|expr| expr.partition_by().len())
+        .min()
+        .unwrap_or(usize::MAX)
+}
+
+/// Carry the merge's ordering from `node`'s output into its input's expression
+/// space: a projection moves and renames columns, so the ordering is rewritten
+/// through it, and a node that changes the schema any other way ends the 
carry.
+fn carry_down(node: &Arc<dyn ExecutionPlan>, ctx: &mut TraverseState) {
+    let Some(exprs) = ctx.merge_exprs.take() else {
+        return;
+    };
+    let Some(child) = node.children().into_iter().next() else {
+        return;
+    };
+    ctx.merge_exprs = if let Some(projection) = 
node.downcast_ref::<ProjectionExec>() {
+        exprs
+            .iter()
+            .map(|expr| projection.projection_expr().unproject_expr(expr).ok())
+            .collect()
+    } else if node.schema() == child.schema() {
+        Some(exprs)
+    } else {
+        None
+    };

Review Comment:
   Thanks for catching that, done fixing.



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