-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Create Schema functionality in SQL #1959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1448bce
bec0c7a
0f0d150
73d01d5
bc65517
877ccd6
71d5a46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,8 @@ use crate::datasource::object_store::{ObjectStore, ObjectStoreRegistry}; | |
use crate::datasource::TableProvider; | ||
use crate::error::{DataFusionError, Result}; | ||
use crate::logical_plan::{ | ||
CreateExternalTable, CreateMemoryTable, DropTable, FunctionRegistry, LogicalPlan, | ||
LogicalPlanBuilder, UNNAMED_TABLE, | ||
CreateCatalogSchema, CreateExternalTable, CreateMemoryTable, DropTable, | ||
FunctionRegistry, LogicalPlan, LogicalPlanBuilder, UNNAMED_TABLE, | ||
}; | ||
use crate::optimizer::common_subexpr_eliminate::CommonSubexprEliminate; | ||
use crate::optimizer::filter_push_down::FilterPushDown; | ||
|
@@ -165,7 +165,7 @@ impl ExecutionContext { | |
let default_catalog = MemoryCatalogProvider::new(); | ||
|
||
default_catalog.register_schema( | ||
config.default_schema.clone(), | ||
config.default_schema.as_str(), | ||
Arc::new(MemorySchemaProvider::new()), | ||
); | ||
|
||
|
@@ -250,7 +250,6 @@ impl ExecutionContext { | |
} else { | ||
Some(Arc::new(schema.as_ref().to_owned().into())) | ||
}; | ||
|
||
self.register_listing_table(name, location, options, provided_schema) | ||
.await?; | ||
let plan = LogicalPlanBuilder::empty(false).build()?; | ||
|
@@ -286,6 +285,39 @@ impl ExecutionContext { | |
Ok(Arc::new(DataFrame::new(self.state.clone(), &plan))) | ||
} | ||
} | ||
LogicalPlan::CreateCatalogSchema(CreateCatalogSchema { | ||
schema_name, | ||
if_not_exists, | ||
.. | ||
}) => { | ||
// sqlparser doesnt accept database / catalog as parameter to CREATE SCHEMA | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe worth a ticket to sqlparser to support this? Or maybe just a PR :) |
||
// so for now, we default to "datafusion" catalog | ||
let default_catalog = "datafusion"; | ||
let catalog = self.catalog(default_catalog).ok_or_else(|| { | ||
DataFusionError::Execution(String::from( | ||
"Missing 'datafusion' catalog", | ||
)) | ||
})?; | ||
|
||
let schema = catalog.schema(&schema_name); | ||
|
||
match (if_not_exists, schema) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
(true, Some(_)) => { | ||
let plan = LogicalPlanBuilder::empty(false).build()?; | ||
Ok(Arc::new(DataFrame::new(self.state.clone(), &plan))) | ||
} | ||
(true, None) | (false, None) => { | ||
let schema = Arc::new(MemorySchemaProvider::new()); | ||
catalog.register_schema(&schema_name, schema); | ||
let plan = LogicalPlanBuilder::empty(false).build()?; | ||
Ok(Arc::new(DataFrame::new(self.state.clone(), &plan))) | ||
} | ||
(false, Some(_)) => Err(DataFusionError::Execution(format!( | ||
"Schema '{:?}' already exists", | ||
schema_name | ||
))), | ||
} | ||
} | ||
|
||
plan => Ok(Arc::new(DataFrame::new( | ||
self.state.clone(), | ||
|
@@ -535,10 +567,18 @@ impl ExecutionContext { | |
|
||
let state = self.state.lock(); | ||
let catalog = if state.config.information_schema { | ||
Arc::new(CatalogWithInformationSchema::new( | ||
Arc::downgrade(&state.catalog_list), | ||
catalog, | ||
)) | ||
let is = state | ||
.catalog_list | ||
.catalog("datafusion") | ||
.unwrap() | ||
.schema("information_schema"); | ||
match is { | ||
Some(_) => catalog, | ||
None => Arc::new(CatalogWithInformationSchema::new( | ||
Arc::downgrade(&state.catalog_list), | ||
catalog, | ||
)), | ||
} | ||
} else { | ||
catalog | ||
}; | ||
|
@@ -2907,6 +2947,29 @@ mod tests { | |
assert_eq!(Weak::strong_count(&catalog_weak), 0); | ||
} | ||
|
||
#[tokio::test] | ||
async fn sql_create_schema() -> Result<()> { | ||
// the information schema used to introduce cyclic Arcs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure what this comment means There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops that may have been straggling comment from a copy paste, sry about that. i can remove in subsequent PR |
||
let mut ctx = ExecutionContext::with_config( | ||
ExecutionConfig::new().with_information_schema(true), | ||
); | ||
|
||
// Create schema | ||
ctx.sql("CREATE SCHEMA abc").await?.collect().await?; | ||
|
||
// Add table to schema | ||
ctx.sql("CREATE TABLE abc.y AS VALUES (1,2,3)") | ||
.await? | ||
.collect() | ||
.await?; | ||
|
||
// Check table exists in schema | ||
let results = ctx.sql("SELECT * FROM information_schema.tables WHERE table_schema='abc' AND table_name = 'y'").await.unwrap().collect().await.unwrap(); | ||
|
||
assert_eq!(results[0].num_rows(), 1); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn normalized_column_identifiers() { | ||
// create local execution context | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻! It's helpful to me!