ClSlaid commented on code in PR #10460:
URL: https://github.com/apache/datafusion/pull/10460#discussion_r1597540519


##########
datafusion/optimizer/src/replace_distinct_aggregate.rs:
##########
@@ -88,60 +94,72 @@ impl OptimizerRule for ReplaceDistinctWithAggregate {
                 input,
                 schema,
             })) => {
+                let expr_cnt = on_expr.len();
+
                 // Construct the aggregation expression to be used to fetch 
the selected expressions.
-                let aggr_expr = select_expr
-                    .iter()
-                    .map(|e| {
-                        Expr::AggregateFunction(AggregateFunction::new(
-                            AggregateFunctionFunc::FirstValue,
-                            vec![e.clone()],
-                            false,
-                            None,
-                            sort_expr.clone(),
-                            None,
-                        ))
-                    })
-                    .collect::<Vec<Expr>>();
+                let aggr_expr = 
vec![Expr::AggregateFunction(AggregateFunction::new(
+                    AggregateFunctionFunc::FirstValue,
+                    select_expr,
+                    false,
+                    None,
+                    sort_expr.clone(),
+                    None,
+                ))];

Review Comment:
   > The difference is that the `select_expr.iter()...` makes a `vec` with the 
same number of elements as `select_expr `
   > 
   > To avoid a clone, perhaps you can use `into_iter` instead of `iter`, like
   > 
   > ```rust
   >                 let aggr_expr = select_expr
   >                     .into_iter() // <---- Use into_iter() here
   >                     .map(|e| {
   >                         Expr::AggregateFunction(AggregateFunction::new(
   >                             AggregateFunctionFunc::FirstValue,
   >                             vec![e],    // <--- to avoid a clone here
   >                             false,
   >                             None,
   >                             sort_expr.clone(),
   >                             None,
   >                         ))
   >                     })
   >                     .collect::<Vec<Expr>>();
   > ```
   
   What bothers me most is that I cannot avoid cloning `sort_expr`. However, it 
looks like the only way to make it correct.



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