Skip to content

Commit 62b6feb

Browse files
xudong963askalt
authored andcommitted
Speed up optimize_projection (apache#15787)
* save * fmt
1 parent dacfed3 commit 62b6feb

File tree

1 file changed

+19
-5
lines changed
  • datafusion/optimizer/src/optimize_projections

1 file changed

+19
-5
lines changed

datafusion/optimizer/src/optimize_projections/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ use datafusion_common::{
3232
use datafusion_expr::expr::Alias;
3333
use datafusion_expr::Unnest;
3434
use datafusion_expr::{
35-
logical_plan::LogicalPlan, projection_schema, Aggregate, Distinct, Expr, Projection,
36-
TableScan, Window,
35+
logical_plan::LogicalPlan, Aggregate, Distinct, Expr, Projection, TableScan, Window,
3736
};
3837

3938
use crate::optimize_projections::required_indices::RequiredIndicies;
@@ -780,9 +779,24 @@ fn rewrite_projection_given_requirements(
780779
/// Projection is unnecessary, when
781780
/// - input schema of the projection, output schema of the projection are same, and
782781
/// - all projection expressions are either Column or Literal
783-
fn is_projection_unnecessary(input: &LogicalPlan, proj_exprs: &[Expr]) -> Result<bool> {
784-
Ok(&projection_schema(input, proj_exprs)? == input.schema()
785-
&& proj_exprs.iter().all(is_expr_trivial))
782+
pub fn is_projection_unnecessary(
783+
input: &LogicalPlan,
784+
proj_exprs: &[Expr],
785+
) -> Result<bool> {
786+
// First check if the number of expressions is equal to the number of fields in the input schema.
787+
if proj_exprs.len() != input.schema().fields().len() {
788+
return Ok(false);
789+
}
790+
Ok(input.schema().iter().zip(proj_exprs.iter()).all(
791+
|((field_relation, field_name), expr)| {
792+
// Check if the expression is a column and if it matches the field name
793+
if let Expr::Column(col) = expr {
794+
col.relation.as_ref() == field_relation && col.name.eq(field_name.name())
795+
} else {
796+
false
797+
}
798+
},
799+
))
786800
}
787801

788802
#[cfg(test)]

0 commit comments

Comments
 (0)