From 3978931b8e14c15b7d5a9fc78f7ac767fcbd50cc Mon Sep 17 00:00:00 2001 From: elijah <30852919+e1ijah1@users.noreply.github.com> Date: Tue, 27 Dec 2022 14:53:40 +0800 Subject: [PATCH] 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 * chore: fmt code Co-authored-by: Yingwen --- src/datanode/src/sql/alter.rs | 17 +++++++++++ src/sql/src/parsers/alter_parser.rs | 44 ++++++++++++++++++++++++++++- src/sql/src/statements/alter.rs | 9 ++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/datanode/src/sql/alter.rs b/src/datanode/src/sql/alter.rs index 1fa48a3b18..38b3a24b00 100644 --- a/src/datanode/src/sql/alter.rs +++ b/src/datanode/src/sql/alter.rs @@ -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 { .. }); + } } diff --git a/src/sql/src/parsers/alter_parser.rs b/src/sql/src/parsers/alter_parser.rs index 9d02042797..7d614ee3ae 100644 --- a/src/sql/src/parsers/alter_parser.rs +++ b/src/sql/src/parsers/alter_parser.rs @@ -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!(), + } + } } diff --git a/src/sql/src/statements/alter.rs b/src/sql/src/statements/alter.rs index 6eca043661..372170bf7b 100644 --- a/src/sql/src/statements/alter.rs +++ b/src/sql/src/statements/alter.rs @@ -49,6 +49,8 @@ pub enum AlterTableOperation { AddColumn { column_def: ColumnDef }, /// `DROP COLUMN ` DropColumn { name: Ident }, + /// `RENAME ` + RenameTable { new_table_name: String }, } /// Convert `AlterTable` statement to `AlterExpr` for gRPC @@ -78,6 +80,13 @@ impl TryFrom 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,