Dandandan commented on a change in pull request #9309: URL: https://github.com/apache/arrow/pull/9309#discussion_r564458819
########## 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: :+1: Yes, agreed this ATM doesn't need a loop as there is no interaction yet with other rules. But I guess once you'll add them you will need it if you want to combine it with other rules. Using `expr != exprB` for `changed` might be a good way to start. The main thing I want to stress is that at some point we don't want the recursion itself in a rule, but in a more general "optimization framework". The optimization loop itself can be written in such a way it does both top down and bottom up replacements, applying the same rule while the optimization "generates" a replacement for the node, so it doesn't need multiple traversals for cases like you mention. The concept is here in polars https://github.com/ritchie46/polars/blob/master/polars/polars-lazy/src/logical_plan/optimizer/mod.rs#L69 And sounds like a perfect candidate to try for `ExprRewriter` :+1: ---------------------------------------------------------------- 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: [email protected]
