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

Eliminate the scalar value filter #2002

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions datafusion/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
MemTable,
},
logical_plan::{PlanType, ToStringifiedPlan},
optimizer::eliminate_filter::EliminateFilter,
optimizer::eliminate_limit::EliminateLimit,
physical_optimizer::{
aggregate_statistics::AggregateStatistics,
Expand Down Expand Up @@ -847,6 +848,7 @@ impl Default for ExecutionConfig {
// Simplify expressions first to maximize the chance
// of applying other optimizations
Arc::new(SimplifyExpressions::new()),
Arc::new(EliminateFilter::new()),
Arc::new(CommonSubexprEliminate::new()),
Arc::new(EliminateLimit::new()),
Arc::new(ProjectionPushDown::new()),
Expand Down
143 changes: 143 additions & 0 deletions datafusion/src/optimizer/eliminate_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//! Optimizer rule to replace `where false` on a plan with an empty relation.
//! This saves time in planning and executing the query.
//! Note that this rule should be applied after simplify expressions optimizer rule.
use datafusion_common::ScalarValue;
use datafusion_expr::Expr;

use crate::error::Result;
use crate::logical_plan::plan::Filter;
use crate::logical_plan::{EmptyRelation, LogicalPlan};
use crate::optimizer::optimizer::OptimizerRule;

use super::utils;
use crate::execution::context::ExecutionProps;

/// Optimization rule that elimanate the scalar value filter with an [LogicalPlan::EmptyRelation]
#[derive(Default)]
pub struct EliminateFilter;

impl EliminateFilter {
#[allow(missing_docs)]
pub fn new() -> Self {
Self {}
}
}

impl OptimizerRule for EliminateFilter {
fn optimize(
&self,
plan: &LogicalPlan,
execution_props: &ExecutionProps,
) -> Result<LogicalPlan> {
match plan {
LogicalPlan::Filter(Filter {
predicate: Expr::Literal(ScalarValue::Boolean(Some(v))),
input,
}) => {
if !*v {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
Copy link
Member

@Ted-Jiang Ted-Jiang Mar 13, 2022

Choose a reason for hiding this comment

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

i have checked in pg , it will return 0 row. 👍

postgres=# select 1 from user_tbl where false;
 ?column?
----------
(0 rows)

postgres=# select * from user_tbl where false;
 name | signup_date
------+-------------
(0 rows)

schema: input.schema().clone(),
}));
} else {
return Ok((**input).clone());
}
}
_ => {
// apply the optimization to all inputs of the plan
let inputs = plan.inputs();
let new_inputs = inputs
.iter()
.map(|plan| self.optimize(plan, execution_props))
.collect::<Result<Vec<_>>>()?;

utils::from_plan(plan, &plan.expressions(), &new_inputs)
}
}
}

fn name(&self) -> &str {
"eliminate_filter"
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::logical_plan::LogicalPlanBuilder;
use crate::logical_plan::{col, sum};
use crate::test::*;

fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
let rule = EliminateFilter::new();
let optimized_plan = rule
.optimize(plan, &ExecutionProps::new())
.expect("failed to optimize plan");
let formatted_plan = format!("{:?}", optimized_plan);
assert_eq!(formatted_plan, expected);
assert_eq!(plan.schema(), optimized_plan.schema());
}

#[test]
fn fliter_false() {
jackwener marked this conversation as resolved.
Show resolved Hide resolved
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(false)));

let table_scan = test_table_scan().unwrap();
let plan = LogicalPlanBuilder::from(table_scan)
.aggregate(vec![col("a")], vec![sum(col("b"))])
.unwrap()
.filter(filter_expr)
.unwrap()
.build()
.unwrap();

// No aggregate / scan / limit
let expected = "EmptyRelation";
assert_optimized_plan_eq(&plan, expected);
}

#[test]
fn fliter_false_nested() {
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(false)));

let table_scan = test_table_scan().unwrap();
let plan1 = LogicalPlanBuilder::from(table_scan.clone())
.aggregate(vec![col("a")], vec![sum(col("b"))])
.unwrap()
.build()
.unwrap();
let plan = LogicalPlanBuilder::from(table_scan)
.aggregate(vec![col("a")], vec![sum(col("b"))])
.unwrap()
.filter(filter_expr)
.unwrap()
.union(plan1)
.unwrap()
.build()
.unwrap();

// Left side is removed
let expected = "Union\
\n EmptyRelation\
\n Aggregate: groupBy=[[#test.a]], aggr=[[SUM(#test.b)]]\
\n TableScan: test projection=None";
assert_optimized_plan_eq(&plan, expected);
}
}
1 change: 1 addition & 0 deletions datafusion/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#![allow(clippy::module_inception)]
pub mod common_subexpr_eliminate;
pub mod eliminate_filter;
pub mod eliminate_limit;
pub mod filter_push_down;
pub mod limit_push_down;
Expand Down