Skip to content

Commit

Permalink
return empty dataframe on create table, remove a duplicate optimize c…
Browse files Browse the repository at this point in the history
…all (#3361)

update docs
  • Loading branch information
kmitchener authored Sep 5, 2022
1 parent 4c948a3 commit ede82fb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 6 additions & 8 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion datafusion/core/tests/sql/create_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
12 changes: 6 additions & 6 deletions docs/source/user-guide/sql/ddl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
<pre>
CREATE [OR REPLACE] TABLE [IF NOT EXISTS] <b><i>table_name</i></b> AS [SELECT | VALUES LIST];
</pre>

```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;
```
Expand Down

0 comments on commit ede82fb

Please sign in to comment.