From a48d93517990dc62a2613e24f46e4ded9a98f7d7 Mon Sep 17 00:00:00 2001 From: NiwakaDev Date: Mon, 20 Jan 2025 21:51:37 +0900 Subject: [PATCH] fix: add parser test --- src/operator/src/statement/ddl.rs | 2 +- src/sql/src/parsers/alter_parser.rs | 61 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/operator/src/statement/ddl.rs b/src/operator/src/statement/ddl.rs index 6e215f3375d7..23cba11de8da 100644 --- a/src/operator/src/statement/ddl.rs +++ b/src/operator/src/statement/ddl.rs @@ -889,7 +889,7 @@ impl StatementExecutor { /// /// # Returns /// - /// Returns true If the alter need to be porformed; otherwise, it returns false. + /// Returns true if the alter need to be porformed; otherwise, it returns false. fn verify_alter( &self, table_id: TableId, diff --git a/src/sql/src/parsers/alter_parser.rs b/src/sql/src/parsers/alter_parser.rs index 29c6c729edb7..511caee0f47f 100644 --- a/src/sql/src/parsers/alter_parser.rs +++ b/src/sql/src/parsers/alter_parser.rs @@ -524,6 +524,67 @@ mod tests { } } + #[test] + fn test_parse_add_column_if_not_exists() { + let sql = "ALTER TABLE test ADD COLUMN IF NOT EXISTS a INTEGER, ADD COLUMN b STRING, ADD COLUMN IF NOT EXISTS c INT;"; + let mut result = + ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default()) + .unwrap(); + assert_eq!(result.len(), 1); + let statement = result.remove(0); + assert_matches!(statement, Statement::AlterTable { .. }); + match statement { + Statement::AlterTable(alter) => { + assert_eq!(alter.table_name.0[0].value, "test"); + assert_matches!( + alter.alter_operation, + AlterTableOperation::AddColumns { .. } + ); + match alter.alter_operation { + AlterTableOperation::AddColumns { add_columns } => { + let expected = vec![ + AddColumn { + column_def: ColumnDef { + name: Ident::new("a"), + data_type: DataType::Integer(None), + collation: None, + options: vec![], + }, + location: None, + add_if_not_exists: true, + }, + AddColumn { + column_def: ColumnDef { + name: Ident::new("b"), + data_type: DataType::String(None), + collation: None, + options: vec![], + }, + location: None, + add_if_not_exists: false, + }, + AddColumn { + column_def: ColumnDef { + name: Ident::new("c"), + data_type: DataType::Int(None), + collation: None, + options: vec![], + }, + location: None, + add_if_not_exists: true, + }, + ]; + for (idx, add_column) in add_columns.into_iter().enumerate() { + assert_eq!(add_column, expected[idx]); + } + } + _ => unreachable!(), + } + } + _ => unreachable!(), + } + } + #[test] fn test_parse_alter_drop_column() { let sql = "ALTER TABLE my_metric_1 DROP a";