alamb commented on code in PR #11505:
URL: https://github.com/apache/datafusion/pull/11505#discussion_r1683380691
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -611,6 +607,67 @@ impl Unparser<'_> {
}
}
+// This logic is to work out the columns and inner query for SubqueryAlias
plan for both types of
+// subquery
+// - `(SELECT column_a as a from table) AS A`
+// - `(SELECT column_a from table) AS A (a)`
+//
+// A roundtrip example for table alias with columns
+//
+// query: SELECT id FROM (SELECT j1_id from j1) AS c (id)
+//
+// LogicPlan:
+// Projection: c.id
+// SubqueryAlias: c
+// Projection: j1.j1_id AS id
+// Projection: j1.j1_id
+// TableScan: j1
+//
+// Before introducing this logic, the unparsed query would be `SELECT c.id
FROM (SELECT j1.j1_id AS
+// id FROM (SELECT j1.j1_id FROM j1)) AS c`.
+// The query is invalid as `j1.j1_id` is not a valid identifier in the derived
table
+// `(SELECT j1.j1_id FROM j1)`
+//
+// With this logic, the unparsed query will be:
+// `SELECT c.id FROM (SELECT j1.j1_id FROM j1) AS c (id)`
+//
+// Caveat: this won't handle the case like `select * from (select 1, 2) AS a
(b, c)`
+// as the parser gives a wrong plan which has mismatch `Int(1)` types: Literal
and
+// Column in the Projections. Once the parser side is fixed, this logic should
work
Review Comment:
Thank you for the comments
##########
datafusion/sql/tests/cases/plan_to_sql.rs:
##########
@@ -240,6 +240,35 @@ fn roundtrip_statement_with_dialect() -> Result<()> {
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(UnparserDefaultDialect {}),
},
+ // more tests around subquery/derived table roundtrip
+ TestStatementWithDialect {
+ sql: "SELECT string_count FROM (
+ SELECT
Review Comment:
I recommend you add a query that has an additional test that selects more
than one column and an expression
something like
```sql
select string_count * 100, id FROM ...
```
--
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]