Skip to content

Commit 7cbc6b4

Browse files
authored
Remove as_any from scalar UDF trait definition (#20812)
## Which issue does this PR close? None. ## Rationale for this change This PR reduces the amount of boilerplate code that users need to write for scalar UDFs. I didn't address aggregate or window functions yet just to keep the size of the PR manageable. ## What changes are included in this PR? Now that we have [trait upcasting](https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/) since rust 1.86, we no longer need every implementation of `ScalarUDFImpl` to have the `as_any` function that returns `&self`. This PR makes `Any` an supertrait for `ScalarUDFImpl` and makes the appropriate casts when necessary. The overall diff is about 1200 lines of code removed. ## Are these changes tested? Existing unit tests. ## Are there any user-facing changes? Users simply need to remove the `as_any()` function from their implementations.
1 parent 4f13319 commit 7cbc6b4

222 files changed

Lines changed: 96 additions & 1260 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

datafusion-examples/examples/builtin_functions/function_factory.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ struct ScalarFunctionWrapper {
118118
}
119119

120120
impl ScalarUDFImpl for ScalarFunctionWrapper {
121-
fn as_any(&self) -> &dyn std::any::Any {
122-
self
123-
}
124-
125121
fn name(&self) -> &str {
126122
&self.name
127123
}

datafusion-examples/examples/data_io/json_shredding.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
//! See `main.rs` for how to run it.
1919
20-
use std::any::Any;
2120
use std::sync::Arc;
2221

2322
use arrow::array::{RecordBatch, StringArray};
@@ -207,10 +206,6 @@ impl Default for JsonGetStr {
207206
}
208207

209208
impl ScalarUDFImpl for JsonGetStr {
210-
fn as_any(&self) -> &dyn Any {
211-
self
212-
}
213-
214209
fn name(&self) -> &str {
215210
"json_get_str"
216211
}

datafusion-examples/examples/query_planning/optimizer_rule.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use datafusion::logical_expr::{
2828
use datafusion::optimizer::ApplyOrder;
2929
use datafusion::optimizer::{OptimizerConfig, OptimizerRule};
3030
use datafusion::prelude::SessionContext;
31-
use std::any::Any;
3231
use std::sync::Arc;
3332

3433
/// This example demonstrates how to add your own [`OptimizerRule`]
@@ -190,10 +189,6 @@ impl MyEq {
190189
}
191190

192191
impl ScalarUDFImpl for MyEq {
193-
fn as_any(&self) -> &dyn Any {
194-
self
195-
}
196-
197192
fn name(&self) -> &str {
198193
"my_eq"
199194
}

datafusion-examples/examples/udf/advanced_udf.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
//! See `main.rs` for how to run it.
1919
20-
use std::any::Any;
2120
use std::sync::Arc;
2221

2322
use arrow::array::{
@@ -66,10 +65,6 @@ impl PowUdf {
6665

6766
impl ScalarUDFImpl for PowUdf {
6867
/// We implement as_any so that we can downcast the ScalarUDFImpl trait object
69-
fn as_any(&self) -> &dyn Any {
70-
self
71-
}
72-
7368
/// Return the name of this function
7469
fn name(&self) -> &str {
7570
"pow"

datafusion-examples/examples/udf/async_udf.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! making network requests. This can be used for tasks like fetching
2424
//! data from an external API such as a LLM service or an external database.
2525
26-
use std::{any::Any, sync::Arc};
26+
use std::sync::Arc;
2727

2828
use arrow::array::{ArrayRef, BooleanArray, Int64Array, RecordBatch, StringArray};
2929
use arrow_schema::{DataType, Field, Schema};
@@ -160,10 +160,6 @@ impl AskLLM {
160160
/// information for the function, such as its name, signature, and return type.
161161
/// [async_trait]
162162
impl ScalarUDFImpl for AskLLM {
163-
fn as_any(&self) -> &dyn Any {
164-
self
165-
}
166-
167163
fn name(&self) -> &str {
168164
"ask_llm"
169165
}

datafusion/core/tests/fuzz_cases/equivalence/utils.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::any::Any;
1918
use std::cmp::Ordering;
2019
use std::sync::Arc;
2120

@@ -531,9 +530,6 @@ impl TestScalarUDF {
531530
}
532531

533532
impl ScalarUDFImpl for TestScalarUDF {
534-
fn as_any(&self) -> &dyn Any {
535-
self
536-
}
537533
fn name(&self) -> &str {
538534
"test-scalar-udf"
539535
}

datafusion/core/tests/physical_optimizer/projection_pushdown.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::any::Any;
1918
use std::sync::Arc;
2019

2120
use arrow::compute::SortOptions;
@@ -79,10 +78,6 @@ impl DummyUDF {
7978
}
8079

8180
impl ScalarUDFImpl for DummyUDF {
82-
fn as_any(&self) -> &dyn Any {
83-
self
84-
}
85-
8681
fn name(&self) -> &str {
8782
"dummy_udf"
8883
}

datafusion/core/tests/user_defined/user_defined_async_scalar_functions.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ impl TestAsyncUDFImpl {
129129
}
130130

131131
impl ScalarUDFImpl for TestAsyncUDFImpl {
132-
fn as_any(&self) -> &dyn std::any::Any {
133-
self
134-
}
135-
136132
fn name(&self) -> &str {
137133
"test_async_udf"
138134
}

datafusion/core/tests/user_defined/user_defined_scalar_functions.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::any::Any;
1918
use std::collections::HashMap;
2019
use std::hash::{Hash, Hasher};
2120
use std::sync::Arc;
@@ -201,10 +200,6 @@ impl std::fmt::Debug for Simple0ArgsScalarUDF {
201200
}
202201

203202
impl ScalarUDFImpl for Simple0ArgsScalarUDF {
204-
fn as_any(&self) -> &dyn Any {
205-
self
206-
}
207-
208203
fn name(&self) -> &str {
209204
&self.name
210205
}
@@ -511,10 +506,6 @@ impl AddIndexToStringVolatileScalarUDF {
511506
}
512507

513508
impl ScalarUDFImpl for AddIndexToStringVolatileScalarUDF {
514-
fn as_any(&self) -> &dyn Any {
515-
self
516-
}
517-
518509
fn name(&self) -> &str {
519510
&self.name
520511
}
@@ -678,9 +669,6 @@ impl CastToI64UDF {
678669
}
679670

680671
impl ScalarUDFImpl for CastToI64UDF {
681-
fn as_any(&self) -> &dyn Any {
682-
self
683-
}
684672
fn name(&self) -> &str {
685673
"cast_to_i64"
686674
}
@@ -800,9 +788,6 @@ impl TakeUDF {
800788

801789
/// Implement a ScalarUDFImpl whose return type is a function of the input values
802790
impl ScalarUDFImpl for TakeUDF {
803-
fn as_any(&self) -> &dyn Any {
804-
self
805-
}
806791
fn name(&self) -> &str {
807792
"take"
808793
}
@@ -949,10 +934,6 @@ struct ScalarFunctionWrapper {
949934
}
950935

951936
impl ScalarUDFImpl for ScalarFunctionWrapper {
952-
fn as_any(&self) -> &dyn Any {
953-
self
954-
}
955-
956937
fn name(&self) -> &str {
957938
&self.name
958939
}
@@ -1441,10 +1422,6 @@ impl MyRegexUdf {
14411422
}
14421423

14431424
impl ScalarUDFImpl for MyRegexUdf {
1444-
fn as_any(&self) -> &dyn Any {
1445-
self
1446-
}
1447-
14481425
fn name(&self) -> &str {
14491426
"regex_udf"
14501427
}
@@ -1609,10 +1586,6 @@ impl MetadataBasedUdf {
16091586
}
16101587

16111588
impl ScalarUDFImpl for MetadataBasedUdf {
1612-
fn as_any(&self) -> &dyn Any {
1613-
self
1614-
}
1615-
16161589
fn name(&self) -> &str {
16171590
&self.name
16181591
}
@@ -1818,10 +1791,6 @@ impl Default for ExtensionBasedUdf {
18181791
}
18191792
}
18201793
impl ScalarUDFImpl for ExtensionBasedUdf {
1821-
fn as_any(&self) -> &dyn Any {
1822-
self
1823-
}
1824-
18251794
fn name(&self) -> &str {
18261795
&self.name
18271796
}
@@ -1988,9 +1957,6 @@ async fn test_config_options_work_for_scalar_func() -> Result<()> {
19881957
}
19891958

19901959
impl ScalarUDFImpl for TestScalarUDF {
1991-
fn as_any(&self) -> &dyn Any {
1992-
self
1993-
}
19941960
fn name(&self) -> &str {
19951961
"TestScalarUDF"
19961962
}
@@ -2052,10 +2018,6 @@ async fn test_extension_metadata_preserve_in_sql_values() -> Result<()> {
20522018
}
20532019

20542020
impl ScalarUDFImpl for MakeExtension {
2055-
fn as_any(&self) -> &dyn Any {
2056-
self
2057-
}
2058-
20592021
fn name(&self) -> &str {
20602022
"make_extension"
20612023
}
@@ -2133,10 +2095,6 @@ async fn test_extension_metadata_preserve_in_subquery() -> Result<()> {
21332095
}
21342096

21352097
impl ScalarUDFImpl for ExtensionScalarPredicate {
2136-
fn as_any(&self) -> &dyn Any {
2137-
self
2138-
}
2139-
21402098
fn name(&self) -> &str {
21412099
"extension_predicate"
21422100
}

datafusion/expr/src/async_udf.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl PartialEq for AsyncScalarUDF {
6363
fn eq(&self, other: &Self) -> bool {
6464
// Deconstruct to catch any new fields added in future
6565
let Self { inner } = self;
66-
inner.dyn_eq(other.inner.as_any())
66+
inner.as_ref().dyn_eq(other.inner.as_ref() as &dyn Any)
6767
}
6868
}
6969
impl Eq for AsyncScalarUDF {}
@@ -102,10 +102,6 @@ impl AsyncScalarUDF {
102102
}
103103

104104
impl ScalarUDFImpl for AsyncScalarUDF {
105-
fn as_any(&self) -> &dyn Any {
106-
self
107-
}
108-
109105
fn name(&self) -> &str {
110106
self.inner.name()
111107
}
@@ -156,10 +152,6 @@ mod tests {
156152
}
157153

158154
impl ScalarUDFImpl for TestAsyncUDFImpl1 {
159-
fn as_any(&self) -> &dyn std::any::Any {
160-
self
161-
}
162-
163155
fn name(&self) -> &str {
164156
todo!()
165157
}
@@ -193,10 +185,6 @@ mod tests {
193185
}
194186

195187
impl ScalarUDFImpl for TestAsyncUDFImpl2 {
196-
fn as_any(&self) -> &dyn std::any::Any {
197-
self
198-
}
199-
200188
fn name(&self) -> &str {
201189
todo!()
202190
}

0 commit comments

Comments
 (0)