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


##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -507,6 +513,66 @@ fn leaf_column_name(index: usize) -> String {
     format!("{LEAF_COLUMN_PREFIX}_{index}")
 }
 
+/// Calls [`try_split_leaf_projection`] on each node of `plan`, from the root 
down.
+///
+/// `order_matters` is true if something above `plan` can rely on its ordering.
+#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
+fn split_projections(
+    plan: Arc<dyn ExecutionPlan>,
+    order_matters: bool,
+) -> Result<Arc<dyn ExecutionPlan>> {
+    let plan = try_split_leaf_projection(&plan, 
order_matters)?.unwrap_or(plan);
+
+    let required = plan.required_input_ordering();
+    let only_declared = relies_only_on_declared_ordering(&plan);
+    let mut changed = false;
+    let mut children = vec![];
+    for (child, required) in plan.children().into_iter().zip(required) {
+        let order_matters = order_matters || required.is_some() || 
!only_declared;
+        // A sort replaces the ordering of its input, unless the input is 
sorted.
+        let order_matters = match plan.downcast_ref::<SortExec>() {
+            Some(sort) => {
+                order_matters
+                    && child
+                        .equivalence_properties()
+                        .ordering_satisfy(sort.expr().clone())?
+            }
+            None => order_matters,
+        };
+        let new_child = split_projections(Arc::clone(child), order_matters)?;
+        changed |= !Arc::ptr_eq(child, &new_child);
+        children.push(new_child);
+    }
+    if !changed {
+        return Ok(plan);
+    }
+    plan.replace_children(
+        children,
+        ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
+    )
+}
+
+/// Returns true for operators known to need no ordering of their input beyond
+/// `required_input_ordering`.
+///
+/// Any other operator can need one that it does not declare. `PartialSortExec`
+/// does, and `AggregateExec` chooses its mode from the ordering of its input.
+/// Nothing detects the wrong rows that a lost ordering then gives.
+fn relies_only_on_declared_ordering(plan: &Arc<dyn ExecutionPlan>) -> bool {

Review Comment:
   Great find Andrew!



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