alamb commented on code in PR #11527:
URL: https://github.com/apache/datafusion/pull/11527#discussion_r1683372622


##########
datafusion/sql/src/unparser/rewrite.rs:
##########
@@ -99,3 +99,61 @@ fn rewrite_sort_expr_for_union(exprs: Vec<Expr>) -> 
Result<Vec<Expr>> {
 
     Ok(sort_exprs)
 }
+
+// Rewrite logic plan for query that order by columns are not in projections
+// Plan before rewrite:
+//
+// Projection: j1.j1_string, j2.j2_string
+//   Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
+//     Projection: j1.j1_string, j2.j2_string, j1.j1_id, j2.j2_id
+//       Inner Join:  Filter: j1.j1_id = j2.j2_id
+//         TableScan: j1
+//         TableScan: j2
+//
+// Plan after rewrite
+//
+// Sort: j1.j1_id DESC NULLS FIRST, j2.j2_id DESC NULLS FIRST
+//   Projection: j1.j1_string, j2.j2_string
+//     Inner Join:  Filter: j1.j1_id = j2.j2_id
+//       TableScan: j1
+//       TableScan: j2
+//
+// This prevents the original plan generate query with derived table but 
missing alias.
+pub(super) fn rewrite_plan_for_sort_on_non_projected_fields(
+    p: &Projection,
+) -> Option<LogicalPlan> {
+    let mut collects = vec![];
+
+    for expr in p.expr.clone() {
+        collects.push(expr.clone());
+    }

Review Comment:
   In fact it looks like `p.expr` is a `Vec` already, so we simply deep-copy 
the vector like this
   
   ```rust
       let mut collects = p.expr.clone()
   ```
   
   Since the code returns early for `Sort` and `Projection` it would be better 
to do the clone after those checks, otherwise the Exprs will be copied even if 
they are just then thrown away



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to