18
18
use std:: sync:: Arc ;
19
19
20
20
use async_trait:: async_trait;
21
- use datafusion_common:: {
22
- error:: { DataFusionError , Result } ,
23
- HashMap , TableReference ,
24
- } ;
21
+ use datafusion_common:: { error:: Result , not_impl_err, HashMap , TableReference } ;
25
22
use datafusion_execution:: config:: SessionConfig ;
26
23
27
24
use crate :: { CatalogProvider , CatalogProviderList , SchemaProvider , TableProvider } ;
28
25
29
26
/// A schema provider that looks up tables in a cache
30
27
///
31
- /// This is created by the [`AsyncSchemaProvider::resolve`] method
28
+ /// Instances are created by the [`AsyncSchemaProvider::resolve`] method
32
29
#[ derive( Debug ) ]
33
30
struct ResolvedSchemaProvider {
34
31
owner_name : Option < String > ,
@@ -58,14 +55,14 @@ impl SchemaProvider for ResolvedSchemaProvider {
58
55
name : String ,
59
56
_table : Arc < dyn TableProvider > ,
60
57
) -> Result < Option < Arc < dyn TableProvider > > > {
61
- Err ( DataFusionError :: Execution ( format ! (
58
+ not_impl_err ! (
62
59
"Attempt to register table '{name}' with ResolvedSchemaProvider which is not supported"
63
- ) ) )
60
+ )
64
61
}
65
62
66
63
#[ allow( unused_variables) ]
67
64
fn deregister_table ( & self , name : & str ) -> Result < Option < Arc < dyn TableProvider > > > {
68
- Err ( DataFusionError :: Execution ( format ! ( "Attempt to deregister table '{name}' with ResolvedSchemaProvider which is not supported" ) ) )
65
+ not_impl_err ! ( "Attempt to deregister table '{name}' with ResolvedSchemaProvider which is not supported" )
69
66
}
70
67
71
68
fn table_exist ( & self , name : & str ) -> bool {
@@ -88,6 +85,15 @@ impl ResolvedSchemaProviderBuilder {
88
85
}
89
86
}
90
87
88
+ async fn resolve_table ( & mut self , table_name : & str ) -> Result < ( ) > {
89
+ if !self . cached_tables . contains_key ( table_name) {
90
+ let resolved_table = self . async_provider . table ( table_name) . await ?;
91
+ self . cached_tables
92
+ . insert ( table_name. to_string ( ) , resolved_table) ;
93
+ }
94
+ Ok ( ( ) )
95
+ }
96
+
91
97
fn finish ( self ) -> Arc < dyn SchemaProvider > {
92
98
let cached_tables = self
93
99
. cached_tables
@@ -103,7 +109,7 @@ impl ResolvedSchemaProviderBuilder {
103
109
104
110
/// A catalog provider that looks up schemas in a cache
105
111
///
106
- /// This is created by the [`AsyncCatalogProvider::resolve`] method
112
+ /// Instances are created by the [`AsyncCatalogProvider::resolve`] method
107
113
#[ derive( Debug ) ]
108
114
struct ResolvedCatalogProvider {
109
115
cached_schemas : HashMap < String , Arc < dyn SchemaProvider > > ,
@@ -148,7 +154,7 @@ impl ResolvedCatalogProviderBuilder {
148
154
149
155
/// A catalog provider list that looks up catalogs in a cache
150
156
///
151
- /// This is created by the [`AsyncCatalogProviderList::resolve`] method
157
+ /// Instances are created by the [`AsyncCatalogProviderList::resolve`] method
152
158
#[ derive( Debug ) ]
153
159
struct ResolvedCatalogProviderList {
154
160
cached_catalogs : HashMap < String , Arc < dyn CatalogProvider > > ,
@@ -205,6 +211,9 @@ pub trait AsyncSchemaProvider: Send + Sync {
205
211
/// This method will walk through the references and look them up once, creating a cache of table
206
212
/// providers. This cache will be returned as a synchronous TableProvider that can be used to plan
207
213
/// and execute a query containing the given references.
214
+ ///
215
+ /// This cache is intended to be short-lived for the execution of a single query. There is no mechanism
216
+ /// for refresh or eviction of stale entries.
208
217
async fn resolve (
209
218
& self ,
210
219
references : & [ TableReference ] ,
@@ -271,6 +280,9 @@ pub trait AsyncCatalogProvider: Send + Sync {
271
280
/// providers (each with their own cache of table providers). This cache will be returned as a
272
281
/// synchronous CatalogProvider that can be used to plan and execute a query containing the given
273
282
/// references.
283
+ ///
284
+ /// This cache is intended to be short-lived for the execution of a single query. There is no mechanism
285
+ /// for refresh or eviction of stale entries.
274
286
async fn resolve (
275
287
& self ,
276
288
references : & [ TableReference ] ,
@@ -310,13 +322,7 @@ pub trait AsyncCatalogProvider: Send + Sync {
310
322
// If we can't find the catalog don't bother checking the table
311
323
let Some ( schema) = schema else { continue } ;
312
324
313
- if !schema. cached_tables . contains_key ( reference. table ( ) ) {
314
- let resolved_table =
315
- schema. async_provider . table ( reference. table ( ) ) . await ?;
316
- schema
317
- . cached_tables
318
- . insert ( reference. table ( ) . to_string ( ) , resolved_table) ;
319
- }
325
+ schema. resolve_table ( reference. table ( ) ) . await ?;
320
326
}
321
327
322
328
let cached_schemas = cached_schemas
@@ -346,6 +352,9 @@ pub trait AsyncCatalogProviderList: Send + Sync {
346
352
/// providers, schema providers, and table providers. This cache will be returned as a
347
353
/// synchronous CatalogProvider that can be used to plan and execute a query containing the given
348
354
/// references.
355
+ ///
356
+ /// This cache is intended to be short-lived for the execution of a single query. There is no mechanism
357
+ /// for refresh or eviction of stale entries.
349
358
async fn resolve (
350
359
& self ,
351
360
references : & [ TableReference ] ,
@@ -405,13 +414,7 @@ pub trait AsyncCatalogProviderList: Send + Sync {
405
414
// If we can't find the catalog don't bother checking the table
406
415
let Some ( schema) = schema else { continue } ;
407
416
408
- if !schema. cached_tables . contains_key ( reference. table ( ) ) {
409
- let resolved_table =
410
- schema. async_provider . table ( reference. table ( ) ) . await ?;
411
- schema
412
- . cached_tables
413
- . insert ( reference. table ( ) . to_string ( ) , resolved_table) ;
414
- }
417
+ schema. resolve_table ( reference. table ( ) ) . await ?;
415
418
}
416
419
417
420
// Build the cached catalog provider list
0 commit comments