alamb commented on PR #10560:
URL: https://github.com/apache/datafusion/pull/10560#issuecomment-2154554804
> I'm not sure if we really need the error check since we don't have any now
I guess what I was thinking is that the reason there is no check now is
because the rust compiler will ensure the types are correct (specifically that
they are `AggregateUDF`.
With the builder API as it is written now, you can call `order_by` on any
arbitrary Expr (not just the appropriate `Expr::AggregateFunction` variant)
So I was imagining if any of the methods had been called on an invalid Expr,
`build()` would return an error
Maybe something like
```rust
/// Traits to help build aggregate functions
trait AggregateExt {
// return a builder
fn order_by(self, order_by: Vec<Expr>) -> AggregateBuilder;
fn filter(self, filter: Box<Expr>) -> AggregateBuilder;
fn null_treatment(self, null_treatment: NullTreatment) ->
AggregateBuilder;
fn distinct(self) -> AggregateBuilder;
}
pub struct AggregateBuilder {
// if set to none, builder errors
udf: Option<Arc<AggregateUdf>>,
order_by: Vec<Expr>,
....
}
impl AggregateBuilder {
fn order_by(self, order_by: Vec<Expr>) -> AggregateBuilder {
self.order_by = order_by;
self
}
fn filter(self, filter: Box<Expr>) -> AggregateBuilder {..}
fn null_treatment(self, null_treatment: NullTreatment) ->
AggregateBuilder {..}
fn distinct(self) -> AggregateBuilder {..}
// builds up any in progress aggregate
fn build(self) -> Result<Expr> {
let Some(udf) = self.udf else {
return plan_err!("Expr of type XXX is not an aggregate")
}
udf.order_by = self.order_by;
...
Ok(Expr::AggregateFunction(udf))
}
}
impl AggregateExt for Expr {
fn order_by(self, order_by: Vec<Expr>) -> AggregateBuilder {
match self {
Expr::AggregateFunction(mut udaf) => {
AggregateBuilder { udf: Some(udaf) }
}
// wrong type passed -- error when build is called
_ => {
AggregateBuilder { udf: None }
}
}
}
```
--
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]