DhamoPS commented on code in PR #3482: URL: https://github.com/apache/arrow-datafusion/pull/3482#discussion_r972244058
########## datafusion/optimizer/src/reduce_cross_join.rs: ########## @@ -0,0 +1,442 @@ +// 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 reduce cross join to inner join if join predicates are available in filters. +use crate::{OptimizerConfig, OptimizerRule}; +use datafusion_common::{Column, Result}; +use datafusion_expr::{ + logical_plan::{Filter, Join, CrossJoin, JoinType, LogicalPlan}, + utils::from_plan, and, or, utils::can_hash +}; +use datafusion_expr::{Expr, Operator}; + +use std::collections::HashSet; + +//use std::collections::HashMap; +use std::sync::Arc; +use datafusion_expr::logical_plan::JoinConstraint; + +#[derive(Default)] +pub struct ReduceCrossJoin; + +impl ReduceCrossJoin { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} + +impl OptimizerRule for ReduceCrossJoin { + fn optimize( + &self, + plan: &LogicalPlan, + optimizer_config: &mut OptimizerConfig, + ) -> Result<LogicalPlan> { + let mut possible_join_keys : Vec<(Column, Column)> = vec![]; + let mut all_join_keys = HashSet::new(); + + reduce_cross_join(self, plan, &mut possible_join_keys, &mut all_join_keys, optimizer_config) + } + + fn name(&self) -> &str { + "reduce_cross_join" + } +} + +/// Attempt to reduce cross joins to inner joins. +/// for queries: select ... from a, b where a.x = b.y and b.xx = 100; +/// select ... from a, b where (a.x = b.y and b.xx = 100) or (a.x = b.y and b.xx = 200); +/// select ... from a, b, c where (a.x = b.y and b.xx = 100 and a.z = c.z) +/// or (a.x = b.y and b.xx = 200 and a.z=c.z); +/// For above queries, the join predicate is available in filters and they are moved to +/// join nodes appropriately +/// This fix helps to improve the performance of TPCH Q19. issue#78 +/// +fn reduce_cross_join( + _optimizer: &ReduceCrossJoin, + plan: &LogicalPlan, + possible_join_keys: &mut Vec<(Column, Column)>, + all_join_keys: &mut HashSet<(Column, Column)>, + _optimizer_config: &OptimizerConfig, +) -> Result<LogicalPlan> { + match plan { + LogicalPlan::Filter(Filter { input, predicate }) => { + extract_possible_join_keys(predicate, possible_join_keys)?; + let left = reduce_cross_join(_optimizer, input, possible_join_keys, all_join_keys, _optimizer_config)?; Review Comment: ok, renamed it to new_plan -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org