You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/26 07:19:35 UTC

[GitHub] [arrow] Dandandan commented on a change in pull request #9309: ARROW-11366: [Datafusion] support boolean literal in comparison expression

Dandandan commented on a change in pull request #9309:
URL: https://github.com/apache/arrow/pull/9309#discussion_r564293129



##########
File path: rust/datafusion/src/optimizer/boolean_comparison.rs
##########
@@ -0,0 +1,270 @@
+// 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.
+
+//! Boolean comparision rule rewrites redudant comparison expression involing boolean literal into
+//! unary expression.
+
+use std::sync::Arc;
+
+use crate::error::Result;
+use crate::logical_plan::{Expr, LogicalPlan, Operator};
+use crate::optimizer::optimizer::OptimizerRule;
+use crate::optimizer::utils;
+use crate::scalar::ScalarValue;
+
+/// Optimizer that simplifies comparison expressions involving boolean literals.
+///
+/// Recursively go through all expressionss and simplify the following cases:
+/// * `expr = ture` to `expr`
+/// * `expr = false` to `!expr`
+pub struct BooleanComparison {}
+
+impl BooleanComparison {

Review comment:
       I think we could make this optimizer a bit more generic (not necessary in this PR) to split recursion / pattern match
   
   This is the more "optimizer framework" I had in mind in the comment on the roadmap
   @alamb @jorgecarleitao @vertexclique .
   
   A common strategy (used by Spark) for rule / replacement based  to have a loop that just does something like this:
   
   ```rust
   let changed = False;
   
   while !changed {
       (logical_plan, changed)  = apply_optimizations(rules, logical_plan);
   }
   ```
   
   A rule could be something like (returning `Some` on replaced output) and doesn't need the boilerplate of recursion for every rule.
   
   ```rust
   // Optimizer can work both on expr and logical plans, default returns `None`
   impl OptimizerRule for BooleanComparison {
      fn optimize_expr(&mut self, plan: &Expr) -> Option<Expr> {
          match e {
                Expr::BinaryExpr { left, op, right } => {
               let left = optimize_expr(left);
               let right = optimize_expr(right);
               match op {
                   Operator::Eq => match (&left, &right) {
                       (Expr::Literal(ScalarValue::Boolean(b)), _) => match b {
                           Some(true) => Some(right),
                           Some(false) | None => Some(Expr::Not(Box::new(right))),
                       },
                       (_, Expr::Literal(ScalarValue::Boolean(b))) => match b {
                           Some(true) => Some(left),
                           Some(false) | None => Some(Expr::Not(Box::new(left))),
                       },
                       _ => None,
                   },
                   Operator::NotEq => match (&left, &right) {
                       (Expr::Literal(ScalarValue::Boolean(b)), _) => match b {
                           Some(false) | None => Some(right),
                           Some(true) => Some(Expr::Not(Box::new(right))),
                       },
                       (_, Expr::Literal(ScalarValue::Boolean(b))) => match b {
                           Some(false) | None => left,
                           Some(true) => Some(Expr::Not(Box::new(left))),
                       },
                       _ => None,
                   },
                   _ => None
               }
           }
          }
      }
   }
   
   ```
       




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org