Apache DataFusion SQL Query Engine
Add OuterReferenceColumn to Expr to represent correlated expression (#5593)
mingmwang26e1b20ea336
diff --git a/datafusion/core/src/datasource/listing/helpers.rs b/datafusion/core/src/datasource/listing/helpers.rsindex fa7f7070c3..7ed6326a90 100644--- a/datafusion/core/src/datasource/listing/helpers.rs+++ b/datafusion/core/src/datasource/listing/helpers.rs@@ -81,6 +81,7 @@ impl ExpressionVisitor for ApplicabilityVisitor<'_> {}Expr::Literal(_)| Expr::Alias(_, _)+ | Expr::OuterReferenceColumn(_, _)| Expr::ScalarVariable(_, _)| Expr::Not(_)| Expr::IsNotNull(_)diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rsindex 9cc907b28b..51653450a6 100644--- a/datafusion/core/src/physical_plan/planner.rs+++ b/datafusion/core/src/physical_plan/planner.rs@@ -346,6 +346,9 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {Expr::Placeholder { .. } => Err(DataFusionError::Internal("Create physical name does not support placeholder".to_string(),)),+ Expr::OuterReferenceColumn(_, _) => Err(DataFusionError::Internal(+ "Create physical name does not support OuterReferenceColumn".to_string(),+ )),}}diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rsindex 8b810737cf..65cbcdfe06 100644--- a/datafusion/core/tests/sql/mod.rs+++ b/datafusion/core/tests/sql/mod.rs@@ -231,6 +231,82 @@ fn create_join_context(Ok(ctx)}+fn create_sub_query_join_context(+ column_outer: &str,+ column_inner_left: &str,+ column_inner_right: &str,+ repartition_joins: bool,+) -> Result<SessionContext> {+ let ctx = SessionContext::with_config(+ SessionConfig::new()+ .with_repartition_joins(repartition_joins)+ .with_target_partitions(2)+ .with_batch_size(4096),+ );++ let t0_schema = Arc::new(Schema::new(vec![+ Field::new(column_outer, DataType::UInt32, true),+ Field::new("t0_name", DataType::Utf8, true),+ Field::new("t0_int", DataType::UInt32, true),+ ]));+ let t0_data = RecordBatch::try_new(+ t0_schema,+ vec![+ Arc::new(UInt32Array::from_slice([11, 22, 33, 44])),+ Arc::new(StringArray::from(vec![+ Some("a"),+ Some("b"),+ Some("c"),+ Some("d"),+ ])),+ Arc::new(UInt32Array::from_slice([1, 2, 3, 4])),+ ],+ )?;+ ctx.register_batch("t0", t0_data)?;++ let t1_schema = Arc::new(Schema::new(vec![+ Field::new(column_inner_left, DataType::UInt32, true),+ Field::new("t1_name", DataType::Utf8, true),+ Field::new("t1_int", DataType::UInt32, true),+ ]));+ let t1_data = RecordBatch::try_new(+ t1_schema,+ vec![+ Arc::new(UInt32Array::from_slice([11, 22, 33, 44])),+ Arc::new(StringArray::from(vec![+ Some("a"),+ Some("b"),+ Some("c"),+ Some("d"),+ ])),+ Arc::new(UInt32Array::from_slice([1, 2, 3, 4])),+ ],+ )?;+ ctx.register_batch("t1", t1_data)?;++ let t2_schema = Arc::new(Schema::new(vec![+ Field::new(column_inner_right, DataType::UInt32, true),+ Field::new("t2_name", DataType::Utf8, true),+ Field::new("t2_int", DataType::UInt32, true),+ ]));+ let t2_data = RecordBatch::try_new(+ t2_schema,+ vec![+ Arc::new(UInt32Array::from_slice([11, 22, 44, 55])),+ Arc::new(StringArray::from(vec![+ Some("z"),+ Some("y"),+ Some("x"),+ Some("w"),+ ])),+ Arc::new(UInt32Array::from_slice([3, 1, 3, 3])),+ ],+ )?;+ ctx.register_batch("t2", t2_data)?;++ Ok(ctx)+}+fn create_left_semi_anti_join_context_with_null_ids(column_left: &str,column_right: &str,diff --git a/datafusion/core/tests/sql/subqueries.rs b/datafusion/core/tests/sql/subqueries.rsindex 1dc4814c01..6711384d17 100644--- a/datafusion/core/tests/sql/subqueries.rs+++ b/datafusion/core/tests/sql/subqueries.rs@@ -213,3 +213,228 @@ async fn subquery_not_allowed() -> Result<()> {Ok(())}++#[tokio::test]+async fn support_agg_correlated_columns() -> Result<()> {+ let ctx = create_join_context("t1_id", "t2_id", true)?;++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT sum(t1.t1_int + t2.t2_id) FROM t2 WHERE t1.t1_name = t2.t2_name)";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [SUM(outer_ref(t1.t1_int) + t2.t2_id):UInt64;N]",+ " Projection: SUM(outer_ref(t1.t1_int) + t2.t2_id) [SUM(outer_ref(t1.t1_int) + t2.t2_id):UInt64;N]",+ " Aggregate: groupBy=[[]], aggr=[[SUM(outer_ref(t1.t1_int) + t2.t2_id)]] [SUM(outer_ref(t1.t1_int) + t2.t2_id):UInt64;N]",+ " Filter: outer_ref(t1.t1_name) = t2.t2_name [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_agg_correlated_columns2() -> Result<()> {+ let ctx = create_join_context("t1_id", "t2_id", true)?;++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT count(*) FROM t2 WHERE t1.t1_name = t2.t2_name having sum(t1_int + t2_id) >0)";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [COUNT(UInt8(1)):Int64;N]",+ " Projection: COUNT(UInt8(1)) [COUNT(UInt8(1)):Int64;N]",+ " Filter: CAST(SUM(outer_ref(t1.t1_int) + t2.t2_id) AS Int64) > Int64(0) [COUNT(UInt8(1)):Int64;N, SUM(outer_ref(t1.t1_int) + t2.t2_id):UInt64;N]",+ " Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)), SUM(outer_ref(t1.t1_int) + t2.t2_id)]] [COUNT(UInt8(1)):Int64;N, SUM(outer_ref(t1.t1_int) + t2.t2_id):UInt64;N]",+ " Filter: outer_ref(t1.t1_name) = t2.t2_name [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_join_correlated_columns() -> Result<()> {+ let ctx = create_sub_query_join_context("t0_id", "t1_id", "t2_id", true)?;+ let sql = "SELECT t0_id, t0_name FROM t0 WHERE EXISTS (SELECT 1 FROM t1 INNER JOIN t2 ON(t1.t1_id = t2.t2_id and t1.t1_name = t0.t0_name))";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t0_id:UInt32;N, t0_name:Utf8;N]",+ " Subquery: [Int64(1):Int64]",+ " Projection: Int64(1) [Int64(1):Int64]",+ " Inner Join: Filter: t1.t1_id = t2.t2_id AND t1.t1_name = outer_ref(t0.t0_name) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t0 projection=[t0_id, t0_name] [t0_id:UInt32;N, t0_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_join_correlated_columns2() -> Result<()> {+ let ctx = create_sub_query_join_context("t0_id", "t1_id", "t2_id", true)?;+ let sql = "SELECT t0_id, t0_name FROM t0 WHERE EXISTS (SELECT 1 FROM t1 INNER JOIN (select * from t2 where t2.t2_name = t0.t0_name) as t2 ON(t1.t1_id = t2.t2_id ))";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t0_id:UInt32;N, t0_name:Utf8;N]",+ " Subquery: [Int64(1):Int64]",+ " Projection: Int64(1) [Int64(1):Int64]",+ " Inner Join: Filter: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",+ " SubqueryAlias: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Projection: t2.t2_id, t2.t2_name, t2.t2_int [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Filter: t2.t2_name = outer_ref(t0.t0_name) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t0 projection=[t0_id, t0_name] [t0_id:UInt32;N, t0_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_order_by_correlated_columns() -> Result<()> {+ let ctx = create_join_context("t1_id", "t2_id", true)?;++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2_id >= t1_id order by t1_id)";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Sort: outer_ref(t1.t1_id) ASC NULLS LAST [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Projection: t2.t2_id, t2.t2_name, t2.t2_int [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Filter: t2.t2_id >= outer_ref(t1.t1_id) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_limit_subquery() -> Result<()> {+ let ctx = create_join_context("t1_id", "t2_id", true)?;++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2_id = t1_id limit 1)";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Limit: skip=0, fetch=1 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Projection: t2.t2_id, t2.t2_name, t2.t2_int [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Filter: t2.t2_id = outer_ref(t1.t1_id) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE t1_id in (SELECT t2_id FROM t2 where t1_name = t2_name limit 10)";+ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: t1.t1_id IN (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [t2_id:UInt32;N]",+ " Limit: skip=0, fetch=10 [t2_id:UInt32;N]",+ " Projection: t2.t2_id [t2_id:UInt32;N]",+ " Filter: outer_ref(t1.t1_name) = t2.t2_name [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}++#[tokio::test]+async fn support_union_subquery() -> Result<()> {+ let ctx = create_join_context("t1_id", "t2_id", true)?;++ let sql = "SELECT t1_id, t1_name FROM t1 WHERE EXISTS \+ (SELECT * FROM t2 WHERE t2_id = t1_id UNION ALL \+ SELECT * FROM t2 WHERE upper(t2_name) = upper(t1.t1_name))";++ let msg = format!("Creating logical plan for '{sql}'");+ let dataframe = ctx.sql(sql).await.expect(&msg);+ let plan = dataframe.into_optimized_plan()?;++ let expected = vec![+ "Filter: EXISTS (<subquery>) [t1_id:UInt32;N, t1_name:Utf8;N]",+ " Subquery: [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Union [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Projection: t2.t2_id, t2.t2_name, t2.t2_int [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Filter: t2.t2_id = outer_ref(t1.t1_id) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Projection: t2.t2_id, t2.t2_name, t2.t2_int [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " Filter: upper(t2.t2_name) = upper(outer_ref(t1.t1_name)) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t2 [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",+ " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",+ ];+ let formatted = plan.display_indent_schema().to_string();+ let actual: Vec<&str> = formatted.trim().lines().collect();+ assert_eq!(+ expected, actual,+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"+ );++ Ok(())+}diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rsindex 1530513e96..01860ae7d4 100644--- a/datafusion/expr/src/expr.rs+++ b/datafusion/expr/src/expr.rs@@ -21,7 +21,7 @@ use crate::aggregate_function;use crate::built_in_function;use crate::expr_fn::binary_expr;use crate::logical_plan::Subquery;-use crate::utils::expr_to_columns;+use crate::utils::{expr_to_columns, find_out_reference_exprs};use crate::window_frame;use crate::window_function;use crate::AggregateUDF;@@ -220,6 +220,9 @@ pub enum Expr {/// The type the parameter will be filled in withdata_type: Option<DataType>,},+ /// A place holder which hold a reference to a qualified field+ /// in the outer query, used for correlated sub queries.+ OuterReferenceColumn(DataType, Column),}/// Binary expression@@ -567,6 +570,7 @@ impl Expr {Expr::Case { .. } => "Case",Expr::Cast { .. } => "Cast",Expr::Column(..) => "Column",+ Expr::OuterReferenceColumn(_, _) => "Outer",Expr::Exists { .. } => "Exists",Expr::GetIndexedField { .. } => "GetIndexedField",Expr::GroupingSet(..) => "GroupingSet",@@ -785,6 +789,11 @@ impl Expr {Ok(using_columns)}++ /// Return true when the expression contains out reference(correlated) expressions.+ pub fn contains_outer(&self) -> bool {+ !find_out_reference_exprs(self).is_empty()+ }}impl Not for Expr {@@ -830,6 +839,7 @@ impl fmt::Debug for Expr {match self {Expr::Alias(expr, alias) => write!(f, "{expr:?} AS {alias}"),Expr::Column(c) => write!(f, "{c}"),+ Expr::OuterReferenceColumn(_, c) => write!(f, "outer_ref({})", c),Expr::ScalarVariable(_, var_names) => write!(f, "{}", var_names.join(".")),Expr::Literal(v) => write!(f, "{v:?}"),Expr::Case(case) => {@@ -1110,6 +1120,7 @@ fn create_name(e: &Expr) -> Result<String> {match e {Expr::Alias(_, name) => Ok(name.clone()),Expr::Column(c) => Ok(c.flat_name()),+ Expr::OuterReferenceColumn(_, c) => Ok(format!("outer_ref({})", c.flat_name())),Expr::ScalarVariable(_, variable_names) => Ok(variable_names.join(".")),Expr::Literal(value) => Ok(format!("{value:?}")),Expr::BinaryExpr(binary_expr) => {diff --git a/datafusion/expr/src/expr_fn.rs b/datafusion/expr/src/expr_fn.rsindex ef6f8ac50f..b20629946b 100644--- a/datafusion/expr/src/expr_fn.rs+++ b/datafusion/expr/src/expr_fn.rs@@ -297,7 +297,10 @@ pub fn approx_percentile_cont_with_weight(/// Create an EXISTS subquery expressionpub fn exists(subquery: Arc<LogicalPlan>) -> Expr {Expr::Exists {- subquery: Subquery { subquery },+ subquery: Subquery {+ subquery,+ outer_ref_columns: vec![],+ },negated: false,}}@@ -305,7 +308,10 @@ pub fn exists(subquery: Arc<LogicalPlan>) -> Expr {/// Create a NOT EXISTS subquery expressionpub fn not_exists(subquery: Arc<LogicalPlan>) -> Expr {Expr::Exists {- subquery: Subquery { subquery },+ subquery: Subquery {+ subquery,+ outer_ref_columns: vec![],+ },negated: true,}}@@ -314,7 +320,10 @@ pub fn not_exists(subquery: Arc<LogicalPlan>) -> Expr {pub fn in_subquery(expr: Expr, subquery: Arc<LogicalPlan>) -> Expr {Expr::InSubquery {expr: Box::new(expr),- subquery: Subquery { subquery },+ subquery: Subquery {+ subquery,+ outer_ref_columns: vec![],+ },negated: false,}}@@ -323,14 +332,20 @@ pub fn in_subquery(expr: Expr, subquery: Arc<LogicalPlan>) -> Expr {pub fn not_in_subquery(expr: Expr, subquery: Arc<LogicalPlan>) -> Expr {Expr::InSubquery {expr: Box::new(expr),- subquery: Subquery { subquery },+ subquery: Subquery {+ subquery,+ outer_ref_columns: vec![],+ },negated: true,}}/// Create a scalar subquery expressionpub fn scalar_subquery(subquery: Arc<LogicalPlan>) -> Expr {- Expr::ScalarSubquery(Subquery { subquery })+ Expr::ScalarSubquery(Subquery {+ subquery,+ outer_ref_columns: vec![],+ })}/// Create an expression to represent the stddev() aggregate functiondiff --git a/datafusion/expr/src/expr_rewriter.rs b/datafusion/expr/src/expr_rewriter.rsindex ade276babf..b4e82be578 100644--- a/datafusion/expr/src/expr_rewriter.rs+++ b/datafusion/expr/src/expr_rewriter.rs@@ -121,6 +121,7 @@ impl ExprRewritable for Expr {let expr = match self {Expr::Alias(expr, name) => Expr::Alias(rewrite_boxed(expr, rewriter)?, name),Expr::Column(_) => self.clone(),+ Expr::OuterReferenceColumn(_, _) => self.clone(),Expr::Exists { .. } => self.clone(),Expr::InSubquery {expr,@@ -446,6 +447,19 @@ pub fn unnormalize_cols(exprs: impl IntoIterator<Item = Expr>) -> Vec<Expr> {exprs.into_iter().map(unnormalize_col).collect()}+/// Recursively remove all the ['OuterReferenceColumn'] and return the inside Column+/// in the expression tree.+pub fn strip_outer_reference(expr: Expr) -> Expr {+ rewrite_expr(expr, |expr| {+ if let Expr::OuterReferenceColumn(_, col) = expr {+ Ok(Expr::Column(col))+ } else {+ Ok(expr)+ }+ })+ .expect("strip_outer_reference is infallable")+}+/// Implementation of [`ExprRewriter`] that calls a function, for use/// with [`rewrite_expr`]struct RewriterAdapter<F> {diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rsindex 32e831b430..fafda79a6f 100644--- a/datafusion/expr/src/expr_schema.rs+++ b/datafusion/expr/src/expr_schema.rs@@ -65,6 +65,7 @@ impl ExprSchemable for Expr {},Expr::Sort(Sort { expr, .. }) | Expr::Negative(expr) => expr.get_type(schema),Expr::Column(c) => Ok(schema.data_type(c)?.clone()),+ Expr::OuterReferenceColumn(ty, _) => Ok(ty.clone()),Expr::ScalarVariable(ty, _) => Ok(ty.clone()),Expr::Literal(l) => Ok(l.get_datatype()),Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),@@ -174,6 +175,7 @@ impl ExprSchemable for Expr {| Expr::InList { expr, .. } => expr.nullable(input_schema),Expr::Between(Between { expr, .. }) => expr.nullable(input_schema),Expr::Column(c) => input_schema.nullable(c),+ Expr::OuterReferenceColumn(_, _) => Ok(true),Expr::Literal(value) => Ok(value.is_null()),Expr::Case(case) => {// this expression is nullable if any of the input expressions are nullablediff --git a/datafusion/expr/src/expr_visitor.rs b/datafusion/expr/src/expr_visitor.rsindex e3336a8c46..84ca6f7ed9 100644--- a/datafusion/expr/src/expr_visitor.rs+++ b/datafusion/expr/src/expr_visitor.rs@@ -134,6 +134,8 @@ impl ExprVisitable for Expr {})}Expr::Column(_)+ // Treat OuterReferenceColumn as a leaf expression+ | Expr::OuterReferenceColumn(_, _)| Expr::ScalarVariable(_, _)| Expr::Literal(_)| Expr::Exists { .. }diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rsindex 237e7a81c5..0e0cfca920 100644--- a/datafusion/expr/src/logical_plan/plan.rs+++ b/datafusion/expr/src/logical_plan/plan.rs@@ -24,7 +24,8 @@ use crate::logical_plan::display::{GraphvizVisitor, IndentVisitor};use crate::logical_plan::extension::UserDefinedLogicalNode;use crate::logical_plan::plan;use crate::utils::{- exprlist_to_fields, from_plan, grouping_set_expr_count, grouping_set_to_exprlist,+ exprlist_to_fields, find_out_reference_exprs, from_plan, grouping_set_expr_count,+ grouping_set_to_exprlist,};use crate::{build_join_schema, Expr, ExprSchemable, TableProviderFilterPushDown, TableSource,@@ -265,6 +266,31 @@ impl LogicalPlan {exprs}+ /// Returns all the out reference(correlated) expressions (recursively) in the current+ /// logical plan nodes and all its descendant nodes.+ pub fn all_out_ref_exprs(self: &LogicalPlan) -> Vec<Expr> {+ let mut exprs = vec![];+ self.inspect_expressions(|e| {+ find_out_reference_exprs(e).into_iter().for_each(|e| {+ if !exprs.contains(&e) {+ exprs.push(e)+ }+ });+ Ok(()) as Result<(), DataFusionError>+ })+ // closure always returns OK+ .unwrap();+ self.inputs()+ .into_iter()+ .flat_map(|child| child.all_out_ref_exprs())+ .for_each(|e| {+ if !exprs.contains(&e) {+ exprs.push(e)+ }+ });+ exprs+ }+/// Calls `f` on all expressions (non-recursively) in the current/// logical plan node. This does not include expressions in any/// children.@@ -748,7 +774,10 @@ impl LogicalPlan {Expr::ScalarSubquery(qry) => {let subquery =Arc::new(qry.subquery.replace_params_with_values(param_values)?);- Ok(Expr::ScalarSubquery(plan::Subquery { subquery }))+ Ok(Expr::ScalarSubquery(plan::Subquery {+ subquery,+ outer_ref_columns: qry.outer_ref_columns.clone(),+ }))}_ => Ok(expr),}@@ -1921,14 +1950,11 @@ impl Join {pub struct Subquery {/// The subquerypub subquery: Arc<LogicalPlan>,+ /// The outer references used in the subquery+ pub outer_ref_columns: Vec<Expr>,}impl Subquery {- pub fn new(plan: LogicalPlan) -> Self {- Subquery {- subquery: Arc::new(plan),- }- }pub fn try_from_expr(plan: &Expr) -> datafusion_common::Result<&Subquery> {match plan {Expr::ScalarSubquery(it) => Ok(it),diff --git a/datafusion/expr/src/utils.rs b/datafusion/expr/src/utils.rsindex ea8607feeb..d55338adc5 100644--- a/datafusion/expr/src/utils.rs+++ b/datafusion/expr/src/utils.rs@@ -134,7 +134,8 @@ pub fn expr_to_columns(expr: &Expr, accum: &mut HashSet<Column>) -> Result<()> {| Expr::Wildcard| Expr::QualifiedWildcard { .. }| Expr::GetIndexedField { .. }- | Expr::Placeholder { .. } => {}+ | Expr::Placeholder { .. }+ | Expr::OuterReferenceColumn { .. } => {}}Ok(())})@@ -382,6 +383,14 @@ pub fn find_window_exprs(exprs: &[Expr]) -> Vec<Expr> {})}+/// Collect all deeply nested `Expr::OuterReferenceColumn`. They are returned in order of occurrence+/// (depth first), with duplicates omitted.+pub fn find_out_reference_exprs(expr: &Expr) -> Vec<Expr> {+ find_exprs_in_expr(expr, &|nested_expr| {+ matches!(nested_expr, Expr::OuterReferenceColumn { .. })+ })+}+/// Search the provided `Expr`'s, and all of their nested `Expr`, for any that/// pass the provided test. The returned `Expr`'s are deduplicated and returned/// in order of appearance (depth first).@@ -637,10 +646,13 @@ pub fn from_plan(let right = inputs[1].clone();LogicalPlanBuilder::from(left).cross_join(right)?.build()}- LogicalPlan::Subquery(_) => {+ LogicalPlan::Subquery(Subquery {+ outer_ref_columns, ..+ }) => {let subquery = LogicalPlanBuilder::from(inputs[0].clone()).build()?;Ok(LogicalPlan::Subquery(Subquery {subquery: Arc::new(subquery),+ outer_ref_columns: outer_ref_columns.clone(),}))}LogicalPlan::SubqueryAlias(SubqueryAlias { alias, .. }) => {@@ -850,6 +862,7 @@ pub fn exprlist_to_fields<'a>(pub fn columnize_expr(e: Expr, input_schema: &DFSchema) -> Expr {match e {Expr::Column(_) => e,+ Expr::OuterReferenceColumn(_, _) => e,Expr::Alias(inner_expr, name) => {columnize_expr(*inner_expr, input_schema).alias(name)}diff --git a/datafusion/optimizer/src/decorrelate_where_exists.rs b/datafusion/optimizer/src/decorrelate_where_exists.rsindex ffc12c1582..35d7858ba3 100644--- a/datafusion/optimizer/src/decorrelate_where_exists.rs+++ b/datafusion/optimizer/src/decorrelate_where_exists.rs@@ -58,11 +58,14 @@ impl DecorrelateWhereExists {for it in filters.iter() {match it {Expr::Exists { subquery, negated } => {- let subquery = self+ let subquery_plan = self.try_optimize(&subquery.subquery, config)?.map(Arc::new).unwrap_or_else(|| subquery.subquery.clone());- let subquery = Subquery { subquery };+ let subquery = Subquery {+ subquery: subquery_plan,+ outer_ref_columns: subquery.outer_ref_columns.clone(),+ };let subquery = SubqueryInfo::new(subquery.clone(), *negated);subqueries.push(subquery);}diff --git a/datafusion/optimizer/src/decorrelate_where_in.rs b/datafusion/optimizer/src/decorrelate_where_in.rsindex 5eb7e99e79..3439757f08 100644--- a/datafusion/optimizer/src/decorrelate_where_in.rs+++ b/datafusion/optimizer/src/decorrelate_where_in.rs@@ -65,11 +65,14 @@ impl DecorrelateWhereIn {subquery,negated,} => {- let subquery = self+ let subquery_plan = self.try_optimize(&subquery.subquery, config)?.map(Arc::new).unwrap_or_else(|| subquery.subquery.clone());- let subquery = Subquery { subquery };+ let subquery = Subquery {+ subquery: subquery_plan,+ outer_ref_columns: subquery.outer_ref_columns.clone(),+ };let subquery =SubqueryInfo::new(subquery.clone(), (**expr).clone(), *negated);subqueries.push(subquery);@@ -149,6 +152,7 @@ fn optimize_where_in(let projection = Projection::try_from_plan(&query_info.query.subquery).map_err(|e| context!("a projection is required", e))?;let subquery_input = projection.input.clone();+ // TODO add the validate logic to Analyzerlet subquery_expr = only_or_err(projection.expr.as_slice()).map_err(|e| context!("single expression projection required", e))?;diff --git a/datafusion/optimizer/src/scalar_subquery_to_join.rs b/datafusion/optimizer/src/scalar_subquery_to_join.rsindex 34ea208b30..303d97bb4f 100644--- a/datafusion/optimizer/src/scalar_subquery_to_join.rs+++ b/datafusion/optimizer/src/scalar_subquery_to_join.rs@@ -72,11 +72,14 @@ impl ScalarSubqueryToJoin {Ok(subquery) => subquery,_ => return Ok(()),};- let subquery = self+ let subquery_plan = self.try_optimize(&subquery.subquery, config)?.map(Arc::new).unwrap_or_else(|| subquery.subquery.clone());- let subquery = Subquery { subquery };+ let subquery = Subquery {+ subquery: subquery_plan,+ outer_ref_columns: subquery.outer_ref_columns.clone(),+ };let res = SubqueryInfo::new(subquery, expr, *op, lhs);subqueries.push(res);Ok(())@@ -324,7 +327,6 @@ fn optimize_scalar(new_plan = new_plan.filter(expr)?}let new_plan = new_plan.build()?;-Ok(Some(new_plan))}diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rsindex f70e1fef4e..bfac8da643 100644--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs@@ -254,6 +254,7 @@ impl<'a> ConstEvaluator<'a> {| Expr::AggregateUDF { .. }| Expr::ScalarVariable(_, _)| Expr::Column(_)+ | Expr::OuterReferenceColumn(_, _)| Expr::Exists { .. }| Expr::InSubquery { .. }| Expr::ScalarSubquery(_)diff --git a/datafusion/optimizer/src/type_coercion.rs b/datafusion/optimizer/src/type_coercion.rsindex 7cfd6cc8d7..437d9cd47d 100644--- a/datafusion/optimizer/src/type_coercion.rs+++ b/datafusion/optimizer/src/type_coercion.rs@@ -125,14 +125,23 @@ impl ExprRewriter for TypeCoercionRewriter {fn mutate(&mut self, expr: Expr) -> Result<Expr> {match expr {- Expr::ScalarSubquery(Subquery { subquery }) => {+ Expr::ScalarSubquery(Subquery {+ subquery,+ outer_ref_columns,+ }) => {let new_plan = optimize_internal(&self.schema, &subquery)?;- Ok(Expr::ScalarSubquery(Subquery::new(new_plan)))+ Ok(Expr::ScalarSubquery(Subquery {+ subquery: Arc::new(new_plan),+ outer_ref_columns,+ }))}Expr::Exists { subquery, negated } => {let new_plan = optimize_internal(&self.schema, &subquery.subquery)?;Ok(Expr::Exists {- subquery: Subquery::new(new_plan),+ subquery: Subquery {+ subquery: Arc::new(new_plan),+ outer_ref_columns: subquery.outer_ref_columns,+ },negated,})}@@ -144,7 +153,10 @@ impl ExprRewriter for TypeCoercionRewriter {let new_plan = optimize_internal(&self.schema, &subquery.subquery)?;Ok(Expr::InSubquery {expr,- subquery: Subquery::new(new_plan),+ subquery: Subquery {+ subquery: Arc::new(new_plan),+ outer_ref_columns: subquery.outer_ref_columns,+ },negated,})}diff --git a/datafusion/optimizer/src/utils.rs b/datafusion/optimizer/src/utils.rsindex 617c123d83..235b07d9e8 100644--- a/datafusion/optimizer/src/utils.rs+++ b/datafusion/optimizer/src/utils.rs@@ -21,7 +21,9 @@ use crate::{OptimizerConfig, OptimizerRule};use datafusion_common::{plan_err, Column, DFSchemaRef, DataFusionError};use datafusion_common::{DFSchema, Result};use datafusion_expr::expr::{BinaryExpr, Sort};-use datafusion_expr::expr_rewriter::{ExprRewritable, ExprRewriter};+use datafusion_expr::expr_rewriter::{+ strip_outer_reference, ExprRewritable, ExprRewriter,+};use datafusion_expr::expr_visitor::inspect_expr_pre;use datafusion_expr::logical_plan::LogicalPlanBuilder;use datafusion_expr::utils::{check_all_columns_from_schema, from_plan};@@ -291,46 +293,52 @@ pub fn find_join_exprs(let mut joins = vec![];let mut others = vec![];for filter in exprs.iter() {- let (left, op, right) = match filter {- Expr::BinaryExpr(BinaryExpr { left, op, right }) => {- (*left.clone(), *op, *right.clone())- }- _ => {- others.push((*filter).clone());- continue;- }- };- let left = match left {- Expr::Column(c) => c,- _ => {+ // If the expression contains correlated predicates, add it to join filters+ if filter.contains_outer() {+ joins.push(strip_outer_reference((*filter).clone()));+ } else {+ // TODO remove the logic+ let (left, op, right) = match filter {+ Expr::BinaryExpr(BinaryExpr { left, op, right }) => {+ (*left.clone(), *op, *right.clone())+ }+ _ => {+ others.push((*filter).clone());+ continue;+ }+ };+ let left = match left {+ Expr::Column(c) => c,+ _ => {+ others.push((*filter).clone());+ continue;+ }+ };+ let right = match right {+ Expr::Column(c) => c,+ _ => {+ others.push((*filter).clone());+ continue;+ }+ };+ if fields.contains(&left.flat_name()) && fields.contains(&right.flat_name()) {others.push((*filter).clone());- continue;+ continue; // both columns present (none closed-upon)}- };- let right = match right {- Expr::Column(c) => c,- _ => {+ if !fields.contains(&left.flat_name()) && !fields.contains(&right.flat_name())+ {others.push((*filter).clone());- continue;+ continue; // neither column present (syntax error?)}- };- if fields.contains(&left.flat_name()) && fields.contains(&right.flat_name()) {- others.push((*filter).clone());- continue; // both columns present (none closed-upon)- }- if !fields.contains(&left.flat_name()) && !fields.contains(&right.flat_name()) {- others.push((*filter).clone());- continue; // neither column present (syntax error?)- }- match op {- Operator::Eq => {}- Operator::NotEq => {}- _ => {- plan_err!(format!("can't optimize {op} column comparison"))?;+ match op {+ Operator::Eq => {}+ Operator::NotEq => {}+ _ => {+ plan_err!(format!("can't optimize {op} column comparison"))?;+ }}+ joins.push((*filter).clone())}-- joins.push((*filter).clone())}Ok((joins, others))@@ -460,13 +468,17 @@ fn add_alias_if_changed(original_name: String, expr: Expr) -> Result<Expr> {/// merge inputs schema into a single schema.pub fn merge_schema(inputs: Vec<&LogicalPlan>) -> DFSchema {- inputs- .iter()- .map(|input| input.schema())- .fold(DFSchema::empty(), |mut lhs, rhs| {- lhs.merge(rhs);- lhs- })+ if inputs.len() == 1 {+ inputs[0].schema().clone().as_ref().clone()+ } else {+ inputs.iter().map(|input| input.schema()).fold(+ DFSchema::empty(),+ |mut lhs, rhs| {+ lhs.merge(rhs);+ lhs+ },+ )+ }}/// Extract join predicates from the correclated subquery.@@ -485,11 +497,16 @@ pub(crate) fn extract_join_filters(let mut join_filters: Vec<Expr> = vec![];let mut subquery_filters: Vec<Expr> = vec![];for expr in subquery_filter_exprs {- let cols = expr.to_columns()?;- if check_all_columns_from_schema(&cols, input_schema.clone())? {- subquery_filters.push(expr.clone());+ // If the expression contains correlated predicates, add it to join filters+ if expr.contains_outer() {+ join_filters.push(strip_outer_reference(expr.clone()))} else {- join_filters.push(expr.clone())+ let cols = expr.to_columns()?;+ if check_all_columns_from_schema(&cols, input_schema.clone())? {+ subquery_filters.push(expr.clone());+ } else {+ join_filters.push(expr.clone())+ }}}diff --git a/datafusion/proto/src/logical_plan/to_proto.rs b/datafusion/proto/src/logical_plan/to_proto.rsindex a794c9bd06..da16cd9e02 100644--- a/datafusion/proto/src/logical_plan/to_proto.rs+++ b/datafusion/proto/src/logical_plan/to_proto.rs@@ -854,10 +854,10 @@ impl TryFrom<&Expr> for protobuf::LogicalExprNode {Expr::Wildcard => Self {expr_type: Some(ExprType::Wildcard(true)),},- Expr::ScalarSubquery(_) | Expr::InSubquery { .. } | Expr::Exists { .. } => {+ Expr::ScalarSubquery(_) | Expr::InSubquery { .. } | Expr::Exists { .. } | Expr::OuterReferenceColumn{..} => {// we would need to add logical plan operators to datafusion.proto to support this// see discussion in https://github.com/apache/arrow-datafusion/issues/2565- return Err(Error::General("Proto serialization error: Expr::ScalarSubquery(_) | Expr::InSubquery { .. } | Expr::Exists { .. } not supported".to_string()));+ return Err(Error::General("Proto serialization error: Expr::ScalarSubquery(_) | Expr::InSubquery { .. } | Expr::Exists { .. } | Exp:OuterReferenceColumn not supported".to_string()));}Expr::GetIndexedField(GetIndexedField { key, expr }) =>Self {diff --git a/datafusion/sql/src/expr/function.rs b/datafusion/sql/src/expr/function.rsindex 68a5df054b..e5af0eb269 100644--- a/datafusion/sql/src/expr/function.rs+++ b/datafusion/sql/src/expr/function.rs@@ -48,7 +48,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// next, scalar built-inif let Ok(fun) = BuiltinScalarFunction::from_str(&name) {- let args = self.function_args_to_expr(function.args, schema)?;+ let args =+ self.function_args_to_expr(function.args, schema, planner_context)?;return Ok(Expr::ScalarFunction { fun, args });};@@ -62,7 +63,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let order_by = window.order_by.into_iter()- .map(|e| self.order_by_to_sort_expr(e, schema))+ .map(|e| self.order_by_to_sort_expr(e, schema, planner_context)).collect::<Result<Vec<_>>>()?;let window_frame = window.window_frame@@ -80,8 +81,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let fun = self.find_window_func(&name)?;let expr = match fun {WindowFunction::AggregateFunction(aggregate_fun) => {- let (aggregate_fun, args) =- self.aggregate_fn_to_expr(aggregate_fun, function.args, schema)?;+ let (aggregate_fun, args) = self.aggregate_fn_to_expr(+ aggregate_fun,+ function.args,+ schema,+ planner_context,+ )?;Expr::WindowFunction(expr::WindowFunction::new(WindowFunction::AggregateFunction(aggregate_fun),@@ -93,7 +98,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {}_ => Expr::WindowFunction(expr::WindowFunction::new(fun,- self.function_args_to_expr(function.args, schema)?,+ self.function_args_to_expr(function.args, schema, planner_context)?,partition_by,order_by,window_frame,@@ -105,7 +110,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// next, aggregate built-insif let Ok(fun) = AggregateFunction::from_str(&name) {let distinct = function.distinct;- let (fun, args) = self.aggregate_fn_to_expr(fun, function.args, schema)?;+ let (fun, args) =+ self.aggregate_fn_to_expr(fun, function.args, schema, planner_context)?;return Ok(Expr::AggregateFunction(expr::AggregateFunction::new(fun, args, distinct, None,)));@@ -113,13 +119,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// finally, user-defined functions (UDF) and UDAFif let Some(fm) = self.schema_provider.get_function_meta(&name) {- let args = self.function_args_to_expr(function.args, schema)?;+ let args =+ self.function_args_to_expr(function.args, schema, planner_context)?;return Ok(Expr::ScalarUDF { fun: fm, args });}// User defined aggregate functionsif let Some(fm) = self.schema_provider.get_aggregate_meta(&name) {- let args = self.function_args_to_expr(function.args, schema)?;+ let args =+ self.function_args_to_expr(function.args, schema, planner_context)?;return Ok(Expr::AggregateUDF {fun: fm,args,@@ -129,7 +137,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// Special case arrow_cast (as its type is dependent on its argument value)if name == ARROW_CAST_NAME {- let args = self.function_args_to_expr(function.args, schema)?;+ let args =+ self.function_args_to_expr(function.args, schema, planner_context)?;return super::arrow_cast::create_arrow_cast(args, schema);}@@ -189,11 +198,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,args: Vec<FunctionArg>,schema: &DFSchema,+ planner_context: &mut PlannerContext,) -> Result<Vec<Expr>> {args.into_iter()- .map(|a| {- self.sql_fn_arg_to_logical_expr(a, schema, &mut PlannerContext::new())- })+ .map(|a| self.sql_fn_arg_to_logical_expr(a, schema, planner_context)).collect::<Result<Vec<Expr>>>()}@@ -202,6 +210,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {fun: AggregateFunction,args: Vec<FunctionArg>,schema: &DFSchema,+ planner_context: &mut PlannerContext,) -> Result<(AggregateFunction, Vec<Expr>)> {let args = match fun {// Special case rewrite COUNT(*) to COUNT(constant)@@ -211,14 +220,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {FunctionArg::Unnamed(FunctionArgExpr::Wildcard) => {Ok(Expr::Literal(COUNT_STAR_EXPANSION.clone()))}- _ => self.sql_fn_arg_to_logical_expr(- a,- schema,- &mut PlannerContext::new(),- ),+ _ => self.sql_fn_arg_to_logical_expr(a, schema, planner_context),}).collect::<Result<Vec<Expr>>>()?,- _ => self.function_args_to_expr(args, schema)?,+ _ => self.function_args_to_expr(args, schema, planner_context)?,};Ok((fun, args))diff --git a/datafusion/sql/src/expr/identifier.rs b/datafusion/sql/src/expr/identifier.rsindex 7548fc8bd4..5c561270f9 100644--- a/datafusion/sql/src/expr/identifier.rs+++ b/datafusion/sql/src/expr/identifier.rs@@ -24,7 +24,12 @@ use datafusion_expr::{Case, Expr, GetIndexedField};use sqlparser::ast::{Expr as SQLExpr, Ident};impl<'a, S: ContextProvider> SqlToRel<'a, S> {- pub(super) fn sql_identifier_to_expr(&self, id: Ident) -> Result<Expr> {+ pub(super) fn sql_identifier_to_expr(+ &self,+ id: Ident,+ schema: &DFSchema,+ planner_context: &mut PlannerContext,+ ) -> Result<Expr> {if id.value.starts_with('@') {// TODO: figure out if ScalarVariables should be insensitive.let var_names = vec![id.value];@@ -42,11 +47,42 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// interpret names with '.' as if they were// compound identifiers, but this is not a compound// identifier. (e.g. it is "foo.bar" not foo.bar)-- Ok(Expr::Column(Column {- relation: None,- name: normalize_ident(id),- }))+ let normalize_ident = normalize_ident(id);+ match schema.field_with_unqualified_name(normalize_ident.as_str()) {+ Ok(_) => {+ // found a match without a qualified name, this is a inner table column+ Ok(Expr::Column(Column {+ relation: None,+ name: normalize_ident,+ }))+ }+ Err(_) => {+ // check the outer_query_schema and try to find a match+ let outer_query_schema_opt =+ planner_context.outer_query_schema.as_ref();+ if let Some(outer) = outer_query_schema_opt {+ match outer.field_with_unqualified_name(normalize_ident.as_str())+ {+ Ok(field) => {+ // found an exact match on a qualified name in the outer plan schema, so this is an outer reference column+ Ok(Expr::OuterReferenceColumn(+ field.data_type().clone(),+ field.qualified_column(),+ ))+ }+ Err(_) => Ok(Expr::Column(Column {+ relation: None,+ name: normalize_ident,+ })),+ }+ } else {+ Ok(Expr::Column(Column {+ relation: None,+ name: normalize_ident,+ }))+ }+ }+ }}}@@ -54,6 +90,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,ids: Vec<Ident>,schema: &DFSchema,+ planner_context: &mut PlannerContext,) -> Result<Expr> {if ids.len() < 2 {return Err(DataFusionError::Internal(format!(@@ -114,7 +151,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {Some((field, _nested_names)) => {Ok(Expr::Column(field.qualified_column()))}- // found no matching field, will return a defaultNone => {// return default where use all identifiers to not have a nested field// this len check is because at 5 identifiers will have to have a nested field@@ -123,11 +159,48 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {"Unsupported compound identifier: {ids:?}")))} else {- let s = &ids[0..ids.len()];- // safe unwrap as s can never be empty or exceed the bounds- let (relation, column_name) = form_identifier(s).unwrap();- let relation = relation.map(|r| r.to_owned_reference());- Ok(Expr::Column(Column::new(relation, column_name)))+ let outer_query_schema_opt =+ planner_context.outer_query_schema.as_ref();+ // check the outer_query_schema and try to find a match+ if let Some(outer) = outer_query_schema_opt {+ let search_result = search_dfschema(&ids, outer);+ match search_result {+ // found matching field with spare identifier(s) for nested field(s) in structure+ Some((field, nested_names))+ if !nested_names.is_empty() =>+ {+ // TODO: remove when can support nested identifiers for OuterReferenceColumn+ Err(DataFusionError::Internal(format!(+ "Nested identifiers are not yet supported for OuterReferenceColumn {}",+ field.qualified_column().quoted_flat_name()+ )))+ }+ // found matching field with no spare identifier(s)+ Some((field, _nested_names)) => {+ // found an exact match on a qualified name in the outer plan schema, so this is an outer reference column+ Ok(Expr::OuterReferenceColumn(+ field.data_type().clone(),+ field.qualified_column(),+ ))+ }+ // found no matching field, will return a default+ None => {+ let s = &ids[0..ids.len()];+ // safe unwrap as s can never be empty or exceed the bounds+ let (relation, column_name) =+ form_identifier(s).unwrap();+ let relation =+ relation.map(|r| r.to_owned_reference());+ Ok(Expr::Column(Column::new(relation, column_name)))+ }+ }+ } else {+ let s = &ids[0..ids.len()];+ // safe unwrap as s can never be empty or exceed the bounds+ let (relation, column_name) = form_identifier(s).unwrap();+ let relation = relation.map(|r| r.to_owned_reference());+ Ok(Expr::Column(Column::new(relation, column_name)))+ }}}}diff --git a/datafusion/sql/src/expr/mod.rs b/datafusion/sql/src/expr/mod.rsindex daad118e38..441c29775c 100644--- a/datafusion/sql/src/expr/mod.rs+++ b/datafusion/sql/src/expr/mod.rs@@ -144,7 +144,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {last_field,fractional_seconds_precision,),- SQLExpr::Identifier(id) => self.sql_identifier_to_expr(id),+ SQLExpr::Identifier(id) => self.sql_identifier_to_expr(id, schema, planner_context),SQLExpr::MapAccess { column, keys } => {if let SQLExpr::Identifier(id) = *column {@@ -161,7 +161,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {plan_indexed(expr, indexes)}- SQLExpr::CompoundIdentifier(ids) => self.sql_compound_identifier_to_expr(ids, schema),+ SQLExpr::CompoundIdentifier(ids) => self.sql_compound_identifier_to_expr(ids, schema, planner_context),SQLExpr::Case {operand,diff --git a/datafusion/sql/src/expr/order_by.rs b/datafusion/sql/src/expr/order_by.rsindex f3a4f2b043..d8c0e34f34 100644--- a/datafusion/sql/src/expr/order_by.rs+++ b/datafusion/sql/src/expr/order_by.rs@@ -27,6 +27,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,e: OrderByExpr,schema: &DFSchema,+ planner_context: &mut PlannerContext,) -> Result<Expr> {let OrderByExpr {asc,@@ -55,7 +56,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let field = schema.field(field_index - 1);Expr::Column(field.qualified_column())}- e => self.sql_expr_to_logical_expr(e, schema, &mut PlannerContext::new())?,+ e => self.sql_expr_to_logical_expr(e, schema, planner_context)?,};Ok({let asc = asc.unwrap_or(true);diff --git a/datafusion/sql/src/expr/subquery.rs b/datafusion/sql/src/expr/subquery.rsindex bb2cf30d33..8bcb7af959 100644--- a/datafusion/sql/src/expr/subquery.rs+++ b/datafusion/sql/src/expr/subquery.rs@@ -30,13 +30,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {input_schema: &DFSchema,planner_context: &mut PlannerContext,) -> Result<Expr> {+ let old_outer_query_schema = planner_context.outer_query_schema.clone();+ planner_context.outer_query_schema = Some(input_schema.clone());+ let sub_plan = self.query_to_plan(subquery, planner_context)?;+ let outer_ref_columns = sub_plan.all_out_ref_exprs();+ planner_context.outer_query_schema = old_outer_query_schema;Ok(Expr::Exists {subquery: Subquery {- subquery: Arc::new(self.subquery_to_plan(- subquery,- planner_context,- input_schema,- )?),+ subquery: Arc::new(sub_plan),+ outer_ref_columns,},negated,})@@ -50,14 +52,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {input_schema: &DFSchema,planner_context: &mut PlannerContext,) -> Result<Expr> {+ let old_outer_query_schema = planner_context.outer_query_schema.clone();+ planner_context.outer_query_schema = Some(input_schema.clone());+ let sub_plan = self.query_to_plan(subquery, planner_context)?;+ let outer_ref_columns = sub_plan.all_out_ref_exprs();+ planner_context.outer_query_schema = old_outer_query_schema;+ let expr = Box::new(self.sql_to_expr(expr, input_schema, planner_context)?);Ok(Expr::InSubquery {- expr: Box::new(self.sql_to_expr(expr, input_schema, planner_context)?),+ expr,subquery: Subquery {- subquery: Arc::new(self.subquery_to_plan(- subquery,- planner_context,- input_schema,- )?),+ subquery: Arc::new(sub_plan),+ outer_ref_columns,},negated,})@@ -69,12 +74,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {input_schema: &DFSchema,planner_context: &mut PlannerContext,) -> Result<Expr> {+ let old_outer_query_schema = planner_context.outer_query_schema.clone();+ planner_context.outer_query_schema = Some(input_schema.clone());+ let sub_plan = self.query_to_plan(subquery, planner_context)?;+ let outer_ref_columns = sub_plan.all_out_ref_exprs();+ planner_context.outer_query_schema = old_outer_query_schema;Ok(Expr::ScalarSubquery(Subquery {- subquery: Arc::new(self.subquery_to_plan(- subquery,- planner_context,- input_schema,- )?),+ subquery: Arc::new(sub_plan),+ outer_ref_columns,}))}}diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rsindex ac3a34cab6..87c090e3ee 100644--- a/datafusion/sql/src/planner.rs+++ b/datafusion/sql/src/planner.rs@@ -70,13 +70,18 @@ impl Default for ParserOptions {}#[derive(Debug, Clone)]-/// Struct to store Common Table Expression (CTE) provided with WITH clause and-/// Parameter Data Types provided with PREPARE statement+/// Struct to store the states used by the Planner. The Planner will leverage the states to resolve+/// CTEs, Views, subqueries and PREPARE statements. The states include+/// Common Table Expression (CTE) provided with WITH clause and+/// Parameter Data Types provided with PREPARE statement and the query schema of the+/// outer query planpub struct PlannerContext {/// Data type provided with prepare statementpub prepare_param_data_types: Vec<DataType>,/// Map of CTE name to logical plan of the WITH clausepub ctes: HashMap<String, LogicalPlan>,+ /// The query schema of the outer query plan, used to resolve the columns in subquery+ pub outer_query_schema: Option<DFSchema>,}impl Default for PlannerContext {@@ -91,6 +96,7 @@ impl PlannerContext {Self {prepare_param_data_types: vec![],ctes: HashMap::new(),+ outer_query_schema: None,}}@@ -101,6 +107,7 @@ impl PlannerContext {Self {prepare_param_data_types,ctes: HashMap::new(),+ outer_query_schema: None,}}}diff --git a/datafusion/sql/src/query.rs b/datafusion/sql/src/query.rsindex eb7ece87d6..df64888f58 100644--- a/datafusion/sql/src/query.rs+++ b/datafusion/sql/src/query.rs@@ -17,7 +17,7 @@use crate::planner::{ContextProvider, PlannerContext, SqlToRel};use crate::utils::normalize_ident;-use datafusion_common::{DFSchema, DataFusionError, Result, ScalarValue};+use datafusion_common::{DataFusionError, Result, ScalarValue};use datafusion_expr::{Expr, LogicalPlan, LogicalPlanBuilder};use sqlparser::ast::{Expr as SQLExpr, Offset as SQLOffset, OrderByExpr, Query};@@ -30,17 +30,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {query: Query,planner_context: &mut PlannerContext,) -> Result<LogicalPlan> {- self.query_to_plan_with_schema(query, planner_context, None)- }-- /// Generate a logical plan from a SQL subquery- pub(crate) fn subquery_to_plan(- &self,- query: Query,- planner_context: &mut PlannerContext,- outer_query_schema: &DFSchema,- ) -> Result<LogicalPlan> {- self.query_to_plan_with_schema(query, planner_context, Some(outer_query_schema))+ self.query_to_plan_with_schema(query, planner_context)}/// Generate a logic plan from an SQL query.@@ -50,7 +40,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,query: Query,planner_context: &mut PlannerContext,- outer_query_schema: Option<&DFSchema>,) -> Result<LogicalPlan> {let set_expr = query.body;if let Some(with) = query.with {@@ -82,9 +71,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {planner_context.ctes.insert(cte_name, logical_plan);}}- let plan =- self.set_expr_to_plan(*set_expr, planner_context, outer_query_schema)?;- let plan = self.order_by(plan, query.order_by)?;+ let plan = self.set_expr_to_plan(*set_expr, planner_context)?;+ let plan = self.order_by(plan, query.order_by, planner_context)?;self.limit(plan, query.offset, query.limit)}@@ -145,6 +133,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,plan: LogicalPlan,order_by: Vec<OrderByExpr>,+ planner_context: &mut PlannerContext,) -> Result<LogicalPlan> {if order_by.is_empty() {return Ok(plan);@@ -152,7 +141,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let order_by_rex = order_by.into_iter()- .map(|e| self.order_by_to_sort_expr(e, plan.schema()))+ .map(|e| self.order_by_to_sort_expr(e, plan.schema(), planner_context)).collect::<Result<Vec<_>>>()?;LogicalPlanBuilder::from(plan).sort(order_by_rex)?.build()diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rsindex bdf9a7c302..cde4946571 100644--- a/datafusion/sql/src/select.rs+++ b/datafusion/sql/src/select.rs@@ -20,7 +20,7 @@ use crate::utils::{check_columns_satisfy_exprs, extract_aliases, normalize_ident, rebase_expr,resolve_aliases_to_exprs, resolve_columns, resolve_positions_to_exprs,};-use datafusion_common::{DFSchema, DataFusionError, Result};+use datafusion_common::{DataFusionError, Result};use datafusion_expr::expr_rewriter::{normalize_col, normalize_col_with_schemas_and_ambiguity_check,};@@ -44,7 +44,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,select: Select,planner_context: &mut PlannerContext,- outer_query_schema: Option<&DFSchema>,) -> Result<LogicalPlan> {// check for unsupported syntax firstif !select.cluster_by.is_empty() {@@ -68,12 +67,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let empty_from = matches!(plan, LogicalPlan::EmptyRelation(_));// process `where` clause- let plan = self.plan_selection(- select.selection,- plan,- outer_query_schema,- planner_context,- )?;+ let plan = self.plan_selection(select.selection, plan, planner_context)?;// process the SELECT expressions, with wildcards expanded.let select_exprs = self.prepare_select_exprs(@@ -234,28 +228,26 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,selection: Option<SQLExpr>,plan: LogicalPlan,- outer_query_schema: Option<&DFSchema>,planner_context: &mut PlannerContext,) -> Result<LogicalPlan> {match selection {Some(predicate_expr) => {- let mut join_schema = (**plan.schema()).clone();-let fallback_schemas = plan.fallback_normalize_schemas();- let outer_query_schema = if let Some(outer) = outer_query_schema {- join_schema.merge(outer);- vec![outer]- } else {- vec![]- };+ let outer_query_schema = planner_context.outer_query_schema.clone();+ let outer_query_schema_vec =+ if let Some(outer) = outer_query_schema.as_ref() {+ vec![outer]+ } else {+ vec![]+ };let filter_expr =- self.sql_to_expr(predicate_expr, &join_schema, planner_context)?;+ self.sql_to_expr(predicate_expr, plan.schema(), planner_context)?;let mut using_columns = HashSet::new();expr_to_columns(&filter_expr, &mut using_columns)?;let filter_expr = normalize_col_with_schemas_and_ambiguity_check(filter_expr,- &[&[plan.schema()], &fallback_schemas, &outer_query_schema],+ &[&[plan.schema()], &fallback_schemas, &outer_query_schema_vec],&[using_columns],)?;diff --git a/datafusion/sql/src/set_expr.rs b/datafusion/sql/src/set_expr.rsindex d870501769..b615f04442 100644--- a/datafusion/sql/src/set_expr.rs+++ b/datafusion/sql/src/set_expr.rs@@ -16,7 +16,7 @@// under the License.use crate::planner::{ContextProvider, PlannerContext, SqlToRel};-use datafusion_common::{DFSchema, DataFusionError, Result};+use datafusion_common::{DataFusionError, Result};use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};use sqlparser::ast::{SetExpr, SetOperator, SetQuantifier};@@ -25,12 +25,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,set_expr: SetExpr,planner_context: &mut PlannerContext,- outer_query_schema: Option<&DFSchema>,) -> Result<LogicalPlan> {match set_expr {- SetExpr::Select(s) => {- self.select_to_plan(*s, planner_context, outer_query_schema)- }+ SetExpr::Select(s) => self.select_to_plan(*s, planner_context),SetExpr::Values(v) => {self.sql_values_to_plan(v, &planner_context.prepare_param_data_types)}@@ -45,10 +42,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {SetQuantifier::Distinct | SetQuantifier::None => false,};- let left_plan =- self.set_expr_to_plan(*left, planner_context, outer_query_schema)?;- let right_plan =- self.set_expr_to_plan(*right, planner_context, outer_query_schema)?;+ let left_plan = self.set_expr_to_plan(*left, planner_context)?;+ let right_plan = self.set_expr_to_plan(*right, planner_context)?;match (op, all) {(SetOperator::Union, true) => LogicalPlanBuilder::from(left_plan).union(right_plan)?diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rsindex 8d5b46d29c..e32443e01a 100644--- a/datafusion/sql/src/statement.rs+++ b/datafusion/sql/src/statement.rs@@ -427,6 +427,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {&self,order_exprs: Vec<OrderByExpr>,schema: &DFSchemaRef,+ planner_context: &mut PlannerContext,) -> Result<Vec<datafusion_expr::Expr>> {// Ask user to provide a schema if schema is empty.if !order_exprs.is_empty() && schema.fields().is_empty() {@@ -438,7 +439,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {// Convert each OrderByExpr to a SortExpr:let result = order_exprs.into_iter()- .map(|e| self.order_by_to_sort_expr(e, schema))+ .map(|e| self.order_by_to_sort_expr(e, schema, planner_context)).collect::<Result<Vec<_>>>()?;// Verify that columns of all SortExprs exist in the schema:for expr in result.iter() {@@ -496,7 +497,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let schema = self.build_schema(columns)?;let df_schema = schema.to_dfschema_ref()?;- let ordered_exprs = self.build_order_by(order_exprs, &df_schema)?;+ let ordered_exprs =+ self.build_order_by(order_exprs, &df_schema, &mut PlannerContext::new())?;// External tables do not support schemas at the moment, so the name is just a table namelet name = OwnedTableReference::bare(name);diff --git a/datafusion/sql/src/utils.rs b/datafusion/sql/src/utils.rsindex a7f7a3dd69..91cef6d471 100644--- a/datafusion/sql/src/utils.rs+++ b/datafusion/sql/src/utils.rs@@ -361,6 +361,7 @@ where*nulls_first,))),Expr::Column { .. }+ | Expr::OuterReferenceColumn(_, _)| Expr::Literal(_)| Expr::ScalarVariable(_, _)| Expr::Exists { .. }diff --git a/datafusion/sql/tests/integration_test.rs b/datafusion/sql/tests/integration_test.rsindex 4e2f22caba..a77c1f1f96 100644--- a/datafusion/sql/tests/integration_test.rs+++ b/datafusion/sql/tests/integration_test.rs@@ -2646,7 +2646,7 @@ fn exists_subquery() {\n Filter: EXISTS (<subquery>)\\n Subquery:\\n Projection: person.first_name\- \n Filter: person.last_name = p.last_name AND person.state = p.state\+ \n Filter: person.last_name = outer_ref(p.last_name) AND person.state = outer_ref(p.state)\\n TableScan: person\\n SubqueryAlias: p\\n TableScan: person";@@ -2667,7 +2667,7 @@ fn exists_subquery_schema_outer_schema_overlap() {\n Filter: person.id = p.id AND EXISTS (<subquery>)\\n Subquery:\\n Projection: person.first_name\- \n Filter: person.id = p2.id AND person.last_name = p.last_name AND person.state = p.state\+ \n Filter: person.id = p2.id AND person.last_name = outer_ref(p.last_name) AND person.state = outer_ref(p.state)\\n CrossJoin:\\n TableScan: person\\n SubqueryAlias: p2\@@ -2690,7 +2690,7 @@ fn exists_subquery_wildcard() {\n Filter: EXISTS (<subquery>)\\n Subquery:\\n Projection: person.id, person.first_name, person.last_name, person.age, person.state, person.salary, person.birth_date, person.π\- \n Filter: person.last_name = p.last_name AND person.state = p.state\+ \n Filter: person.last_name = outer_ref(p.last_name) AND person.state = outer_ref(p.state)\\n TableScan: person\\n SubqueryAlias: p\\n TableScan: person";@@ -2721,7 +2721,7 @@ fn not_in_subquery_correlated() {\n Filter: p.id NOT IN (<subquery>)\\n Subquery:\\n Projection: person.id\- \n Filter: person.last_name = p.last_name AND person.state = Utf8(\"CO\")\+ \n Filter: person.last_name = outer_ref(p.last_name) AND person.state = Utf8(\"CO\")\\n TableScan: person\\n SubqueryAlias: p\\n TableScan: person";@@ -2736,7 +2736,7 @@ fn scalar_subquery() {\n Subquery:\\n Projection: MAX(person.id)\\n Aggregate: groupBy=[[]], aggr=[[MAX(person.id)]]\- \n Filter: person.last_name = p.last_name\+ \n Filter: person.last_name = outer_ref(p.last_name)\\n TableScan: person\\n SubqueryAlias: p\\n TableScan: person";@@ -2758,7 +2758,7 @@ fn scalar_subquery_reference_outer_field() {\n Subquery:\\n Projection: COUNT(UInt8(1))\\n Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\- \n Filter: j2.j2_id = j1.j1_id AND j1.j1_id = j3.j3_id\+ \n Filter: outer_ref(j2.j2_id) = j1.j1_id AND j1.j1_id = j3.j3_id\\n CrossJoin:\\n TableScan: j1\\n TableScan: j3\@@ -2779,7 +2779,7 @@ fn subquery_references_cte() {\n Filter: EXISTS (<subquery>)\\n Subquery:\\n Projection: cte.id, cte.first_name, cte.last_name, cte.age, cte.state, cte.salary, cte.birth_date, cte.π\- \n Filter: cte.id = person.id\+ \n Filter: cte.id = outer_ref(person.id)\\n SubqueryAlias: cte\\n Projection: person.id, person.first_name, person.last_name, person.age, person.state, person.salary, person.birth_date, person.π\\n TableScan: person\