alamb commented on code in PR #15068:
URL: https://github.com/apache/datafusion/pull/15068#discussion_r1986318680
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -984,11 +984,18 @@ impl Unparser<'_> {
Ok(Some(builder.build()?))
}
LogicalPlan::SubqueryAlias(subquery_alias) => {
- Self::unparse_table_scan_pushdown(
+ let ret = Self::unparse_table_scan_pushdown(
&subquery_alias.input,
Some(subquery_alias.alias.clone()),
already_projected,
- )
+ );
+ if let Some(alias) = alias {
+ if let Ok(Some(plan)) = ret {
+ let plan =
LogicalPlanBuilder::new(plan).alias(alias)?.build()?;
+ return Ok(Some(plan));
+ }
+ }
Review Comment:
I think you can write this a bit more concisely by unwrapping the error
earlier if you wanted:
Something like
```suggestion
)?;
if let Some(alias) = alias {
let plan =
LogicalPlanBuilder::new(plan).alias(alias)?.build()?;
return Ok(Some(plan));
}
Ok(ret)
```
##########
datafusion/core/tests/sql/select.rs:
##########
@@ -350,3 +351,48 @@ async fn test_version_function() {
assert_eq!(version.value(0), expected_version);
}
+
+#[tokio::test]
+async fn test_unparse_subqueryalias() -> Result<()> {
+ let ctx = SessionContext::new();
+ let sql = r#"
+SELECT
+ customer_view.c_custkey,
+ customer_view.c_name,
+ customer_view.custkey_plus
+FROM
+ (
+ SELECT
+ customer.c_custkey,
+ customer.c_name,
+ customer.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 AS c_custkey,
+ customer.c_name AS c_name
+ FROM
+ customer
+ ) AS customer
+ ) AS customer
+ ) AS customer_view
+ "#;
+ /// Return a RecordBatch with made up data about customer
+ fn customer() -> RecordBatch {
+ let custkey: ArrayRef = Arc::new(Int64Array::from(vec![1]));
+ let name: ArrayRef = Arc::new(StringArray::from_iter_values([""]));
+ RecordBatch::try_from_iter(vec![("c_custkey", custkey), ("c_name",
name)])
+ .unwrap()
+ }
+ ctx.register_batch("customer", customer())?;
+ let plan = ctx.sql(sql).await?.into_optimized_plan()?;
+ let sql = plan_to_sql(&plan)?;
+ assert_eq!(sql.to_string(), "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");
Review Comment:
I parsed this with some structure and it looks right to me
Thanks @chenkovsky
```sql
"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");
```
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]