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


##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -31,20 +31,24 @@ use datafusion_common::{
 use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
 use datafusion_expr::utils::{format_state_name, AggregateOrderSensitivity};
 use datafusion_expr::{
-    Accumulator, AggregateUDFImpl, ArrayFunctionSignature, Signature, 
TypeSignature,
-    Volatility,
+    Accumulator, AggregateUDFExprBuilder, AggregateUDFImpl, 
ArrayFunctionSignature, Expr,
+    Signature, TypeSignature, Volatility,
 };
 use datafusion_physical_expr_common::aggregate::utils::get_sort_options;
 use datafusion_physical_expr_common::sort_expr::{
     limited_convert_logical_sort_exprs_to_physical, LexOrdering, 
PhysicalSortExpr,
 };
 
-make_udaf_expr_and_func!(
-    FirstValue,
-    first_value,
-    "Returns the first value in a group of values.",
-    first_value_udaf
-);
+create_func!(FirstValue, first_value_udaf);
+
+/// Returns the first value in a group of values.
+pub fn first_value(expression: Expr, order_by: Option<Vec<Expr>>) -> Expr {
+    if let Some(order_by) = order_by {

Review Comment:
   that is pretty cool indeed



##########
datafusion-examples/examples/udaf_expr.rs:
##########
@@ -0,0 +1,44 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion::{
+    execution::{
+        config::SessionConfig,
+        context::{SessionContext, SessionState},
+    },
+    functions_aggregate::{expr_fn::first_value, register_all},
+};
+
+use datafusion_common::Result;
+use datafusion_expr::{col, AggregateUDFExprBuilder};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+    let ctx = SessionContext::new();
+    let config = SessionConfig::new();
+    let mut state = SessionState::new_with_config_rt(config, 
ctx.runtime_env());
+    let _ = register_all(&mut state);
+
+    let first_value_udaf = 
state.aggregate_functions().get("first_value").unwrap();
+    let first_value_builder = first_value_udaf
+        .call(vec![col("a")])
+        .order_by(vec![col("b")]);

Review Comment:
   I think it would help to add comments to this example explaining what it was 
trying to show
   
   Something like
   ```rust
   // DataFusion also supports more advanced aggregate expressions, like
   // `FIRST_VALUE(a ORDER BY b)`. To create such an expression using the 
expr_api
   // you can use `AggregateUDFExprBuilder` trait as follows:
   ```
   
   Also, I think it would actually be easier to find as a function in 
https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/expr_api.rs
 rather than its own separate example



##########
datafusion/expr/src/udaf.rs:
##########
@@ -606,3 +607,49 @@ impl AggregateUDFImpl for AggregateUDFLegacyWrapper {
         (self.accumulator)(acc_args)
     }
 }
+
+pub trait AggregateUDFExprBuilder {

Review Comment:
   Also, since it works with `Expr` (not just `AggregateUDF`) I wonder if we 
should call it something more general like `AggregateExprBuilder` 🤔 
   
   Also, I think we should document this trait with some examples, and 
specifically documenting the behavior of what happens  when the Expr is *not* 
an `Expr::AggregateFunction` (the code silently ignores the expr)
   
   Something like this maybe
   
   ```rust
   /// Trait to assist building aggregate expressions.
   ///
   /// # Error handing
   /// (what happens if the Expr is not an aggregate)
   ///
   /// # Example
   /// ```
   /// show it in use
   /// ```
   ```
   
   
   



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