avantgardnerio commented on code in PR #2885: URL: https://github.com/apache/arrow-datafusion/pull/2885#discussion_r926071237
########## datafusion/core/tests/sql/subqueries.rs: ########## @@ -0,0 +1,461 @@ +// 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 super::*; +use crate::sql::execute_to_batches; +use datafusion::assert_batches_eq; +use datafusion::prelude::SessionContext; +use log::debug; + +#[cfg(test)] +#[ctor::ctor] +fn init() { + let _ = env_logger::try_init(); +} + +#[tokio::test] +async fn correlated_recursive_scalar_subquery() -> Result<()> { + let ctx = SessionContext::new(); + register_tpch_csv(&ctx, "customer").await?; + register_tpch_csv(&ctx, "orders").await?; + register_tpch_csv(&ctx, "lineitem").await?; + + let sql = r#" +select c_custkey from customer +where c_acctbal < ( + select sum(o_totalprice) from orders + where o_custkey = c_custkey + and o_totalprice < ( + select sum(l_extendedprice) as price from lineitem where l_orderkey = o_orderkey + ) +) order by c_custkey;"#; + + // assert plan + let plan = ctx.create_logical_plan(sql).unwrap(); + debug!("input:\n{}", plan.display_indent()); + + let plan = ctx.optimize(&plan).unwrap(); + let actual = format!("{}", plan.display_indent()); + let expected = r#"Sort: #customer.c_custkey ASC NULLS LAST + Projection: #customer.c_custkey + Filter: #customer.c_acctbal < #__sq_2.__value + Inner Join: #customer.c_custkey = #__sq_2.o_custkey + TableScan: customer projection=[c_custkey, c_acctbal] + Projection: #orders.o_custkey, #SUM(orders.o_totalprice) AS __value, alias=__sq_2 + Aggregate: groupBy=[[#orders.o_custkey]], aggr=[[SUM(#orders.o_totalprice)]] + Filter: #orders.o_totalprice < #__sq_1.__value + Inner Join: #orders.o_orderkey = #__sq_1.l_orderkey + TableScan: orders projection=[o_orderkey, o_custkey, o_totalprice] + Projection: #lineitem.l_orderkey, #SUM(lineitem.l_extendedprice) AS price AS __value, alias=__sq_1 + Aggregate: groupBy=[[#lineitem.l_orderkey]], aggr=[[SUM(#lineitem.l_extendedprice)]] + TableScan: lineitem projection=[l_orderkey, l_extendedprice]"# + .to_string(); + assert_eq!(actual, expected); + + Ok(()) +} + +#[tokio::test] +async fn correlated_where_in() -> Result<()> { + let ctx = SessionContext::new(); + register_tpch_csv(&ctx, "orders").await?; + register_tpch_csv(&ctx, "lineitem").await?; + register_tpch_csv(&ctx, "partsupp").await?; + + let sql = r#"select o_orderkey from orders +inner join lineitem on o_orderkey = l_orderkey +where l_partkey in ( select ps_partkey from partsupp where ps_suppkey = l_suppkey );"#; + + // assert plan + let plan = ctx.create_logical_plan(sql).unwrap(); + let plan = ctx.optimize(&plan).unwrap(); + let actual = format!("{}", plan.display_indent()); + let expected = r#"Projection: #orders.o_orderkey + Semi Join: #lineitem.l_partkey = #__sq_1.ps_partkey, #lineitem.l_suppkey = #__sq_1.ps_suppkey + Inner Join: #orders.o_orderkey = #lineitem.l_orderkey + TableScan: orders projection=[o_orderkey] + TableScan: lineitem projection=[l_orderkey, l_partkey, l_suppkey] + Projection: #partsupp.ps_partkey AS ps_partkey, #partsupp.ps_suppkey AS ps_suppkey, alias=__sq_1 + TableScan: partsupp projection=[ps_partkey, ps_suppkey]"# + .to_string(); + assert_eq!(actual, expected); + + // assert data + let results = execute_to_batches(&ctx, sql).await; + let expected = vec!["++", "++"]; Review Comment: Correction, you found a bug, thank you! I've pushed a test that catches it and I'll fix it tomorrow. -- 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]
