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