TinyMurky commented on code in PR #27773:
URL: https://github.com/apache/datafusion/pull/27773#discussion_r4732433075


##########
datafusion/sql/src/select.rs:
##########
@@ -564,6 +564,28 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
             }
             Some(Distinct::Distinct) => {
                 let plan = self.try_process_unnest(plan, 
select_exprs_post_aggr)?;
+                if let LogicalPlan::Projection(projection) = &plan {
+                    // DISTINCT exposes only the projection's output columns.
+                    //
+                    // SELECT DISTINCT a + 1 AS v FROM t ORDER BY a + 1;
+                    //
+                    // The sort input has v, so trying to resolve t.a would 
fail.
+                    // Match a + 1 against the aliased projection expression 
and
+                    // use v as the sort key, as in ORDER BY v.
+                    for sort in &mut order_by_rex {
+                        if matches!(sort.expr, Expr::Column(_)) {
+                            continue;
+                        }

Review Comment:
   Hello !@jayzhan211
   
   I had added the test and the comment in the new commit!



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