@@ -175,6 +175,26 @@ impl SessionContext {
175175 Self :: with_config ( SessionConfig :: new ( ) )
176176 }
177177
178+ /// Finds any ListSchemaProviders and instructs them to reload tables from "disk"
179+ pub async fn refresh_catalogs ( & self ) -> Result < ( ) > {
180+ let cat_names = self . catalog_names ( ) . clone ( ) ;
181+ for cat_name in cat_names. iter ( ) {
182+ let cat = self . catalog ( cat_name. as_str ( ) ) . ok_or_else ( || {
183+ DataFusionError :: Internal ( "Catalog not found!" . to_string ( ) )
184+ } ) ?;
185+ for schema_name in cat. schema_names ( ) {
186+ let schema = cat. schema ( schema_name. as_str ( ) ) . ok_or_else ( || {
187+ DataFusionError :: Internal ( "Schema not found!" . to_string ( ) )
188+ } ) ?;
189+ let lister = schema. as_any ( ) . downcast_ref :: < ListingSchemaProvider > ( ) ;
190+ if let Some ( lister) = lister {
191+ lister. refresh ( & self . state ( ) ) . await ?;
192+ }
193+ }
194+ }
195+ Ok ( ( ) )
196+ }
197+
178198 /// Creates a new session context using the provided session configuration.
179199 pub fn with_config ( config : SessionConfig ) -> Self {
180200 let runtime = Arc :: new ( RuntimeEnv :: default ( ) ) ;
@@ -486,7 +506,7 @@ impl SessionContext {
486506 cmd. file_type
487507 ) )
488508 } ) ?;
489- let table = ( * factory) . create ( cmd. location . as_str ( ) ) . await ?;
509+ let table = ( * factory) . create ( & state , cmd. location . as_str ( ) ) . await ?;
490510 self . register_table ( cmd. name . as_str ( ) , table) ?;
491511 let plan = LogicalPlanBuilder :: empty ( false ) . build ( ) ?;
492512 Ok ( Arc :: new ( DataFrame :: new ( self . state . clone ( ) , & plan) ) )
@@ -1764,7 +1784,7 @@ impl ContextProvider for SessionState {
17641784 Ok ( schema) => {
17651785 let provider = schema. table ( resolved_ref. table ) . ok_or_else ( || {
17661786 DataFusionError :: Plan ( format ! (
1767- "'{}.{}.{}' not found" ,
1787+ "table '{}.{}.{}' not found" ,
17681788 resolved_ref. catalog, resolved_ref. schema, resolved_ref. table
17691789 ) )
17701790 } ) ?;
@@ -2005,11 +2025,12 @@ mod tests {
20052025 use super :: * ;
20062026 use crate :: assert_batches_eq;
20072027 use crate :: datasource:: datasource:: TableProviderFactory ;
2028+ use crate :: datasource:: listing_table_factory:: ListingTableFactory ;
20082029 use crate :: execution:: context:: QueryPlanner ;
20092030 use crate :: execution:: runtime_env:: RuntimeConfig ;
20102031 use crate :: physical_plan:: expressions:: AvgAccumulator ;
20112032 use crate :: test;
2012- use crate :: test_util:: { parquet_test_data, TestTableFactory } ;
2033+ use crate :: test_util:: parquet_test_data;
20132034 use crate :: variable:: VarType ;
20142035 use arrow:: array:: ArrayRef ;
20152036 use arrow:: datatypes:: * ;
@@ -2267,30 +2288,33 @@ mod tests {
22672288
22682289 let mut table_factories: HashMap < String , Arc < dyn TableProviderFactory > > =
22692290 HashMap :: new ( ) ;
2270- table_factories. insert ( "test" . to_string ( ) , Arc :: new ( TestTableFactory { } ) ) ;
2291+ let factory = Arc :: new ( ListingTableFactory :: new ( FileType :: CSV ) ) ;
2292+ table_factories. insert ( "test" . to_string ( ) , factory) ;
22712293 let rt_cfg = RuntimeConfig :: new ( ) . with_table_factories ( table_factories) ;
22722294 let runtime = Arc :: new ( RuntimeEnv :: new ( rt_cfg) . unwrap ( ) ) ;
22732295 let cfg = SessionConfig :: new ( )
22742296 . set_str ( "datafusion.catalog.location" , url. as_str ( ) )
22752297 . set_str ( "datafusion.catalog.type" , "test" ) ;
22762298 let session_state = SessionState :: with_config_rt ( cfg, runtime) ;
22772299 let ctx = SessionContext :: with_state ( session_state) ;
2300+ ctx. refresh_catalogs ( ) . await ?;
22782301
2279- let mut table_count = 0 ;
2280- for cat_name in ctx. catalog_names ( ) . iter ( ) {
2281- let cat = ctx. catalog ( cat_name) . unwrap ( ) ;
2282- for s_name in cat. schema_names ( ) . iter ( ) {
2283- let schema = cat. schema ( s_name) . unwrap ( ) ;
2284- if let Some ( listing) =
2285- schema. as_any ( ) . downcast_ref :: < ListingSchemaProvider > ( )
2286- {
2287- listing. refresh ( ) . await . unwrap ( ) ;
2288- table_count = schema. table_names ( ) . len ( ) ;
2289- }
2290- }
2291- }
2302+ let result =
2303+ plan_and_collect ( & ctx, "select c_name from default.customer limit 3;" )
2304+ . await ?;
2305+
2306+ let actual = arrow:: util:: pretty:: pretty_format_batches ( & result)
2307+ . unwrap ( )
2308+ . to_string ( ) ;
2309+ let expected = r#"+--------------------+
2310+ | c_name |
2311+ +--------------------+
2312+ | Customer#000000002 |
2313+ | Customer#000000003 |
2314+ | Customer#000000004 |
2315+ +--------------------+"# ;
2316+ assert_eq ! ( actual, expected) ;
22922317
2293- assert_eq ! ( table_count, 8 ) ;
22942318 Ok ( ( ) )
22952319 }
22962320
0 commit comments