waynexia commented on code in PR #4733:
URL: https://github.com/apache/arrow-datafusion/pull/4733#discussion_r1057415911


##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -209,16 +214,21 @@ impl OptimizerRule for CommonSubexprEliminate {
             }
             LogicalPlan::Sort(Sort { expr, input, fetch }) => {
                 let input_schema = Arc::clone(input.schema());
-                let arrays = to_arrays(expr, input_schema, &mut expr_set)?;
+                let arrays = to_arrays(expr, input_schema.clone(), &mut 
expr_set)?;
 
                 let (mut new_expr, new_input) =
                     self.rewrite_expr(&[expr], &[&arrays], input, &mut 
expr_set, config)?;
 
-                Ok(Some(LogicalPlan::Sort(Sort {
+                let sort = LogicalPlan::Sort(Sort {
                     expr: pop_expr(&mut new_expr)?,
                     input: Arc::new(new_input),
                     fetch: *fetch,
-                })))
+                });
+                if sort.schema() == &input_schema {
+                    Ok(Some(sort))
+                } else {
+                    Ok(Some(build_recover_project_plan(&input_schema, sort)))

Review Comment:
   Thanks for this! It's really a good practice 👍 



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

Reply via email to