mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-29 19:30:37 +00:00
feat: support parsing the RENAME TABLE statements in the parser (#780)
* feat: add parsing `alter rename table` syntax to the parser * chore: fix clippy * chore: add test for parser * fix: add test for parsing RENAME keyword * chore: remove unused code * fix: parse table name object Co-authored-by: Yingwen <realevenyag@gmail.com> * chore: fmt code Co-authored-by: Yingwen <realevenyag@gmail.com>
This commit is contained in:
@@ -76,6 +76,13 @@ impl SqlHandler {
|
||||
AlterTableOperation::DropColumn { name } => AlterKind::DropColumns {
|
||||
names: vec![name.value.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()),
|
||||
@@ -133,4 +140,14 @@ mod tests {
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
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 err = handler
|
||||
.alter_to_request(alter_table, TableReference::bare("test_table"))
|
||||
.unwrap_err();
|
||||
assert_matches!(err, crate::error::Error::InvalidSql { .. });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,9 +51,20 @@ impl<'a> ParserContext<'a> {
|
||||
parser.peek_token()
|
||||
)));
|
||||
}
|
||||
} else if parser.parse_keyword(Keyword::RENAME) {
|
||||
let new_table_name_obj = parser.parse_object_name()?;
|
||||
let new_table_name = match &new_table_name_obj.0[..] {
|
||||
[table] => table.value.clone(),
|
||||
_ => {
|
||||
return Err(ParserError::ParserError(format!(
|
||||
"expect table name, actual: {new_table_name_obj}"
|
||||
)))
|
||||
}
|
||||
};
|
||||
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()
|
||||
)));
|
||||
};
|
||||
@@ -130,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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ pub enum AlterTableOperation {
|
||||
AddColumn { column_def: ColumnDef },
|
||||
/// `DROP COLUMN <name>`
|
||||
DropColumn { name: Ident },
|
||||
/// `RENAME <new_table_name>`
|
||||
RenameTable { new_table_name: String },
|
||||
}
|
||||
|
||||
/// Convert `AlterTable` statement to `AlterExpr` for gRPC
|
||||
@@ -78,6 +80,13 @@ impl TryFrom<AlterTable> for AlterExpr {
|
||||
drop_columns: vec![DropColumn { name: name.value }],
|
||||
})
|
||||
}
|
||||
AlterTableOperation::RenameTable { .. } => {
|
||||
// TODO update proto to support alter table name
|
||||
return UnsupportedAlterTableStatementSnafu {
|
||||
msg: "rename table not supported yet",
|
||||
}
|
||||
.fail();
|
||||
}
|
||||
};
|
||||
let expr = AlterExpr {
|
||||
catalog_name,
|
||||
|
||||
Reference in New Issue
Block a user