gruuya commented on code in PR #10690: URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617161884
########## 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: The `order_by`/`sort_by` clause is fetched from the `sqlparser::ast::Query` struct while parsing the `DISTINCT ON` query, so it makes sense that it is added there when de-parsing https://github.com/apache/datafusion/blob/3dc1773403532752db01da5c545b2bdab415e489/datafusion/sql/src/query.rs#L132-L136 Indeed `sqlparser::ast::Select`s `sort_by` is not being used in DataFusion https://github.com/apache/datafusion/blob/3dc1773403532752db01da5c545b2bdab415e489/datafusion/sql/src/select.rs#L68-L70 so it also seems that `SelectBuilder::sort_by` is not needed atm. -- 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