czhang06 commented on code in PR #27802:
URL: https://github.com/apache/datafusion/pull/27802#discussion_r4758110630


##########
datafusion/sql/src/select.rs:
##########
@@ -1486,6 +1486,18 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                 let rewritten_expr =
                     rebase_expr(&sort_expr.expr, &aggr_projection_exprs, 
input)?;
 
+                // `ORDER BY v` (or its position) where `v` is a SELECT alias
+                // already names that output column. Matching it below would
+                // pick the alias of a same-named input column instead.
+                if let Expr::Column(c) = &sort_expr.expr
+                    && c.relation.is_none()
+                    && select_exprs
+                        .iter()
+                        .any(|e| matches!(e, Expr::Alias(a) if a.name == 
c.name))
+                {
+                    return Ok(sort_expr.with_expr(rewritten_expr));
+                }

Review Comment:
   Same cancellation again, just through expressions. Applied your 
references_select_alias change, added both tests, reworded the sum(price) + 1 
comment, and updated the description.
   
   I also ran a bigger local matrix (ORDER BY by name/position/expr × plain, 
DISTINCT, GROUP BY, HAVING) against the same queries over FROM (...) AS s, and 
they all match now. Didn't add it to the PR, but can if you want.
   
   fmt, clippy, doc and the full slt suite pass.



-- 
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]

Reply via email to