@@ -39,14 +39,90 @@ pub struct DefaultTableSource {
39
39
}
40
40
41
41
impl DefaultTableSource {
42
- /// Create a new DefaultTableSource to wrap a TableProvider
43
- pub fn new ( table_provider : Arc < dyn TableProvider > ) -> Self {
44
- Self { table_provider }
42
+ /// Wraps a [TableProvider] as a [TableSource], to be used in planning.
43
+ ///
44
+ /// # Example
45
+ /// ```
46
+ /// # use std::sync::Arc;
47
+ /// # use std::any::Any;
48
+ /// # use arrow::datatypes::{Schema, SchemaRef};
49
+ /// # use datafusion_expr::{Expr, TableType, TableSource};
50
+ /// # use datafusion_physical_plan::ExecutionPlan;
51
+ /// # use datafusion_common::Result;
52
+ /// # use datafusion_catalog::{TableProvider, default_table_source::DefaultTableSource};
53
+ /// # use datafusion_session::Session;
54
+ /// # use async_trait::async_trait;
55
+ ///
56
+ /// # #[derive(Debug, Eq, PartialEq)]
57
+ /// # struct MyTableProvider {};
58
+ ///
59
+ /// # #[async_trait]
60
+ /// # impl TableProvider for MyTableProvider {
61
+ /// # fn as_any(&self) -> &dyn Any { self }
62
+ /// # fn schema(&self) -> SchemaRef { Arc::new(Schema::empty()) }
63
+ /// # fn table_type(&self) -> TableType { TableType::Base }
64
+ /// # async fn scan(
65
+ /// # &self,
66
+ /// # _: &dyn Session,
67
+ /// # _: Option<&Vec<usize>>,
68
+ /// # _: &[Expr],
69
+ /// # _: Option<usize>,
70
+ /// # ) -> Result<Arc<dyn ExecutionPlan>> {
71
+ /// # unimplemented!()
72
+ /// # }
73
+ /// # }
74
+ ///
75
+ /// let provider = Arc::new(MyTableProvider {});
76
+ /// let table_source = DefaultTableSource::wrap(provider);
77
+ /// ```
78
+ pub fn wrap ( table_provider : Arc < dyn TableProvider > ) -> Arc < Self > {
79
+ Arc :: new ( Self { table_provider } )
45
80
}
46
81
47
- /// Attempt to downcast a TableSource to DefaultTableSource and access the
48
- /// TableProvider. This will only work with a TableSource created by DataFusion.
49
- pub fn unwrap_provider < T : TableProvider + ' static > (
82
+ /// Attempt to downcast a `TableSource` to `DefaultTableSource` and access
83
+ /// the [TableProvider]. This will only work with a [TableSource] created
84
+ /// by [`DefaultTableSource::wrap`].
85
+ ///
86
+ /// # Example
87
+ /// ```
88
+ /// # use std::sync::Arc;
89
+ /// # use std::any::Any;
90
+ /// # use arrow::datatypes::{Schema, SchemaRef};
91
+ /// # use datafusion_common::Result;
92
+ /// # use datafusion_expr::{Expr, TableType, TableSource};
93
+ /// # use datafusion_physical_plan::ExecutionPlan;
94
+ /// # use datafusion_catalog::{TableProvider, default_table_source::DefaultTableSource};
95
+ /// # use datafusion_session::Session;
96
+ /// # use async_trait::async_trait;
97
+ ///
98
+ /// # #[derive(Debug, Eq, PartialEq)]
99
+ /// # struct MyTableProvider {}
100
+ ///
101
+ /// # #[async_trait]
102
+ /// # impl TableProvider for MyTableProvider {
103
+ /// # fn as_any(&self) -> &dyn Any { self }
104
+ /// # fn schema(&self) -> SchemaRef { Arc::new(Schema::empty()) }
105
+ /// # fn table_type(&self) -> TableType { TableType::Base }
106
+ /// # async fn scan(
107
+ /// # &self,
108
+ /// # _: &dyn Session,
109
+ /// # _: Option<&Vec<usize>>,
110
+ /// # _: &[Expr],
111
+ /// # _: Option<usize>,
112
+ /// # ) -> Result<Arc<dyn ExecutionPlan>> {
113
+ /// # unimplemented!()
114
+ /// # }
115
+ /// # }
116
+ ///
117
+ /// # fn example() -> Result<()> {
118
+ /// let provider = Arc::new(MyTableProvider {});
119
+ /// let table_source: Arc<dyn TableSource> = DefaultTableSource::wrap(provider.clone());
120
+ /// let unwrapped = DefaultTableSource::unwrap::<MyTableProvider>(&table_source)?;
121
+ /// assert_eq!(provider.as_ref(), unwrapped);
122
+ /// # Ok(())
123
+ /// # }
124
+ /// ```
125
+ pub fn unwrap < T : TableProvider + ' static > (
50
126
source : & Arc < dyn TableSource > ,
51
127
) -> datafusion_common:: Result < & T > {
52
128
if let Some ( source) = source
@@ -106,14 +182,16 @@ impl TableSource for DefaultTableSource {
106
182
}
107
183
108
184
/// Wrap a TableProvider as a TableSource.
185
+ #[ deprecated( note = "use DefaultTableSource::wrap instead" ) ]
109
186
pub fn provider_as_source (
110
187
table_provider : Arc < dyn TableProvider > ,
111
188
) -> Arc < dyn TableSource > {
112
- Arc :: new ( DefaultTableSource :: new ( table_provider) )
189
+ DefaultTableSource :: wrap ( table_provider)
113
190
}
114
191
115
192
/// Attempt to downcast a TableSource to DefaultTableSource and access the
116
193
/// TableProvider. This will only work with a TableSource created by DataFusion.
194
+ #[ deprecated( note = "use DefaultTableSource::unwrap instead" ) ]
117
195
pub fn source_as_provider (
118
196
source : & Arc < dyn TableSource > ,
119
197
) -> datafusion_common:: Result < Arc < dyn TableProvider > > {
@@ -161,6 +239,6 @@ fn preserves_table_type() {
161
239
}
162
240
}
163
241
164
- let table_source = DefaultTableSource :: new ( Arc :: new ( TestTempTable ) ) ;
242
+ let table_source = DefaultTableSource :: wrap ( Arc :: new ( TestTempTable ) ) ;
165
243
assert_eq ! ( table_source. table_type( ) , TableType :: Temporary ) ;
166
244
}
0 commit comments