avantgardnerio commented on code in PR #2885: URL: https://github.com/apache/arrow-datafusion/pull/2885#discussion_r926050111
########## datafusion/optimizer/src/decorrelate_where_in.rs: ########## @@ -0,0 +1,695 @@ +// 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. + +use crate::utils::{ + alias_cols, exprs_to_join_cols, find_join_exprs, has_disjunction, merge_cols, + only_or_err, split_conjunction, swap_table, +}; +use crate::{utils, OptimizerConfig, OptimizerRule}; +use datafusion_common::{context, plan_err}; +use datafusion_expr::logical_plan::{Filter, JoinType, Projection, Subquery}; +use datafusion_expr::{combine_filters, Expr, LogicalPlan, LogicalPlanBuilder}; +use log::debug; +use std::sync::Arc; + +#[derive(Default)] +pub struct DecorrelateWhereIn {} + +impl DecorrelateWhereIn { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } + + /// Finds expressions that have a where in subquery (and recurses when found) + /// + /// # Arguments + /// + /// * `predicate` - A conjunction to split and search + /// * `optimizer_config` - For generating unique subquery aliases + /// + /// Returns a tuple (subqueries, non-subquery expressions) + fn extract_subquery_exprs( + &self, + predicate: &Expr, + optimizer_config: &mut OptimizerConfig, + ) -> datafusion_common::Result<(Vec<SubqueryInfo>, Vec<Expr>)> { + let mut filters = vec![]; + split_conjunction(predicate, &mut filters); // TODO: disjunctions + + let mut subqueries = vec![]; + let mut others = vec![]; + for it in filters.iter() { + match it { + Expr::InSubquery { + expr, + subquery, + negated, + } => { + let subquery = + self.optimize(&*subquery.subquery, optimizer_config)?; + let subquery = Arc::new(subquery); + let subquery = Subquery { subquery }; + let subquery = + SubqueryInfo::new(subquery.clone(), (**expr).clone(), *negated); + subqueries.push(subquery); + // TODO: if subquery doesn't get optimized, optimized children are lost Review Comment: To be clear, what will happen is: 1. a recursive subquery was optimized (or not) 2. it returned up the call stack to this parent `Filter` 3. this one couldn't figure out what to do, so returns an `Err` 4. the `Err` is caught by @andygrove 's recent change 5. this optimizer is ignored 6. either another one down the chain _does_ handle it, or we are no worse off than we were before the PR and the query fails to plan What should really happen is that if any recursive optimization _may_ have succeeded, then the parent should at minimum return that plan - it's a bit of a testing nightmare. -- 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]
