2020
2121use std:: any:: Any ;
2222use std:: collections:: HashMap ;
23+ use std:: error:: Error ;
2324use std:: fmt:: Debug ;
2425use std:: sync:: Arc ;
2526
@@ -35,7 +36,7 @@ use datafusion::datasource::TableProvider;
3536use datafusion:: physical_plan:: ExecutionPlan ;
3637use datafusion_expr:: { CreateExternalTable , Expr , TableType } ;
3738use serde:: { Deserialize , Serialize } ;
38- use tonic:: transport:: Channel ;
39+ use tonic:: transport:: { Channel , ClientTlsConfig } ;
3940
4041pub mod codec;
4142mod exec;
@@ -107,16 +108,12 @@ impl FlightTableFactory {
107108 options : HashMap < String , String > ,
108109 ) -> datafusion:: common:: Result < FlightTable > {
109110 let origin = entry_point. into ( ) ;
110- let channel = Channel :: from_shared ( origin. clone ( ) )
111- . unwrap ( )
112- . connect ( )
113- . await
114- . map_err ( |e| DataFusionError :: External ( Box :: new ( e) ) ) ?;
111+ let channel = flight_channel ( & origin) . await ?;
115112 let metadata = self
116113 . driver
117114 . metadata ( channel. clone ( ) , & options)
118115 . await
119- . map_err ( |e| DataFusionError :: External ( Box :: new ( e ) ) ) ?;
116+ . map_err ( to_df_err ) ?;
120117 let num_rows = precision ( metadata. info . total_records ) ;
121118 let total_byte_size = precision ( metadata. info . total_bytes ) ;
122119 let logical_schema = metadata. schema ;
@@ -136,14 +133,6 @@ impl FlightTableFactory {
136133 }
137134}
138135
139- fn precision ( total : i64 ) -> Precision < usize > {
140- if total < 0 {
141- Precision :: Absent
142- } else {
143- Precision :: Exact ( total as usize )
144- }
145- }
146-
147136#[ async_trait]
148137impl TableProviderFactory for FlightTableFactory {
149138 async fn create (
@@ -292,7 +281,7 @@ impl TableProvider for FlightTable {
292281 . driver
293282 . metadata ( self . channel . clone ( ) , & self . options )
294283 . await
295- . map_err ( |e| DataFusionError :: External ( Box :: new ( e ) ) ) ?;
284+ . map_err ( to_df_err ) ?;
296285 Ok ( Arc :: new ( FlightExec :: try_new (
297286 metadata,
298287 projection,
@@ -304,3 +293,26 @@ impl TableProvider for FlightTable {
304293 Some ( self . stats . clone ( ) )
305294 }
306295}
296+
297+ fn to_df_err < E : Error + Send + Sync + ' static > ( err : E ) -> DataFusionError {
298+ DataFusionError :: External ( Box :: new ( err) )
299+ }
300+
301+ async fn flight_channel ( source : impl Into < String > ) -> datafusion:: common:: Result < Channel > {
302+ let tls_config = ClientTlsConfig :: new ( ) . with_enabled_roots ( ) ;
303+ Channel :: from_shared ( source. into ( ) )
304+ . map_err ( to_df_err) ?
305+ . tls_config ( tls_config)
306+ . map_err ( to_df_err) ?
307+ . connect ( )
308+ . await
309+ . map_err ( to_df_err)
310+ }
311+
312+ fn precision ( total : i64 ) -> Precision < usize > {
313+ if total < 0 {
314+ Precision :: Absent
315+ } else {
316+ Precision :: Exact ( total as usize )
317+ }
318+ }
0 commit comments