Skip to content

Commit

Permalink
feat(optimizer): remove useless predicate in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Oct 18, 2022
1 parent 93543ae commit e3a9af0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ use crate::sql::optimizer::rule::Rule;
use crate::sql::optimizer::rule::RuleID;
use crate::sql::optimizer::rule::TransformResult;
use crate::sql::optimizer::SExpr;
use crate::sql::plans::BoundColumnRef;
use crate::sql::plans::ComparisonExpr;
use crate::sql::plans::ComparisonOp;
use crate::sql::plans::Filter;
use crate::sql::plans::PatternPlan;
use crate::sql::plans::RelOp;
use crate::sql::plans::Scalar;

pub struct RuleEliminateFilter {
id: RuleID,
Expand All @@ -30,7 +34,7 @@ pub struct RuleEliminateFilter {
impl RuleEliminateFilter {
pub fn new() -> Self {
Self {
id: RuleID::EliminateEvalScalar,
id: RuleID::EliminateFilter,
// Filter
// \
// *
Expand All @@ -56,8 +60,34 @@ impl Rule for RuleEliminateFilter {
}

fn apply(&self, s_expr: &SExpr, state: &mut TransformResult) -> Result<()> {
let eval_scalar: Filter = s_expr.plan().clone().try_into()?;
if eval_scalar.predicates.is_empty() {
let filter: Filter = s_expr.plan().clone().try_into()?;
let mut predicates = Vec::with_capacity(filter.predicates.len());
// Delete the predicate if the predicate is `a = a` syntax.
for predicate in filter.predicates.iter() {
if let Scalar::ComparisonExpr(ComparisonExpr {
left, right, op, ..
}) = predicate
{
if op == &ComparisonOp::Equal {
if let Scalar::BoundColumnRef(BoundColumnRef {
column: left_column,
}) = &**left
{
if let Scalar::BoundColumnRef(BoundColumnRef {
column: right_column,
}) = &**right
{
if left_column.index == right_column.index {
continue;
}
}
}
}
}
predicates.push(predicate.clone());
}

if predicates.is_empty() {
state.add_result(s_expr.child(0)?.clone());
}
Ok(())
Expand Down

0 comments on commit e3a9af0

Please sign in to comment.