From 87b3e80310a82e697b3f33f5213b05aa3d5182bf Mon Sep 17 00:00:00 2001 From: Kirk Mitchener Date: Mon, 5 Sep 2022 09:29:56 -0400 Subject: [PATCH] return empty dataframe on create table, remove a duplicate optimize call update docs --- datafusion/core/src/execution/context.rs | 14 ++++++-------- datafusion/core/tests/sql/create_drop.rs | 4 +++- docs/source/user-guide/sql/ddl.md | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs index 1a4a7c9df9d3..2a9d011245dd 100644 --- a/datafusion/core/src/execution/context.rs +++ b/datafusion/core/src/execution/context.rs @@ -269,35 +269,33 @@ impl SessionContext { (true, false, Ok(_)) => self.return_empty_dataframe(), (false, true, Ok(_)) => { self.deregister_table(name.as_str())?; - let plan = self.optimize(&input)?; let physical = - Arc::new(DataFrame::new(self.state.clone(), &plan)); + Arc::new(DataFrame::new(self.state.clone(), &input)); let batches: Vec<_> = physical.collect_partitioned().await?; let table = Arc::new(MemTable::try_new( - Arc::new(plan.schema().as_ref().into()), + Arc::new(input.schema().as_ref().into()), batches, )?); self.register_table(name.as_str(), table)?; - Ok(Arc::new(DataFrame::new(self.state.clone(), &plan))) + self.return_empty_dataframe() } (true, true, Ok(_)) => Err(DataFusionError::Internal( "'IF NOT EXISTS' cannot coexist with 'REPLACE'".to_string(), )), (_, _, Err(_)) => { - let plan = self.optimize(&input)?; let physical = - Arc::new(DataFrame::new(self.state.clone(), &plan)); + Arc::new(DataFrame::new(self.state.clone(), &input)); let batches: Vec<_> = physical.collect_partitioned().await?; let table = Arc::new(MemTable::try_new( - Arc::new(plan.schema().as_ref().into()), + Arc::new(input.schema().as_ref().into()), batches, )?); self.register_table(name.as_str(), table)?; - Ok(Arc::new(DataFrame::new(self.state.clone(), &plan))) + self.return_empty_dataframe() } (false, false, Ok(_)) => Err(DataFusionError::Execution(format!( "Table '{:?}' already exists", diff --git a/datafusion/core/tests/sql/create_drop.rs b/datafusion/core/tests/sql/create_drop.rs index e887da53509e..b327d64fe33a 100644 --- a/datafusion/core/tests/sql/create_drop.rs +++ b/datafusion/core/tests/sql/create_drop.rs @@ -57,12 +57,14 @@ async fn create_or_replace_table_as() -> Result<()> { SessionContext::with_config(SessionConfig::new().with_information_schema(true)); // Create table - ctx.sql("CREATE TABLE y AS VALUES (1,2),(3,4)") + let result = ctx + .sql("CREATE TABLE y AS VALUES (1,2),(3,4)") .await .unwrap() .collect() .await .unwrap(); + assert!(result.is_empty()); // Replace table ctx.sql("CREATE OR REPLACE TABLE y AS VALUES (5,6)") diff --git a/docs/source/user-guide/sql/ddl.md b/docs/source/user-guide/sql/ddl.md index 8d24a8e4ce58..118314fdc057 100644 --- a/docs/source/user-guide/sql/ddl.md +++ b/docs/source/user-guide/sql/ddl.md @@ -78,16 +78,16 @@ PARTITIONED BY (year, month) LOCATION '/mnt/nyctaxi'; ``` -## CREATE MEMORY TABLE +## CREATE TABLE -Memory table can be created with query. +An in-memory table can be created with a query or values list. -``` -CREATE TABLE TABLE_NAME AS [SELECT | VALUES LIST] -``` +
+CREATE [OR REPLACE] TABLE [IF NOT EXISTS] table_name AS [SELECT | VALUES LIST];
+
```sql -CREATE TABLE valuetable AS VALUES(1,'HELLO'),(12,'DATAFUSION'); +CREATE TABLE valuetable IF NOT EXISTS AS VALUES(1,'HELLO'),(12,'DATAFUSION'); CREATE TABLE memtable as select * from valuetable; ```