zhuqi-lucas commented on code in PR #23288:
URL: https://github.com/apache/datafusion/pull/23288#discussion_r3520720530


##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -1042,6 +1055,43 @@ enum RequirementsCompatibility {
     NonCompatible,
 }
 
+/// Attempts to push parent ordering requirements through a [`ProjectionExec`].
+///
+/// This is safe when every required sort expression refers to a projected 
output
+/// column that is backed by a simple input column. In that case, the 
requirement
+/// can be remapped from the projection output schema to the projection input
+/// schema while preserving the original sort options.
+///
+/// For example, a parent requirement on `a@2` over:
+///
+/// ```text
+/// ProjectionExec: expr=[c@2 as c, b@1 as b, a@0 as a]
+/// ```
+///
+/// is remapped to a child requirement on `a@0`.
+///
+/// The implementation is intentionally conservative: computed projection
+/// expressions and non-column sort expressions are not pushed down. Returning
+/// `Ok(None)` leaves sorting above the projection, preserving correctness.
+fn handle_projection_pushdown(
+    projection_exec: &ProjectionExec,
+    parent_required: OrderingRequirements,
+) -> Result<Option<Vec<Option<OrderingRequirements>>>> {
+    let projected_exprs = projection_exec.expr();
+
+    // Only push sorting through pure column projections. Source-dependent
+    // expressions must stay close enough to the scan to be rewritten
+    // by the source and cannot be evaluated by [`ProjectionExec`].
+    if projected_exprs.iter().any(|expr| !expr.expr.is::<Column>()) {

Review Comment:
   It makes sense, thanks!



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