yukkit commented on code in PR #6084: URL: https://github.com/apache/arrow-datafusion/pull/6084#discussion_r1177745258
########## datafusion/optimizer/src/analyzer/subquery.rs: ########## @@ -0,0 +1,340 @@ +// 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::analyzer::check_plan; +use crate::utils::{collect_subquery_cols, split_conjunction}; +use datafusion_common::tree_node::{TreeNode, VisitRecursion}; +use datafusion_common::{DataFusionError, Result}; +use datafusion_expr::expr_rewriter::strip_outer_reference; +use datafusion_expr::{ + Aggregate, BinaryExpr, Cast, Expr, Filter, Join, JoinType, LogicalPlan, Operator, + Window, +}; +use std::ops::Deref; + +/// Do necessary check on subquery expressions and fail the invalid plan +/// 1) Check whether the outer plan is in the allowed outer plans list to use subquery expressions, +/// the allowed while list: [Projection, Filter, Window, Aggregate, Join]. +/// 2) Check whether the inner plan is in the allowed inner plans list to use correlated(outer) expressions. +/// 3) Check and validate unsupported cases to use the correlated(outer) expressions inside the subquery(inner) plans/inner expressions. +/// For example, we do not want to support to use correlated expressions as the Join conditions in the subquery plan when the Join +/// is a Full Out Join +pub fn check_subquery_expr( + outer_plan: &LogicalPlan, + inner_plan: &LogicalPlan, + expr: &Expr, +) -> Result<()> { + check_plan(inner_plan)?; + if let Expr::ScalarSubquery(subquery) = expr { + // Scalar subquery should only return one column + if subquery.subquery.schema().fields().len() > 1 { + return Err(datafusion_common::DataFusionError::Plan( + "Scalar subquery should only return one column".to_string(), + )); + } + // Correlated scalar subquery must be aggregated to return at most one row + if !subquery.outer_ref_columns.is_empty() { + match strip_inner_query(inner_plan) { + LogicalPlan::Aggregate(agg) => { + check_aggregation_in_scalar_subquery(inner_plan, agg) + } + LogicalPlan::Filter(Filter { input, .. }) + if matches!(input.as_ref(), LogicalPlan::Aggregate(_)) => + { + if let LogicalPlan::Aggregate(agg) = input.as_ref() { + check_aggregation_in_scalar_subquery(inner_plan, agg) + } else { + Ok(()) + } + } + _ => { + if inner_plan + .max_rows() + .filter(|max_row| *max_row <= 1) + .is_some() + { + Ok(()) + } else { + Err(DataFusionError::Plan( + "Correlated scalar subquery must be aggregated to return at most one row" + .to_string(), + )) + } Review Comment: If it is checked at runtime, additional operators need to be introduced. The current pr is much better than before, and this feature can be implemented in future plans. The code of presto is here https://github.com/prestodb/presto/blob/master/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/TransformCorrelatedScalarSubquery.java -- 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]
