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


##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -30,10 +34,38 @@ use sqlparser::ast::{
 
 use super::Unparser;
 
+/// DataFusion's Exprs can represent either an `Expr` or an `OrderByExpr`
+pub enum Unparsed {
+    // SQL Expression
+    Expr(ast::Expr),
+    // SQL ORDER BY expression (e.g. `col ASC NULLS FIRST`)
+    OrderByExpr(ast::OrderByExpr),
+}
+
+impl Unparsed {
+    fn into_order_by_expr(self) -> Result<ast::OrderByExpr> {

Review Comment:
   Maybe we can call this `try_into_order_by_expr` to reflect the fact it is 
fallable
   
   Perhaps it should also be marked `pub` 🤔 
   
   



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -204,6 +245,12 @@ impl Unparser<'_> {
                         end_bound: Option::from(end_bound),
                     }),
                 }));
+
+                let order_by_expr_vec: Vec<ast::OrderByExpr> = order_by
+                    .iter()
+                    .flat_map(|expr| 
expr_to_unparsed(expr)?.into_order_by_expr())
+                    .collect();

Review Comment:
   I think this should be just `map` (flat map I think will discard Result`s 🤔 
   
   ```suggestion
                   let order_by: Vec<ast::OrderByExpr> = order_by
                       .iter()
                       .map(|expr| expr_to_unparsed(expr)?.into_order_by_expr())
                       .collect()?;
   ```



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -366,6 +413,27 @@ impl Unparser<'_> {
         }
     }
 
+    pub fn expr_to_unparsed(&self, expr: &Expr) -> Result<Unparsed> {

Review Comment:
   Can we please add a doc string here explaining the difference here to 
`expr_to_sql`?



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -305,10 +352,10 @@ impl Unparser<'_> {
                 })
             }
             Expr::Sort(Sort {
-                expr,
+                expr: _,
                 asc: _,
                 nulls_first: _,
-            }) => self.expr_to_sql(expr),
+            }) => internal_err!("Sort expression should be handled by 
expr_to_unparsed"),

Review Comment:
   Since this is a public API I don't think this should be an internal_err as 
it isn't a bug in datafusion (it could be a bug in the input that was passed 
in)-- perhaps we can make it a `plan_err`?



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -992,7 +1060,11 @@ mod tests {
                     ),
                     args: vec![wildcard()],
                     partition_by: vec![],
-                    order_by: vec![],
+                    order_by: vec![Expr::Sort(Sort::new(
+                        Box::new(col("a")),
+                        false,
+                        true,
+                    ))],

Review Comment:
   this attaches the ordering into the aggregate function arguments -- I think 
it needs to be some part of `WindowFrame`



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -1004,7 +1076,7 @@ mod tests {
                     ),
                     null_treatment: None,
                 }),
-                r#"COUNT(*) OVER (RANGE BETWEEN 6 PRECEDING AND 2 FOLLOWING)"#,
+                r#"COUNT(* ORDER BY "a" DESC NULLS FIRST) OVER (RANGE BETWEEN 
6 PRECEDING AND 2 FOLLOWING)"#,

Review Comment:
   It looks like somehow the ordering was assciated with the aggregate function 
rather than the window frame -- see comment above



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