Skip to content

Commit

Permalink
fix: add test for parsing RENAME keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
e1ijah1 committed Dec 22, 2022
1 parent 1d56332 commit f745a90
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
34 changes: 10 additions & 24 deletions src/datanode/src/sql/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ use crate::sql::SqlHandler;

impl SqlHandler {
pub(crate) async fn alter(&self, req: AlterTableRequest) -> Result<Output> {
if let AlterKind::RenameTable { .. } = req.alter_kind {
return error::InvalidSqlSnafu {
msg: "rename table not unsupported yet".to_string(),
}
.fail();
}

let ctx = EngineContext {};
let catalog_name = req.catalog_name.as_deref().unwrap_or(DEFAULT_CATALOG_NAME);
let schema_name = req.schema_name.as_deref().unwrap_or(DEFAULT_SCHEMA_NAME);
Expand Down Expand Up @@ -83,9 +76,13 @@ impl SqlHandler {
AlterTableOperation::DropColumn { name } => AlterKind::DropColumns {
names: vec![name.value.clone()],
},
AlterTableOperation::RenameTable { new_table_name } => AlterKind::RenameTable {
new_table_name: new_table_name.clone(),
},
AlterTableOperation::RenameTable { .. } => {
// TODO update proto to support alter table name
return error::InvalidSqlSnafu {
msg: "rename table not unsupported yet".to_string(),
}
.fail();
}
};
Ok(AlterTableRequest {
catalog_name: Some(table_ref.catalog.to_string()),
Expand Down Expand Up @@ -148,20 +145,9 @@ mod tests {
async fn test_alter_to_request_with_renaming_table() {
let handler = create_mock_sql_handler().await;
let alter_table = parse_sql("ALTER TABLE test_table RENAME table_t;");
let req = handler
let err = handler
.alter_to_request(alter_table, TableReference::bare("test_table"))
.unwrap();
assert_eq!(req.catalog_name, Some("greptime".to_string()));
assert_eq!(req.schema_name, Some("public".to_string()));
assert_eq!(req.table_name, "test_table");

let alter_kind = req.alter_kind;
assert_matches!(alter_kind, AlterKind::RenameTable { .. });
match alter_kind {
AlterKind::RenameTable { new_table_name } => {
assert_eq!(new_table_name, "table_t");
}
_ => unreachable!(),
}
.unwrap_err();
assert_matches!(err, crate::error::Error::InvalidSql { .. });
}
}
33 changes: 32 additions & 1 deletion src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> ParserContext<'a> {
AlterTableOperation::RenameTable { new_table_name }
} else {
return Err(ParserError::ParserError(format!(
"expect keyword ADD or DROP after ALTER TABLE, found {}",
"expect keyword ADD or DROP or RENAME after ALTER TABLE, found {}",
parser.peek_token()
)));
};
Expand Down Expand Up @@ -141,4 +141,35 @@ mod tests {
_ => unreachable!(),
}
}

#[test]
fn test_parse_alter_rename_table() {
let sql = "ALTER TABLE test_table table_t";
let result = ParserContext::create_with_dialect(sql, &GenericDialect {}).unwrap_err();
assert!(result
.to_string()
.contains("expect keyword ADD or DROP or RENAME after ALTER TABLE"));

let sql = "ALTER TABLE test_table RENAME table_t";
let mut result = ParserContext::create_with_dialect(sql, &GenericDialect {}).unwrap();
assert_eq!(1, result.len());

let statement = result.remove(0);
assert_matches!(statement, Statement::Alter { .. });
match statement {
Statement::Alter(alter_table) => {
assert_eq!("test_table", alter_table.table_name().0[0].value);

let alter_operation = alter_table.alter_operation();
assert_matches!(alter_operation, AlterTableOperation::RenameTable { .. });
match alter_operation {
AlterTableOperation::RenameTable { new_table_name } => {
assert_eq!("table_t", new_table_name);
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
}
4 changes: 1 addition & 3 deletions src/table/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl TableMeta {
AlterKind::AddColumns { columns } => self.add_columns(table_name, columns),
AlterKind::DropColumns { names } => self.remove_columns(table_name, names),
// No need to rebuild table meta when renaming tables.
AlterKind::RenameTable { .. } => {
panic!("No need to rebuild table meta when renaming the table: {table_name}")
}
AlterKind::RenameTable { .. } => Ok(TableMetaBuilder::default()),
}
}

Expand Down

0 comments on commit f745a90

Please sign in to comment.