alamb commented on a change in pull request #2026: URL: https://github.com/apache/arrow-datafusion/pull/2026#discussion_r830579298
########## File path: datafusion/src/optimizer/merge_adjacent_filter.rs ########## @@ -0,0 +1,188 @@ +// 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_expr::{Expr, Operator}; +use std::sync::Arc; + +use crate::error::Result; +use crate::logical_plan::{plan::Filter, LogicalPlan}; +use crate::optimizer::optimizer::OptimizerRule; + +use super::utils; +use crate::execution::context::ExecutionProps; + +/// Optimization rule that merge adjacent filter +#[derive(Default)] +pub struct MergeAdjacentFilter; + +impl MergeAdjacentFilter { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} + +fn optimize(plan: &LogicalPlan) -> LogicalPlan { + match plan { + LogicalPlan::Filter(Filter { + predicate: pred, + input, + }) => { + let sub_plan = optimize(&**input); + match sub_plan { + LogicalPlan::Filter(Filter { + predicate: sub_pred, + input: sub_input, + }) => LogicalPlan::Filter(Filter { + predicate: Expr::BinaryExpr { + left: Box::new(pred.clone()), + op: Operator::And, + right: Box::new(sub_pred), + }, Review comment: FWIW at least some redundant expressions are already removed by the rule in https://github.com/apache/arrow-datafusion/blob/master/datafusion/src/optimizer/simplify_expressions.rs ```sql ❯ create table foo as select * from (values (1, 2, 3), (4, 5, 6), (7, 8, 9)) as sq; ... -- Note the rendant `column1 = column2` filter is removed in the actual plan ❯ explain select * from foo where (column1 = column2) AND (column1 = column3) AND (column1 = column2); +---------------+-----------------------------------------------------------------------------------------+ | plan_type | plan | +---------------+-----------------------------------------------------------------------------------------+ | logical_plan | Projection: #foo.column1, #foo.column2, #foo.column3 | | | Filter: #foo.column1 = #foo.column2 AND #foo.column1 = #foo.column3 | | | TableScan: foo projection=Some([0, 1, 2]) | | physical_plan | ProjectionExec: expr=[column1@0 as column1, column2@1 as column2, column3@2 as column3] | | | CoalesceBatchesExec: target_batch_size=4096 | | | FilterExec: column1@0 = column2@1 AND column1@0 = column3@2 <---- redundant filter is removed | | RepartitionExec: partitioning=RoundRobinBatch(16) | | | MemoryExec: partitions=1, partition_sizes=[1] | | | | +---------------+-----------------------------------------------------------------------------------------+ 2 rows in set. Query took 0.005 seconds. ``` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
