jayzhan211 commented on code in PR #12177:
URL: https://github.com/apache/datafusion/pull/12177#discussion_r1733789566


##########
datafusion/expr/src/tree_node.rs:
##########
@@ -386,3 +379,48 @@ fn transform_vec<F: FnMut(Expr) -> 
Result<Transformed<Expr>>>(
 ) -> Result<Transformed<Vec<Expr>>> {
     ve.into_iter().map_until_stop_and_collect(f)
 }
+
+pub fn transform_sort_option_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
+    sorts_option: Option<Vec<Sort>>,
+    f: &mut F,
+) -> Result<Transformed<Option<Vec<Sort>>>> {
+    sorts_option.map_or(Ok(Transformed::no(None)), |sorts| {
+        Ok(transform_sort_vec(sorts, f)?.update_data(Some))
+    })
+}
+
+pub fn transform_sort_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
+    sorts: Vec<Sort>,
+    mut f: &mut F,
+) -> Result<Transformed<Vec<Sort>>> {
+    Ok(sorts
+        .iter()
+        .map(|sort| sort.expr.clone())
+        .map_until_stop_and_collect(&mut f)?
+        .update_data(|transformed_exprs| {
+            replace_sort_expressions(sorts, transformed_exprs)
+        }))
+}
+
+pub fn replace_sort_expressions(sorts: Vec<Sort>, new_expr: Vec<Expr>) -> 
Vec<Sort> {
+    if sorts.len() != new_expr.len() {
+        panic!(
+            "Incorrect number of new_expr, expected {}, got {}",
+            sorts.len(),
+            new_expr.len()
+        );
+    }
+
+    let mut new_sorts = Vec::with_capacity(sorts.len());
+    for (i, expr) in new_expr.into_iter().enumerate() {
+        new_sorts.push(replace_sort_expression(sorts[i].clone(), expr));

Review Comment:
   modify expr to avoid clone?



##########
datafusion/expr/src/tree_node.rs:
##########
@@ -386,3 +379,48 @@ fn transform_vec<F: FnMut(Expr) -> 
Result<Transformed<Expr>>>(
 ) -> Result<Transformed<Vec<Expr>>> {
     ve.into_iter().map_until_stop_and_collect(f)
 }
+
+pub fn transform_sort_option_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
+    sorts_option: Option<Vec<Sort>>,
+    f: &mut F,
+) -> Result<Transformed<Option<Vec<Sort>>>> {
+    sorts_option.map_or(Ok(Transformed::no(None)), |sorts| {
+        Ok(transform_sort_vec(sorts, f)?.update_data(Some))
+    })
+}
+
+pub fn transform_sort_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
+    sorts: Vec<Sort>,
+    mut f: &mut F,
+) -> Result<Transformed<Vec<Sort>>> {
+    Ok(sorts
+        .iter()
+        .map(|sort| sort.expr.clone())
+        .map_until_stop_and_collect(&mut f)?
+        .update_data(|transformed_exprs| {
+            replace_sort_expressions(sorts, transformed_exprs)
+        }))
+}
+
+pub fn replace_sort_expressions(sorts: Vec<Sort>, new_expr: Vec<Expr>) -> 
Vec<Sort> {

Review Comment:
   Maybe returns `with Result<Vec<Sort>>`?



##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -624,14 +624,14 @@ mod tests {
             vec![col("a")],
             false,
             None,
-            Some(vec![col("a")]),
+            Some(vec![col("a").sort(true, false)]),
             None,
         ));
         let plan = LogicalPlanBuilder::from(table_scan)
             .aggregate(vec![col("c")], vec![expr, count_distinct(col("b"))])?
             .build()?;
         // Do nothing
-        let expected = "Aggregate: groupBy=[[test.c]], aggr=[[sum(test.a) 
ORDER BY [test.a], count(DISTINCT test.b)]] [c:UInt32, sum(test.a) ORDER BY 
[test.a]:UInt64;N, count(DISTINCT test.b):Int64]\
+        let expected = "Aggregate: groupBy=[[test.c]], aggr=[[sum(test.a) 
ORDER BY [test.a ASC NULLS LAST], count(DISTINCT test.b)]] [c:UInt32, 
sum(test.a) ORDER BY [test.a ASC NULLS LAST]:UInt64;N, count(DISTINCT 
test.b):Int64]\

Review Comment:
   Is this the same as previous?



##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -79,14 +51,15 @@ impl OptimizerRule for EliminateDuplicatedExpr {
         match plan {
             LogicalPlan::Sort(sort) => {
                 let len = sort.expr.len();
-                let unique_exprs: Vec<_> = sort
-                    .expr
-                    .into_iter()
-                    .map(|e| SortExprWrapper { expr: e })
-                    .collect::<IndexSet<_>>()
-                    .into_iter()
-                    .map(|wrapper| wrapper.expr)
-                    .collect();
+                let mut first_sort_by_expr: IndexMap<Expr, SortExpr> =
+                    IndexMap::default();
+                for s in &sort.expr {

Review Comment:
   I think we could avoid clone here, take `expr`



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