apache / apache/datafusion

Remove `as_any` from trait definitions

Open
#21,572 1 comment 0 reactions 1 assignee Claimed by @timsaucer View on GitHub
enhancement
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Is your feature request related to a problem or challenge?

Starting in https://github.com/apache/datafusion/pull/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::().is_some()`. Works correctly when
/// called on `Arc` via auto-deref.
pub fn is(&self) -> bool {
(self as &dyn Any).is::()
}

/// 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` via auto-deref,
/// unlike `(&arc as &dyn Any).downcast_ref::()` which would attempt to
/// downcast the `Arc` itself.
pub fn downcast_ref(&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:

- [x] PhysicalExpr: https://github.com/apache/datafusion/pull/21573
- [x] TableSource: https://github.com/apache/datafusion/pull/21576
- [ ] UserDefinedLogicalNode: datafusion/expr/src/logical_plan/extension.rs
- [x] FileSource: https://github.com/apache/datafusion/pull/21576
- [ ] FileMetadata: datafusion/execution/src/cache/cache_manager.rs
- [x] FileFormat: https://github.com/apache/datafusion/pull/21576
- [x] FileFormatFactory: https://github.com/apache/datafusion/pull/21576
- [ ] LazyBatchGenerator: datafusion/physical-plan/src/memory.rs
- [x] DataSource: https://github.com/apache/datafusion/pull/21576
- [ ] Session: datafusion/session/src/session.rs
- [x] DataSink: https://github.com/apache/datafusion/pull/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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.