alamb commented on code in PR #11035: URL: https://github.com/apache/datafusion/pull/11035#discussion_r1687039780
########## datafusion/core/src/catalog/dynamic_file_schema.rs: ########## @@ -0,0 +1,197 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! dynamic_file_schema contains a SchemaProvider that creates tables from file paths + +use std::any::Any; +use std::sync::Arc; + +use async_trait::async_trait; +#[cfg(feature = "dirs")] +use dirs::home_dir; + +use datafusion_common::plan_datafusion_err; + +use crate::catalog::schema::SchemaProvider; +use crate::datasource::listing::{ListingTable, ListingTableConfig, ListingTableUrl}; +use crate::datasource::TableProvider; +use crate::error::Result; +use crate::execution::session_state::StateStore; + +/// Implements the [DynamicFileSchemaProvider] that can create tables provider from the file path. +/// +/// The provider will try to create a table provider from the file path if the table provider +/// isn't exist in the inner schema provider. The required object store must be registered in the session context. +pub struct DynamicFileSchemaProvider { + inner: Arc<dyn SchemaProvider>, + factory: Arc<dyn UrlTableFactory>, +} + +impl DynamicFileSchemaProvider { + /// Create a new [DynamicFileSchemaProvider] with the given inner schema provider. + pub fn new( + inner: Arc<dyn SchemaProvider>, + factory: Arc<dyn UrlTableFactory>, + ) -> Self { + Self { inner, factory } + } +} + +#[async_trait] +impl SchemaProvider for DynamicFileSchemaProvider { + fn as_any(&self) -> &dyn Any { + self + } + + fn table_names(&self) -> Vec<String> { + self.inner.table_names() + } + + fn register_table( + &self, + name: String, + table: Arc<dyn TableProvider>, + ) -> Result<Option<Arc<dyn TableProvider>>> { + self.inner.register_table(name, table) + } + + async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> { + if let Ok(Some(inner_table)) = self.inner.table(name).await { + return Ok(Some(inner_table)); + } + + let optimized_url = substitute_tilde(name.to_owned()); + self.factory.try_new(optimized_url.as_str()).await + } + + fn deregister_table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> { + self.inner.deregister_table(name) + } + + fn table_exist(&self, name: &str) -> bool { + self.inner.table_exist(name) + } +} + +/// Substitute the tilde character in the file path with the user home directory. +#[cfg(feature = "dirs")] +pub fn substitute_tilde(cur: String) -> String { + if let Some(usr_dir_path) = home_dir() { + if let Some(usr_dir) = usr_dir_path.to_str() { + if cur.starts_with('~') && !usr_dir.is_empty() { + return cur.replacen('~', usr_dir, 1); + } + } + } + cur +} + +/// Do nothing if the feature "dirs" is disabled. +#[cfg(not(feature = "dirs"))] +pub fn substitute_tilde(cur: String) -> String { + cur +} + +/// [UrlTableFactory] is a factory that can create a table provider from the given url. +#[async_trait] +pub trait UrlTableFactory: Sync + Send { + /// create a new table provider from the provided url + async fn try_new(&self, url: &str) -> Result<Option<Arc<dyn TableProvider>>>; +} + +/// [DynamicListTableFactory] is a factory that can create a [ListingTable] from the given url. +#[derive(Default)] +pub struct DynamicListTableFactory { + state_store: Arc<StateStore>, +} + +impl DynamicListTableFactory { + /// Create a new [DynamicListTableFactory] with the given state store. + pub fn new(state_store: Arc<StateStore>) -> Self { + Self { state_store } + } +} Review Comment: Here is an alternate proposal for removing the SessionState dependency: https://github.com/apache/datafusion/pull/11516 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org