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


##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -4631,6 +4631,8 @@ fn roundtrip_statement() -> Result<()> {
             "select (id-1)/2, count(*) / (sum(id/10)-1) as agg_expr from 
(select (id-1) as id from person) group by id",
             "select CAST(id/2 as VARCHAR) NOT LIKE 'foo*' from person where 
NOT EXISTS (select ta.j1_id, tb.j2_string from j1 ta join j2 tb on (ta.j1_id = 
tb.j2_id))",
             r#"select "First Name" from person_quoted_cols"#,
+            "select DISTINCT id FROM person;",
+            "select DISTINCT on (id) id, first_name from person order by id;",

Review Comment:
   Can you also please add a test like this (without an order by)?
   
   ```sql
   select DISTINCT on (id) id, first_name from person;
   ```



##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
                     relation,
                 )
             }
-            LogicalPlan::Distinct(_distinct) => {
-                not_impl_err!("Unsupported operator: {plan:?}")
+            LogicalPlan::Distinct(distinct) => {
+                let (select_distinct, input) = match distinct {
+                    Distinct::All(input) => (ast::Distinct::Distinct, 
input.as_ref()),
+                    Distinct::On(on) => {
+                        let exprs = on
+                            .on_expr
+                            .iter()
+                            .map(|e| self.expr_to_sql(e))
+                            .collect::<Result<Vec<_>>>()?;
+                        let items = on
+                            .select_expr
+                            .iter()
+                            .map(|e| self.select_item_to_sql(e))
+                            .collect::<Result<Vec<_>>>()?;
+                        match &on.sort_expr {
+                            Some(sort_expr) => {
+                                if let Some(query_ref) = query {
+                                    query_ref
+                                        
.order_by(self.sort_to_sql(sort_expr.clone())?);
+                                } else {
+                                    return internal_err!(
+                                "Sort operator only valid in a statement 
context."
+                            );

Review Comment:
   I think it is to support the postgres style `DISTINCT ON`, which may not 
have all the columns of the select list
   
   Perhaps @gruuya  knows more



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