Skip to content

Commit

Permalink
fix: concurrent rename two table to same name may cause override
Browse files Browse the repository at this point in the history
  • Loading branch information
v0y4g3r committed Apr 12, 2023
1 parent ae21c1c commit bd68f2a
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/catalog/src/local/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,20 @@ impl SchemaProvider for MemorySchemaProvider {

fn rename_table(&self, name: &str, new_name: String) -> Result<TableRef> {
let mut tables = self.tables.write().unwrap();
if tables.get(name).is_some() {
let table = tables.remove(name).unwrap();
tables.insert(new_name, table.clone());
Ok(table)
} else {
TableNotFoundSnafu {
let Some(table) = tables.remove(name) else {
return TableNotFoundSnafu {
table_info: name.to_string(),
}
.fail()?
}
.fail()?;
};
let e = match tables.entry(new_name) {
Entry::Vacant(e) => e,
Entry::Occupied(e) => {
return TableExistsSnafu { table: e.key() }.fail();
}
};
e.insert(table.clone());
Ok(table)
}

fn deregister_table(&self, name: &str) -> Result<Option<TableRef>> {
Expand Down Expand Up @@ -459,7 +463,7 @@ mod tests {
assert!(schema.table_exist(table_name).unwrap());

// rename table
let new_table_name = "numbers";
let new_table_name = "numbers_new";
let rename_table_req = RenameTableRequest {
catalog: DEFAULT_CATALOG_NAME.to_string(),
schema: DEFAULT_SCHEMA_NAME.to_string(),
Expand Down

0 comments on commit bd68f2a

Please sign in to comment.