Skip to content

Commit 7b3c89f

Browse files
authored
feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax (#237)
- Add PaimonDdlHandler for CREATE TABLE, ALTER TABLE (ADD/DROP/RENAME COLUMN, RENAME TABLE) - Add PaimonTableFactory for CREATE EXTERNAL TABLE via DataFusion TableProviderFactory - Extend PaimonCatalogProvider/SchemaProvider with CREATE/DROP SCHEMA and DROP TABLE - Add arrow_to_paimon_type and arrow_fields_to_paimon to paimon::arrow for Arrow-to-Paimon type conversion - Support PRIMARY KEY (col, ...) constraint syntax in CREATE TABLE DDL
1 parent ecbf458 commit 7b3c89f

6 files changed

Lines changed: 2186 additions & 16 deletions

File tree

crates/integrations/datafusion/src/catalog.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! Paimon catalog integration for DataFusion.
1919
2020
use std::any::Any;
21+
use std::collections::HashMap;
2122
use std::fmt::Debug;
2223
use std::sync::Arc;
2324

@@ -86,6 +87,50 @@ impl CatalogProvider for PaimonCatalogProvider {
8687
"paimon catalog access thread panicked",
8788
)
8889
}
90+
91+
fn register_schema(
92+
&self,
93+
name: &str,
94+
_schema: Arc<dyn SchemaProvider>,
95+
) -> DFResult<Option<Arc<dyn SchemaProvider>>> {
96+
let catalog = Arc::clone(&self.catalog);
97+
let name = name.to_string();
98+
block_on_with_runtime(
99+
async move {
100+
catalog
101+
.create_database(&name, false, HashMap::new())
102+
.await
103+
.map_err(to_datafusion_error)?;
104+
Ok(Some(
105+
Arc::new(PaimonSchemaProvider::new(Arc::clone(&catalog), name))
106+
as Arc<dyn SchemaProvider>,
107+
))
108+
},
109+
"paimon catalog access thread panicked",
110+
)
111+
}
112+
113+
fn deregister_schema(
114+
&self,
115+
name: &str,
116+
cascade: bool,
117+
) -> DFResult<Option<Arc<dyn SchemaProvider>>> {
118+
let catalog = Arc::clone(&self.catalog);
119+
let name = name.to_string();
120+
block_on_with_runtime(
121+
async move {
122+
catalog
123+
.drop_database(&name, false, cascade)
124+
.await
125+
.map_err(to_datafusion_error)?;
126+
Ok(Some(
127+
Arc::new(PaimonSchemaProvider::new(Arc::clone(&catalog), name))
128+
as Arc<dyn SchemaProvider>,
129+
))
130+
},
131+
"paimon catalog access thread panicked",
132+
)
133+
}
89134
}
90135

91136
/// Represents a [`SchemaProvider`] for the Paimon [`Catalog`], managing
@@ -159,4 +204,36 @@ impl SchemaProvider for PaimonSchemaProvider {
159204
"paimon catalog access thread panicked",
160205
)
161206
}
207+
208+
fn register_table(
209+
&self,
210+
_name: String,
211+
table: Arc<dyn TableProvider>,
212+
) -> DFResult<Option<Arc<dyn TableProvider>>> {
213+
// DataFusion calls register_table after table creation, so we just
214+
// acknowledge it here.
215+
Ok(Some(table))
216+
}
217+
218+
fn deregister_table(&self, name: &str) -> DFResult<Option<Arc<dyn TableProvider>>> {
219+
let catalog = Arc::clone(&self.catalog);
220+
let identifier = Identifier::new(self.database.clone(), name);
221+
block_on_with_runtime(
222+
async move {
223+
// Try to get the table first so we can return it.
224+
let table = match catalog.get_table(&identifier).await {
225+
Ok(t) => t,
226+
Err(paimon::Error::TableNotExist { .. }) => return Ok(None),
227+
Err(e) => return Err(to_datafusion_error(e)),
228+
};
229+
let provider = PaimonTableProvider::try_new(table)?;
230+
catalog
231+
.drop_table(&identifier, false)
232+
.await
233+
.map_err(to_datafusion_error)?;
234+
Ok(Some(Arc::new(provider) as Arc<dyn TableProvider>))
235+
},
236+
"paimon catalog access thread panicked",
237+
)
238+
}
162239
}

0 commit comments

Comments
 (0)