Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Nov 20, 2024
1 parent f88f119 commit c95b240
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
42 changes: 36 additions & 6 deletions src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ mod tests {
}
}

fn check_parse_alter_table(sql: &str, expected: &[(&str, &str)]) {
fn check_parse_alter_table_set_options(sql: &str, expected: &[(&str, &str)]) {
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
Expand All @@ -578,18 +578,36 @@ mod tests {
assert_eq!(expected, &res);
}

fn check_parse_alter_table_unset_options(sql: &str, expected: &[&str]) {
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, result.len());
let Statement::Alter(alter) = &result[0] else {
unreachable!()
};
assert_eq!("test_table", alter.table_name.0[0].value);
let AlterTableOperation::UnsetTableOptions { keys } = &alter.alter_operation else {
unreachable!()
};

assert_eq!(sql, alter.to_string());
let res = keys.iter().map(|o| o.to_string()).collect::<Vec<_>>();
assert_eq!(expected, &res);
}

#[test]
fn test_parse_alter_column() {
check_parse_alter_table("ALTER TABLE test_table SET 'a'='A'", &[("a", "A")]);
check_parse_alter_table(
fn test_parse_alter_table_set_options() {
check_parse_alter_table_set_options("ALTER TABLE test_table SET 'a'='A'", &[("a", "A")]);
check_parse_alter_table_set_options(
"ALTER TABLE test_table SET 'a'='A','b'='B'",
&[("a", "A"), ("b", "B")],
);
check_parse_alter_table(
check_parse_alter_table_set_options(
"ALTER TABLE test_table SET 'a'='A','b'='B','c'='C'",
&[("a", "A"), ("b", "B"), ("c", "C")],
);
check_parse_alter_table("ALTER TABLE test_table SET 'a'=NULL", &[("a", "")]);
check_parse_alter_table_set_options("ALTER TABLE test_table SET 'a'=NULL", &[("a", "")]);

ParserContext::create_with_dialect(
"ALTER TABLE test_table SET a INTEGER",
Expand All @@ -599,6 +617,18 @@ mod tests {
.unwrap_err();
}

#[test]
fn test_parse_alter_table_unset_options() {
check_parse_alter_table_unset_options("ALTER TABLE test_table UNSET 'a'", &["a"]);
check_parse_alter_table_unset_options("ALTER TABLE test_table UNSET 'a','b'", &["a", "b"]);
ParserContext::create_with_dialect(
"ALTER TABLE test_table UNSET a INTEGER",
&GreptimeDbDialect {},
ParseOptions::default(),
)
.unwrap_err();
}

#[test]
fn test_parse_alter_column_fulltext() {
let sql = "ALTER TABLE test_table MODIFY COLUMN a SET FULLTEXT WITH(analyzer='English',case_sensitive='false')";
Expand Down
2 changes: 1 addition & 1 deletion src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Display for AlterTableOperation {
write!(f, "SET {kvs}")
}
AlterTableOperation::UnsetTableOptions { keys } => {
let keys = keys.join(",");
let keys = keys.iter().map(|k| format!("'{k}'")).join(",");
write!(f, "UNSET {keys}")
}
AlterTableOperation::SetColumnFulltext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ SHOW CREATE TABLE ato;
| | ) |
+-------+----------------------------------------------------+

ALTER TABLE ato SET 'compaction.twcs.time_window'='';
ALTER TABLE ato UNSET 'compaction.twcs.time_window';

Affected Rows: 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ALTER TABLE ato SET 'compaction.twcs.max_inactive_window_runs'='6';

SHOW CREATE TABLE ato;

ALTER TABLE ato SET 'compaction.twcs.time_window'='';
ALTER TABLE ato UNSET 'compaction.twcs.time_window';

SHOW CREATE TABLE ato;

Expand Down

0 comments on commit c95b240

Please sign in to comment.