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

fix: create table after rename table #894

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions src/datanode/src/tests/instance_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ pub async fn test_execute_create() {
}

#[tokio::test]
#[should_panic]
async fn test_rename_table() {
let instance = MockInstance::new("test_rename_table_local").await;

Expand Down Expand Up @@ -392,6 +393,58 @@ async fn test_rename_table() {
"
.to_string();
check_output_stream(output, expected).await;

execute_sql_in_db(&instance, "select * from demo", "db").await;
e1ijah1 marked this conversation as resolved.
Show resolved Hide resolved
}

#[tokio::test]
async fn test_create_table_after_rename_table() {
let instance = MockInstance::new("test_rename_table_local").await;

let output = execute_sql(&instance, "create database db").await;
assert!(matches!(output, Output::AffectedRows(1)));

// create test table
let table_name = "demo";
let output = execute_sql_in_db(
&instance,
&format!("create table {table_name}(host string, cpu double, memory double, ts timestamp, time index(ts))"),
"db",
)
.await;
assert!(matches!(output, Output::AffectedRows(0)));

// rename table
let new_table_name = "test_table";
let output = execute_sql_in_db(
&instance,
&format!("alter table {table_name} rename {new_table_name}"),
"db",
)
.await;
assert!(matches!(output, Output::AffectedRows(0)));

// create table with same name
// create test table
let output = execute_sql_in_db(
&instance,
&format!("create table {table_name}(host string, cpu double, memory double, ts timestamp, time index(ts))"),
"db",
)
.await;
assert!(matches!(output, Output::AffectedRows(0)));

let expect = "\
+------------+
| Tables |
+------------+
| demo |
| test_table |
+------------+\
"
.to_string();
let output = execute_sql_in_db(&instance, "show tables", "db").await;
check_output_stream(output, expect).await;
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
7 changes: 3 additions & 4 deletions src/mito/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,10 @@ impl<S: StorageEngine> MitoEngineInner<S> {
.context(error::AlterTableSnafu { table_name })?;

if let AlterKind::RenameTable { new_table_name } = &req.alter_kind {
table_ref.table = new_table_name.as_str();
let full_table_name = table_ref.to_string();
let mut tables = self.tables.write().unwrap();
tables.remove(&full_table_name);
tables.insert(full_table_name, table.clone());
tables.remove(&table_ref.to_string());
table_ref.table = new_table_name.as_str();
tables.insert(table_ref.to_string(), table.clone());
}
Ok(table)
}
Expand Down