Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions datafusion/src/optimizer/projection_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,17 @@ fn optimize_plan(
true,
execution_props,
)?;
if new_fields.is_empty() {

let new_required_columns_optimized = new_input
.schema()
.fields()
.iter()
.map(|f| f.qualified_column())
.collect::<HashSet<Column>>();

if new_fields.is_empty()
|| (has_projection && &new_required_columns_optimized == required_columns)
{
// no need for an expression at all
Ok(new_input)
} else {
Expand Down Expand Up @@ -496,6 +506,60 @@ mod tests {
Ok(())
}

#[test]
fn redundunt_project() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn redundunt_project() -> Result<()> {
fn redundant_project() -> Result<()> {

let table_scan = test_table_scan()?;

let plan = LogicalPlanBuilder::from(table_scan)
.project(vec![col("a"), col("b"), col("c")])?
.project(vec![col("a"), col("c"), col("b")])?
.build()?;
let expected = "Projection: #test.a, #test.c, #test.b\
\n TableScan: test projection=Some([0, 1, 2])";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}

#[test]
fn reorder_projection() -> Result<()> {
let table_scan = test_table_scan()?;

let plan = LogicalPlanBuilder::from(table_scan)
.project(vec![col("c"), col("b"), col("a")])?
.build()?;
let expected = "Projection: #test.c, #test.b, #test.a\
\n TableScan: test projection=Some([0, 1, 2])";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}

#[test]
fn noncontiguous_redundunt_projection() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn noncontiguous_redundunt_projection() -> Result<()> {
fn noncontiguous_redundant_projection() -> Result<()> {

let table_scan = test_table_scan()?;

let plan = LogicalPlanBuilder::from(table_scan)
.project(vec![col("c"), col("b"), col("a")])?
.filter(col("c").gt(lit(1)))?
.project(vec![col("c"), col("a"), col("b")])?
.filter(col("b").gt(lit(1)))?
.filter(col("a").gt(lit(1)))?
.project(vec![col("a"), col("c"), col("b")])?
.build()?;
let expected = "Projection: #test.a, #test.c, #test.b\
\n Filter: #test.a Gt Int32(1)\
\n Filter: #test.b Gt Int32(1)\
\n Filter: #test.c Gt Int32(1)\
\n TableScan: test projection=Some([0, 1, 2])";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}

#[test]
fn join_schema_trim_full_join_column_projection() -> Result<()> {
let table_scan = test_table_scan()?;
Expand Down Expand Up @@ -812,8 +876,7 @@ mod tests {

assert_fields_eq(&plan, vec!["c", "a", "MAX(test.b)"]);

let expected = "\
Projection: #test.c, #test.a, #MAX(test.b)\
let expected = "Projection: #test.c, #test.a, #MAX(test.b)\
\n Filter: #test.c Gt Int32(1)\
\n Aggregate: groupBy=[[#test.a, #test.c]], aggr=[[MAX(#test.b)]]\
\n TableScan: test projection=Some([0, 1, 2])";
Expand Down