Skip to content
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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

irenjj
Copy link
Collaborator

@irenjj irenjj commented Aug 28, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

closes: #4395

What's changed and what's your intention?

Supports alter column options like:

ALTER TABLE monitor SET COLUMN load_15 fulltext WITH(analyzer = 'Chinese', case_sensitive = 'false');

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Copy link
Contributor

coderabbitai bot commented Aug 28, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Aug 28, 2024
@irenjj irenjj changed the title feat: Alter column option feat: Alter column fulltext option Sep 7, 2024
@irenjj irenjj marked this pull request as ready for review September 7, 2024 12:07
@WenyXu
Copy link
Member

WenyXu commented Sep 9, 2024

Hi @irenjj there are some conflicts

column_name,
target_type,
if self.parser.parse_keyword(Keyword::SET) {
let _ = self.parser.parse_keyword(Keyword::FULLTEXT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can check the parse result to avoid receiving unexpected input

Comment on lines 84 to 101
let options = self
.parser
.parse_options(Keyword::WITH)?
.into_iter()
.map(|option: SqlOption| -> Result<(String, String)> {
let (key, value) = (option.name, option.value);
let v = match value {
Expr::Value(sqlparser::ast::Value::SingleQuotedString(v))
| Expr::Value(sqlparser::ast::Value::DoubleQuotedString(v)) => v,
Expr::Identifier(v) => v.value,
Expr::Value(sqlparser::ast::Value::Number(v, _)) => v.to_string(),
_ => return error::InvalidTableOptionValueSnafu { key, value }.fail(),
};
let k = key.value.to_lowercase();
Ok((k, v))
})
.collect::<Result<HashMap<String, String>>>()
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplied as

Suggested change
let options = self
.parser
.parse_options(Keyword::WITH)?
.into_iter()
.map(|option: SqlOption| -> Result<(String, String)> {
let (key, value) = (option.name, option.value);
let v = match value {
Expr::Value(sqlparser::ast::Value::SingleQuotedString(v))
| Expr::Value(sqlparser::ast::Value::DoubleQuotedString(v)) => v,
Expr::Identifier(v) => v.value,
Expr::Value(sqlparser::ast::Value::Number(v, _)) => v.to_string(),
_ => return error::InvalidTableOptionValueSnafu { key, value }.fail(),
};
let k = key.value.to_lowercase();
Ok((k, v))
})
.collect::<Result<HashMap<String, String>>>()
.unwrap();
let options = self
.parser
.parse_options(Keyword::WITH)?
.into_iter()
.map(parse_option_string)
.collect::<Result<HashMap<String, String>>>()?;

Comment on lines 609 to 615
let index = table_schema
.column_index_by_name(column_name)
.with_context(|| error::ColumnNotExistsSnafu {
column_name,
table_name,
})?;
let column = &table_schema.column_schemas()[index];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let index = table_schema
.column_index_by_name(column_name)
.with_context(|| error::ColumnNotExistsSnafu {
column_name,
table_name,
})?;
let column = &table_schema.column_schemas()[index];
let column = &table_schema.column_schema_by_name(column_name).with_context(...);

Copy link
Contributor

@zhongzc zhongzc Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose that we only allow modify column to create a fulltext index on columns that do not already have one, rather than modifying fulltext options for existing columns.

@@ -262,6 +262,14 @@ impl ColumnSchema {
);
Ok(self)
}

pub fn set_fulltext_options(&mut self, options: FulltextOptions) -> Result<()> {
self.metadata.insert(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add a check whether the column type is string: self.data_type == ConcreteDataType::string_datatype(), and also change the with_fulltext_options above

Copy link
Member

@WenyXu WenyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Some nitpicking:

  1. Add more unit tests
  2. Reduce duplicate code by introducing functions
  3. Avoid unnecessary cloning

Cargo.toml Outdated
@@ -151,7 +151,7 @@ reqwest = { version = "0.12", default-features = false, features = [
"stream",
"multipart",
] }
# SCRAM-SHA-512 requires https://github.com/dequbed/rsasl/pull/48, https://github.com/influxdata/rskafka/pull/247
# SCRAM-SHA-512 requires https://github.com/dequbed/rsasl/pull/48, https://github.com/influxdata/rskafka/pull/247
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No need to change this. The comments have been removed in the latest main.

Comment on lines 266 to 272
pub fn set_fulltext_options(&mut self, options: FulltextOptions) -> Result<()> {
self.metadata.insert(
FULLTEXT_KEY.to_string(),
serde_json::to_string(&options).context(error::SerializeSnafu)?,
);
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to reduce the duplicated code

Option 1: refactor the with_fulltext_options method

    pub fn with_fulltext_options(mut self, options: FulltextOptions) -> Result<Self> {
        self. set_fulltext_options(options)?;
        Ok(self)
    }

Option 2: use the with_fulltext_options method

column_meta.column_schema = column_meta.column_schema.with_fulltext_options(fulltext)?;

src/sql/src/statements/alter.rs Outdated Show resolved Hide resolved
_ => {}
}
}
column_meta.column_schema.set_fulltext_options(fulltext).expect("set fulltext");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle the error

fulltext.enable = true;
fulltext.case_sensitive = false;
}
_ => {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to throw an error here. What will happen if the user sets the invalid full-text options while creating a table? @zhongzc

Copy link
Contributor

@zhongzc zhongzc Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will return error

return FulltextInvalidOptionSnafu {
    msg: format!("{analyzer}, expected: 'English' | 'Chinese'"),
}
.fail();

Comment on lines 623 to 652
if let Some(analyzer) = options.get(COLUMN_FULLTEXT_OPT_KEY_ANALYZER) {
match analyzer.to_ascii_lowercase().as_str() {
"english" => {
fulltext.enable = true;
fulltext.analyzer = FulltextAnalyzer::English;
}
"chinese" => {
fulltext.enable = true;
fulltext.analyzer = FulltextAnalyzer::Chinese;
}
_ => {
return error::InvalidFulltextOptionsSnafu { column_name }.fail()?;
}
}
}
if let Some(case_sensitive) = options.get(COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE) {
match case_sensitive.to_ascii_lowercase().as_str() {
"true" => {
fulltext.enable = true;
fulltext.case_sensitive = true;
}
"false" => {
fulltext.enable = true;
fulltext.case_sensitive = false;
}
_ => {
return error::InvalidFulltextOptionsSnafu { column_name }.fail()?;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, repeated code.

src/table/src/metadata.rs Outdated Show resolved Hide resolved
Comment on lines 81 to 113
if self.parser.parse_keyword(Keyword::SET) {
let _ = self.parser.parse_keyword(Keyword::FULLTEXT);

let options = self
.parser
.parse_options(Keyword::WITH)?
.into_iter()
.map(|option: SqlOption| -> Result<(String, String)> {
let (key, value) = (option.name, option.value);
let v = match value {
Expr::Value(sqlparser::ast::Value::SingleQuotedString(v))
| Expr::Value(sqlparser::ast::Value::DoubleQuotedString(v)) => v,
Expr::Identifier(v) => v.value,
Expr::Value(sqlparser::ast::Value::Number(v, _)) => v.to_string(),
_ => return error::InvalidTableOptionValueSnafu { key, value }.fail(),
};
let k = key.value.to_lowercase();
Ok((k, v))
})
.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,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests

@@ -588,6 +597,95 @@ impl TableMeta {
Ok(meta_builder)
}

fn change_column_fulltext(
Copy link
Member

@WenyXu WenyXu Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a way to add tests to verify it?

irenjj and others added 5 commits September 11, 2024 20:45
Co-authored-by: Weny Xu <wenymedia@gmail.com>
Co-authored-by: Weny Xu <wenymedia@gmail.com>
Copy link
Member

@WenyXu WenyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicking

src/datatypes/src/schema.rs Outdated Show resolved Hide resolved
src/store-api/src/region_request.rs Outdated Show resolved Hide resolved
@WenyXu
Copy link
Member

WenyXu commented Sep 18, 2024

Hi @irenjj There are some conflicts 👀

Co-authored-by: Weny Xu <wenymedia@gmail.com>
Co-authored-by: Weny Xu <wenymedia@gmail.com>
@irenjj
Copy link
Collaborator Author

irenjj commented Sep 18, 2024

Hi @irenjj There are some conflicts 👀

yep, it's greptime.proto that conflicts, i'll merge it first when this pr is ready to merge.

Copy link
Contributor

@v0y4g3r v0y4g3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job. Left some comments. I'll start the alter table option work soon, feel free to engage if you're interested in that.

src/datatypes/src/schema/column_schema.rs Outdated Show resolved Hide resolved
src/datatypes/src/schema.rs Outdated Show resolved Hide resolved
src/store-api/src/metadata.rs Outdated Show resolved Hide resolved
src/store-api/src/region_request.rs Outdated Show resolved Hide resolved
src/table/src/metadata.rs Outdated Show resolved Hide resolved
column = column
.clone()
.with_fulltext_options(fulltext.clone())
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the doc for the safety of unwrap here or handle the error

irenjj and others added 2 commits September 19, 2024 20:16
Co-authored-by: Lei, HUANG <6406592+v0y4g3r@users.noreply.github.com>
@github-actions github-actions bot added docs-required This change requires docs update. docs-not-required This change does not impact docs. and removed docs-not-required This change does not impact docs. docs-required This change requires docs update. labels Sep 20, 2024
@WenyXu
Copy link
Member

WenyXu commented Sep 20, 2024

I prefer to use the struct instead of a hashmap, then we don't need to validate the hashmap everywhere. cc @evenyag @zhongzc
GreptimeTeam/greptime-proto#185 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alter column option
4 participants