Skip to content
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
1 change: 1 addition & 0 deletions datafusion/catalog/src/listing_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl ListingSchemaProvider {
file_type: self.format.clone(),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down
7 changes: 7 additions & 0 deletions datafusion/core/src/datasource/listing_table_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -278,6 +279,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -322,6 +324,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -373,6 +376,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -416,6 +420,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -455,6 +460,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down Expand Up @@ -495,6 +501,7 @@ mod tests {
schema: Arc::new(DFSchema::empty()),
table_partition_cols: vec![],
if_not_exists: false,
or_replace: false,
temporary: false,
definition: None,
order_exprs: vec![],
Expand Down
45 changes: 35 additions & 10 deletions datafusion/core/src/execution/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,19 +792,44 @@ impl SessionContext {
return not_impl_err!("Temporary tables not supported");
}

if exist {
match cmd.if_not_exists {
true => return self.return_empty_dataframe(),
false => {
return exec_err!("Table '{}' already exists", cmd.name);
match (cmd.if_not_exists, cmd.or_replace, exist) {
(true, false, true) => self.return_empty_dataframe(),
(false, true, true) => {
let result = self
.find_and_deregister(cmd.name.clone(), TableType::Base)
.await;

match result {
Ok(true) => {
let table_provider: Arc<dyn TableProvider> =
self.create_custom_table(cmd).await?;
self.register_table(cmd.name.clone(), table_provider)?;
self.return_empty_dataframe()
}
Ok(false) => {
let table_provider: Arc<dyn TableProvider> =
self.create_custom_table(cmd).await?;
self.register_table(cmd.name.clone(), table_provider)?;
self.return_empty_dataframe()
}
Err(e) => {
exec_err!("Errored while deregistering external table: {}", e)
}
}
}
(true, true, true) => {
exec_err!("'IF NOT EXISTS' cannot coexist with 'REPLACE'")
}
(_, _, false) => {
let table_provider: Arc<dyn TableProvider> =
self.create_custom_table(cmd).await?;
self.register_table(cmd.name.clone(), table_provider)?;
self.return_empty_dataframe()
}
(false, false, true) => {
exec_err!("External table '{}' already exists", cmd.name)
}
}

let table_provider: Arc<dyn TableProvider> =
self.create_custom_table(cmd).await?;
self.register_table(cmd.name.clone(), table_provider)?;
self.return_empty_dataframe()
}

async fn create_memory_table(&self, cmd: CreateMemoryTable) -> Result<DataFrame> {
Expand Down
2 changes: 2 additions & 0 deletions datafusion/expr/src/logical_plan/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ pub struct CreateExternalTable {
pub table_partition_cols: Vec<String>,
/// Option to not error if table already exists
pub if_not_exists: bool,
/// Option to replace table content if table already exists
pub or_replace: bool,
/// Whether the table is a temporary table
pub temporary: bool,
/// SQL used to create the table, if available
Expand Down
1 change: 1 addition & 0 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ message CreateExternalTableNode {
datafusion_common.DfSchema schema = 4;
repeated string table_partition_cols = 5;
bool if_not_exists = 6;
bool or_replace = 15;
bool temporary = 14;
string definition = 7;
repeated SortExprNodeCollection order_exprs = 10;
Expand Down
18 changes: 18 additions & 0 deletions datafusion/proto/src/generated/pbjson.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions datafusion/proto/src/generated/prost.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions datafusion/proto/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ impl AsLogicalPlan for LogicalPlanNode {
.clone(),
order_exprs,
if_not_exists: create_extern_table.if_not_exists,
or_replace: create_extern_table.or_replace,
temporary: create_extern_table.temporary,
definition,
unbounded: create_extern_table.unbounded,
Expand Down Expand Up @@ -1469,6 +1470,7 @@ impl AsLogicalPlan for LogicalPlanNode {
schema: df_schema,
table_partition_cols,
if_not_exists,
or_replace,
definition,
order_exprs,
unbounded,
Expand Down Expand Up @@ -1502,6 +1504,7 @@ impl AsLogicalPlan for LogicalPlanNode {
schema: Some(df_schema.try_into()?),
table_partition_cols: table_partition_cols.clone(),
if_not_exists: *if_not_exists,
or_replace: *or_replace,
temporary: *temporary,
order_exprs: converted_order_exprs,
definition: definition.clone().unwrap_or_default(),
Expand Down
Loading