Skip to content

Commit

Permalink
fix: add parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
NiwakaDev committed Jan 20, 2025
1 parent 3ca78d5 commit a48d935
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/operator/src/statement/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit a48d935

Please sign in to comment.