Apache DataFusion SQL Query Engine
fix: shouldn't pass alias through into subquery. (#4141)
jakevind91ce0684646
diff --git a/benchmarks/expected-plans/q13.txt b/benchmarks/expected-plans/q13.txtindex 12a9bf1dac..b6106a9711 100644--- a/benchmarks/expected-plans/q13.txt+++ b/benchmarks/expected-plans/q13.txt@@ -2,8 +2,8 @@ Sort: custdist DESC NULLS FIRST, c_orders.c_count DESC NULLS FIRSTProjection: c_orders.c_count, COUNT(UInt8(1)) AS custdistAggregate: groupBy=[[c_orders.c_count]], aggr=[[COUNT(UInt8(1))]]Projection: c_orders.COUNT(orders.o_orderkey) AS c_count, alias=c_orders- Projection: c_orders.COUNT(orders.o_orderkey), alias=c_orders- Projection: COUNT(orders.o_orderkey), alias=c_orders+ Projection: COUNT(orders.o_orderkey), alias=c_orders+ Projection: COUNT(orders.o_orderkey)Aggregate: groupBy=[[customer.c_custkey]], aggr=[[COUNT(orders.o_orderkey)]]Left Join: customer.c_custkey = orders.o_custkeyTableScan: customer projection=[c_custkey]diff --git a/benchmarks/expected-plans/q22.txt b/benchmarks/expected-plans/q22.txtindex b56c8ff96f..81c12ba91a 100644--- a/benchmarks/expected-plans/q22.txt+++ b/benchmarks/expected-plans/q22.txt@@ -1,8 +1,8 @@Sort: custsale.cntrycode ASC NULLS LASTProjection: custsale.cntrycode, COUNT(UInt8(1)) AS numcust, SUM(custsale.c_acctbal) AS totacctbalAggregate: groupBy=[[custsale.cntrycode]], aggr=[[COUNT(UInt8(1)), SUM(custsale.c_acctbal)]]- Projection: custsale.cntrycode, custsale.c_acctbal, alias=custsale- Projection: substr(customer.c_phone, Int64(1), Int64(2)) AS cntrycode, customer.c_acctbal, alias=custsale+ Projection: cntrycode, customer.c_acctbal, alias=custsale+ Projection: substr(customer.c_phone, Int64(1), Int64(2)) AS cntrycode, customer.c_acctbalFilter: CAST(customer.c_acctbal AS Decimal128(19, 6)) > __sq_1.__valueCrossJoin:LeftAnti Join: customer.c_custkey = orders.o_custkeydiff --git a/benchmarks/expected-plans/q7.txt b/benchmarks/expected-plans/q7.txtindex fad02c0988..3a5d88965a 100644--- a/benchmarks/expected-plans/q7.txt+++ b/benchmarks/expected-plans/q7.txt@@ -1,8 +1,8 @@Sort: shipping.supp_nation ASC NULLS LAST, shipping.cust_nation ASC NULLS LAST, shipping.l_year ASC NULLS LASTProjection: shipping.supp_nation, shipping.cust_nation, shipping.l_year, SUM(shipping.volume) AS revenueAggregate: groupBy=[[shipping.supp_nation, shipping.cust_nation, shipping.l_year]], aggr=[[SUM(shipping.volume)]]- Projection: shipping.supp_nation, shipping.cust_nation, shipping.l_year, shipping.volume, alias=shipping- Projection: n1.n_name AS supp_nation, n2.n_name AS cust_nation, datepart(Utf8("YEAR"), lineitem.l_shipdate) AS l_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) AS volume, alias=shipping+ Projection: supp_nation, cust_nation, l_year, volume, alias=shipping+ Projection: n1.n_name AS supp_nation, n2.n_name AS cust_nation, datepart(Utf8("YEAR"), lineitem.l_shipdate) AS l_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) AS volumeFilter: (n1.n_name = Utf8("FRANCE") OR n2.n_name = Utf8("FRANCE")) AND (n2.n_name = Utf8("GERMANY") OR n1.n_name = Utf8("GERMANY"))Inner Join: customer.c_nationkey = n2.n_nationkeyInner Join: supplier.s_nationkey = n1.n_nationkeydiff --git a/benchmarks/expected-plans/q8.txt b/benchmarks/expected-plans/q8.txtindex 20452d4bde..3f5a876808 100644--- a/benchmarks/expected-plans/q8.txt+++ b/benchmarks/expected-plans/q8.txt@@ -1,8 +1,8 @@Sort: all_nations.o_year ASC NULLS LASTProjection: all_nations.o_year, SUM(CASE WHEN all_nations.nation = Utf8("BRAZIL") THEN all_nations.volume ELSE Int64(0) END) / SUM(all_nations.volume) AS mkt_shareAggregate: groupBy=[[all_nations.o_year]], aggr=[[SUM(CASE WHEN all_nations.nation = Utf8("BRAZIL") THEN all_nations.volume ELSE Decimal128(Some(0),38,4) END) AS SUM(CASE WHEN all_nations.nation = Utf8("BRAZIL") THEN all_nations.volume ELSE Int64(0) END), SUM(all_nations.volume)]]- Projection: all_nations.o_year, all_nations.volume, all_nations.nation, alias=all_nations- Projection: datepart(Utf8("YEAR"), orders.o_orderdate) AS o_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) AS volume, n2.n_name AS nation, alias=all_nations+ Projection: o_year, volume, nation, alias=all_nations+ Projection: datepart(Utf8("YEAR"), orders.o_orderdate) AS o_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) AS volume, n2.n_name AS nationInner Join: n1.n_regionkey = region.r_regionkeyInner Join: supplier.s_nationkey = n2.n_nationkeyInner Join: customer.c_nationkey = n1.n_nationkeydiff --git a/benchmarks/expected-plans/q9.txt b/benchmarks/expected-plans/q9.txtindex 954c28a35b..339db70175 100644--- a/benchmarks/expected-plans/q9.txt+++ b/benchmarks/expected-plans/q9.txt@@ -1,8 +1,8 @@Sort: profit.nation ASC NULLS LAST, profit.o_year DESC NULLS FIRSTProjection: profit.nation, profit.o_year, SUM(profit.amount) AS sum_profitAggregate: groupBy=[[profit.nation, profit.o_year]], aggr=[[SUM(profit.amount)]]- Projection: profit.nation, profit.o_year, profit.amount, alias=profit- Projection: nation.n_name AS nation, datepart(Utf8("YEAR"), orders.o_orderdate) AS o_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) - CAST(partsupp.ps_supplycost * lineitem.l_quantity AS Decimal128(38, 4)) AS amount, alias=profit+ Projection: nation, o_year, amount, alias=profit+ Projection: nation.n_name AS nation, datepart(Utf8("YEAR"), orders.o_orderdate) AS o_year, CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4)) - CAST(partsupp.ps_supplycost * lineitem.l_quantity AS Decimal128(38, 4)) AS amountInner Join: supplier.s_nationkey = nation.n_nationkeyInner Join: lineitem.l_orderkey = orders.o_orderkeyInner Join: lineitem.l_suppkey = partsupp.ps_suppkey, lineitem.l_partkey = partsupp.ps_partkeydiff --git a/datafusion/core/tests/sql/explain_analyze.rs b/datafusion/core/tests/sql/explain_analyze.rsindex ca13d61e98..693a99984c 100644--- a/datafusion/core/tests/sql/explain_analyze.rs+++ b/datafusion/core/tests/sql/explain_analyze.rs@@ -64,7 +64,7 @@ async fn explain_analyze_baseline_metrics() {);assert_metrics!(&formatted,- "SortExec: [c1@0 ASC NULLS LAST]",+ "SortExec: [c1@1 ASC NULLS LAST]","metrics=[output_rows=5, elapsed_compute=");assert_metrics!(@@ -746,7 +746,7 @@ async fn test_physical_plan_display_indent_multi_children() {" ProjectionExec: expr=[c2@0 as c2]"," ProjectionExec: expr=[c1@0 as c2]"," RepartitionExec: partitioning=RoundRobinBatch(9000)",- " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1]",+ " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1, c2]",];let normalizer = ExplainNormalizer::new();diff --git a/datafusion/core/tests/sql/joins.rs b/datafusion/core/tests/sql/joins.rsindex ae1e896469..10d0240253 100644--- a/datafusion/core/tests/sql/joins.rs+++ b/datafusion/core/tests/sql/joins.rs@@ -1521,8 +1521,8 @@ async fn reduce_left_join_3() -> Result<()> {"Explain [plan_type:Utf8, plan:Utf8]"," Projection: t3.t1_id, t3.t1_name, t3.t1_int, t2.t2_id, t2.t2_name, t2.t2_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]"," Left Join: t3.t1_int = t2.t2_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",- " Projection: t3.t1_id, t3.t1_name, t3.t1_int, alias=t3 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",- " Projection: t1.t1_id, t1.t1_name, t1.t1_int, alias=t3 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",+ " Projection: t1.t1_id, t1.t1_name, t1.t1_int, alias=t3 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",+ " Projection: t1.t1_id, t1.t1_name, t1.t1_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]"," Inner Join: 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]"," Filter: CAST(t1.t1_id AS Int64) < Int64(100) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]"," TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",diff --git a/datafusion/core/tests/sql/subqueries.rs b/datafusion/core/tests/sql/subqueries.rsindex 4fb97d5eb3..064ef3a35e 100644--- a/datafusion/core/tests/sql/subqueries.rs+++ b/datafusion/core/tests/sql/subqueries.rs@@ -382,8 +382,8 @@ order by cntrycode;"#;let expected = r#"Sort: custsale.cntrycode ASC NULLS LASTProjection: custsale.cntrycode, COUNT(UInt8(1)) AS numcust, SUM(custsale.c_acctbal) AS totacctbalAggregate: groupBy=[[custsale.cntrycode]], aggr=[[COUNT(UInt8(1)), SUM(custsale.c_acctbal)]]- Projection: custsale.cntrycode, custsale.c_acctbal, alias=custsale- Projection: substr(customer.c_phone, Int64(1), Int64(2)) AS cntrycode, customer.c_acctbal, alias=custsale+ Projection: cntrycode, customer.c_acctbal, alias=custsale+ Projection: substr(customer.c_phone, Int64(1), Int64(2)) AS cntrycode, customer.c_acctbalFilter: CAST(customer.c_acctbal AS Decimal128(19, 6)) > __sq_1.__valueCrossJoin:LeftAnti Join: customer.c_custkey = orders.o_custkeydiff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rsindex 0fe8042e00..bd698b505a 100644--- a/datafusion/sql/src/planner.rs+++ b/datafusion/sql/src/planner.rs@@ -781,7 +781,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let normalized_alias = alias.as_ref().map(|a| normalize_ident(&a.name));let logical_plan = self.query_to_plan_with_alias(*subquery,- normalized_alias.clone(),+ None,ctes,outer_query_schema,)?;@@ -3193,10 +3193,10 @@ mod tests {) AS a) AS b";let expected = "Projection: b.fn2, b.last_name\- \n Projection: b.fn2, b.last_name, b.birth_date, alias=b\- \n Projection: a.fn1 AS fn2, a.last_name, a.birth_date, alias=b\- \n Projection: a.fn1, a.last_name, a.birth_date, a.age, alias=a\- \n Projection: person.first_name AS fn1, person.last_name, person.birth_date, person.age, alias=a\+ \n Projection: fn2, a.last_name, a.birth_date, alias=b\+ \n Projection: a.fn1 AS fn2, a.last_name, a.birth_date\+ \n Projection: fn1, person.last_name, person.birth_date, person.age, alias=a\+ \n Projection: person.first_name AS fn1, person.last_name, person.birth_date, person.age\\n TableScan: person";quick_test(sql, expected);}@@ -3213,8 +3213,8 @@ mod tests {let expected = "Projection: a.fn1, a.age\\n Filter: a.fn1 = Utf8(\"X\") AND a.age < Int64(30)\- \n Projection: a.fn1, a.age, alias=a\- \n Projection: person.first_name AS fn1, person.age, alias=a\+ \n Projection: fn1, person.age, alias=a\+ \n Projection: person.first_name AS fn1, person.age\\n Filter: person.age > Int64(20)\\n TableScan: person";@@ -3564,8 +3564,8 @@ mod tests {"SELECT * FROM (SELECT first_name, last_name FROM person) AS a GROUP BY first_name, last_name","Projection: a.first_name, a.last_name\\n Aggregate: groupBy=[[a.first_name, a.last_name]], aggr=[[]]\- \n Projection: a.first_name, a.last_name, alias=a\- \n Projection: person.first_name, person.last_name, alias=a\+ \n Projection: person.first_name, person.last_name, alias=a\+ \n Projection: person.first_name, person.last_name\\n TableScan: person",);}@@ -4473,13 +4473,13 @@ mod tests {\n Union\\n Projection: CAST(x.a AS Float64) AS a\\n Aggregate: groupBy=[[x.a]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Int64(1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Int64(1) AS a\\n EmptyRelation\\n Projection: x.a\\n Aggregate: groupBy=[[x.a]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Float64(1.1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Float64(1.1) AS a\\n EmptyRelation";quick_test(sql, expected);}@@ -4490,13 +4490,13 @@ mod tests {let expected = "Union\\n Projection: CAST(Float64(0) + x.a AS Float64) AS Float64(0) + x.a\\n Aggregate: groupBy=[[CAST(Float64(0) + x.a AS Int32)]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Int64(1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Int64(1) AS a\\n EmptyRelation\\n Projection: Float64(2.1) + x.a\\n Aggregate: groupBy=[[Float64(2.1) + x.a]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Int64(1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Int64(1) AS a\\n EmptyRelation";quick_test(sql, expected);}@@ -4507,13 +4507,13 @@ mod tests {let expected = "Union\\n Projection: CAST(x.a AS Float64) AS a1\\n Aggregate: groupBy=[[x.a]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Int64(1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Int64(1) AS a\\n EmptyRelation\\n Projection: x.a AS a1\\n Aggregate: groupBy=[[x.a]], aggr=[[]]\- \n Projection: x.a, alias=x\- \n Projection: Float64(1.1) AS a, alias=x\+ \n Projection: a, alias=x\+ \n Projection: Float64(1.1) AS a\\n EmptyRelation";quick_test(sql, expected);}