Skip to content

Commit ee77d58

Browse files
authored
fix: unparse for subqueryalias (#15068)
* fix: unparse for subqueryalias * update * move test
1 parent ed2c1ca commit ee77d58

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

datafusion/sql/src/unparser/plan.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,18 @@ impl Unparser<'_> {
984984
Ok(Some(builder.build()?))
985985
}
986986
LogicalPlan::SubqueryAlias(subquery_alias) => {
987-
Self::unparse_table_scan_pushdown(
987+
let ret = Self::unparse_table_scan_pushdown(
988988
&subquery_alias.input,
989989
Some(subquery_alias.alias.clone()),
990990
already_projected,
991-
)
991+
)?;
992+
if let Some(alias) = alias {
993+
if let Some(plan) = ret {
994+
let plan = LogicalPlanBuilder::new(plan).alias(alias)?.build()?;
995+
return Ok(Some(plan));
996+
}
997+
}
998+
Ok(ret)
992999
}
9931000
// SubqueryAlias could be rewritten to a plan with a projection as the top node by [rewrite::subquery_alias_inner_query_and_columns].
9941001
// The inner table scan could be a scan with pushdown operations.

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use datafusion_expr::test::function_stub::{
2121
count_udaf, max_udaf, min_udaf, sum, sum_udaf,
2222
};
2323
use datafusion_expr::{
24-
col, lit, table_scan, wildcard, EmptyRelation, Expr, Extension, LogicalPlan,
24+
cast, col, lit, table_scan, wildcard, EmptyRelation, Expr, Extension, LogicalPlan,
2525
LogicalPlanBuilder, Union, UserDefinedLogicalNode, UserDefinedLogicalNodeCore,
2626
};
2727
use datafusion_functions::unicode;
@@ -37,6 +37,7 @@ use datafusion_sql::unparser::dialect::{
3737
use datafusion_sql::unparser::{expr_to_sql, plan_to_sql, Unparser};
3838
use sqlparser::ast::Statement;
3939
use std::hash::Hash;
40+
use std::ops::Add;
4041
use std::sync::Arc;
4142
use std::{fmt, vec};
4243

@@ -1687,3 +1688,66 @@ fn test_unparse_optimized_multi_union() -> Result<()> {
16871688

16881689
Ok(())
16891690
}
1691+
1692+
/// Test unparse the optimized plan from the following SQL:
1693+
/// ```
1694+
/// SELECT
1695+
/// customer_view.c_custkey,
1696+
/// customer_view.c_name,
1697+
/// customer_view.custkey_plus
1698+
/// FROM
1699+
/// (
1700+
/// SELECT
1701+
/// customer.c_custkey,
1702+
/// customer.c_name,
1703+
/// customer.custkey_plus
1704+
/// FROM
1705+
/// (
1706+
/// SELECT
1707+
/// customer.c_custkey,
1708+
/// CAST(customer.c_custkey AS BIGINT) + 1 AS custkey_plus,
1709+
/// customer.c_name
1710+
/// FROM
1711+
/// (
1712+
/// SELECT
1713+
/// customer.c_custkey AS c_custkey,
1714+
/// customer.c_name AS c_name
1715+
/// FROM
1716+
/// customer
1717+
/// ) AS customer
1718+
/// ) AS customer
1719+
/// ) AS customer_view
1720+
/// ```
1721+
#[test]
1722+
fn test_unparse_subquery_alias_with_table_pushdown() -> Result<()> {
1723+
let schema = Schema::new(vec![
1724+
Field::new("c_custkey", DataType::Int32, false),
1725+
Field::new("c_name", DataType::Utf8, false),
1726+
]);
1727+
1728+
let table_scan = table_scan(Some("customer"), &schema, Some(vec![0, 1]))?.build()?;
1729+
1730+
let plan = LogicalPlanBuilder::from(table_scan)
1731+
.alias("customer")?
1732+
.project(vec![
1733+
col("customer.c_custkey"),
1734+
cast(col("customer.c_custkey"), DataType::Int64)
1735+
.add(lit(1))
1736+
.alias("custkey_plus"),
1737+
col("customer.c_name"),
1738+
])?
1739+
.alias("customer")?
1740+
.project(vec![
1741+
col("customer.c_custkey"),
1742+
col("customer.c_name"),
1743+
col("customer.custkey_plus"),
1744+
])?
1745+
.alias("customer_view")?
1746+
.build()?;
1747+
1748+
let unparser = Unparser::default();
1749+
let sql = unparser.plan_to_sql(&plan)?;
1750+
let expected = "SELECT customer_view.c_custkey, customer_view.c_name, customer_view.custkey_plus FROM (SELECT customer.c_custkey, (CAST(customer.c_custkey AS BIGINT) + 1) AS custkey_plus, customer.c_name FROM (SELECT customer.c_custkey, customer.c_name FROM customer AS customer) AS customer) AS customer_view";
1751+
assert_eq!(sql.to_string(), expected);
1752+
Ok(())
1753+
}

0 commit comments

Comments
 (0)