@@ -101,25 +101,25 @@ fn rewrite_sort_expr_for_union(exprs: Vec<SortExpr>) -> Result<Vec<SortExpr>> {
101101 Ok ( sort_exprs)
102102}
103103
104- // Rewrite logic plan for query that order by columns are not in projections
105- // Plan before rewrite:
106- //
107- // Projection: j1.j1_string, j2.j2_string
108- // Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
109- // Projection: j1.j1_string, j2.j2_string, j1.j1_id, j2.j2_id
110- // Inner Join: Filter: j1.j1_id = j2.j2_id
111- // TableScan: j1
112- // TableScan: j2
113- //
114- // Plan after rewrite
115- //
116- // Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
117- // Projection: j1.j1_string, j2.j2_string
118- // Inner Join: Filter: j1.j1_id = j2.j2_id
119- // TableScan: j1
120- // TableScan: j2
121- //
122- // This prevents the original plan generate query with derived table but missing alias.
104+ /// Rewrite logic plan for query that order by columns are not in projections
105+ /// Plan before rewrite:
106+ ///
107+ /// Projection: j1.j1_string, j2.j2_string
108+ /// Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
109+ /// Projection: j1.j1_string, j2.j2_string, j1.j1_id, j2.j2_id
110+ /// Inner Join: Filter: j1.j1_id = j2.j2_id
111+ /// TableScan: j1
112+ /// TableScan: j2
113+ ///
114+ /// Plan after rewrite
115+ ///
116+ /// Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
117+ /// Projection: j1.j1_string, j2.j2_string
118+ /// Inner Join: Filter: j1.j1_id = j2.j2_id
119+ /// TableScan: j1
120+ /// TableScan: j2
121+ ///
122+ /// This prevents the original plan generate query with derived table but missing alias.
123123pub ( super ) fn rewrite_plan_for_sort_on_non_projected_fields (
124124 p : & Projection ,
125125) -> Option < LogicalPlan > {
@@ -191,33 +191,33 @@ pub(super) fn rewrite_plan_for_sort_on_non_projected_fields(
191191 }
192192}
193193
194- // This logic is to work out the columns and inner query for SubqueryAlias plan for both types of
195- // subquery
196- // - `(SELECT column_a as a from table) AS A`
197- // - `(SELECT column_a from table) AS A (a)`
198- //
199- // A roundtrip example for table alias with columns
200- //
201- // query: SELECT id FROM (SELECT j1_id from j1) AS c (id)
202- //
203- // LogicPlan:
204- // Projection: c.id
205- // SubqueryAlias: c
206- // Projection: j1.j1_id AS id
207- // Projection: j1.j1_id
208- // TableScan: j1
209- //
210- // Before introducing this logic, the unparsed query would be `SELECT c.id FROM (SELECT j1.j1_id AS
211- // id FROM (SELECT j1.j1_id FROM j1)) AS c`.
212- // The query is invalid as `j1.j1_id` is not a valid identifier in the derived table
213- // `(SELECT j1.j1_id FROM j1)`
214- //
215- // With this logic, the unparsed query will be:
216- // `SELECT c.id FROM (SELECT j1.j1_id FROM j1) AS c (id)`
217- //
218- // Caveat: this won't handle the case like `select * from (select 1, 2) AS a (b, c)`
219- // as the parser gives a wrong plan which has mismatch `Int(1)` types: Literal and
220- // Column in the Projections. Once the parser side is fixed, this logic should work
194+ /// This logic is to work out the columns and inner query for SubqueryAlias plan for both types of
195+ /// subquery
196+ /// - `(SELECT column_a as a from table) AS A`
197+ /// - `(SELECT column_a from table) AS A (a)`
198+ ///
199+ /// A roundtrip example for table alias with columns
200+ ///
201+ /// query: SELECT id FROM (SELECT j1_id from j1) AS c (id)
202+ ///
203+ /// LogicPlan:
204+ /// Projection: c.id
205+ /// SubqueryAlias: c
206+ /// Projection: j1.j1_id AS id
207+ /// Projection: j1.j1_id
208+ /// TableScan: j1
209+ ///
210+ /// Before introducing this logic, the unparsed query would be `SELECT c.id FROM (SELECT j1.j1_id AS
211+ /// id FROM (SELECT j1.j1_id FROM j1)) AS c`.
212+ /// The query is invalid as `j1.j1_id` is not a valid identifier in the derived table
213+ /// `(SELECT j1.j1_id FROM j1)`
214+ ///
215+ /// With this logic, the unparsed query will be:
216+ /// `SELECT c.id FROM (SELECT j1.j1_id FROM j1) AS c (id)`
217+ ///
218+ /// Caveat: this won't handle the case like `select * from (select 1, 2) AS a (b, c)`
219+ /// as the parser gives a wrong plan which has mismatch `Int(1)` types: Literal and
220+ /// Column in the Projections. Once the parser side is fixed, this logic should work
221221pub ( super ) fn subquery_alias_inner_query_and_columns (
222222 subquery_alias : & datafusion_expr:: SubqueryAlias ,
223223) -> ( & LogicalPlan , Vec < Ident > ) {
@@ -330,6 +330,7 @@ fn find_projection(logical_plan: &LogicalPlan) -> Option<&Projection> {
330330 _ => None ,
331331 }
332332}
333+
333334/// A `TreeNodeRewriter` implementation that rewrites `Expr::Column` expressions by
334335/// replacing the column's name with an alias if the column exists in the provided schema.
335336///
0 commit comments