Skip to content

Commit

Permalink
Allow constructing ScalarUDF from shared implementation
Browse files Browse the repository at this point in the history
When composing or decorating functions, it's useful to operate on Impl
trait objects (dyn ScalarUDFImpl) instead of the Expr-level adapter
(ScalarUDF). However, before the change, it was impossible to construct
`ScalarUDF` from `Arc<dyn ScalarUDFImpl>`, even though the use of `Arc`
is already part of the type API. This commit allows constructing the
scalar from a shared impl object.

For consistency, same change is applied for window functions and
aggregations.
  • Loading branch information
findepi committed Feb 7, 2025
1 parent 304488d commit e0e57ac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 6 additions & 3 deletions datafusion/expr/src/udaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ impl AggregateUDF {
where
F: AggregateUDFImpl + 'static,
{
Self {
inner: Arc::new(fun),
}
Self::new_from_shared_impl(Arc::new(fun))
}

/// Create a new `AggregateUDF` from a `[AggregateUDFImpl]` trait object
pub fn new_from_shared_impl(fun: Arc<dyn AggregateUDFImpl>) -> AggregateUDF {
Self { inner: fun }
}

/// Return the underlying [`AggregateUDFImpl`] trait object for this function
Expand Down
9 changes: 6 additions & 3 deletions datafusion/expr/src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ impl ScalarUDF {
where
F: ScalarUDFImpl + 'static,
{
Self {
inner: Arc::new(fun),
}
Self::new_from_shared_impl(Arc::new(fun))
}

/// Create a new `ScalarUDF` from a `[ScalarUDFImpl]` trait object
pub fn new_from_shared_impl(fun: Arc<dyn ScalarUDFImpl>) -> ScalarUDF {
Self { inner: fun }
}

/// Return the underlying [`ScalarUDFImpl`] trait object for this function
Expand Down
9 changes: 6 additions & 3 deletions datafusion/expr/src/udwf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ impl WindowUDF {
where
F: WindowUDFImpl + 'static,
{
Self {
inner: Arc::new(fun),
}
Self::new_from_shared_impl(Arc::new(fun))
}

/// Create a new `WindowUDF` from a `[WindowUDFImpl]` trait object
pub fn new_from_shared_impl(fun: Arc<dyn WindowUDFImpl>) -> WindowUDF {
Self { inner: fun }
}

/// Return the underlying [`WindowUDFImpl`] trait object for this function
Expand Down

0 comments on commit e0e57ac

Please sign in to comment.