Skip to content

Commit 22fb5f7

Browse files
authored
Allow constructing ScalarUDF from shared implementation (#14541)
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.
1 parent fc1835d commit 22fb5f7

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

datafusion/expr/src/udaf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ impl AggregateUDF {
119119
where
120120
F: AggregateUDFImpl + 'static,
121121
{
122-
Self {
123-
inner: Arc::new(fun),
124-
}
122+
Self::new_from_shared_impl(Arc::new(fun))
123+
}
124+
125+
/// Create a new `AggregateUDF` from a `[AggregateUDFImpl]` trait object
126+
pub fn new_from_shared_impl(fun: Arc<dyn AggregateUDFImpl>) -> AggregateUDF {
127+
Self { inner: fun }
125128
}
126129

127130
/// Return the underlying [`AggregateUDFImpl`] trait object for this function

datafusion/expr/src/udf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ impl ScalarUDF {
9191
where
9292
F: ScalarUDFImpl + 'static,
9393
{
94-
Self {
95-
inner: Arc::new(fun),
96-
}
94+
Self::new_from_shared_impl(Arc::new(fun))
95+
}
96+
97+
/// Create a new `ScalarUDF` from a `[ScalarUDFImpl]` trait object
98+
pub fn new_from_shared_impl(fun: Arc<dyn ScalarUDFImpl>) -> ScalarUDF {
99+
Self { inner: fun }
97100
}
98101

99102
/// Return the underlying [`ScalarUDFImpl`] trait object for this function

datafusion/expr/src/udwf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ impl WindowUDF {
100100
where
101101
F: WindowUDFImpl + 'static,
102102
{
103-
Self {
104-
inner: Arc::new(fun),
105-
}
103+
Self::new_from_shared_impl(Arc::new(fun))
104+
}
105+
106+
/// Create a new `WindowUDF` from a `[WindowUDFImpl]` trait object
107+
pub fn new_from_shared_impl(fun: Arc<dyn WindowUDFImpl>) -> WindowUDF {
108+
Self { inner: fun }
106109
}
107110

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

0 commit comments

Comments
 (0)