forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically register tables if ObjectStore root is configured (apac…
…he#4095) * squash * Debug test in CI :'( * Hashing inconsistency * Address Andy's concerns * Docs * Docs * fmt * treat empty string like None :( * clippy * PR feedback * Update datafusion/core/src/config.rs Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org> * Update datafusion/core/src/config.rs Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org> Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
- Loading branch information
Showing
6 changed files
with
291 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// 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. | ||
|
||
//! listing_schema contains a SchemaProvider that scans ObjectStores for tables automatically | ||
use crate::catalog::schema::SchemaProvider; | ||
use crate::datasource::datasource::TableProviderFactory; | ||
use crate::datasource::TableProvider; | ||
use datafusion_common::DataFusionError; | ||
use futures::TryStreamExt; | ||
use object_store::ObjectStore; | ||
use std::any::Any; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::path::Path; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
/// A `SchemaProvider` that scans an `ObjectStore` to automatically discover tables | ||
/// | ||
/// A subfolder relationship is assumed, i.e. given: | ||
/// authority = s3://host.example.com:3000 | ||
/// path = /data/tpch | ||
/// factory = `DeltaTableFactory` | ||
/// | ||
/// A table called "customer" will be registered for the folder: | ||
/// s3://host.example.com:3000/data/tpch/customer | ||
/// | ||
/// assuming it contains valid deltalake data, i.e: | ||
/// s3://host.example.com:3000/data/tpch/customer/part-00000-xxxx.snappy.parquet | ||
/// s3://host.example.com:3000/data/tpch/customer/_delta_log/ | ||
pub struct ListingSchemaProvider { | ||
authority: String, | ||
path: object_store::path::Path, | ||
factory: Arc<dyn TableProviderFactory>, | ||
store: Arc<dyn ObjectStore>, | ||
tables: Arc<Mutex<HashMap<String, Arc<dyn TableProvider>>>>, | ||
} | ||
|
||
impl ListingSchemaProvider { | ||
/// Create a new `ListingSchemaProvider` | ||
/// | ||
/// Arguments: | ||
/// `authority`: The scheme (i.e. s3://) + host (i.e. example.com:3000) | ||
/// `path`: The root path that contains subfolders which represent tables | ||
/// `factory`: The `TableProviderFactory` to use to instantiate tables for each subfolder | ||
/// `store`: The `ObjectStore` containing the table data | ||
pub fn new( | ||
authority: String, | ||
path: object_store::path::Path, | ||
factory: Arc<dyn TableProviderFactory>, | ||
store: Arc<dyn ObjectStore>, | ||
) -> Self { | ||
Self { | ||
authority, | ||
path, | ||
factory, | ||
store, | ||
tables: Arc::new(Mutex::new(HashMap::new())), | ||
} | ||
} | ||
|
||
/// Reload table information from ObjectStore | ||
pub async fn refresh(&self) -> datafusion_common::Result<()> { | ||
let entries: Vec<_> = self | ||
.store | ||
.list(Some(&self.path)) | ||
.await? | ||
.try_collect() | ||
.await?; | ||
let base = Path::new(self.path.as_ref()); | ||
let mut tables = HashSet::new(); | ||
for file in entries.iter() { | ||
let mut parent = Path::new(file.location.as_ref()); | ||
while let Some(p) = parent.parent() { | ||
if p == base { | ||
tables.insert(parent); | ||
} | ||
parent = p; | ||
} | ||
} | ||
for table in tables.iter() { | ||
let file_name = table | ||
.file_name() | ||
.ok_or_else(|| { | ||
DataFusionError::Internal("Cannot parse file name!".to_string()) | ||
})? | ||
.to_str() | ||
.ok_or_else(|| { | ||
DataFusionError::Internal("Cannot parse file name!".to_string()) | ||
})?; | ||
let table_name = table.to_str().ok_or_else(|| { | ||
DataFusionError::Internal("Cannot parse file name!".to_string()) | ||
})?; | ||
if !self.table_exist(file_name) { | ||
let table_name = format!("{}/{}", self.authority, table_name); | ||
let provider = self.factory.create(table_name.as_str()).await?; | ||
let _ = self.register_table(file_name.to_string(), provider.clone())?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl SchemaProvider for ListingSchemaProvider { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn table_names(&self) -> Vec<String> { | ||
self.tables | ||
.lock() | ||
.expect("Can't lock tables") | ||
.keys() | ||
.map(|it| it.to_string()) | ||
.collect() | ||
} | ||
|
||
fn table(&self, name: &str) -> Option<Arc<dyn TableProvider>> { | ||
self.tables | ||
.lock() | ||
.expect("Can't lock tables") | ||
.get(name) | ||
.cloned() | ||
} | ||
|
||
fn register_table( | ||
&self, | ||
name: String, | ||
table: Arc<dyn TableProvider>, | ||
) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>> { | ||
self.tables | ||
.lock() | ||
.expect("Can't lock tables") | ||
.insert(name, table.clone()); | ||
Ok(Some(table)) | ||
} | ||
|
||
fn deregister_table( | ||
&self, | ||
name: &str, | ||
) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>> { | ||
Ok(self.tables.lock().expect("Can't lock tables").remove(name)) | ||
} | ||
|
||
fn table_exist(&self, name: &str) -> bool { | ||
self.tables | ||
.lock() | ||
.expect("Can't lock tables") | ||
.contains_key(name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.