Skip to content

Commit 14a85fa

Browse files
timsauceralamb
andauthored
chore: remove as_any from aggregate and window functions (#21209)
## Which issue does this PR close? This is a follow on to #20812 but treats aggregate and window functions. ## Rationale for this change This PR reduces the amount of boilerplate code that users need to write for aggregate and window functions. ## 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 these functions to have the as_any function that returns &self. This PR makes Any an supertrait and makes the appropriate casts when necessary. ## Are these changes tested? Existing unit tests. ## Are there any user-facing changes? Yes, the users simply need to remove the `as_any` function. The upgrade guide is updated. --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 5ff80e4 commit 14a85fa

Some content is hidden

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

51 files changed

+79
-381
lines changed

datafusion-examples/examples/udf/advanced_udaf.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use arrow::datatypes::{Field, Schema};
2121
use datafusion::physical_expr::NullState;
2222
use datafusion::{arrow::datatypes::DataType, logical_expr::Volatility};
23-
use std::{any::Any, sync::Arc};
23+
use std::sync::Arc;
2424

2525
use arrow::array::{
2626
ArrayRef, AsArray, Float32Array, PrimitiveArray, PrimitiveBuilder, UInt32Array,
@@ -64,11 +64,6 @@ impl GeoMeanUdaf {
6464
}
6565

6666
impl AggregateUDFImpl for GeoMeanUdaf {
67-
/// We implement as_any so that we can downcast the AggregateUDFImpl trait object
68-
fn as_any(&self) -> &dyn Any {
69-
self
70-
}
71-
7267
/// Return the name of this function
7368
fn name(&self) -> &str {
7469
"geo_mean"
@@ -387,10 +382,6 @@ impl SimplifiedGeoMeanUdaf {
387382
}
388383

389384
impl AggregateUDFImpl for SimplifiedGeoMeanUdaf {
390-
fn as_any(&self) -> &dyn Any {
391-
self
392-
}
393-
394385
fn name(&self) -> &str {
395386
"simplified_geo_mean"
396387
}

datafusion-examples/examples/udf/advanced_udwf.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//! See `main.rs` for how to run it.
1919
20-
use std::{any::Any, sync::Arc};
20+
use std::sync::Arc;
2121

2222
use arrow::datatypes::Field;
2323
use arrow::{
@@ -68,11 +68,6 @@ impl SmoothItUdf {
6868
}
6969

7070
impl WindowUDFImpl for SmoothItUdf {
71-
/// We implement as_any so that we can downcast the WindowUDFImpl trait object
72-
fn as_any(&self) -> &dyn Any {
73-
self
74-
}
75-
7671
/// Return the name of this function
7772
fn name(&self) -> &str {
7873
"smooth_it"
@@ -176,10 +171,6 @@ impl SimplifySmoothItUdf {
176171
}
177172
}
178173
impl WindowUDFImpl for SimplifySmoothItUdf {
179-
fn as_any(&self) -> &dyn Any {
180-
self
181-
}
182-
183174
fn name(&self) -> &str {
184175
"simplify_smooth_it"
185176
}

datafusion/core/tests/user_defined/user_defined_aggregates.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! This module contains end to end demonstrations of creating
1919
//! user defined aggregate functions
2020
21-
use std::any::Any;
2221
use std::collections::HashMap;
2322
use std::hash::{Hash, Hasher};
2423
use std::mem::{size_of, size_of_val};
@@ -793,10 +792,6 @@ struct TestGroupsAccumulator {
793792
}
794793

795794
impl AggregateUDFImpl for TestGroupsAccumulator {
796-
fn as_any(&self) -> &dyn Any {
797-
self
798-
}
799-
800795
fn name(&self) -> &str {
801796
"geo_mean"
802797
}
@@ -936,10 +931,6 @@ impl MetadataBasedAggregateUdf {
936931
}
937932

938933
impl AggregateUDFImpl for MetadataBasedAggregateUdf {
939-
fn as_any(&self) -> &dyn Any {
940-
self
941-
}
942-
943934
fn name(&self) -> &str {
944935
&self.name
945936
}

datafusion/core/tests/user_defined/user_defined_window_functions.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use datafusion_physical_expr::{
4444
use std::collections::HashMap;
4545
use std::hash::{Hash, Hasher};
4646
use std::{
47-
any::Any,
4847
ops::Range,
4948
sync::{
5049
Arc,
@@ -546,10 +545,6 @@ impl OddCounter {
546545
}
547546

548547
impl WindowUDFImpl for SimpleWindowUDF {
549-
fn as_any(&self) -> &dyn Any {
550-
self
551-
}
552-
553548
fn name(&self) -> &str {
554549
"odd_counter"
555550
}
@@ -675,10 +670,6 @@ impl VariadicWindowUDF {
675670
}
676671

677672
impl WindowUDFImpl for VariadicWindowUDF {
678-
fn as_any(&self) -> &dyn Any {
679-
self
680-
}
681-
682673
fn name(&self) -> &str {
683674
"variadic_window_udf"
684675
}
@@ -819,10 +810,6 @@ impl MetadataBasedWindowUdf {
819810
}
820811

821812
impl WindowUDFImpl for MetadataBasedWindowUdf {
822-
fn as_any(&self) -> &dyn Any {
823-
self
824-
}
825-
826813
fn name(&self) -> &str {
827814
&self.name
828815
}

datafusion/expr/src/expr_fn.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use datafusion_common::{Column, Result, ScalarValue, Spans, TableReference, plan
4343
use datafusion_functions_window_common::field::WindowUDFFieldArgs;
4444
use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
4545
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
46-
use std::any::Any;
4746
use std::collections::HashMap;
4847
use std::fmt::Debug;
4948
use std::hash::Hash;
@@ -588,10 +587,6 @@ impl SimpleAggregateUDF {
588587
}
589588

590589
impl AggregateUDFImpl for SimpleAggregateUDF {
591-
fn as_any(&self) -> &dyn Any {
592-
self
593-
}
594-
595590
fn name(&self) -> &str {
596591
&self.name
597592
}
@@ -681,10 +676,6 @@ impl SimpleWindowUDF {
681676
}
682677

683678
impl WindowUDFImpl for SimpleWindowUDF {
684-
fn as_any(&self) -> &dyn Any {
685-
self
686-
}
687-
688679
fn name(&self) -> &str {
689680
&self.name
690681
}

datafusion/expr/src/test/function_stub.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
//!
2020
//! These are used to avoid a dependence on `datafusion-functions-aggregate` which live in a different crate
2121
22-
use std::any::Any;
23-
2422
use arrow::datatypes::{
2523
DECIMAL32_MAX_PRECISION, DECIMAL32_MAX_SCALE, DECIMAL64_MAX_PRECISION,
2624
DECIMAL64_MAX_SCALE, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE,
@@ -113,10 +111,6 @@ impl Default for Sum {
113111
}
114112

115113
impl AggregateUDFImpl for Sum {
116-
fn as_any(&self) -> &dyn Any {
117-
self
118-
}
119-
120114
fn name(&self) -> &str {
121115
"sum"
122116
}
@@ -245,10 +239,6 @@ impl Count {
245239
}
246240

247241
impl AggregateUDFImpl for Count {
248-
fn as_any(&self) -> &dyn Any {
249-
self
250-
}
251-
252242
fn name(&self) -> &str {
253243
"COUNT"
254244
}
@@ -332,10 +322,6 @@ impl Min {
332322
}
333323

334324
impl AggregateUDFImpl for Min {
335-
fn as_any(&self) -> &dyn Any {
336-
self
337-
}
338-
339325
fn name(&self) -> &str {
340326
"min"
341327
}
@@ -414,10 +400,6 @@ impl Max {
414400
}
415401

416402
impl AggregateUDFImpl for Max {
417-
fn as_any(&self) -> &dyn Any {
418-
self
419-
}
420-
421403
fn name(&self) -> &str {
422404
"max"
423405
}
@@ -476,10 +458,6 @@ impl Default for Avg {
476458
}
477459

478460
impl AggregateUDFImpl for Avg {
479-
fn as_any(&self) -> &dyn Any {
480-
self
481-
}
482-
483461
fn name(&self) -> &str {
484462
"avg"
485463
}

datafusion/expr/src/udaf.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct AggregateUDF {
8484

8585
impl PartialEq for AggregateUDF {
8686
fn eq(&self, other: &Self) -> bool {
87-
self.inner.dyn_eq(other.inner.as_any())
87+
self.inner.dyn_eq(other.inner.as_ref() as &dyn Any)
8888
}
8989
}
9090

@@ -415,7 +415,6 @@ where
415415
///
416416
/// /// Implement the AggregateUDFImpl trait for GeoMeanUdf
417417
/// impl AggregateUDFImpl for GeoMeanUdf {
418-
/// fn as_any(&self) -> &dyn Any { self }
419418
/// fn name(&self) -> &str { "geo_mean" }
420419
/// fn signature(&self) -> &Signature { &self.signature }
421420
/// fn return_type(&self, args: &[DataType]) -> Result<DataType> {
@@ -443,10 +442,7 @@ where
443442
/// // Call the function `geo_mean(col)`
444443
/// let expr = geometric_mean.call(vec![col("a")]);
445444
/// ```
446-
pub trait AggregateUDFImpl: Debug + DynEq + DynHash + Send + Sync {
447-
/// Returns this object as an [`Any`] trait object
448-
fn as_any(&self) -> &dyn Any;
449-
445+
pub trait AggregateUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
450446
/// Returns this function's name
451447
fn name(&self) -> &str;
452448

@@ -913,7 +909,7 @@ pub trait AggregateUDFImpl: Debug + DynEq + DynHash + Send + Sync {
913909

914910
impl PartialEq for dyn AggregateUDFImpl {
915911
fn eq(&self, other: &Self) -> bool {
916-
self.dyn_eq(other.as_any())
912+
self.dyn_eq(other)
917913
}
918914
}
919915

@@ -1231,10 +1227,6 @@ impl AliasedAggregateUDFImpl {
12311227

12321228
#[warn(clippy::missing_trait_methods)] // Delegates, so it should implement every single trait method
12331229
impl AggregateUDFImpl for AliasedAggregateUDFImpl {
1234-
fn as_any(&self) -> &dyn Any {
1235-
self
1236-
}
1237-
12381230
fn name(&self) -> &str {
12391231
self.inner.name()
12401232
}
@@ -1415,7 +1407,6 @@ mod test {
14151407
use datafusion_functions_aggregate_common::accumulator::{
14161408
AccumulatorArgs, StateFieldsArgs,
14171409
};
1418-
use std::any::Any;
14191410
use std::cmp::Ordering;
14201411
use std::hash::{DefaultHasher, Hash, Hasher};
14211412

@@ -1437,9 +1428,6 @@ mod test {
14371428
}
14381429

14391430
impl AggregateUDFImpl for AMeanUdf {
1440-
fn as_any(&self) -> &dyn Any {
1441-
self
1442-
}
14431431
fn name(&self) -> &str {
14441432
"a"
14451433
}
@@ -1477,9 +1465,6 @@ mod test {
14771465
}
14781466

14791467
impl AggregateUDFImpl for BMeanUdf {
1480-
fn as_any(&self) -> &dyn Any {
1481-
self
1482-
}
14831468
fn name(&self) -> &str {
14841469
"b"
14851470
}

datafusion/expr/src/udf_eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl UdfPointer for Arc<dyn ScalarUDFImpl + '_> {
9696

9797
impl UdfPointer for Arc<dyn AggregateUDFImpl + '_> {
9898
fn equals(&self, other: &(dyn AggregateUDFImpl + '_)) -> bool {
99-
self.as_ref().dyn_eq(other.as_any())
99+
self.as_ref().dyn_eq(other)
100100
}
101101

102102
fn hash_value(&self) -> u64 {
@@ -108,7 +108,7 @@ impl UdfPointer for Arc<dyn AggregateUDFImpl + '_> {
108108

109109
impl UdfPointer for Arc<dyn WindowUDFImpl + '_> {
110110
fn equals(&self, other: &(dyn WindowUDFImpl + '_)) -> bool {
111-
self.as_ref().dyn_eq(other.as_any())
111+
self.as_ref().dyn_eq(other as &dyn Any)
112112
}
113113

114114
fn hash_value(&self) -> u64 {

0 commit comments

Comments
 (0)