Skip to content

Commit 39b5971

Browse files
committed
Add default in memory options for adding schema and catalogs
1 parent 987b6c7 commit 39b5971

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

python/datafusion/catalog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def database(self, name: str = "public") -> Schema:
7373
"""Returns the database with the given ``name`` from this catalog."""
7474
return self.schema(name)
7575

76+
def new_in_memory_schema(self, name: str) -> Schema:
77+
"""Create a new schema in this catalog using an in-memory provider."""
78+
self.catalog.new_in_memory_schema(name)
79+
return self.schema(name)
80+
7681
def register_schema(self, name, schema) -> Schema | None:
7782
"""Register a schema with this catalog."""
7883
return self.catalog.register_schema(name, schema)

python/datafusion/context.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,15 @@ def deregister_table(self, name: str) -> None:
755755
"""Remove a table from the session."""
756756
self.ctx.deregister_table(name)
757757

758+
def catalog_names(self) -> set[str]:
759+
"""Returns the list of catalogs in this context."""
760+
return self.ctx.catalog_names()
761+
762+
def new_in_memory_catalog(self, name: str) -> Catalog:
763+
"""Create a new catalog in this context using an in-memory provider."""
764+
self.ctx.new_in_memory_catalog(name)
765+
return self.catalog(name)
766+
758767
def register_catalog_provider(
759768
self, name: str, provider: CatalogProviderExportable
760769
) -> None:

src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::dataset::Dataset;
1919
use crate::errors::{py_datafusion_err, to_datafusion_err, PyDataFusionError, PyDataFusionResult};
2020
use crate::utils::{validate_pycapsule, wait_for_future};
2121
use async_trait::async_trait;
22+
use datafusion::catalog::MemorySchemaProvider;
2223
use datafusion::common::DataFusionError;
2324
use datafusion::{
2425
arrow::pyarrow::ToPyArrow,
@@ -105,6 +106,16 @@ impl PyCatalog {
105106
})
106107
}
107108

109+
fn new_in_memory_schema(&mut self, name: &str) -> PyResult<()> {
110+
let schema = Arc::new(MemorySchemaProvider::new()) as Arc<dyn SchemaProvider>;
111+
let _ = self
112+
.catalog
113+
.register_schema(name, schema)
114+
.map_err(py_datafusion_err)?;
115+
116+
Ok(())
117+
}
118+
108119
fn register_schema(&self, name: &str, schema_provider: Bound<'_, PyAny>) -> PyResult<()> {
109120
let provider = if schema_provider.hasattr("__datafusion_schema_provider__")? {
110121
let capsule = schema_provider

src/context.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::utils::{get_global_ctx, get_tokio_runtime, validate_pycapsule, wait_f
4949
use datafusion::arrow::datatypes::{DataType, Schema, SchemaRef};
5050
use datafusion::arrow::pyarrow::PyArrowType;
5151
use datafusion::arrow::record_batch::RecordBatch;
52-
use datafusion::catalog::CatalogProvider;
52+
use datafusion::catalog::{CatalogProvider, MemoryCatalogProvider};
5353
use datafusion::common::TableReference;
5454
use datafusion::common::{exec_err, ScalarValue};
5555
use datafusion::datasource::file_format::file_compression_type::FileCompressionType;
@@ -612,6 +612,13 @@ impl PySessionContext {
612612
Ok(())
613613
}
614614

615+
pub fn new_in_memory_catalog(&mut self, name: &str) -> PyResult<()> {
616+
let catalog = Arc::new(MemoryCatalogProvider::new()) as Arc<dyn CatalogProvider>;
617+
let _ = self.ctx.register_catalog(name, catalog);
618+
619+
Ok(())
620+
}
621+
615622
pub fn register_catalog_provider(
616623
&mut self,
617624
name: &str,
@@ -870,6 +877,10 @@ impl PySessionContext {
870877
})
871878
}
872879

880+
pub fn catalog_names(&self) -> HashSet<String> {
881+
self.ctx.catalog_names().into_iter().collect()
882+
}
883+
873884
pub fn tables(&self) -> HashSet<String> {
874885
self.ctx
875886
.catalog_names()

0 commit comments

Comments
 (0)