Skip to content

Commit

Permalink
feat: support parsing the RENAME TABLE statements in the parser (Grep…
Browse files Browse the repository at this point in the history
…timeTeam#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 <[email protected]>

* chore: fmt code

Co-authored-by: Yingwen <[email protected]>
  • Loading branch information
2 people authored and paomian committed Oct 19, 2023
1 parent 624d807 commit a424e86
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/datanode/src/sql/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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 { .. });
}
}
44 changes: 43 additions & 1 deletion src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)));
};
Expand Down Expand Up @@ -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!(),
}
}
}
9 changes: 9 additions & 0 deletions src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a424e86

Please sign in to comment.