refactor: split up different stmts (#4997)

* refactor: set and unset

* chore: error message

* fix: reset Cargo.lock

* Apply suggestions from code review

Co-authored-by: jeremyhi <jiachun_feng@proton.me>

* Apply suggestions from code review

Co-authored-by: Weny Xu <wenymedia@gmail.com>

---------

Co-authored-by: jeremyhi <jiachun_feng@proton.me>
Co-authored-by: Weny Xu <wenymedia@gmail.com>
This commit is contained in:
Yohan Wal
2024-11-20 14:02:51 +08:00
committed by GitHub
parent 3633f25d0c
commit 55ced9aa71
15 changed files with 319 additions and 99 deletions

View File

@@ -15,7 +15,7 @@
use std::collections::HashMap;
use common_query::AddColumnLocation;
use datatypes::schema::{FulltextOptions, COLUMN_FULLTEXT_CHANGE_OPT_KEY_ENABLE};
use datatypes::schema::COLUMN_FULLTEXT_CHANGE_OPT_KEY_ENABLE;
use snafu::{ensure, ResultExt};
use sqlparser::ast::Ident;
use sqlparser::keywords::Keyword;
@@ -159,13 +159,7 @@ impl ParserContext<'_> {
.expect_keyword(Keyword::FULLTEXT)
.context(error::SyntaxSnafu)?;
Ok(AlterTableOperation::ChangeColumnFulltext {
column_name,
options: FulltextOptions {
enable: false,
..Default::default()
},
})
Ok(AlterTableOperation::UnsetColumnFulltext { column_name })
} else if w.keyword == Keyword::SET {
self.parse_alter_column_fulltext(column_name)
} else {
@@ -213,7 +207,7 @@ impl ParserContext<'_> {
"true".to_string(),
);
Ok(AlterTableOperation::ChangeColumnFulltext {
Ok(AlterTableOperation::SetColumnFulltext {
column_name,
options: options.try_into().context(SetFulltextOptionSnafu)?,
})
@@ -602,10 +596,10 @@ mod tests {
let alter_operation = alter_table.alter_operation();
assert_matches!(
alter_operation,
AlterTableOperation::ChangeColumnFulltext { .. }
AlterTableOperation::SetColumnFulltext { .. }
);
match alter_operation {
AlterTableOperation::ChangeColumnFulltext {
AlterTableOperation::SetColumnFulltext {
column_name,
options,
} => {
@@ -637,27 +631,15 @@ mod tests {
assert_eq!("test_table", alter_table.table_name().0[0].value);
let alter_operation = alter_table.alter_operation();
assert_matches!(
assert_eq!(
alter_operation,
AlterTableOperation::ChangeColumnFulltext { .. }
);
match alter_operation {
AlterTableOperation::ChangeColumnFulltext {
column_name,
options,
} => {
assert_eq!("a", column_name.value);
assert_eq!(
FulltextOptions {
enable: false,
analyzer: FulltextAnalyzer::English,
case_sensitive: false
},
*options
);
&AlterTableOperation::UnsetColumnFulltext {
column_name: Ident {
value: "a".to_string(),
quote_style: None
}
}
_ => unreachable!(),
}
);
}
_ => unreachable!(),
}

View File

@@ -76,11 +76,13 @@ pub enum AlterTableOperation {
DropColumn { name: Ident },
/// `RENAME <new_table_name>`
RenameTable { new_table_name: String },
/// `MODIFY COLUMN <column_name> [SET | UNSET] FULLTEXT [WITH <options>]`
ChangeColumnFulltext {
/// `MODIFY COLUMN <column_name> SET FULLTEXT [WITH <options>]`
SetColumnFulltext {
column_name: Ident,
options: FulltextOptions,
},
/// `MODIFY COLUMN <column_name> UNSET FULLTEXT`
UnsetColumnFulltext { column_name: Ident },
}
impl Display for AlterTableOperation {
@@ -123,19 +125,15 @@ impl Display for AlterTableOperation {
Ok(())
}
AlterTableOperation::ChangeColumnFulltext {
AlterTableOperation::SetColumnFulltext {
column_name,
options,
} => match options.enable {
true => {
write!(f, "MODIFY COLUMN {column_name} SET FULLTEXT WITH(analyzer={0}, case_sensitive={1})", options.analyzer, options.case_sensitive)?;
Ok(())
}
false => {
write!(f, "MODIFY COLUMN {column_name} UNSET FULLTEXT")?;
Ok(())
}
},
} => {
write!(f, "MODIFY COLUMN {column_name} SET FULLTEXT WITH(analyzer={0}, case_sensitive={1})", options.analyzer, options.case_sensitive)
}
AlterTableOperation::UnsetColumnFulltext { column_name } => {
write!(f, "MODIFY COLUMN {column_name} UNSET FULLTEXT")
}
}
}
}
@@ -269,5 +267,26 @@ ALTER TABLE monitor MODIFY COLUMN a SET FULLTEXT WITH(analyzer=English, case_sen
unreachable!();
}
}
let sql = "ALTER TABLE monitor MODIFY COLUMN a UNSET FULLTEXT";
let stmts =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::Alter { .. });
match &stmts[0] {
Statement::Alter(set) => {
let new_sql = format!("\n{}", set);
assert_eq!(
r#"
ALTER TABLE monitor MODIFY COLUMN a UNSET FULLTEXT"#,
&new_sql
);
}
_ => {
unreachable!();
}
}
}
}