Skip to content

Commit bbc6908

Browse files
committed
Address PR reviews
1 parent 372c920 commit bbc6908

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

datafusion/catalog/src/async.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
use std::sync::Arc;
1919

2020
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};
2522
use datafusion_execution::config::SessionConfig;
2623

2724
use crate::{CatalogProvider, CatalogProviderList, SchemaProvider, TableProvider};
2825

2926
/// A schema provider that looks up tables in a cache
3027
///
31-
/// This is created by the [`AsyncSchemaProvider::resolve`] method
28+
/// Instances are created by the [`AsyncSchemaProvider::resolve`] method
3229
#[derive(Debug)]
3330
struct ResolvedSchemaProvider {
3431
owner_name: Option<String>,
@@ -58,14 +55,14 @@ impl SchemaProvider for ResolvedSchemaProvider {
5855
name: String,
5956
_table: Arc<dyn TableProvider>,
6057
) -> Result<Option<Arc<dyn TableProvider>>> {
61-
Err(DataFusionError::Execution(format!(
58+
not_impl_err!(
6259
"Attempt to register table '{name}' with ResolvedSchemaProvider which is not supported"
63-
)))
60+
)
6461
}
6562

6663
#[allow(unused_variables)]
6764
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")
6966
}
7067

7168
fn table_exist(&self, name: &str) -> bool {
@@ -88,6 +85,15 @@ impl ResolvedSchemaProviderBuilder {
8885
}
8986
}
9087

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+
9197
fn finish(self) -> Arc<dyn SchemaProvider> {
9298
let cached_tables = self
9399
.cached_tables
@@ -103,7 +109,7 @@ impl ResolvedSchemaProviderBuilder {
103109

104110
/// A catalog provider that looks up schemas in a cache
105111
///
106-
/// This is created by the [`AsyncCatalogProvider::resolve`] method
112+
/// Instances are created by the [`AsyncCatalogProvider::resolve`] method
107113
#[derive(Debug)]
108114
struct ResolvedCatalogProvider {
109115
cached_schemas: HashMap<String, Arc<dyn SchemaProvider>>,
@@ -148,7 +154,7 @@ impl ResolvedCatalogProviderBuilder {
148154

149155
/// A catalog provider list that looks up catalogs in a cache
150156
///
151-
/// This is created by the [`AsyncCatalogProviderList::resolve`] method
157+
/// Instances are created by the [`AsyncCatalogProviderList::resolve`] method
152158
#[derive(Debug)]
153159
struct ResolvedCatalogProviderList {
154160
cached_catalogs: HashMap<String, Arc<dyn CatalogProvider>>,
@@ -205,6 +211,9 @@ pub trait AsyncSchemaProvider: Send + Sync {
205211
/// This method will walk through the references and look them up once, creating a cache of table
206212
/// providers. This cache will be returned as a synchronous TableProvider that can be used to plan
207213
/// 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.
208217
async fn resolve(
209218
&self,
210219
references: &[TableReference],
@@ -271,6 +280,9 @@ pub trait AsyncCatalogProvider: Send + Sync {
271280
/// providers (each with their own cache of table providers). This cache will be returned as a
272281
/// synchronous CatalogProvider that can be used to plan and execute a query containing the given
273282
/// 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.
274286
async fn resolve(
275287
&self,
276288
references: &[TableReference],
@@ -310,13 +322,7 @@ pub trait AsyncCatalogProvider: Send + Sync {
310322
// If we can't find the catalog don't bother checking the table
311323
let Some(schema) = schema else { continue };
312324

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?;
320326
}
321327

322328
let cached_schemas = cached_schemas
@@ -346,6 +352,9 @@ pub trait AsyncCatalogProviderList: Send + Sync {
346352
/// providers, schema providers, and table providers. This cache will be returned as a
347353
/// synchronous CatalogProvider that can be used to plan and execute a query containing the given
348354
/// 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.
349358
async fn resolve(
350359
&self,
351360
references: &[TableReference],
@@ -405,13 +414,7 @@ pub trait AsyncCatalogProviderList: Send + Sync {
405414
// If we can't find the catalog don't bother checking the table
406415
let Some(schema) = schema else { continue };
407416

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?;
415418
}
416419

417420
// Build the cached catalog provider list

0 commit comments

Comments
 (0)