jayzhan211 commented on code in PR #15787: URL: https://github.com/apache/datafusion/pull/15787#discussion_r2052256953
########## datafusion/optimizer/src/optimize_projections/mod.rs: ########## @@ -785,13 +785,24 @@ fn rewrite_projection_given_requirements( /// Projection is unnecessary, when /// - input schema of the projection, output schema of the projection are same, and /// - all projection expressions are either Column or Literal -fn is_projection_unnecessary(input: &LogicalPlan, proj_exprs: &[Expr]) -> Result<bool> { - // First check if all expressions are trivial (cheaper operation than `projection_schema`) - if !proj_exprs.iter().all(is_expr_trivial) { +pub fn is_projection_unnecessary( + input: &LogicalPlan, + proj_exprs: &[Expr], +) -> Result<bool> { + // First check if the number of expressions is equal to the number of fields in the input schema. + if proj_exprs.len() != input.schema().fields().len() { return Ok(false); } - let proj_schema = projection_schema(input, proj_exprs)?; - Ok(&proj_schema == input.schema()) + Ok(input.schema().iter().zip(proj_exprs.iter()).all( + |((field_relation, field_name), expr)| { + // Check if the expression is a column and if it matches the field name + if let Expr::Column(col) = expr { Review Comment: Does this works with arbitrary expression as well? For example, could `proj_expr` be `BinaryExpr` or `Alias`? -- 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