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


##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -583,21 +597,41 @@ fn try_split_leaf_projection(
         })
         .collect::<Result<Vec<_>>>()?;
 
-    // The child gained the leaf columns, so the repartition's properties have 
to
+    // The child's columns changed, so the repartition's properties have to
     // be recomputed rather than carried over.
     let new_repartition = Arc::clone(projection.input()).replace_children(
         vec![new_child],
         ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
     )?;
     // `try_new` would derive the output schema from the expressions alone and
     // drop the metadata the planner set, which `schema_check` then rejects.
-    Ok(Some(Arc::new(
-        ProjectionExec::try_new_with_schema_metadata(
+    let new_projection: Arc<dyn ExecutionPlan> =
+        Arc::new(ProjectionExec::try_new_with_schema_metadata(
             new_exprs,
             new_repartition,
             projection.schema().as_ref(),
-        )?,
-    )))
+        )?);
+
+    // An ordering of the output can depend on a column that no expression 
reads:
+    // `c = a + b` with input sorted by `c` sorts `a + b`. An ancestor may 
rely on
+    // it. `preserve_order` does not tell, because a repartition of a single 
input
+    // partition keeps the order without it.
+    let new_properties = new_projection.equivalence_properties();
+    for ordering in plan.equivalence_properties().oeq_class().iter() {
+        if !new_properties.ordering_satisfy(ordering.iter().cloned())? {
+            return Ok(None);

Review Comment:
   Is giving up entirely the right move here? Are there scenarios where soime 
projection pruning just not the columns that order depends on would be the 
right move?



##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -543,6 +548,9 @@ fn try_split_leaf_projection(
     let child = repartition.input();
     let child_schema = child.schema();
     let passthrough_indices = collect_outer_columns(projection.expr(), 
&leaves)?;
+    if leaves.is_empty() && passthrough_indices.len() == 
child_schema.fields().len() {

Review Comment:
   Same here, a comment explaining what the condition is / why we need it would 
be great.



##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -561,11 +569,17 @@ fn try_split_leaf_projection(
     }
     let lower_projection = ProjectionExec::try_new(lower_exprs, 
Arc::clone(child))?;
 
-    // Only commit the split if the child really absorbs the leaf projection.
-    // Otherwise this would just add a projection that helps nobody.
+    // A child that declines the lower projection would leave it in the plan,
+    // where it helps nobody.
     let Some(new_child) = 
child.try_swapping_with_projection(&lower_projection)? else {
         return Ok(None);
     };
+    let new_child = new_child
+        .transform_down(remove_unnecessary_projections)
+        .data()?;
+    if leaves.is_empty() && count_projections(&new_child)? > 
count_projections(child)? {

Review Comment:
   Could we add a comment explaining what this condition is suppossed to do?



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