jayzhan211 commented on code in PR #9960:
URL: https://github.com/apache/arrow-datafusion/pull/9960#discussion_r1554574213
##########
datafusion/functions-aggregate/src/lib.rs:
##########
@@ -82,3 +92,260 @@ pub fn register_all(registry: &mut dyn FunctionRegistry) ->
Result<()> {
Ok(())
}
+
+/// Creates a physical expression of the UDAF, that includes all necessary
type coercion.
+/// This function errors when `args`' can't be coerced to a valid argument
type of the UDAF.
+pub fn create_aggregate_expr(
+ fun: &AggregateUDF,
+ input_phy_exprs: &[Arc<dyn PhysicalExpr>],
+ sort_exprs: &[Expr],
+ ordering_req: &[PhysicalSortExpr],
+ schema: &Schema,
+ name: impl Into<String>,
+ ignore_nulls: bool,
+) -> Result<Arc<dyn AggregateExpr>> {
+ let input_exprs_types = input_phy_exprs
+ .iter()
+ .map(|arg| arg.data_type(schema))
+ .collect::<Result<Vec<_>>>()?;
+
+ let ordering_types = ordering_req
+ .iter()
+ .map(|e| e.expr.data_type(schema))
+ .collect::<Result<Vec<_>>>()?;
+
+ let ordering_fields = ordering_fields(ordering_req, &ordering_types);
+
+ Ok(Arc::new(AggregateFunctionExpr {
+ fun: fun.clone(),
+ args: input_phy_exprs.to_vec(),
+ data_type: fun.return_type(&input_exprs_types)?,
+ name: name.into(),
+ schema: schema.clone(),
+ sort_exprs: sort_exprs.to_vec(),
+ ordering_req: ordering_req.to_vec(),
+ ignore_nulls,
+ ordering_fields,
+ }))
+}
+
+/// An aggregate expression that:
+/// * knows its resulting field
+/// * knows how to create its accumulator
+/// * knows its accumulator's state's field
+/// * knows the expressions from whose its accumulator will receive values
+///
+/// Any implementation of this trait also needs to implement the
+/// `PartialEq<dyn Any>` to allows comparing equality between the
+/// trait objects.
+pub trait AggregateExpr: Send + Sync + Debug + PartialEq<dyn Any> {
Review Comment:
I agree that we should avoid the user pulling out unnecessary things for
them. I thought they would need builtin functions, but they probably don't. I
will keep them in common
--
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]