alamb commented on a change in pull request #1375:
URL: https://github.com/apache/arrow-datafusion/pull/1375#discussion_r759205639



##########
File path: datafusion/src/optimizer/simplify_expressions.rs
##########
@@ -284,9 +274,62 @@ impl OptimizerRule for SimplifyExpressions {
     fn optimize(
         &self,
         plan: &LogicalPlan,
-        _execution_props: &ExecutionProps,
+        execution_props: &ExecutionProps,
     ) -> Result<LogicalPlan> {
-        optimize(plan)
+        // We need to pass down the all schemas within the plan tree to 
`optimize_expr` in order to
+        // to evaluate expression types. For example, a projection plan's 
schema will only include
+        // projected columns. With just the projected schema, it's not 
possible to infer types for
+        // expressions that references non-projected columns within the same 
project plan or its
+        // children plans.
+        let mut simplifier =
+            super::simplify_expressions::Simplifier::new(plan.all_schemas());
+
+        let mut const_evaluator =
+            super::simplify_expressions::ConstEvaluator::new(execution_props);
+
+        let new_inputs = plan
+            .inputs()
+            .iter()
+            .map(|input| self.optimize(input, execution_props))
+            .collect::<Result<Vec<_>>>()?;
+
+        let expr = plan
+            .expressions()
+            .into_iter()
+            .map(|e| {
+                // We need to keep original expression name, if any.
+                // Constant folding should not change expression name.
+                let name = &e.name(plan.schema());
+
+                // TODO combine simplify into Simplifier
+                let e = simplify(&e);
+
+                // TODO iterate until no changes are made
+                // during rewrite (evaluating constants can
+                // enable new simplifications and
+                // simplifications can enable new constant
+                // evaluation)
+                let new_e = e
+                    // fold constants and then simplify
+                    .rewrite(&mut const_evaluator)?
+                    .rewrite(&mut simplifier)?;
+
+                let new_name = &new_e.name(plan.schema());
+
+                // TODO simplify this logic
+                if let (Ok(expr_name), Ok(new_expr_name)) = (name, new_name) {

Review comment:
       Note that now the  fix for alias names (introduced by @viirya in 
https://github.com/apache/arrow-datafusion/pull/1319) is applied to the result 
of `simplify` as well -- previously it was not 🎉 
   




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