@@ -23,12 +23,11 @@ use std::path::PathBuf;
2323use  std:: sync:: { Arc ,  Mutex } ; 
2424
2525use  ballista_core:: config:: BallistaConfig ; 
26- use  ballista_core:: { 
27-     datasource:: DfTableAdapter ,  utils:: create_df_ctx_with_ballista_query_planner, 
28- } ; 
26+ use  ballista_core:: utils:: create_df_ctx_with_ballista_query_planner; 
2927
3028use  datafusion:: catalog:: TableReference ; 
3129use  datafusion:: dataframe:: DataFrame ; 
30+ use  datafusion:: datasource:: TableProvider ; 
3231use  datafusion:: error:: { DataFusionError ,  Result } ; 
3332use  datafusion:: execution:: dataframe_impl:: DataFrameImpl ; 
3433use  datafusion:: logical_plan:: LogicalPlan ; 
@@ -44,7 +43,7 @@ struct BallistaContextState {
4443    /// Scheduler port 
4544scheduler_port :  u16 , 
4645    /// Tables that have been registered with this context 
47- tables :  HashMap < String ,  LogicalPlan > , 
46+ tables :  HashMap < String ,  Arc < dyn   TableProvider > > , 
4847} 
4948
5049impl  BallistaContextState  { 
@@ -197,11 +196,13 @@ impl BallistaContext {
197196    } 
198197
199198    /// Register a DataFrame as a table that can be referenced from a SQL query 
200- pub  fn  register_table ( & self ,  name :  & str ,  table :  & dyn  DataFrame )  -> Result < ( ) >  { 
199+ pub  fn  register_table ( 
200+         & self , 
201+         name :  & str , 
202+         table :  Arc < dyn  TableProvider > , 
203+     )  -> Result < ( ) >  { 
201204        let  mut  state = self . state . lock ( ) . unwrap ( ) ; 
202-         state
203-             . tables 
204-             . insert ( name. to_owned ( ) ,  table. to_logical_plan ( ) ) ; 
205+         state. tables . insert ( name. to_owned ( ) ,  table) ; 
205206        Ok ( ( ) ) 
206207    } 
207208
@@ -211,13 +212,17 @@ impl BallistaContext {
211212        path :  & str , 
212213        options :  CsvReadOptions , 
213214    )  -> Result < ( ) >  { 
214-         let  df = self . read_csv ( path,  options) ?; 
215-         self . register_table ( name,  df. as_ref ( ) ) 
215+         match  self . read_csv ( path,  options) ?. to_logical_plan ( )  { 
216+             LogicalPlan :: TableScan  {  source,  .. }  => self . register_table ( name,  source) , 
217+             _ => Err ( DataFusionError :: Internal ( "Expected tables scan" . to_owned ( ) ) ) , 
218+         } 
216219    } 
217220
218221    pub  fn  register_parquet ( & self ,  name :  & str ,  path :  & str )  -> Result < ( ) >  { 
219-         let  df = self . read_parquet ( path) ?; 
220-         self . register_table ( name,  df. as_ref ( ) ) 
222+         match  self . read_parquet ( path) ?. to_logical_plan ( )  { 
223+             LogicalPlan :: TableScan  {  source,  .. }  => self . register_table ( name,  source) , 
224+             _ => Err ( DataFusionError :: Internal ( "Expected tables scan" . to_owned ( ) ) ) , 
225+         } 
221226    } 
222227
223228    pub  fn  register_avro ( 
@@ -226,9 +231,10 @@ impl BallistaContext {
226231        path :  & str , 
227232        options :  AvroReadOptions , 
228233    )  -> Result < ( ) >  { 
229-         let  df = self . read_avro ( path,  options) ?; 
230-         self . register_table ( name,  df. as_ref ( ) ) ?; 
231-         Ok ( ( ) ) 
234+         match  self . read_avro ( path,  options) ?. to_logical_plan ( )  { 
235+             LogicalPlan :: TableScan  {  source,  .. }  => self . register_table ( name,  source) , 
236+             _ => Err ( DataFusionError :: Internal ( "Expected tables scan" . to_owned ( ) ) ) , 
237+         } 
232238    } 
233239
234240    /// Create a DataFrame from a SQL statement 
@@ -245,12 +251,10 @@ impl BallistaContext {
245251        // register tables with DataFusion context 
246252        { 
247253            let  state = self . state . lock ( ) . unwrap ( ) ; 
248-             for  ( name,  plan)  in  & state. tables  { 
249-                 let  plan = ctx. optimize ( plan) ?; 
250-                 let  execution_plan = ctx. create_physical_plan ( & plan) ?; 
254+             for  ( name,  prov)  in  & state. tables  { 
251255                ctx. register_table ( 
252256                    TableReference :: Bare  {  table :  name } , 
253-                     Arc :: new ( DfTableAdapter :: new ( plan ,  execution_plan ) ) , 
257+                     Arc :: clone ( prov ) , 
254258                ) ?; 
255259            } 
256260        } 
0 commit comments