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


##########
datafusion-examples/examples/udaf_expr.rs:
##########
@@ -0,0 +1,31 @@
+// 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::functions_aggregate::expr_fn::{first_value, 
first_value_builder};
+
+use datafusion_common::Result;
+use datafusion_expr::col;
+
+#[tokio::main]
+async fn main() -> Result<()> {

Review Comment:
   I recommend moving these examples into the doc comments to make them easier 
to find



##########
datafusion/functions-aggregate/src/expr_builder.rs:
##########
@@ -0,0 +1,89 @@
+// 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 std::sync::Arc;
+
+use datafusion_expr::{expr::AggregateFunction, Expr};
+use sqlparser::ast::NullTreatment;
+
+/// Builder for creating an aggregate function expression

Review Comment:
   I suggest:
   1. Add a docstring example here
   2. Consider putting this in `datafusion/expr` (not 
`datafusion-functions-aggregate`) -- if we put it in 
`datafusion-functions-aggregate` people can't use it unless they use the built 
in aggregates of DataFusion 🤔 



##########
datafusion/functions-aggregate/src/macros.rs:
##########
@@ -48,49 +48,98 @@ macro_rules! make_udaf_expr_and_func {
                 None,
             ))
         }
+
+        create_builder!(
+            $EXPR_FN,
+            $($arg)*,
+            $DOC,
+            $AGGREGATE_UDF_FN
+        );
+
         create_func!($UDAF, $AGGREGATE_UDF_FN);
     };
-    ($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $distinct:ident, $DOC:expr, 
$AGGREGATE_UDF_FN:ident) => {

Review Comment:
   Instead of `count_distinct_builder` what do you think about adding a 
`distinct` type method instead?
   
   Like
   ```rust
   let agg = count_builder()
     .args(col("a"))
     .distinct()
     .build()?
   ```
   
   🤔 



##########
datafusion/functions-aggregate/src/expr_builder.rs:
##########
@@ -0,0 +1,89 @@
+// 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 std::sync::Arc;
+
+use datafusion_expr::{expr::AggregateFunction, Expr};
+use sqlparser::ast::NullTreatment;
+
+/// Builder for creating an aggregate function expression
+///
+/// Has the same arguments from [AggregateFunction]
+pub struct ExprBuilder {

Review Comment:
   I wonder if we should call it `AggBuilder` 🤔  or AggregeExprBuilder 🤔 



##########
datafusion/functions-aggregate/Cargo.toml:
##########
@@ -39,6 +39,7 @@ path = "src/lib.rs"
 
 [dependencies]
 arrow = { workspace = true }
+concat-idents = "1.1.5"

Review Comment:
   Would it be possible to use https://docs.rs/paste/latest/paste/ (which is 
already a dependency) instead? I think that is what binary expression 
evaluation uses 🤔 



##########
docs/source/user-guide/expressions.md:
##########
@@ -304,6 +304,16 @@ select log(-1), log(0), sqrt(-1);
 | rollup(exprs)                                                     | Creates 
a grouping set for rollup sets.                                                 
|
 | sum(expr)                                                         | 
Сalculates the sum of `expr`.                                                   
        |
 
+## Aggregate Function Builder
+
+Another builder expression that ends with `build()`, it is useful if the 
functions has multiple optional arguments
+
+See datafusion-examples/examples/udaf_expr.rs for example usage.
+
+| Syntax                                                 | Equivalent to       
                |
+| ------------------------------------------------------ | 
----------------------------------- |
+| first_value_builder(expr).order_by(vec![expr]).build() | first_value(expr, 
Some(vec![expr])) |

Review Comment:
   Yes I think adding examples in doc comments is likely the best for this 
particular API as people will be looking at the code already if they want to 
programatically create Exprs



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