Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support limit pushdown through left right outer join #2596

Merged
merged 1 commit into from
May 23, 2022
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
157 changes: 141 additions & 16 deletions datafusion/core/src/optimizer/limit_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use crate::logical_plan::plan::Projection;
use crate::logical_plan::{Limit, TableScan};
use crate::logical_plan::{LogicalPlan, Union};
use crate::optimizer::optimizer::OptimizerRule;
use datafusion_expr::logical_plan::Offset;
use datafusion_common::DataFusionError;
use datafusion_expr::logical_plan::{Join, JoinType, Offset};
use datafusion_expr::utils::from_plan;
use std::sync::Arc;

Expand Down Expand Up @@ -157,25 +158,99 @@ fn limit_push_down(
)?),
}))
}
(LogicalPlan::Join(Join { join_type, .. }), upper_limit) => match join_type {
JoinType::Left => {
//if LeftOuter join push limit to left
generate_push_down_join(
_optimizer,
_execution_props,
plan,
upper_limit,
None,
)
}
JoinType::Right =>
// If RightOuter join push limit to right
{
generate_push_down_join(
_optimizer,
_execution_props,
plan,
None,
upper_limit,
)
}
_ => generate_push_down_join(_optimizer, _execution_props, plan, None, None),
},
// For other nodes we can't push down the limit
// But try to recurse and find other limit nodes to push down
_ => {
let expr = plan.expressions();

// apply the optimization to all inputs of the plan
let inputs = plan.inputs();
let new_inputs = inputs
.iter()
.map(|plan| {
limit_push_down(_optimizer, None, plan, _execution_props, false)
})
.collect::<Result<Vec<_>>>()?;
_ => push_down_children_limit(_optimizer, _execution_props, plan),
}
}

from_plan(plan, &expr, &new_inputs)
}
fn generate_push_down_join(
_optimizer: &LimitPushDown,
_execution_props: &ExecutionProps,
join: &LogicalPlan,
left_limit: Option<usize>,
right_limit: Option<usize>,
) -> Result<LogicalPlan> {
if let LogicalPlan::Join(Join {
left,
right,
on,
join_type,
join_constraint,
schema,
null_equals_null,
}) = join
{
return Ok(LogicalPlan::Join(Join {
left: Arc::new(limit_push_down(
_optimizer,
left_limit,
left.as_ref(),
_execution_props,
true,
)?),
right: Arc::new(limit_push_down(
_optimizer,
right_limit,
right.as_ref(),
_execution_props,
true,
)?),
on: on.clone(),
join_type: *join_type,
join_constraint: *join_constraint,
schema: schema.clone(),
null_equals_null: *null_equals_null,
}));
} else {
Err(DataFusionError::Internal(format!(
"{:?} must be join type",
join
)))
}
}

fn push_down_children_limit(
_optimizer: &LimitPushDown,
_execution_props: &ExecutionProps,
plan: &LogicalPlan,
) -> Result<LogicalPlan> {
let expr = plan.expressions();

// apply the optimization to all inputs of the plan
let inputs = plan.inputs();
let new_inputs = inputs
.iter()
.map(|plan| limit_push_down(_optimizer, None, plan, _execution_props, false))
.collect::<Result<Vec<_>>>()?;

from_plan(plan, &expr, &new_inputs)
}

impl OptimizerRule for LimitPushDown {
fn optimize(
&self,
Expand Down Expand Up @@ -429,7 +504,7 @@ mod test {
let plan = LogicalPlanBuilder::from(table_scan_1)
.join(
&LogicalPlanBuilder::from(table_scan_2).build()?,
JoinType::Left,
JoinType::Inner,
(vec!["a"], vec!["a"]),
)?
.limit(1000)?
Expand All @@ -439,7 +514,7 @@ mod test {
// Limit pushdown Not supported in Join
let expected = "Offset: 10\
\n Limit: 1010\
\n Left Join: #test.a = #test2.a\
\n Inner Join: #test.a = #test2.a\
\n TableScan: test projection=None\
\n TableScan: test2 projection=None";

Expand Down Expand Up @@ -478,4 +553,54 @@ mod test {

Ok(())
}

#[test]
fn limit_should_push_down_left_outer_join() -> Result<()> {
let table_scan_1 = test_table_scan()?;
let table_scan_2 = test_table_scan_with_name("test2")?;

let plan = LogicalPlanBuilder::from(table_scan_1)
.join(
&LogicalPlanBuilder::from(table_scan_2).build()?,
JoinType::Left,
(vec!["a"], vec!["a"]),
)?
.limit(1000)?
.build()?;

// Limit pushdown Not supported in Join
let expected = "Limit: 1000\
\n Left Join: #test.a = #test2.a\
\n TableScan: test projection=None, limit=1000\
\n TableScan: test2 projection=None";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}

#[test]
fn limit_should_push_down_right_outer_join() -> Result<()> {
let table_scan_1 = test_table_scan()?;
let table_scan_2 = test_table_scan_with_name("test2")?;

let plan = LogicalPlanBuilder::from(table_scan_1)
.join(
&LogicalPlanBuilder::from(table_scan_2).build()?,
JoinType::Right,
(vec!["a"], vec!["a"]),
)?
.limit(1000)?
.build()?;

// Limit pushdown Not supported in Join
let expected = "Limit: 1000\
\n Right Join: #test.a = #test2.a\
\n TableScan: test projection=None\
\n TableScan: test2 projection=None, limit=1000";

assert_optimized_plan_eq(&plan, expected);

Ok(())
}
}