houqp commented on pull request #1712:
URL: 
https://github.com/apache/arrow-datafusion/pull/1712#issuecomment-1025262177


   @cpcloud good point on lack of access to the trait_upcasting feature :( I 
found an interesting workaround for this online based on 
https://articles.bchlr.de/traits-dynamic-dispatch-upcasting:
   
   ```rust
   diff --git a/datafusion/src/datasource/datasource.rs 
b/datafusion/src/datasource/datasource.rs
   index 1b59c857f..703d14493 100644
   --- a/datafusion/src/datasource/datasource.rs
   +++ b/datafusion/src/datasource/datasource.rs
   @@ -55,9 +55,15 @@ pub enum TableType {
        Temporary,
    }
    
   +pub trait AsDynTableProvider {
   +    fn as_dyn_table<'a>(self: Arc<Self>) -> Arc<dyn TableProvider + 'a>
   +    where
   +        Self: 'a;
   +}
   +
    /// Source table
    #[async_trait]
   -pub trait TableProvider: Sync + Send {
   +pub trait TableProvider: Sync + Send + AsDynTableProvider {
        /// Returns the table provider as [`Any`](std::any::Any) so that it can 
be
        /// downcast to a specific implementation.
        fn as_any(&self) -> &dyn Any;
   @@ -94,3 +100,12 @@ pub trait TableProvider: Sync + Send {
            Ok(TableProviderFilterPushDown::Unsupported)
        }
    }
   +
   +impl<T: TableProvider + Sized> AsDynTableProvider for T {
   +    fn as_dyn_table<'a>(self: Arc<Self>) -> Arc<dyn TableProvider + 'a>
   +    where
   +        Self: 'a,
   +    {
   +        self
   +    }
   +}
   diff --git a/datafusion/src/execution/dataframe_impl.rs 
b/datafusion/src/execution/dataframe_impl.rs
   index c1933adaa..eb59e3a93 100644
   --- a/datafusion/src/execution/dataframe_impl.rs
   +++ b/datafusion/src/execution/dataframe_impl.rs
   @@ -550,7 +550,7 @@ mod tests {
            let mut ctx = ExecutionContext::new();
    
            // register a dataframe as a table
   -        ctx.register_table("test_table", df)?;
   +        ctx.register_table("test_table", df.as_dyn_table())?;
            Ok(())
        }
   ```
   
   The schema method name conflict is unfortunate and I don't have a good 
solution for that other than renaming one of the methods :( Perhaps other 
contributors can jump in to help suggest workarounds.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to