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

Recursively apply remove filter rule if filter is a true scalar value #3175

Merged
merged 3 commits into from
Aug 16, 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
33 changes: 31 additions & 2 deletions datafusion/optimizer/src/eliminate_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl OptimizerRule for EliminateFilter {
schema: input.schema().clone(),
}))
} else {
Ok((**input).clone())
self.optimize(input, optimizer_config)
}
}
_ => {
Expand All @@ -79,7 +79,7 @@ impl OptimizerRule for EliminateFilter {
mod tests {
use super::*;
use crate::test::*;
use datafusion_expr::{col, logical_plan::builder::LogicalPlanBuilder, sum};
use datafusion_expr::{col, lit, logical_plan::builder::LogicalPlanBuilder, sum};

fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
let rule = EliminateFilter::new();
Expand Down Expand Up @@ -183,4 +183,33 @@ mod tests {
\n TableScan: test";
assert_optimized_plan_eq(&plan, expected);
}

#[test]
fn fliter_from_subquery() {
// SELECT a FROM (SELECT a FROM test WHERE FALSE) WHERE TRUE

let false_filter = lit(false);
let table_scan = test_table_scan().unwrap();
let plan1 = LogicalPlanBuilder::from(table_scan.clone())
.project(vec![col("a")])
.unwrap()
.filter(false_filter)
.unwrap()
.build()
.unwrap();

let true_filter = lit(true);
let plan = LogicalPlanBuilder::from(plan1.clone())
.project(vec![col("a")])
.unwrap()
.filter(true_filter)
.unwrap()
.build()
.unwrap();

// Filter is removed
let expected = "Projection: #test.a\
\n EmptyRelation";
assert_optimized_plan_eq(&plan, expected);
}
}