Skip to content
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

return empty dataframe on create table, remove a duplicate optimize call #3361

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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