Repository navigation
Remove as_any from trait definitions #21572
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Is your feature request related to a problem or challenge?
Starting in #20812 I have opened PRs to remove the as_any function in our trait definitions. Since we have upgrade our MSRV to >1.86 we can take advantage of trait upcasting to get the same result with less boilerplate code.
Describe the solution you'd like
For all trait definitions, remove the as_any() function.
Add a trait object implementation to give the downcasting for easier usage. Here is an example for TableProvider
impl dyn TableProvider {
/// Returns `true` if the table provider is of type `T`.
///
/// Prefer this over `downcast_ref::<T>().is_some()`. Works correctly when
/// called on `Arc<dyn TableProvider>` via auto-deref.
pub fn is<T: TableProvider>(&self) -> bool {
(self as &dyn Any).is::<T>()
}
/// Attempts to downcast this table provider to a concrete type `T`,
/// returning `None` if the provider is not of that type.
///
/// Works correctly when called on `Arc<dyn TableProvider>` via auto-deref,
/// unlike `(&arc as &dyn Any).downcast_ref::<T>()` which would attempt to
/// downcast the `Arc` itself.
pub fn downcast_ref<T: TableProvider>(&self) -> Option<&T> {
(self as &dyn Any).downcast_ref()
}
}
Describe alternatives you've considered
We could leave code as is, but this is removing >1000 of lines of boilerplate code across the PRs.
Additional context
List of identified traits:
- PhysicalExpr: Remove
as_anyon thePhysicalExprtrait #21573 - TableSource: Remove trait function
as_anyfrom datafusion-datasource #21576 - UserDefinedLogicalNode: datafusion/expr/src/logical_plan/extension.rs
- FileSource: Remove trait function
as_anyfrom datafusion-datasource #21576 - FileMetadata: datafusion/execution/src/cache/cache_manager.rs
- FileFormat: Remove trait function
as_anyfrom datafusion-datasource #21576 - FileFormatFactory: Remove trait function
as_anyfrom datafusion-datasource #21576 - LazyBatchGenerator: datafusion/physical-plan/src/memory.rs
- DataSource: Remove trait function
as_anyfrom datafusion-datasource #21576 - Session: datafusion/session/src/session.rs
- DataSink: Remove trait function
as_anyfrom datafusion-datasource #21576 - ExtensionOptions: datafusion/common/src/config.rs
- FileType: datafusion/common/src/file_options/file_type.rs
- StandardWindowFunctionExpr: datafusion/physical-expr/src/window/standard_window_function_expr.rs
- WindowExpr: datafusion/physical-expr/src/window/window_expr.rs
- CustomMetricValue: datafusion/physical-expr-common/src/metrics/custom.rs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity
Remove `as_any` on the `PhysicalExpr` trait (#21573)
Remove trait function `as_any` from datafusion-datasource (#21576)
Remove `as_any` on the `PhysicalExpr` trait (apache#21573)
Remove trait function `as_any` from datafusion-datasource (apache#21576)
LorrensP-2158466 commentedon Sep 3, 2026
Hi @timsaucer, is it okey if I do some of these? It would be useful in my own projects :)