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
38 changes: 37 additions & 1 deletion datafusion/optimizer/src/push_down_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,24 @@ fn optimize_plan(
)?;
from_plan(plan, &plan.expressions(), &[child])
}
// at a distinct, all columns are required
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

LogicalPlan::Distinct(distinct) => {
let new_required_columns = distinct
.input
.schema()
.fields()
.iter()
.map(|f| f.qualified_column())
.collect();
let child = optimize_plan(
_optimizer,
distinct.input.as_ref(),
&new_required_columns,
has_projection,
_config,
)?;
from_plan(plan, &[], &[child])
}
// all other nodes: Add any additional columns used by
// expressions in this node to the list of required columns
LogicalPlan::Limit(_)
Expand All @@ -392,7 +410,6 @@ fn optimize_plan(
| LogicalPlan::DropView(_)
| LogicalPlan::SetVariable(_)
| LogicalPlan::CrossJoin(_)
| LogicalPlan::Distinct(_)
| LogicalPlan::Extension { .. }
| LogicalPlan::Prepare(_) => {
let expr = plan.expressions();
Expand Down Expand Up @@ -1009,6 +1026,25 @@ mod tests {
Ok(())
}

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

let plan = LogicalPlanBuilder::from(table_scan)
.project(vec![col("a"), col("b")])?
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

.distinct()?
.project(vec![col("a")])?
.build()?;

let expected = "Projection: test.a\
\n Distinct:\
\n TableScan: test projection=[a, b]";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}

fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
let optimized_plan = optimize(plan).expect("failed to optimize plan");
let formatted_plan = format!("{optimized_plan:?}");
Expand Down