alamb commented on code in PR #10356:
URL: https://github.com/apache/datafusion/pull/10356#discussion_r1589027335


##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -99,25 +102,75 @@ fn analyze_internal(
     // select t2.c2 from t1 where t1.c1 in (select t2.c1 from t2 where 
t2.c2=t1.c3)
     schema.merge(external_schema);
 
-    let mut expr_rewrite = TypeCoercionRewriter { schema: &schema };
-
-    let new_expr = plan
-        .expressions()
-        .into_iter()
-        .map(|expr| {
-            // ensure aggregate names don't change:
-            // https://github.com/apache/datafusion/issues/3555
-            rewrite_preserving_name(expr, &mut expr_rewrite)
-        })
-        .collect::<Result<Vec<_>>>()?;
-
-    plan.with_new_exprs(new_expr, new_inputs)
+    let mut expr_rewrite = TypeCoercionRewriter::new(&schema);
+
+    let name_preserver = NamePreserver::new(&plan);
+    // apply coercion rewrite all expressions in the plan indivdually
+    plan.map_expressions(|expr| {
+        let original_name = name_preserver.save(&expr)?;
+        expr.rewrite(&mut expr_rewrite)?
+            .map_data(|expr| original_name.restore(expr))
+    })?
+    // coerce join expressions specially
+    .map_data(|plan| expr_rewrite.coerce_joins(plan))?
+    // recompute the schema after the expressions have been rewritten as the 
types may have changed
+    .map_data(|plan| plan.recompute_schema())

Review Comment:
   You are correct (of course!) thank you for pointing it out.  Now that 
`analyze_internal` returns Transformed would work. However,  there is still 
code like this:
   
   ```rust
                   let new_plan =
                       analyze_internal(self.schema, 
unwrap_arc(subquery.subquery))?.data;
                   Ok(Transformed::yes(Expr::Exists(Exists {
                       subquery: Subquery {
                           subquery: Arc::new(new_plan),
                           outer_ref_columns: subquery.outer_ref_columns,
                       },
                       negated,
                   })))
               }
   ```
   
   Which discards the transformed information (and in this case always returns 
Transformed::true).
   
   
   In order to keep the PRs small and easier to review I would like to not 
change this PR (it is no worse than `main` in regards to recomputing schema) 
and I will make a follow on PR to avoid recomputing schema when unecessary
   
   



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to