irenjj commented on code in PR #16174: URL: https://github.com/apache/datafusion/pull/16174#discussion_r2105721928
########## datafusion/optimizer/src/create_dependent_join.rs: ########## @@ -0,0 +1,163 @@ +// 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 datafusion_common::tree_node::Transformed; +use datafusion_common::{Result, ScalarValue}; +use datafusion_expr::{Expr, JoinType, LogicalPlan, LogicalPlanBuilder, Subquery}; + +use crate::{ApplyOrder, OptimizerConfig, OptimizerRule}; + +/// (temporary) OPtimizer rule for rewriting current plan with +/// DependentJoin to jj +#[derive(Default, Debug)] +pub struct CreateDependentJoin {} + +impl CreateDependentJoin { + #[allow(missing_docs)] + pub fn new() -> Self { + Self::default() + } +} + +impl OptimizerRule for CreateDependentJoin { + fn supports_rewrite(&self) -> bool { + true + } + + fn name(&self) -> &str { + "create_dependent_join" + } + + fn apply_order(&self) -> Option<ApplyOrder> { + Some(ApplyOrder::TopDown) + } + + fn rewrite( + &self, + plan: LogicalPlan, + _config: &dyn OptimizerConfig, + ) -> Result<Transformed<LogicalPlan>> { + if let LogicalPlan::Filter(ref filter) = plan { + match &filter.predicate { + Expr::BinaryExpr(binary) => { + // Check if right hand side is a scalar subquery + if let Expr::ScalarSubquery(subquery) = binary.right.as_ref() { + let new_plan = build_dependent_join( + subquery, + filter.input.as_ref().clone(), + JoinType::Left, + )?; + return Ok(Transformed::yes(new_plan)); + } + // Continue searching in children if no subquery found + return Ok(Transformed::no(plan)); + } + _ => { + // TODO: add other type of subqueries. + return Ok(Transformed::no(plan)); + } + } + } + + // No Filter found, continue searching in children + Ok(Transformed::no(plan)) + } +} + +fn build_dependent_join( + subquery: &Subquery, + root: LogicalPlan, + join_type: JoinType, +) -> Result<LogicalPlan> { + let subquery_plan = (subquery.subquery).as_ref().clone(); + + let new_plan = LogicalPlanBuilder::from(root) + .dependent_join_on( + subquery_plan, + join_type, + vec![Expr::Literal(ScalarValue::Boolean(Some(true)))], + subquery.outer_ref_columns.clone(), Review Comment: For cases where depth > 1, DataFusion doesn't support it at the planner stage. The reason is that each time parse_subquery is called, it uses the outer_query_schema, which is the schema from the previous layer of the query: ```rust pub(super) fn parse_scalar_subquery( &self, subquery: Query, input_schema: &DFSchema, planner_context: &mut PlannerContext, ) -> Result<Expr> { let old_outer_query_schema = planner_context.set_outer_query_schema(Some(input_schema.clone().into())); ... ``` In #16060, I attempted to layer the schemas of query blocks at different depths within the `PlannerContext`, and record the depth of the subquery's own layer within the `Subquery`, then pass the `PlannerContext` into the optimizer. What are your thoughts on this approach? Welcome discussion of your ideas. For multi-layer cases, more detailed design and discussion may be needed. Currently, I'm more inclined to handle simple use cases between adjacent layers first. -- 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...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org