-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Alter column fulltext option #4637
Changes from 16 commits
693130c
ebc5ced
1cfc57b
8f94e7f
8b7f57f
093c5c6
7ac9a20
1543268
3601a40
0356c66
20a7b82
8a61fdd
6484664
820d75b
5331ed4
6ae407c
aaf7b4b
be4bfcb
e436242
6e591b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,12 +255,25 @@ impl ColumnSchema { | |
} | ||
} | ||
|
||
pub fn with_fulltext_options(mut self, options: FulltextOptions) -> Result<Self> { | ||
pub fn with_fulltext_options(mut self, options: &FulltextOptions) -> Result<Self> { | ||
self.set_fulltext_options(options)?; | ||
Ok(self) | ||
} | ||
|
||
pub fn set_fulltext_options(&mut self, options: &FulltextOptions) -> Result<()> { | ||
if self.data_type != ConcreteDataType::string_datatype() { | ||
return error::InvalidFulltextDataTypeSnafu { | ||
data_type: self.data_type.to_string(), | ||
} | ||
.fail(); | ||
} | ||
|
||
self.metadata.insert( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can add a check whether the column type is string: |
||
FULLTEXT_KEY.to_string(), | ||
serde_json::to_string(&options).context(error::SerializeSnafu)?, | ||
serde_json::to_string(options).context(error::SerializeSnafu)?, | ||
); | ||
Ok(self) | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
@@ -12,6 +10,8 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
|
||
use common_query::AddColumnLocation; | ||
use snafu::ResultExt; | ||
use sqlparser::keywords::Keyword; | ||
|
@@ -22,6 +22,7 @@ use crate::error::{self, Result}; | |
use crate::parser::ParserContext; | ||
use crate::statements::alter::{AlterTable, AlterTableOperation}; | ||
use crate::statements::statement::Statement; | ||
use crate::util::parse_option_string; | ||
|
||
impl<'a> ParserContext<'a> { | ||
pub(crate) fn parse_alter(&mut self) -> Result<Statement> { | ||
|
@@ -74,13 +75,36 @@ impl<'a> ParserContext<'a> { | |
))); | ||
} | ||
} else if self.consume_token("MODIFY") { | ||
let _ = self.parser.parse_keyword(Keyword::COLUMN); | ||
if !self.parser.parse_keyword(Keyword::COLUMN) { | ||
return Err(ParserError::ParserError(format!( | ||
"expect keyword COLUMN after ALTER TABLE MODIFY, found {}", | ||
self.parser.peek_token() | ||
))); | ||
} | ||
let column_name = Self::canonicalize_identifier(self.parser.parse_identifier(false)?); | ||
let target_type = self.parser.parse_data_type()?; | ||
|
||
AlterTableOperation::ChangeColumnType { | ||
column_name, | ||
target_type, | ||
if self.parser.parse_keyword(Keyword::SET) { | ||
let _ = self.parser.parse_keyword(Keyword::FULLTEXT); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can check the parse result to avoid receiving unexpected input |
||
let options = self | ||
.parser | ||
.parse_options(Keyword::WITH)? | ||
.into_iter() | ||
.map(parse_option_string) | ||
.collect::<Result<HashMap<String, String>>>() | ||
.unwrap(); | ||
|
||
AlterTableOperation::AlterColumnFulltext { | ||
column_name, | ||
options: options.into(), | ||
} | ||
} else { | ||
let target_type = self.parser.parse_data_type()?; | ||
|
||
AlterTableOperation::ChangeColumnType { | ||
column_name, | ||
target_type, | ||
} | ||
} | ||
} else if self.parser.parse_keyword(Keyword::RENAME) { | ||
let new_table_name_obj_raw = self.parse_object_name()?; | ||
|
@@ -96,7 +120,7 @@ impl<'a> ParserContext<'a> { | |
AlterTableOperation::RenameTable { new_table_name } | ||
} else { | ||
return Err(ParserError::ParserError(format!( | ||
"expect keyword ADD or DROP or MODIFY or RENAME after ALTER TABLE, found {}", | ||
"expect keyword ADD or DROP or MODIFY or RENAME or SET after ALTER TABLE, found {}", | ||
self.parser.peek_token() | ||
))); | ||
}; | ||
|
@@ -406,4 +430,38 @@ mod tests { | |
_ => unreachable!(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse_alter_change_fulltext() { | ||
let sql = "ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'true');"; | ||
let mut result = | ||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default()) | ||
.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", alter_table.table_name().0[0].value); | ||
|
||
let alter_operation = alter_table.alter_operation(); | ||
assert_matches!( | ||
alter_operation, | ||
AlterTableOperation::AlterColumnFulltext { .. } | ||
); | ||
match alter_operation { | ||
AlterTableOperation::AlterColumnFulltext { | ||
column_name, | ||
options, | ||
} => { | ||
assert_eq!(column_name.value, r#"message"#); | ||
assert_eq!(options.get("analyzer").unwrap(), "Chinese"); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add column name in error